From abf6630bf434d5eb341fd1db1f0e69b776e1f5a6 Mon Sep 17 00:00:00 2001 From: vfsfitvnm Date: Fri, 26 Aug 2022 11:04:27 +0200 Subject: [PATCH] Make Controls composable @Stable --- .../vfsfitvnm/vimusic/ui/views/PlayerView.kt | 8 +++++-- .../vimusic/ui/views/player/Controls.kt | 24 +++++++------------ 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/views/PlayerView.kt b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/views/PlayerView.kt index 324032d..fb0ac39 100644 --- a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/views/PlayerView.kt +++ b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/views/PlayerView.kt @@ -263,7 +263,9 @@ fun PlayerView( } Controls( - mediaItem = mediaItem, + mediaId = mediaItem.mediaId, + title = mediaItem.mediaMetadata.title?.toString(), + artist = mediaItem.mediaMetadata.artist?.toString(), shouldBePlaying = shouldBePlaying, position = positionAndDuration.first, duration = positionAndDuration.second, @@ -302,7 +304,9 @@ fun PlayerView( } Controls( - mediaItem = mediaItem, + mediaId = mediaItem.mediaId, + title = mediaItem.mediaMetadata.title?.toString(), + artist = mediaItem.mediaMetadata.artist?.toString(), shouldBePlaying = shouldBePlaying, position = positionAndDuration.first, duration = positionAndDuration.second, diff --git a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/views/player/Controls.kt b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/views/player/Controls.kt index 99c5802..c83400d 100644 --- a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/views/player/Controls.kt +++ b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/views/player/Controls.kt @@ -34,12 +34,10 @@ import androidx.compose.ui.res.painterResource import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.dp import androidx.media3.common.C -import androidx.media3.common.MediaItem import androidx.media3.common.Player import it.vfsfitvnm.vimusic.Database import it.vfsfitvnm.vimusic.LocalPlayerServiceBinder import it.vfsfitvnm.vimusic.R -import it.vfsfitvnm.vimusic.models.Song import it.vfsfitvnm.vimusic.query import it.vfsfitvnm.vimusic.ui.components.SeekBar import it.vfsfitvnm.vimusic.ui.styling.LocalAppearance @@ -53,7 +51,9 @@ import kotlinx.coroutines.flow.distinctUntilChanged @Composable fun Controls( - mediaItem: MediaItem, + mediaId: String, + title: String?, + artist: String?, shouldBePlaying: Boolean, position: Long, duration: Long, @@ -66,12 +66,12 @@ fun Controls( val repeatMode by rememberRepeatMode(binder.player) - var scrubbingPosition by remember(mediaItem.mediaId) { + var scrubbingPosition by remember(mediaId) { mutableStateOf(null) } - val likedAt by remember(mediaItem.mediaId) { - Database.likedAt(mediaItem.mediaId).distinctUntilChanged() + val likedAt by remember(mediaId) { + Database.likedAt(mediaId).distinctUntilChanged() }.collectAsState(initial = null, context = Dispatchers.IO) val shouldBePlayingTransition = updateTransition(shouldBePlaying, label = "shouldBePlaying") @@ -94,14 +94,14 @@ fun Controls( ) BasicText( - text = mediaItem.mediaMetadata.title?.toString() ?: "", + text = title ?: "", style = typography.l.bold, maxLines = 1, overflow = TextOverflow.Ellipsis ) BasicText( - text = mediaItem.mediaMetadata.artist?.toString() ?: "", + text = artist ?: "", style = typography.s.semiBold.secondary, maxLines = 1, overflow = TextOverflow.Ellipsis @@ -180,13 +180,7 @@ fun Controls( modifier = Modifier .clickable { query { - if (Database.like( - mediaItem.mediaId, - if (likedAt == null) System.currentTimeMillis() else null - ) == 0 - ) { - Database.insert(mediaItem, Song::toggleLike) - } + Database.like(mediaId, if (likedAt == null) System.currentTimeMillis() else null) } } .weight(1f)