From c45be1720ab3aaa959cf3706f887bd8ea1e347db Mon Sep 17 00:00:00 2001 From: Bryan Van Deusen Date: Fri, 27 Nov 2026 14:08:58 -0500 Subject: [PATCH] fix(android): Previous starts the song over after 3 seconds, as on the web (DRY B4) Previous always went back a song. It now does what the web player does: past the first 3 seconds it starts the current song over, and before that it goes back one. It greys out only in the opening seconds of the queue's first song. PlayerController decides it, so a cast to a Sonos speaker behaves the same: the restart is a seek to 0 there. Co-Authored-By: Claude Opus 5.5 --- .../minstrel/player/PlayerController.kt | 11 +++++- .../minstrel/player/PlayerUiState.kt | 22 ++++++++++- .../minstrel/player/ui/MiniPlayer.kt | 2 +- .../minstrel/player/ui/NowPlayingScreen.kt | 2 +- .../minstrel/player/PreviousTest.kt | 39 +++++++++++++++++++ 5 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 android/app/src/test/java/com/fabledsword/minstrel/player/PreviousTest.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 54e72ad2..3708cbc6 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 @@ -205,8 +205,17 @@ class PlayerController @Inject constructor( fun skipToNext() { mediaController?.let { runOnControllerThread(it) { it.seekToNextMediaItem() } } } + /** + * Previous, as the web player does it: past the first few seconds of a + * song it starts the song over, otherwise it goes back a song. Decided + * here rather than by Media3's seekToPrevious so a cast to a Sonos speaker + * behaves the same (the restart is a seek to 0 on the speaker). + */ fun skipToPrevious() { - mediaController?.let { runOnControllerThread(it) { it.seekToPreviousMediaItem() } } + val restart = uiStateInternal.value.previousRestarts + mediaController?.let { c -> + runOnControllerThread(c) { if (restart) c.seekTo(0L) else c.seekToPreviousMediaItem() } + } } /** Flip shuffle on/off. Media3 emits onEvents → uiState reflects. */ 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 02566f1a..db0fe1ed 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 @@ -42,4 +42,24 @@ data class PlayerUiState( */ val hasPrevious: Boolean = false, val hasNext: Boolean = false, -) +) { + /** Whether Previous starts this song over rather than going back one. */ + val previousRestarts: Boolean get() = previousRestarts(positionMs, hasPrevious) + + /** + * Previous always has something to do except in the opening seconds of a + * queue's first song: past [RESTART_THRESHOLD_MS] it starts the song + * over, as the web player's does. + */ + val canSkipPrevious: Boolean get() = currentTrack != null && (hasPrevious || positionMs >= RESTART_THRESHOLD_MS) +} + +/** + * How far into a song Previous starts it over instead of going back a song: + * 3 seconds, the web player's RESTART_THRESHOLD_SEC and Media3's default. + */ +const val RESTART_THRESHOLD_MS = 3_000L + +/** Previous restarts the song once it is [RESTART_THRESHOLD_MS] in, or when nothing comes before it. */ +fun previousRestarts(positionMs: Long, hasPrevious: Boolean): Boolean = + positionMs >= RESTART_THRESHOLD_MS || !hasPrevious 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 3e3ae298..e9d7c9bd 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 @@ -153,7 +153,7 @@ fun MiniPlayer( isPlaying = state.isPlaying, isUpnpLoading = state.isUpnpLoading, isLiked = isLiked, - hasPrevious = state.hasPrevious, + hasPrevious = state.canSkipPrevious, hasNext = state.hasNext, onExpandClick = onExpandClick, onPlayPause = { if (state.isPlaying) viewModel.pause() else viewModel.play() }, 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 ebc89112..4099d06d 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 @@ -416,7 +416,7 @@ private fun PlaybackControlsBlock( TransportRow( isPlaying = state.isPlaying, isUpnpLoading = state.isUpnpLoading, - hasPrevious = state.hasPrevious, + hasPrevious = state.canSkipPrevious, hasNext = state.hasNext, onPrev = viewModel::skipToPrevious, onPlayPause = { if (state.isPlaying) viewModel.pause() else viewModel.play() }, diff --git a/android/app/src/test/java/com/fabledsword/minstrel/player/PreviousTest.kt b/android/app/src/test/java/com/fabledsword/minstrel/player/PreviousTest.kt new file mode 100644 index 00000000..38bc9758 --- /dev/null +++ b/android/app/src/test/java/com/fabledsword/minstrel/player/PreviousTest.kt @@ -0,0 +1,39 @@ +package com.fabledsword.minstrel.player + +import com.fabledsword.minstrel.models.TrackRef +import org.junit.jupiter.api.Test +import kotlin.test.assertFalse +import kotlin.test.assertTrue + +/** + * Previous works as the web player's does: past the first 3 seconds it starts + * the song over; before that it goes back a song. It greys out only in the + * opening seconds of the queue's first song, where it has nothing to do. + */ +class PreviousTest { + private val track = TrackRef(id = "t-1", title = "A song", albumId = "al-1", artistId = "ar-1") + + @Test + fun `past three seconds, Previous starts the song over`() { + assertTrue(previousRestarts(positionMs = 3_000, hasPrevious = true)) + assertTrue(PlayerUiState(currentTrack = track, positionMs = 45_000, hasPrevious = true).previousRestarts) + } + + @Test + fun `in the first three seconds, Previous goes back a song`() { + assertFalse(previousRestarts(positionMs = 2_999, hasPrevious = true)) + } + + @Test + fun `with nothing before it, Previous starts the song over`() { + assertTrue(previousRestarts(positionMs = 500, hasPrevious = false)) + } + + @Test + fun `Previous greys out only at the very start of the first song`() { + assertFalse(PlayerUiState(currentTrack = track, positionMs = 1_000, hasPrevious = false).canSkipPrevious) + assertTrue(PlayerUiState(currentTrack = track, positionMs = 3_000, hasPrevious = false).canSkipPrevious) + assertTrue(PlayerUiState(currentTrack = track, positionMs = 0, hasPrevious = true).canSkipPrevious) + assertFalse(PlayerUiState(currentTrack = null, positionMs = 10_000, hasPrevious = false).canSkipPrevious) + } +}