playback: add gotoDeck for queue-drawer taps

This commit is contained in:
jay
2026-11-12 13:28:48 -05:00
parent a19bfbaddf
commit d17f789dd2
3 changed files with 109 additions and 1 deletions
@@ -198,6 +198,17 @@ interface PlaybackStateManager {
*/
fun clearQueue()
/**
* Start playing the deck [Song] at the given drawer row. Row 0 is the currently playing
* deck [Song] when there is one (tapping it restarts the song); otherwise the rows are the
* waiting deck [Song]s in order. A tapped waiting [Song] plays immediately while the rest
* of the deck keeps its place and order: a displaced playing deck [Song] drains, a
* displaced heap [Song] passes by as if played.
*
* @param row The drawer row to play. Does nothing if out of range.
*/
fun gotoDeck(row: Int)
/**
* (Re)shuffle or (Re)order this instance.
*
@@ -582,6 +593,32 @@ class PlaybackStateManagerImpl @Inject constructor() : PlaybackStateManager {
}
}
@Synchronized
override fun gotoDeck(row: Int) {
val stateHolder = stateHolder ?: return
val rowCount = deck.size + if (deckCurrent != null) 1 else 0
if (row !in 0 until rowCount) {
return
}
L.d("Going to deck row $row")
if (deckCurrent != null && row == 0) {
// Tapping the playing row restarts the song.
stateHolder.goto(stateMirror.index)
return
}
// Pull the target out of the waiting deck, re-insert it directly behind the current
// song, and jump to it. The rest of the deck keeps its place and order; a displaced
// playing deck song drains in the ack, a displaced heap song passes by behind the
// playhead.
val deckIndex = if (deckCurrent != null) row - 1 else row
val target = deck[deckIndex]
val targetAt = stateMirror.index + 1 + deckIndex
stateHolder.remove(targetAt, StateAck.Remove(targetAt))
stateHolder.playAfter(listOf(target), StateAck.PlayNext(stateMirror.index + 1, 1))
stateHolder.goto(stateMirror.index + 1)
deckCurrent = target
}
@Synchronized
override fun shuffled(shuffled: Boolean) {
val stateHolder = stateHolder ?: return
@@ -155,7 +155,10 @@ class FakePlaybackStateHolder : PlaybackStateHolder {
}
}
override fun goto(index: Int) {}
override fun goto(index: Int) {
// A jump is an advance to the position, as the real holder's seek is.
moveTo(index)
}
override fun addToQueue(songs: List<Song>, ack: StateAck.AddToQueue) {
insertAt(ack.at, songs)
@@ -23,6 +23,74 @@ import org.junit.Assert.assertSame
import org.junit.Test
class PlaybackStateManagerImplTest {
/**
* Tapping a deck row plays it immediately in drawer-row coordinates: row 0 restarts the
* playing deck song; a waiting row pulls the song out of line and plays it, retaining the
* rest of the queue. A displaced deck song drains; a displaced heap song passes by behind
* the playhead, the heap resuming at its successor once the queue drains.
*/
@Test
fun `gotoDeck plays a drawer row`() {
val manager = PlaybackStateManagerImpl()
val holder = FakePlaybackStateHolder()
holder.manager = manager
manager.registerStateHolder(holder)
// Playing index 2 of a 10-song queue; deck of three songs.
val heap = List(10) { fakeSong() }
manager.play(PlaybackCommandFactoryImpl.PlaybackCommandImpl(heap[2], null, heap, false))
manager.playAfter(listOf(fakeSong(), fakeSong(), fakeSong()))
// Enter the first deck song: row 0 = playing, rows 1-2 = waiting.
holder.moveTo(3)
val playing = manager.currentSong
// Row 0 restarts the playing song: no movement, no drain.
manager.gotoDeck(0)
assertEquals(playing, manager.currentSong)
assertEquals(13, manager.queue.size)
// Tapping the last waiting row: the displaced playing deck song drains, the tapped
// song plays, and the skipped waiting entry keeps its place.
val tapped = manager.queue[5]
val skipped = manager.queue[4]
manager.gotoDeck(2)
assertEquals(12, manager.queue.size)
assertEquals(tapped, manager.currentSong)
assertEquals(skipped, manager.queue[4])
// Playing the queue out resumes the heap after the interrupted song.
holder.advanceToNext()
holder.advanceToNext()
assertEquals(heap[3], manager.currentSong)
assertEquals(10, manager.queue.size)
// With a heap song playing, tapping a waiting row keeps everything: the interrupted
// heap song passes by behind the playhead, and the waiting entries keep their order.
manager.playAfter(listOf(fakeSong(), fakeSong(), fakeSong()))
val tappedAgain = manager.queue[5]
val firstWaiting = manager.queue[4]
val lastWaiting = manager.queue[6]
manager.gotoDeck(1)
assertEquals(13, manager.queue.size)
assertEquals(tappedAgain, manager.currentSong)
assertEquals(heap[3], manager.queue[3])
assertEquals(firstWaiting, manager.queue[5])
assertEquals(lastWaiting, manager.queue[6])
// The queue drains in order and the heap resumes after the interrupted song.
holder.advanceToNext()
holder.advanceToNext()
holder.advanceToNext()
assertEquals(heap[4], manager.currentSong)
assertEquals(10, manager.queue.size)
// Out-of-range rows do nothing.
manager.gotoDeck(9)
assertEquals(heap[4], manager.currentSong)
assertEquals(10, manager.queue.size)
}
/**
* Clearing the queue drops the waiting deck entries from the timeline; a queue-sourced
* current song keeps playing and drains normally afterwards.