Skip to content

Commit

Permalink
fix: favorites playlist not being inserted in database
Browse files Browse the repository at this point in the history
  • Loading branch information
zyrouge committed Nov 6, 2024
1 parent 3382d87 commit 051e61f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,14 @@ class SQLiteKeyValueDatabaseAdapter<T>(
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<String>): Int {
Expand All @@ -55,14 +53,10 @@ class SQLiteKeyValueDatabaseAdapter<T>(
}
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<String> {
val keys = mutableListOf<String>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Long>

@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<String>)
suspend fun delete(playlistIds: Collection<String>): Int

@Query("SELECT * FROM playlists")
suspend fun entries(): Map<@MapColumn("id") String, Playlist>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -122,10 +125,13 @@ class PlaylistRepository(private val symphony: Symphony) {

fun get(id: String) = cache[id]
fun get(ids: List<String>) = ids.mapNotNull { get(it) }
fun getFavorites() = cache[FAVORITE_PLAYLIST] ?: createFavorites()

fun create(title: String, songIds: List<String>) = Playlist(
id = idGenerator.next(),
fun getFavorites() = cache[FAVORITE_PLAYLIST]
?: create(FAVORITE_PLAYLIST, "Favorites", emptyList())

fun create(title: String, songIds: List<String>) = create(idGenerator.next(), title, songIds)
private fun create(id: String, title: String, songIds: List<String>) = Playlist(
id = id,
title = title,
songPaths = songIds.mapNotNull { symphony.groove.song.get(it)?.path },
uri = null,
Expand Down Expand Up @@ -159,23 +165,23 @@ class PlaylistRepository(private val symphony: Symphony) {
}

suspend fun update(id: String, songIds: List<String>) {
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) {
_favorites.update {
songIds
}
}
symphony.database.playlists.update(new)
symphony.database.playlists.update(updated)
}

// NOTE: maybe we shouldn't use groove's coroutine scope?
Expand Down Expand Up @@ -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"
}
Expand Down

0 comments on commit 051e61f

Please sign in to comment.