Update album fetch logic

This commit is contained in:
vfsfitvnm
2022-08-12 14:50:55 +02:00
parent d792715976
commit bf7c90e71f
2 changed files with 9 additions and 9 deletions

View File

@@ -138,7 +138,7 @@ interface Database {
fun artist(id: String): Flow<Artist?> fun artist(id: String): Flow<Artist?>
@Query("SELECT * FROM Album WHERE id = :id") @Query("SELECT * FROM Album WHERE id = :id")
fun album(id: String): Album? fun album(id: String): Flow<Album?>
@Query("UPDATE Song SET totalPlayTimeMs = totalPlayTimeMs + :addition WHERE id = :id") @Query("UPDATE Song SET totalPlayTimeMs = totalPlayTimeMs + :addition WHERE id = :id")
fun incrementTotalPlayTimeMs(id: String, addition: Long) fun incrementTotalPlayTimeMs(id: String, addition: Long)

View File

@@ -27,7 +27,6 @@ import androidx.compose.foundation.text.BasicText
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.produceState
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
@@ -79,22 +78,23 @@ import it.vfsfitvnm.youtubemusic.YouTube
import java.text.DateFormat import java.text.DateFormat
import java.util.Date import java.util.Date
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
@ExperimentalAnimationApi @ExperimentalAnimationApi
@Composable @Composable
fun AlbumScreen(browseId: String) { fun AlbumScreen(browseId: String) {
val lazyListState = rememberLazyListState() val lazyListState = rememberLazyListState()
val albumResult by produceState<Result<Album>?>(initialValue = null, browseId) { val albumResult by remember(browseId) {
value = withContext(Dispatchers.IO) { Database.album(browseId).map { album ->
Database.album(browseId) album
?.takeIf { it.timestamp != null } ?.takeIf { album.timestamp != null }
?.let(Result.Companion::success) ?.let(Result.Companion::success)
?: fetchAlbum(browseId) ?: fetchAlbum(browseId)
} }.distinctUntilChanged()
} }.collectAsState(initial = null, context = Dispatchers.IO)
val songs by remember(browseId) { val songs by remember(browseId) {
Database.albumSongs(browseId) Database.albumSongs(browseId)