The first public release of Fabled Minstrel #158

Merged
bvandeusen merged 256 commits from dev into main 2026-12-03 21:41:58 -05:00
5 changed files with 72 additions and 4 deletions
Showing only changes of commit c45be1720a - Show all commits
@@ -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() },
@@ -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)
}
}