playback: add clearQueue for the drawer's clear button

This commit is contained in:
jay
2026-11-12 13:11:01 -05:00
parent ecc90524c6
commit a19bfbaddf
2 changed files with 56 additions and 0 deletions
@@ -192,6 +192,12 @@ interface PlaybackStateManager {
*/
fun removeQueueItem(at: Int)
/**
* Clear the queue of all waiting deck [Song]s. The currently playing [Song] is unaffected,
* even if it came from the deck.
*/
fun clearQueue()
/**
* (Re)shuffle or (Re)order this instance.
*
@@ -565,6 +571,17 @@ class PlaybackStateManagerImpl @Inject constructor() : PlaybackStateManager {
stateHolder.remove(at, StateAck.Remove(at))
}
@Synchronized
override fun clearQueue() {
val stateHolder = stateHolder ?: return
L.d("Clearing the deck (${deck.size} waiting songs)")
// The waiting deck entries sit directly behind the current song; removing at the same
// position repeatedly pops them in order. The ack handler shrinks the deck each time.
while (deck.isNotEmpty()) {
stateHolder.remove(stateMirror.index + 1, StateAck.Remove(stateMirror.index + 1))
}
}
@Synchronized
override fun shuffled(shuffled: Boolean) {
val stateHolder = stateHolder ?: return
@@ -23,6 +23,45 @@ import org.junit.Assert.assertSame
import org.junit.Test
class PlaybackStateManagerImplTest {
/**
* Clearing the queue drops the waiting deck entries from the timeline; a queue-sourced
* current song keeps playing and drains normally afterwards.
*/
@Test
fun `clearing the queue drops the waiting deck entries`() {
val manager = PlaybackStateManagerImpl()
val holder = FakePlaybackStateHolder()
holder.manager = manager
manager.registerStateHolder(holder)
// Playing index 2 of a 10-song queue; deck of two songs.
val heap = List(10) { fakeSong() }
manager.play(PlaybackCommandFactoryImpl.PlaybackCommandImpl(heap[2], null, heap, false))
manager.playAfter(listOf(fakeSong(), fakeSong()))
// Clear: the deck block leaves the timeline.
manager.clearQueue()
assertEquals(10, manager.queue.size)
assertEquals(heap[2], manager.currentSong)
// Queueing again lands directly behind the current song — no phantom boundary.
manager.playAfter(listOf(fakeSong()))
assertEquals(11, manager.queue.size)
assertEquals(StateAck.PlayNext(at = 3, size = 1), holder.insertions.last())
// Enter the deck song; clearing with nothing waiting is a no-op for the playing song.
holder.moveTo(3)
val deckSong = manager.currentSong
manager.clearQueue()
assertEquals(11, manager.queue.size)
assertEquals(deckSong, manager.currentSong)
// It drains normally when playback leaves it: the heap resumes.
holder.advanceToNext()
assertEquals(10, manager.queue.size)
assertEquals(heap[3], manager.currentSong)
}
/**
* Persistence: a deck song missing from the library on restore drops only itself; the
* rest of the deck survives, matching the heap's own realignment.