-
-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
424 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...main/java/io/github/zyrouge/symphony/services/database/store/SongQueueSongMappingStore.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package io.github.zyrouge.symphony.services.database.store | ||
|
||
import androidx.room.Dao | ||
import androidx.room.Insert | ||
import androidx.room.MapColumn | ||
import androidx.room.Query | ||
import androidx.room.RawQuery | ||
import androidx.sqlite.db.SimpleSQLiteQuery | ||
import androidx.sqlite.db.SupportSQLiteQuery | ||
import io.github.zyrouge.symphony.services.groove.entities.Song | ||
import io.github.zyrouge.symphony.services.groove.entities.SongQueueSongMapping | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.mapLatest | ||
|
||
@Dao | ||
interface SongQueueSongMappingStore { | ||
@Insert | ||
suspend fun insert(vararg entities: SongQueueSongMapping) | ||
|
||
@Query("DELETE FROM ${SongQueueSongMapping.TABLE} WHERE ${SongQueueSongMapping.COLUMN_QUEUE_ID} IN (:ids)") | ||
suspend fun deleteSongQueueIds(ids: Collection<String>) | ||
|
||
@RawQuery(observedEntities = [Song::class, SongQueueSongMapping::class]) | ||
fun entriesAsFlowRaw(query: SupportSQLiteQuery): Flow<Map<@MapColumn(SongQueueSongMapping.COLUMN_SONG_ID) String, Song.AlongSongQueueMapping>> | ||
} | ||
|
||
fun SongQueueSongMappingStore.entriesAsFlow(queueId: String): Flow<Map<String, Song.AlongSongQueueMapping>> { | ||
val query = "SELECT ${Song.TABLE}.*, " + | ||
"${SongQueueSongMapping.TABLE}.* " + | ||
"FROM ${SongQueueSongMapping.TABLE} " + | ||
"WHERE ${SongQueueSongMapping.TABLE}.${SongQueueSongMapping.COLUMN_QUEUE_ID} = ? " + | ||
"LEFT JOIN ${Song.TABLE} ON ${Song.TABLE}.${Song.COLUMN_ID} = ${SongQueueSongMapping.COLUMN_SONG_ID} " + | ||
"ORDER BY ${SongQueueSongMapping.TABLE}.${SongQueueSongMapping.COLUMN_IS_HEAD} DESC" | ||
val args = arrayOf(queueId) | ||
return entriesAsFlowRaw(SimpleSQLiteQuery(query, args)) | ||
} | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
fun SongQueueSongMappingStore.transformEntriesAsValuesFlow(entries: Flow<Map<String, Song.AlongSongQueueMapping>>): Flow<List<Song>> { | ||
return entries.mapLatest { | ||
val list = mutableListOf<Song>() | ||
var head = it.firstNotNullOfOrNull { | ||
when { | ||
it.value.mapping.isStart -> it.value | ||
else -> null | ||
} | ||
} | ||
while (head != null) { | ||
list.add(head.song) | ||
head = it[head.mapping.nextId] | ||
} | ||
list.toList() | ||
} | ||
} | ||
|
||
fun SongQueueSongMappingStore.valuesAsFlow(queueId: String): Flow<List<Song>> { | ||
val entries = entriesAsFlow(queueId) | ||
return transformEntriesAsValuesFlow(entries) | ||
} |
31 changes: 31 additions & 0 deletions
31
app/src/main/java/io/github/zyrouge/symphony/services/database/store/SongQueueStore.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package io.github.zyrouge.symphony.services.database.store | ||
|
||
import androidx.room.Dao | ||
import androidx.room.Insert | ||
import androidx.room.Query | ||
import androidx.room.RawQuery | ||
import androidx.sqlite.db.SimpleSQLiteQuery | ||
import androidx.sqlite.db.SupportSQLiteQuery | ||
import io.github.zyrouge.symphony.services.groove.entities.SongQueue | ||
import io.github.zyrouge.symphony.services.groove.entities.SongQueueSongMapping | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
@Dao | ||
interface SongQueueStore { | ||
@Insert | ||
suspend fun insert(vararg entities: SongQueue): List<String> | ||
|
||
@Query("DELETE FROM ${SongQueue.TABLE} WHERE ${SongQueue.COLUMN_ID} = :id") | ||
suspend fun delete(id: String): Int | ||
|
||
@RawQuery(observedEntities = [SongQueue::class, SongQueueSongMapping::class]) | ||
fun valuesAsFlowRaw(query: SupportSQLiteQuery): Flow<List<SongQueue.AlongAttributes>> | ||
} | ||
|
||
fun SongQueueStore.valuesAsFlow(): Flow<List<SongQueue.AlongAttributes>> { | ||
val query = "SELECT ${SongQueue.TABLE}.*, " + | ||
"COUNT(${SongQueueSongMapping.TABLE}.${SongQueueSongMapping.COLUMN_SONG_ID}) as ${SongQueue.AlongAttributes.EMBEDDED_TRACKS_COUNT} " + | ||
"FROM ${SongQueue.TABLE} " + | ||
"LEFT JOIN ${SongQueueSongMapping.TABLE} ON ${SongQueueSongMapping.TABLE}.${SongQueueSongMapping.COLUMN_QUEUE_ID} = ${SongQueue.TABLE}.${SongQueue.COLUMN_ID}" | ||
return valuesAsFlowRaw(SimpleSQLiteQuery(query)) | ||
} |
Oops, something went wrong.