Fix player thumbnail not updating when changing playlist from android auto

This commit is contained in:
vfsfitvnm 2022-10-11 09:22:20 +02:00
parent 270986215c
commit 6d6a839c2d
2 changed files with 16 additions and 16 deletions

View file

@ -17,7 +17,6 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.size
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
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
import androidx.compose.ui.draw.clip import androidx.compose.ui.draw.clip
@ -36,6 +35,7 @@ import it.vfsfitvnm.vimusic.ui.styling.Dimensions
import it.vfsfitvnm.vimusic.ui.styling.LocalAppearance import it.vfsfitvnm.vimusic.ui.styling.LocalAppearance
import it.vfsfitvnm.vimusic.ui.styling.px import it.vfsfitvnm.vimusic.ui.styling.px
import it.vfsfitvnm.vimusic.utils.rememberError import it.vfsfitvnm.vimusic.utils.rememberError
import it.vfsfitvnm.vimusic.utils.rememberMediaItem
import it.vfsfitvnm.vimusic.utils.rememberMediaItemIndex import it.vfsfitvnm.vimusic.utils.rememberMediaItemIndex
import it.vfsfitvnm.vimusic.utils.thumbnail import it.vfsfitvnm.vimusic.utils.thumbnail
import java.net.UnknownHostException import java.net.UnknownHostException
@ -58,15 +58,16 @@ fun Thumbnail(
} }
val mediaItemIndex by rememberMediaItemIndex(player) val mediaItemIndex by rememberMediaItemIndex(player)
val mediaItem by rememberMediaItem(player)
val error by rememberError(player) val error by rememberError(player)
AnimatedContent( AnimatedContent(
targetState = mediaItemIndex, targetState = mediaItemIndex to mediaItem,
transitionSpec = { transitionSpec = {
val duration = 500 val duration = 500
val slideDirection = val slideDirection =
if (targetState > initialState) AnimatedContentScope.SlideDirection.Left else AnimatedContentScope.SlideDirection.Right if (targetState.first > initialState.first) AnimatedContentScope.SlideDirection.Left else AnimatedContentScope.SlideDirection.Right
ContentTransform( ContentTransform(
targetContentEnter = slideIntoContainer( targetContentEnter = slideIntoContainer(
@ -91,10 +92,8 @@ fun Thumbnail(
) )
}, },
contentAlignment = Alignment.Center contentAlignment = Alignment.Center
) { currentMediaItemIndex -> ) { (_, currentMediaItem) ->
val mediaItem = remember(currentMediaItemIndex) { val currentMediaItem = currentMediaItem ?: return@AnimatedContent
player.getMediaItemAt(currentMediaItemIndex)
}
Box( Box(
modifier = modifier modifier = modifier
@ -103,7 +102,7 @@ fun Thumbnail(
.size(thumbnailSizeDp) .size(thumbnailSizeDp)
) { ) {
AsyncImage( AsyncImage(
model = mediaItem.mediaMetadata.artworkUri.thumbnail(thumbnailSizePx), model = currentMediaItem.mediaMetadata.artworkUri.thumbnail(thumbnailSizePx),
contentDescription = null, contentDescription = null,
contentScale = ContentScale.Crop, contentScale = ContentScale.Crop,
modifier = Modifier modifier = Modifier
@ -117,23 +116,23 @@ fun Thumbnail(
) )
Lyrics( Lyrics(
mediaId = mediaItem.mediaId, mediaId = currentMediaItem.mediaId,
isDisplayed = isShowingLyrics && error == null, isDisplayed = isShowingLyrics && error == null,
onDismiss = { onShowLyrics(false) }, onDismiss = { onShowLyrics(false) },
onLyricsUpdate = { areSynchronized, mediaId, lyrics -> onLyricsUpdate = { areSynchronized, mediaId, lyrics ->
query { query {
if (areSynchronized) { if (areSynchronized) {
if (Database.updateSynchronizedLyrics(mediaId, lyrics) == 0) { if (Database.updateSynchronizedLyrics(mediaId, lyrics) == 0) {
if (mediaId == mediaItem.mediaId) { if (mediaId == currentMediaItem.mediaId) {
Database.insert(mediaItem) { song -> Database.insert(currentMediaItem) { song ->
song.copy(synchronizedLyrics = lyrics) song.copy(synchronizedLyrics = lyrics)
} }
} }
} }
} else { } else {
if (Database.updateLyrics(mediaId, lyrics) == 0) { if (Database.updateLyrics(mediaId, lyrics) == 0) {
if (mediaId == mediaItem.mediaId) { if (mediaId == currentMediaItem.mediaId) {
Database.insert(mediaItem) { song -> Database.insert(currentMediaItem) { song ->
song.copy(lyrics = lyrics) song.copy(lyrics = lyrics)
} }
} }
@ -142,12 +141,12 @@ fun Thumbnail(
} }
}, },
size = thumbnailSizeDp, size = thumbnailSizeDp,
mediaMetadataProvider = mediaItem::mediaMetadata, mediaMetadataProvider = currentMediaItem::mediaMetadata,
durationProvider = player::getDuration, durationProvider = player::getDuration,
) )
StatsForNerds( StatsForNerds(
mediaId = mediaItem.mediaId, mediaId = currentMediaItem.mediaId,
isDisplayed = isShowingStatsForNerds && error == null, isDisplayed = isShowingStatsForNerds && error == null,
onDismiss = { onShowStatsForNerds(false) } onDismiss = { onShowStatsForNerds(false) }
) )

View file

@ -6,6 +6,7 @@ import androidx.compose.runtime.DisposableEffectResult
import androidx.compose.runtime.DisposableEffectScope import androidx.compose.runtime.DisposableEffectScope
import androidx.compose.runtime.State import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.neverEqualPolicy
import androidx.compose.runtime.produceState import androidx.compose.runtime.produceState
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.media3.common.MediaItem import androidx.media3.common.MediaItem
@ -50,7 +51,7 @@ fun rememberMediaItemIndex(player: Player): State<Int> {
@Composable @Composable
fun rememberMediaItem(player: Player): State<MediaItem?> { fun rememberMediaItem(player: Player): State<MediaItem?> {
val state = remember(player) { val state = remember(player) {
mutableStateOf(player.currentMediaItem) mutableStateOf(player.currentMediaItem, neverEqualPolicy())
} }
DisposableEffect(player) { DisposableEffect(player) {