From a19bfbaddf47253785127c2403586cb3bae4fee2 Mon Sep 17 00:00:00 2001 From: Jay Date: Thu, 12 Nov 2026 13:11:01 -0500 Subject: [PATCH] playback: add clearQueue for the drawer's clear button --- .../playback/state/PlaybackStateManager.kt | 17 ++++++++ .../state/PlaybackStateManagerImplTest.kt | 39 +++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/app/src/main/java/org/oxycblt/auxio/playback/state/PlaybackStateManager.kt b/app/src/main/java/org/oxycblt/auxio/playback/state/PlaybackStateManager.kt index 5571cf271..accf78094 100644 --- a/app/src/main/java/org/oxycblt/auxio/playback/state/PlaybackStateManager.kt +++ b/app/src/main/java/org/oxycblt/auxio/playback/state/PlaybackStateManager.kt @@ -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 diff --git a/app/src/test/java/org/oxycblt/auxio/playback/state/PlaybackStateManagerImplTest.kt b/app/src/test/java/org/oxycblt/auxio/playback/state/PlaybackStateManagerImplTest.kt index 2bda5a3b2..330d8e6c8 100644 --- a/app/src/test/java/org/oxycblt/auxio/playback/state/PlaybackStateManagerImplTest.kt +++ b/app/src/test/java/org/oxycblt/auxio/playback/state/PlaybackStateManagerImplTest.kt @@ -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.