Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Commit

Permalink
Update album fetch logic
Browse files Browse the repository at this point in the history
  • Loading branch information
vfsfitvnm committed Aug 12, 2022
1 parent d792715 commit bf7c90e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/src/main/kotlin/it/vfsfitvnm/vimusic/Database.kt
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ interface Database {
fun artist(id: String): Flow<Artist?>

@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")
fun incrementTotalPlayTimeMs(id: String, addition: Long)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<Result<Album>?>(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)
Expand Down

0 comments on commit bf7c90e

Please sign in to comment.