diff --git a/app/src/main/java/io/github/zyrouge/symphony/services/database/adapters/SQLiteKeyValueDatabaseAdapter.kt b/app/src/main/java/io/github/zyrouge/symphony/services/database/adapters/SQLiteKeyValueDatabaseAdapter.kt index e412af32..ef6a279a 100644 --- a/app/src/main/java/io/github/zyrouge/symphony/services/database/adapters/SQLiteKeyValueDatabaseAdapter.kt +++ b/app/src/main/java/io/github/zyrouge/symphony/services/database/adapters/SQLiteKeyValueDatabaseAdapter.kt @@ -34,16 +34,14 @@ class SQLiteKeyValueDatabaseAdapter( put(COLUMN_KEY, key) put(COLUMN_VALUE, transformer.serialize(value)) } - val conflict = SQLiteDatabase.CONFLICT_REPLACE - val rowId = writableDatabase.insertWithOnConflict(name, null, values, conflict) + val rowId = writableDatabase.insert(name, null, values) return rowId != -1L } fun delete(key: String): Boolean { val selection = "$COLUMN_KEY = ?" val selectionArgs = arrayOf(key) - val count = writableDatabase.delete(name, selection, selectionArgs) - return count == 1 + return writableDatabase.delete(name, selection, selectionArgs) == 1 } fun delete(keys: Collection): Int { @@ -55,14 +53,10 @@ class SQLiteKeyValueDatabaseAdapter( } val selection = "$COLUMN_KEY IN (${selectionPlaceholder})" val selectionArgs = keys.toTypedArray() - val count = writableDatabase.delete(name, selection, selectionArgs) - return count + return writableDatabase.delete(name, selection, selectionArgs) } - fun clear(): Int { - val count = writableDatabase.delete(name, null, null) - return count - } + fun clear() = writableDatabase.delete(name, null, null) fun keys(): List { val keys = mutableListOf() diff --git a/app/src/main/java/io/github/zyrouge/symphony/services/database/store/PlaylistStore.kt b/app/src/main/java/io/github/zyrouge/symphony/services/database/store/PlaylistStore.kt index 0f5df04c..21b4987c 100644 --- a/app/src/main/java/io/github/zyrouge/symphony/services/database/store/PlaylistStore.kt +++ b/app/src/main/java/io/github/zyrouge/symphony/services/database/store/PlaylistStore.kt @@ -10,13 +10,13 @@ import io.github.zyrouge.symphony.services.groove.Playlist @Dao interface PlaylistStore { @Insert - suspend fun insert(vararg playlist: Playlist) + suspend fun insert(vararg playlist: Playlist): List @Update - suspend fun update(vararg playlist: Playlist) + suspend fun update(vararg playlist: Playlist): Int @Query("DELETE FROM playlists WHERE id IN (:playlistIds)") - suspend fun delete(playlistIds: Collection) + suspend fun delete(playlistIds: Collection): Int @Query("SELECT * FROM playlists") suspend fun entries(): Map<@MapColumn("id") String, Playlist> diff --git a/app/src/main/java/io/github/zyrouge/symphony/services/groove/repositories/PlaylistRepository.kt b/app/src/main/java/io/github/zyrouge/symphony/services/groove/repositories/PlaylistRepository.kt index 97504d00..ac88ad6b 100644 --- a/app/src/main/java/io/github/zyrouge/symphony/services/groove/repositories/PlaylistRepository.kt +++ b/app/src/main/java/io/github/zyrouge/symphony/services/groove/repositories/PlaylistRepository.kt @@ -61,7 +61,7 @@ class PlaylistRepository(private val symphony: Symphony) { val playlist = when { x.isLocal -> { ActivityUtils.makePersistableReadableUri(context, x.uri!!) - Playlist.Companion.parse(symphony, x.uri) + Playlist.parse(symphony, x.uri) } else -> x @@ -73,6 +73,9 @@ class PlaylistRepository(private val symphony: Symphony) { emitUpdateId() emitCount() } + if (!cache.containsKey(FAVORITE_PLAYLIST)) { + add(getFavorites()) + } } catch (_: FileNotFoundException) { } catch (err: Exception) { Logger.error("PlaylistRepository", "fetch failed", err) @@ -122,10 +125,13 @@ class PlaylistRepository(private val symphony: Symphony) { fun get(id: String) = cache[id] fun get(ids: List) = ids.mapNotNull { get(it) } - fun getFavorites() = cache[FAVORITE_PLAYLIST] ?: createFavorites() - fun create(title: String, songIds: List) = Playlist( - id = idGenerator.next(), + fun getFavorites() = cache[FAVORITE_PLAYLIST] + ?: create(FAVORITE_PLAYLIST, "Favorites", emptyList()) + + fun create(title: String, songIds: List) = create(idGenerator.next(), title, songIds) + private fun create(id: String, title: String, songIds: List) = Playlist( + id = id, title = title, songPaths = songIds.mapNotNull { symphony.groove.song.get(it)?.path }, uri = null, @@ -159,15 +165,15 @@ class PlaylistRepository(private val symphony: Symphony) { } suspend fun update(id: String, songIds: List) { - val old = get(id) ?: return - val new = Playlist( + val playlist = get(id) ?: return + val updated = Playlist( id = id, - title = old.title, + title = playlist.title, songPaths = songIds.mapNotNull { symphony.groove.song.get(it)?.path }, - uri = old.uri, - path = old.path, + uri = playlist.uri, + path = playlist.path, ) - cache[id] = new + cache[id] = updated emitUpdateId() emitCount() if (id == FAVORITE_PLAYLIST) { @@ -175,7 +181,7 @@ class PlaylistRepository(private val symphony: Symphony) { songIds } } - symphony.database.playlists.update(new) + symphony.database.playlists.update(updated) } // NOTE: maybe we shouldn't use groove's coroutine scope? @@ -219,23 +225,6 @@ class PlaylistRepository(private val symphony: Symphony) { symphony.database.playlists.update(renamed) } - private fun createFavorites(): Playlist { - val playlist = Playlist( - id = FAVORITE_PLAYLIST, - title = "Favorites", - songPaths = emptyList(), - uri = null, - path = null, - ) - cache[playlist.id] = playlist - _all.update { - it + playlist.id - } - emitUpdateId() - emitCount() - return playlist - } - companion object { private const val FAVORITE_PLAYLIST = "favorites" }