Make likeAt in Controls saveable

This commit is contained in:
vfsfitvnm 2022-10-07 16:11:12 +02:00
parent d466f7f1a6
commit f7f65c44a7

View file

@ -21,10 +21,10 @@ import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
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.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.autoSaver
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
@ -47,11 +47,13 @@ import it.vfsfitvnm.vimusic.ui.styling.favoritesIcon
import it.vfsfitvnm.vimusic.utils.bold
import it.vfsfitvnm.vimusic.utils.forceSeekToNext
import it.vfsfitvnm.vimusic.utils.forceSeekToPrevious
import it.vfsfitvnm.vimusic.utils.produceSaveableState
import it.vfsfitvnm.vimusic.utils.rememberRepeatMode
import it.vfsfitvnm.vimusic.utils.secondary
import it.vfsfitvnm.vimusic.utils.semiBold
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.flowOn
@Composable
fun Controls(
@ -74,9 +76,13 @@ fun Controls(
mutableStateOf<Long?>(null)
}
val likedAt by remember(mediaId) {
Database.likedAt(mediaId).distinctUntilChanged()
}.collectAsState(initial = null, context = Dispatchers.IO)
val likedAt by produceSaveableState<Long?>(initialValue = null, stateSaver = autoSaver()) {
Database
.likedAt(mediaId)
.flowOn(Dispatchers.IO)
.distinctUntilChanged()
.collect { value = it }
}
val shouldBePlayingTransition = updateTransition(shouldBePlaying, label = "shouldBePlaying")