From 1eb490d209bdb3da16f36403561afd5d9babd0f9 Mon Sep 17 00:00:00 2001 From: Jay Date: Sat, 28 Nov 2026 16:11:38 -0500 Subject: [PATCH] Add deck shuffle to the playback manager --- .../service/ExoPlaybackStateHolder.kt | 29 +++++++++++ .../playback/state/PlaybackStateHolder.kt | 12 +++++ .../playback/state/PlaybackStateManager.kt | 45 ++++++++++++++++ .../org/oxycblt/auxio/playback/state/Fakes.kt | 15 ++++++ .../state/PlaybackStateManagerImplTest.kt | 52 +++++++++++++++++++ 5 files changed, 153 insertions(+) diff --git a/app/src/main/java/org/oxycblt/auxio/playback/service/ExoPlaybackStateHolder.kt b/app/src/main/java/org/oxycblt/auxio/playback/service/ExoPlaybackStateHolder.kt index d3f0d1601..e4b45a9c0 100644 --- a/app/src/main/java/org/oxycblt/auxio/playback/service/ExoPlaybackStateHolder.kt +++ b/app/src/main/java/org/oxycblt/auxio/playback/service/ExoPlaybackStateHolder.kt @@ -287,6 +287,35 @@ class ExoPlaybackStateHolder( deferSave() } + override fun shuffleDeck(deckSize: Int, ack: StateAck.ShuffleDeck) { + if (player.currentTimeline.isEmpty || deckSize < 2) { + return + } + val current = player.currentMediaItemIndex + // Walk the play order before touching the timeline: the waiting deck block occupies + // the positions directly behind the current song, and the rest keeps its sequence. + val playOrder = player.unscrambleQueueIndices() + check(playOrder.size >= 1 + deckSize) { "Deck block missing from the timeline" } + val anchor = playOrder[0] + val tail = playOrder.drop(1 + deckSize) + // Permute the block and put it back: the physical order is the deck's order. + val block = + (current + 1 until current + 1 + deckSize).map { + checkNotNull(player.getMediaItemAt(it).song) { "Untagged media item in the deck block" } + } + player.removeMediaItems(current + 1, current + 1 + deckSize) + player.addMediaItems(current + 1, block.map { it.buildMediaItem() }) + if (player.shuffleModeEnabled) { + // Re-pin the new deck order after the current song; the rest of the play order + // keeps its existing shuffled sequence. + val order = + listOf(anchor) + (current + 1 until current + 1 + deckSize).toList() + tail + player.setShuffleOrder(BetterShuffleOrder(order.toIntArray())) + } + playbackManager.ack(this, ack) + deferSave() + } + override fun next() { // Replicate the old pseudo-circular queue behavior when no repeat option is implemented. // Basically, you can't skip back and wrap around the queue, but you can skip forward and diff --git a/app/src/main/java/org/oxycblt/auxio/playback/state/PlaybackStateHolder.kt b/app/src/main/java/org/oxycblt/auxio/playback/state/PlaybackStateHolder.kt index 7cb4c2763..21fbdbdb1 100644 --- a/app/src/main/java/org/oxycblt/auxio/playback/state/PlaybackStateHolder.kt +++ b/app/src/main/java/org/oxycblt/auxio/playback/state/PlaybackStateHolder.kt @@ -145,6 +145,15 @@ interface PlaybackStateHolder { */ fun shuffled(shuffled: Boolean, deckSize: Int) + /** + * Randomly reorder the waiting deck songs, which physically occupy the positions directly + * behind the current song. + * + * @param deckSize The number of waiting deck songs to permute. + * @param ack The [StateAck] to return to [PlaybackStateManager]. + */ + fun shuffleDeck(deckSize: Int, ack: StateAck.ShuffleDeck) + /** * Handle a deferred playback action. * @@ -206,6 +215,9 @@ sealed interface StateAck { /** @see PlaybackStateHolder.shuffled */ data object QueueReordered : StateAck + /** @see PlaybackStateHolder.shuffleDeck */ + data object ShuffleDeck : StateAck + /** * @see PlaybackStateHolder.newPlayback * @see PlaybackStateHolder.applySavedState 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 43727b867..980a4d4f0 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 @@ -204,6 +204,12 @@ interface PlaybackStateManager { */ fun clearQueue() + /** + * Randomly reorder the waiting deck [Song]s. The currently playing [Song] is unaffected, + * even if it came from the deck, and the heap keeps its order. + */ + fun shuffleDeck() + /** * 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 @@ -618,6 +624,17 @@ class PlaybackStateManagerImpl @Inject constructor() : PlaybackStateManager { } } + @Synchronized + override fun shuffleDeck() { + val stateHolder = stateHolder ?: return + if (deck.size < 2) { + L.d("Too few waiting deck entries to shuffle") + return + } + L.d("Shuffling the deck (${deck.size} waiting songs)") + stateHolder.shuffleDeck(deck.size, StateAck.ShuffleDeck) + } + @Synchronized override fun gotoDeck(row: Int) { val stateHolder = stateHolder ?: return @@ -874,6 +891,34 @@ class PlaybackStateManagerImpl @Inject constructor() : PlaybackStateManager { ) } } + is StateAck.ShuffleDeck -> { + val rawQueue = stateHolder.resolveQueue() + stateMirror = stateMirror.copy(queue = rawQueue.resolveSongs(), rawQueue = rawQueue) + // The waiting deck is exactly the block behind the current song, and the + // shuffle only permutes it: re-extract it from the authoritative queue, + // guarding that the operation moved nothing else. + val block = + stateMirror.queue.subList( + stateMirror.index + 1, + stateMirror.index + 1 + deck.size, + ) + val before = deck.map { it.uid }.toSet() + val after = block.map { it.uid }.toSet() + if (before != after) { + L.w("Deck shuffle changed deck membership; reconciling from the queue") + } + deck = block.toList() + listeners.forEach { + it.onQueueChanged( + stateMirror.queue, + stateMirror.index, + QueueChange( + QueueChange.Type.MAPPING, + UpdateInstructions.Replace(stateMirror.index + 1), + ), + ) + } + } is StateAck.NewPlayback -> { val rawQueue = stateHolder.resolveQueue() stateMirror = diff --git a/app/src/test/java/org/oxycblt/auxio/playback/state/Fakes.kt b/app/src/test/java/org/oxycblt/auxio/playback/state/Fakes.kt index 1e21b42a5..3906fde8d 100644 --- a/app/src/test/java/org/oxycblt/auxio/playback/state/Fakes.kt +++ b/app/src/test/java/org/oxycblt/auxio/playback/state/Fakes.kt @@ -47,6 +47,9 @@ class FakePlaybackStateHolder : PlaybackStateHolder { /** Every [StateAck.PlayNext] receipt handed to this holder, in order. */ val insertions = mutableListOf() + /** Every [StateAck.ShuffleDeck] receipt handed to this holder, in order. */ + val shuffles = mutableListOf() + var rawQueue = RawQueue.nil() private set @@ -225,6 +228,18 @@ class FakePlaybackStateHolder : PlaybackStateHolder { manager?.ack(this, StateAck.QueueReordered) } + override fun shuffleDeck(deckSize: Int, ack: StateAck.ShuffleDeck) { + shuffles += ack + // The waiting deck block directly behind the current song reverses, as a stand-in + // permutation. The playback order pins that block in place, so the resolved order + // follows the new physical order. + val current = rawQueue.heapIndex + val heap = rawQueue.heap.toMutableList() + heap.subList(current + 1, current + 1 + deckSize).reverse() + rawQueue = RawQueue(heap, rawQueue.shuffledMapping, rawQueue.heapIndex) + manager?.ack(this, ack) + } + override fun handleDeferred(action: DeferredPlayback) = false override fun applySavedState( 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 8e36a71c0..3eb53394f 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 @@ -255,6 +255,58 @@ class PlaybackStateManagerImplTest { assertEquals(heap[3], manager.currentSong) } + /** + * Shuffle: shuffling the deck permutes the waiting entries in place — the playing row, + * the heap, and the deck size are untouched, and listeners observe the final state. + */ + @Test + fun `shuffleDeck permutes the waiting deck entries in place`() { + val manager = PlaybackStateManagerImpl() + val holder = FakePlaybackStateHolder() + holder.manager = manager + manager.registerStateHolder(holder) + + // Record the deck state observed at each callback. + val observedDecks = mutableListOf>() + manager.addListener( + object : PlaybackStateManager.Listener { + override fun onQueueChanged(queue: List, index: Int, change: QueueChange) { + observedDecks += manager.deck.toList() + } + } + ) + + // Playing index 2 of a 10-song queue. + val heap = List(10) { fakeSong() } + manager.play(PlaybackCommandFactoryImpl.PlaybackCommandImpl(heap[2], null, heap, false)) + + // Deck of 3 songs occupies positions 3..5. + val deckBefore = List(3) { fakeSong() } + manager.playAfter(deckBefore) + val queueBefore = manager.queue.toList() + + manager.shuffleDeck() + + // The holder saw exactly one shuffle receipt. + assertEquals(1, holder.shuffles.size) + + // The same waiting entries, possibly reordered: a permutation, not a replacement. + assertEquals(3, manager.deck.size) + assertEquals(deckBefore.map { it.uid }.toSet(), manager.deck.map { it.uid }.toSet()) + + // The deck mirrors the physical block behind the current song; the heap is untouched. + assertEquals(queueBefore.size, manager.queue.size) + assertEquals(queueBefore.subList(0, 3), manager.queue.subList(0, 3)) + assertEquals(queueBefore.subList(6, 13), manager.queue.subList(6, 13)) + assertEquals(manager.queue.subList(3, 6).toList(), manager.deck.toList()) + + // The playing song is heap-sourced here; no playing deck song exists. + assertEquals(null, manager.deckCurrent) + + // The callback observed the post-shuffle deck, not a stale one. + assertEquals(manager.deck.toList(), observedDecks.last()) + } + /** * 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.