From bf7c90e71f0fb4f8e7542b8c88f69bd902c8bd65 Mon Sep 17 00:00:00 2001 From: vfsfitvnm Date: Fri, 12 Aug 2022 14:50:55 +0200 Subject: [PATCH] Update album fetch logic --- .../main/kotlin/it/vfsfitvnm/vimusic/Database.kt | 2 +- .../vfsfitvnm/vimusic/ui/screens/AlbumScreen.kt | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/app/src/main/kotlin/it/vfsfitvnm/vimusic/Database.kt b/app/src/main/kotlin/it/vfsfitvnm/vimusic/Database.kt index 586b6217f9..2def87efa0 100644 --- a/app/src/main/kotlin/it/vfsfitvnm/vimusic/Database.kt +++ b/app/src/main/kotlin/it/vfsfitvnm/vimusic/Database.kt @@ -138,7 +138,7 @@ interface Database { fun artist(id: String): Flow @Query("SELECT * FROM Album WHERE id = :id") - fun album(id: String): Album? + fun album(id: String): Flow @Query("UPDATE Song SET totalPlayTimeMs = totalPlayTimeMs + :addition WHERE id = :id") fun incrementTotalPlayTimeMs(id: String, addition: Long) diff --git a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/AlbumScreen.kt b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/AlbumScreen.kt index e77d690b65..0379f47dda 100644 --- a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/AlbumScreen.kt +++ b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/AlbumScreen.kt @@ -27,7 +27,6 @@ import androidx.compose.foundation.text.BasicText import androidx.compose.runtime.Composable import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue -import androidx.compose.runtime.produceState import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier @@ -79,22 +78,23 @@ import it.vfsfitvnm.youtubemusic.YouTube import java.text.DateFormat import java.util.Date import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.flow.distinctUntilChanged +import kotlinx.coroutines.flow.map import kotlinx.coroutines.runBlocking -import kotlinx.coroutines.withContext @ExperimentalAnimationApi @Composable fun AlbumScreen(browseId: String) { val lazyListState = rememberLazyListState() - val albumResult by produceState?>(initialValue = null, browseId) { - value = withContext(Dispatchers.IO) { - Database.album(browseId) - ?.takeIf { it.timestamp != null } + val albumResult by remember(browseId) { + Database.album(browseId).map { album -> + album + ?.takeIf { album.timestamp != null } ?.let(Result.Companion::success) ?: fetchAlbum(browseId) - } - } + }.distinctUntilChanged() + }.collectAsState(initial = null, context = Dispatchers.IO) val songs by remember(browseId) { Database.albumSongs(browseId)