From abdb689e082559ceb1325144159310968b432053 Mon Sep 17 00:00:00 2001 From: Bryan Van Deusen Date: Fri, 27 Nov 2026 10:16:40 -0500 Subject: [PATCH] refactor(android): one transport button pair, and Previous/Next grey out at the ends of the queue (DRY A2) The mini player and Now Playing each drew their own play/pause button with its UPnP loading spinner, and their own Previous/Next. They share TransportControls.kt now (PlayPauseButton, SkipButton). PlayerUiState gains hasPrevious/hasNext from Media3's hasPrevious/NextMediaItem, and both screens disable the skip buttons when there is nowhere to go, as the web does. The cover's shared-element hero modifier, also written twice, is one nowPlayingCoverHero(). Co-Authored-By: Claude Opus 5.5 --- .../minstrel/player/PlayerController.kt | 2 + .../minstrel/player/PlayerUiState.kt | 7 ++ .../minstrel/player/ui/MiniPlayer.kt | 94 ++++++------------- .../minstrel/player/ui/NowPlayingCoverHero.kt | 28 ++++++ .../minstrel/player/ui/NowPlayingScreen.kt | 85 ++++++----------- .../minstrel/player/ui/TransportControls.kt | 76 +++++++++++++++ 6 files changed, 173 insertions(+), 119 deletions(-) create mode 100644 android/app/src/main/java/com/fabledsword/minstrel/player/ui/NowPlayingCoverHero.kt create mode 100644 android/app/src/main/java/com/fabledsword/minstrel/player/ui/TransportControls.kt diff --git a/android/app/src/main/java/com/fabledsword/minstrel/player/PlayerController.kt b/android/app/src/main/java/com/fabledsword/minstrel/player/PlayerController.kt index 3c3fbc66..54e72ad2 100644 --- a/android/app/src/main/java/com/fabledsword/minstrel/player/PlayerController.kt +++ b/android/app/src/main/java/com/fabledsword/minstrel/player/PlayerController.kt @@ -663,6 +663,8 @@ class PlayerController @Inject constructor( }, isUpnpLoading = isUpnpLoading, listenSeq = listenSeq, + hasPrevious = player.hasPreviousMediaItem(), + hasNext = player.hasNextMediaItem(), ) } }, diff --git a/android/app/src/main/java/com/fabledsword/minstrel/player/PlayerUiState.kt b/android/app/src/main/java/com/fabledsword/minstrel/player/PlayerUiState.kt index cb940279..02566f1a 100644 --- a/android/app/src/main/java/com/fabledsword/minstrel/player/PlayerUiState.kt +++ b/android/app/src/main/java/com/fabledsword/minstrel/player/PlayerUiState.kt @@ -35,4 +35,11 @@ data class PlayerUiState( * on repeat-one, or the same song twice in a row, is a play per listen. */ val listenSeq: Long = 0, + /** + * Whether Previous and Next lead anywhere, as Media3 reckons it from the + * queue, shuffle and repeat. At the ends of a queue with repeat off or + * repeat one they don't, and the buttons grey out rather than do nothing. + */ + val hasPrevious: Boolean = false, + val hasNext: Boolean = false, ) diff --git a/android/app/src/main/java/com/fabledsword/minstrel/player/ui/MiniPlayer.kt b/android/app/src/main/java/com/fabledsword/minstrel/player/ui/MiniPlayer.kt index 529efea7..3e3ae298 100644 --- a/android/app/src/main/java/com/fabledsword/minstrel/player/ui/MiniPlayer.kt +++ b/android/app/src/main/java/com/fabledsword/minstrel/player/ui/MiniPlayer.kt @@ -1,7 +1,6 @@ package com.fabledsword.minstrel.player.ui import androidx.compose.animation.Crossfade -import androidx.compose.animation.ExperimentalSharedTransitionApi import androidx.compose.foundation.background import androidx.compose.foundation.clickable import androidx.compose.foundation.gestures.Orientation @@ -17,9 +16,7 @@ import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.width import androidx.compose.foundation.shape.RoundedCornerShape -import androidx.compose.material3.CircularProgressIndicator import androidx.compose.material3.Icon -import androidx.compose.material3.IconButton import androidx.compose.material3.MaterialTheme import androidx.compose.foundation.layout.fillMaxHeight import androidx.compose.material3.Surface @@ -36,14 +33,9 @@ import androidx.hilt.navigation.compose.hiltViewModel import androidx.lifecycle.compose.collectAsStateWithLifecycle import com.composables.icons.lucide.Lucide import com.composables.icons.lucide.Music -import com.composables.icons.lucide.Pause -import com.composables.icons.lucide.Play import com.composables.icons.lucide.SkipBack import com.composables.icons.lucide.SkipForward import com.fabledsword.minstrel.models.TrackRef -import com.fabledsword.minstrel.nav.HERO_KEY_NOW_PLAYING_COVER -import com.fabledsword.minstrel.nav.LocalAnimatedContentScope -import com.fabledsword.minstrel.nav.LocalSharedTransitionScope import com.fabledsword.minstrel.shared.widgets.LikeButton import com.fabledsword.minstrel.shared.widgets.ServerImage import com.fabledsword.minstrel.shared.widgets.trackactions.TrackActionsViewModel @@ -59,25 +51,10 @@ private const val SCRUBBER_ROW_HEIGHT_DP = 4 // same across screens. private const val SWIPE_UP_VELOCITY_DP = 200 -@OptIn(ExperimentalSharedTransitionApi::class) @Composable private fun MiniCover(coverUrl: String, contentDescription: String) { - val sharedScope = LocalSharedTransitionScope.current - val animatedScope = LocalAnimatedContentScope.current - val heroModifier = if (sharedScope != null && animatedScope != null) { - with(sharedScope) { - Modifier.sharedElement( - sharedContentState = rememberSharedContentState( - key = HERO_KEY_NOW_PLAYING_COVER, - ), - animatedVisibilityScope = animatedScope, - ) - } - } else { - Modifier - } Box( - modifier = heroModifier + modifier = nowPlayingCoverHero() .size(COVER_SIZE_DP.dp) .clip(RoundedCornerShape(FabledSwordFlatTokens.radiusSm)) .background(MaterialTheme.colorScheme.surfaceVariant), @@ -176,6 +153,8 @@ fun MiniPlayer( isPlaying = state.isPlaying, isUpnpLoading = state.isUpnpLoading, isLiked = isLiked, + hasPrevious = state.hasPrevious, + hasNext = state.hasNext, onExpandClick = onExpandClick, onPlayPause = { if (state.isPlaying) viewModel.pause() else viewModel.play() }, onPrev = viewModel::skipToPrevious, @@ -218,6 +197,8 @@ private fun MiniRow( isPlaying: Boolean, isUpnpLoading: Boolean, isLiked: Boolean, + hasPrevious: Boolean, + hasNext: Boolean, onExpandClick: () -> Unit, onPlayPause: () -> Unit, onPrev: () -> Unit, @@ -260,51 +241,34 @@ private fun MiniRow( } } LikeButton(liked = isLiked, onToggle = onToggleLike) - TransportButton(icon = Lucide.SkipBack, description = "Previous", onClick = onPrev) - MiniPlayPauseButton( + val tint = MaterialTheme.colorScheme.onSurface + SkipButton( + icon = Lucide.SkipBack, + description = "Previous", + enabled = hasPrevious, + tint = tint, + iconSize = MINI_TRANSPORT_ICON_DP.dp, + onClick = onPrev, + ) + PlayPauseButton( isPlaying = isPlaying, isUpnpLoading = isUpnpLoading, + tint = tint, + iconSize = MINI_TRANSPORT_ICON_DP.dp, + spinnerStroke = 2.dp, onClick = onPlayPause, ) - TransportButton(icon = Lucide.SkipForward, description = "Next", onClick = onNext) - } -} - -@Composable -private fun MiniPlayPauseButton( - isPlaying: Boolean, - isUpnpLoading: Boolean, - onClick: () -> Unit, -) { - IconButton(onClick = onClick, enabled = !isUpnpLoading) { - if (isUpnpLoading) { - CircularProgressIndicator( - modifier = Modifier.size(MINI_PLAY_PAUSE_SPINNER_DP.dp), - strokeWidth = 2.dp, - ) - } else { - Icon( - imageVector = if (isPlaying) Lucide.Pause else Lucide.Play, - contentDescription = if (isPlaying) "Pause" else "Play", - tint = MaterialTheme.colorScheme.onSurface, - ) - } - } -} - -private const val MINI_PLAY_PAUSE_SPINNER_DP = 24 - -@Composable -private fun TransportButton( - icon: androidx.compose.ui.graphics.vector.ImageVector, - description: String, - onClick: () -> Unit, -) { - IconButton(onClick = onClick) { - Icon( - imageVector = icon, - contentDescription = description, - tint = MaterialTheme.colorScheme.onSurface, + SkipButton( + icon = Lucide.SkipForward, + description = "Next", + enabled = hasNext, + tint = tint, + iconSize = MINI_TRANSPORT_ICON_DP.dp, + onClick = onNext, ) } } + +/** The mini player's transport icons, at Material's standard icon size. */ +private const val MINI_TRANSPORT_ICON_DP = 24 + diff --git a/android/app/src/main/java/com/fabledsword/minstrel/player/ui/NowPlayingCoverHero.kt b/android/app/src/main/java/com/fabledsword/minstrel/player/ui/NowPlayingCoverHero.kt new file mode 100644 index 00000000..80ab0723 --- /dev/null +++ b/android/app/src/main/java/com/fabledsword/minstrel/player/ui/NowPlayingCoverHero.kt @@ -0,0 +1,28 @@ +package com.fabledsword.minstrel.player.ui + +import androidx.compose.animation.ExperimentalSharedTransitionApi +import androidx.compose.runtime.Composable +import androidx.compose.ui.Modifier +import com.fabledsword.minstrel.nav.HERO_KEY_NOW_PLAYING_COVER +import com.fabledsword.minstrel.nav.LocalAnimatedContentScope +import com.fabledsword.minstrel.nav.LocalSharedTransitionScope + +/** + * The shared-element modifier that carries the playing song's cover + * between the mini player and Now Playing, so opening Now Playing grows + * the small cover into the large one. Outside a shared transition (a + * preview, a test) it is a plain Modifier. + */ +@OptIn(ExperimentalSharedTransitionApi::class) +@Composable +internal fun nowPlayingCoverHero(): Modifier { + val sharedScope = LocalSharedTransitionScope.current + val animatedScope = LocalAnimatedContentScope.current + if (sharedScope == null || animatedScope == null) return Modifier + return with(sharedScope) { + Modifier.sharedElement( + sharedContentState = rememberSharedContentState(key = HERO_KEY_NOW_PLAYING_COVER), + animatedVisibilityScope = animatedScope, + ) + } +} diff --git a/android/app/src/main/java/com/fabledsword/minstrel/player/ui/NowPlayingScreen.kt b/android/app/src/main/java/com/fabledsword/minstrel/player/ui/NowPlayingScreen.kt index e0171ee8..ebc89112 100644 --- a/android/app/src/main/java/com/fabledsword/minstrel/player/ui/NowPlayingScreen.kt +++ b/android/app/src/main/java/com/fabledsword/minstrel/player/ui/NowPlayingScreen.kt @@ -6,7 +6,6 @@ import android.Manifest import android.content.pm.PackageManager import androidx.activity.compose.rememberLauncherForActivityResult import androidx.activity.result.contract.ActivityResultContracts -import androidx.compose.animation.ExperimentalSharedTransitionApi import androidx.compose.foundation.background import androidx.compose.foundation.focusable import androidx.compose.foundation.rememberScrollState @@ -26,7 +25,6 @@ import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.widthIn import androidx.compose.foundation.shape.CircleShape import androidx.compose.foundation.shape.RoundedCornerShape -import androidx.compose.material3.CircularProgressIndicator import androidx.compose.material3.Icon import androidx.compose.material3.IconButton import androidx.compose.material3.MaterialTheme @@ -77,8 +75,6 @@ import com.composables.icons.lucide.ChevronDown import com.composables.icons.lucide.ListMusic import com.composables.icons.lucide.Lucide import com.composables.icons.lucide.Music -import com.composables.icons.lucide.Pause -import com.composables.icons.lucide.Play import com.composables.icons.lucide.Repeat import com.composables.icons.lucide.Repeat1 import com.composables.icons.lucide.Shuffle @@ -94,9 +90,6 @@ import com.fabledsword.minstrel.player.output.RouteSnapshot import kotlinx.coroutines.launch import com.fabledsword.minstrel.nav.AlbumDetail import com.fabledsword.minstrel.nav.ArtistDetail -import com.fabledsword.minstrel.nav.HERO_KEY_NOW_PLAYING_COVER -import com.fabledsword.minstrel.nav.LocalAnimatedContentScope -import com.fabledsword.minstrel.nav.LocalSharedTransitionScope import com.fabledsword.minstrel.nav.NowPlaying import com.fabledsword.minstrel.nav.Queue import com.fabledsword.minstrel.shared.formatDuration @@ -423,6 +416,8 @@ private fun PlaybackControlsBlock( TransportRow( isPlaying = state.isPlaying, isUpnpLoading = state.isUpnpLoading, + hasPrevious = state.hasPrevious, + hasNext = state.hasNext, onPrev = viewModel::skipToPrevious, onPlayPause = { if (state.isPlaying) viewModel.pause() else viewModel.play() }, onNext = viewModel::skipToNext, @@ -537,25 +532,10 @@ private fun ViewQueueButton(onClick: () -> Unit) { } } -@OptIn(ExperimentalSharedTransitionApi::class) @Composable private fun NowPlayingCover(coverUrl: String, contentDescription: String) { - val sharedScope = LocalSharedTransitionScope.current - val animatedScope = LocalAnimatedContentScope.current - val heroModifier = if (sharedScope != null && animatedScope != null) { - with(sharedScope) { - Modifier.sharedElement( - sharedContentState = rememberSharedContentState( - key = HERO_KEY_NOW_PLAYING_COVER, - ), - animatedVisibilityScope = animatedScope, - ) - } - } else { - Modifier - } Box( - modifier = heroModifier + modifier = nowPlayingCoverHero() .widthIn(max = COVER_MAX_WIDTH_DP.dp) .fillMaxWidth() .aspectRatio(1f) @@ -700,51 +680,48 @@ private fun ScrubTrack(fraction: Float, accent: Color) { } } +@Suppress("LongParameterList") // Compose row wiring — transport state + callbacks, not logic. @Composable private fun TransportRow( isPlaying: Boolean, isUpnpLoading: Boolean, + hasPrevious: Boolean, + hasNext: Boolean, onPrev: () -> Unit, onPlayPause: () -> Unit, onNext: () -> Unit, ) { val actionColors = LocalActionColors.current + val skipTint = MaterialTheme.colorScheme.onBackground Row( modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceEvenly, verticalAlignment = Alignment.CenterVertically, ) { - IconButton(onClick = onPrev) { - Icon( - imageVector = Lucide.SkipBack, - contentDescription = "Previous", - tint = MaterialTheme.colorScheme.onBackground, - modifier = Modifier.size(TRANSPORT_ICON_DP.dp), - ) - } - IconButton(onClick = onPlayPause, enabled = !isUpnpLoading) { - if (isUpnpLoading) { - CircularProgressIndicator( - modifier = Modifier.size(PLAY_PAUSE_ICON_DP.dp), - strokeWidth = 3.dp, - ) - } else { - Icon( - imageVector = if (isPlaying) Lucide.Pause else Lucide.Play, - contentDescription = if (isPlaying) "Pause" else "Play", - tint = actionColors.primary, - modifier = Modifier.size(PLAY_PAUSE_ICON_DP.dp), - ) - } - } - IconButton(onClick = onNext) { - Icon( - imageVector = Lucide.SkipForward, - contentDescription = "Next", - tint = MaterialTheme.colorScheme.onBackground, - modifier = Modifier.size(TRANSPORT_ICON_DP.dp), - ) - } + SkipButton( + icon = Lucide.SkipBack, + description = "Previous", + enabled = hasPrevious, + tint = skipTint, + iconSize = TRANSPORT_ICON_DP.dp, + onClick = onPrev, + ) + PlayPauseButton( + isPlaying = isPlaying, + isUpnpLoading = isUpnpLoading, + tint = actionColors.primary, + iconSize = PLAY_PAUSE_ICON_DP.dp, + spinnerStroke = 3.dp, + onClick = onPlayPause, + ) + SkipButton( + icon = Lucide.SkipForward, + description = "Next", + enabled = hasNext, + tint = skipTint, + iconSize = TRANSPORT_ICON_DP.dp, + onClick = onNext, + ) } } diff --git a/android/app/src/main/java/com/fabledsword/minstrel/player/ui/TransportControls.kt b/android/app/src/main/java/com/fabledsword/minstrel/player/ui/TransportControls.kt new file mode 100644 index 00000000..80ac653a --- /dev/null +++ b/android/app/src/main/java/com/fabledsword/minstrel/player/ui/TransportControls.kt @@ -0,0 +1,76 @@ +package com.fabledsword.minstrel.player.ui + +import androidx.compose.foundation.layout.size +import androidx.compose.material3.CircularProgressIndicator +import androidx.compose.material3.Icon +import androidx.compose.material3.IconButton +import androidx.compose.runtime.Composable +import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.graphics.vector.ImageVector +import androidx.compose.ui.unit.Dp +import com.composables.icons.lucide.Lucide +import com.composables.icons.lucide.Pause +import com.composables.icons.lucide.Play + +/** Material's alpha for disabled content. */ +private const val DISABLED_ALPHA = 0.38f + +/** + * A Previous or Next button, shared by the mini player and Now Playing. + * It greys out when [enabled] is false: at the ends of the queue (repeat + * off or repeat one) there is nowhere to go, and a button that does + * nothing should look it. + */ +@Suppress("LongParameterList") // Compose button wiring — look + state + click, not logic. +@Composable +internal fun SkipButton( + icon: ImageVector, + description: String, + enabled: Boolean, + tint: Color, + iconSize: Dp, + onClick: () -> Unit, +) { + IconButton(onClick = onClick, enabled = enabled) { + Icon( + imageVector = icon, + contentDescription = description, + tint = if (enabled) tint else tint.copy(alpha = DISABLED_ALPHA), + modifier = Modifier.size(iconSize), + ) + } +} + +/** + * The play/pause button, shared by the mini player and Now Playing. While + * a speaker (Sonos or another UPnP renderer) is still taking the queue, a + * spinner stands in for the icon and the button is disabled, since a tap + * then would reach a player that isn't ready. + */ +@Suppress("LongParameterList") // Compose button wiring — look + state + click, not logic. +@Composable +internal fun PlayPauseButton( + isPlaying: Boolean, + isUpnpLoading: Boolean, + tint: Color, + iconSize: Dp, + spinnerStroke: Dp, + onClick: () -> Unit, +) { + IconButton(onClick = onClick, enabled = !isUpnpLoading) { + if (isUpnpLoading) { + CircularProgressIndicator( + modifier = Modifier.size(iconSize), + strokeWidth = spinnerStroke, + ) + } else { + Icon( + imageVector = if (isPlaying) Lucide.Pause else Lucide.Play, + contentDescription = if (isPlaying) "Pause" else "Play", + tint = tint, + modifier = Modifier.size(iconSize), + ) + } + } +}