From 6d6a839c2d0395c7f95f1d207a6e8840bfd7f250 Mon Sep 17 00:00:00 2001 From: vfsfitvnm Date: Tue, 11 Oct 2022 09:22:20 +0200 Subject: [PATCH] Fix player thumbnail not updating when changing playlist from android auto --- .../vimusic/ui/screens/player/Thumbnail.kt | 29 +++++++++---------- .../it/vfsfitvnm/vimusic/utils/PlayerState.kt | 3 +- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/player/Thumbnail.kt b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/player/Thumbnail.kt index fde8bb3..a7ffd99 100644 --- a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/player/Thumbnail.kt +++ b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/player/Thumbnail.kt @@ -17,7 +17,6 @@ import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.size import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue -import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier 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.px import it.vfsfitvnm.vimusic.utils.rememberError +import it.vfsfitvnm.vimusic.utils.rememberMediaItem import it.vfsfitvnm.vimusic.utils.rememberMediaItemIndex import it.vfsfitvnm.vimusic.utils.thumbnail import java.net.UnknownHostException @@ -58,15 +58,16 @@ fun Thumbnail( } val mediaItemIndex by rememberMediaItemIndex(player) + val mediaItem by rememberMediaItem(player) val error by rememberError(player) AnimatedContent( - targetState = mediaItemIndex, + targetState = mediaItemIndex to mediaItem, transitionSpec = { val duration = 500 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( targetContentEnter = slideIntoContainer( @@ -91,10 +92,8 @@ fun Thumbnail( ) }, contentAlignment = Alignment.Center - ) { currentMediaItemIndex -> - val mediaItem = remember(currentMediaItemIndex) { - player.getMediaItemAt(currentMediaItemIndex) - } + ) { (_, currentMediaItem) -> + val currentMediaItem = currentMediaItem ?: return@AnimatedContent Box( modifier = modifier @@ -103,7 +102,7 @@ fun Thumbnail( .size(thumbnailSizeDp) ) { AsyncImage( - model = mediaItem.mediaMetadata.artworkUri.thumbnail(thumbnailSizePx), + model = currentMediaItem.mediaMetadata.artworkUri.thumbnail(thumbnailSizePx), contentDescription = null, contentScale = ContentScale.Crop, modifier = Modifier @@ -117,23 +116,23 @@ fun Thumbnail( ) Lyrics( - mediaId = mediaItem.mediaId, + mediaId = currentMediaItem.mediaId, isDisplayed = isShowingLyrics && error == null, onDismiss = { onShowLyrics(false) }, onLyricsUpdate = { areSynchronized, mediaId, lyrics -> query { if (areSynchronized) { if (Database.updateSynchronizedLyrics(mediaId, lyrics) == 0) { - if (mediaId == mediaItem.mediaId) { - Database.insert(mediaItem) { song -> + if (mediaId == currentMediaItem.mediaId) { + Database.insert(currentMediaItem) { song -> song.copy(synchronizedLyrics = lyrics) } } } } else { if (Database.updateLyrics(mediaId, lyrics) == 0) { - if (mediaId == mediaItem.mediaId) { - Database.insert(mediaItem) { song -> + if (mediaId == currentMediaItem.mediaId) { + Database.insert(currentMediaItem) { song -> song.copy(lyrics = lyrics) } } @@ -142,12 +141,12 @@ fun Thumbnail( } }, size = thumbnailSizeDp, - mediaMetadataProvider = mediaItem::mediaMetadata, + mediaMetadataProvider = currentMediaItem::mediaMetadata, durationProvider = player::getDuration, ) StatsForNerds( - mediaId = mediaItem.mediaId, + mediaId = currentMediaItem.mediaId, isDisplayed = isShowingStatsForNerds && error == null, onDismiss = { onShowStatsForNerds(false) } ) diff --git a/app/src/main/kotlin/it/vfsfitvnm/vimusic/utils/PlayerState.kt b/app/src/main/kotlin/it/vfsfitvnm/vimusic/utils/PlayerState.kt index d1b7c08..3d55986 100644 --- a/app/src/main/kotlin/it/vfsfitvnm/vimusic/utils/PlayerState.kt +++ b/app/src/main/kotlin/it/vfsfitvnm/vimusic/utils/PlayerState.kt @@ -6,6 +6,7 @@ import androidx.compose.runtime.DisposableEffectResult import androidx.compose.runtime.DisposableEffectScope import androidx.compose.runtime.State import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.neverEqualPolicy import androidx.compose.runtime.produceState import androidx.compose.runtime.remember import androidx.media3.common.MediaItem @@ -50,7 +51,7 @@ fun rememberMediaItemIndex(player: Player): State { @Composable fun rememberMediaItem(player: Player): State { val state = remember(player) { - mutableStateOf(player.currentMediaItem) + mutableStateOf(player.currentMediaItem, neverEqualPolicy()) } DisposableEffect(player) {