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 <noreply@anthropic.com>
This commit is contained in:
2026-11-27 14:08:58 -05:00
co-authored by Claude Opus 5.5
parent cb7028c745
commit c45be1720a
5 changed files with 72 additions and 4 deletions
@@ -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. */
@@ -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
@@ -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() },
@@ -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() },