Add deck shuffle to the playback manager
This commit is contained in:
@@ -287,6 +287,35 @@ class ExoPlaybackStateHolder(
|
|||||||
deferSave()
|
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() {
|
override fun next() {
|
||||||
// Replicate the old pseudo-circular queue behavior when no repeat option is implemented.
|
// 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
|
// Basically, you can't skip back and wrap around the queue, but you can skip forward and
|
||||||
|
|||||||
@@ -145,6 +145,15 @@ interface PlaybackStateHolder {
|
|||||||
*/
|
*/
|
||||||
fun shuffled(shuffled: Boolean, deckSize: Int)
|
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.
|
* Handle a deferred playback action.
|
||||||
*
|
*
|
||||||
@@ -206,6 +215,9 @@ sealed interface StateAck {
|
|||||||
/** @see PlaybackStateHolder.shuffled */
|
/** @see PlaybackStateHolder.shuffled */
|
||||||
data object QueueReordered : StateAck
|
data object QueueReordered : StateAck
|
||||||
|
|
||||||
|
/** @see PlaybackStateHolder.shuffleDeck */
|
||||||
|
data object ShuffleDeck : StateAck
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see PlaybackStateHolder.newPlayback
|
* @see PlaybackStateHolder.newPlayback
|
||||||
* @see PlaybackStateHolder.applySavedState
|
* @see PlaybackStateHolder.applySavedState
|
||||||
|
|||||||
@@ -204,6 +204,12 @@ interface PlaybackStateManager {
|
|||||||
*/
|
*/
|
||||||
fun clearQueue()
|
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
|
* 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
|
* 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
|
@Synchronized
|
||||||
override fun gotoDeck(row: Int) {
|
override fun gotoDeck(row: Int) {
|
||||||
val stateHolder = stateHolder ?: return
|
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 -> {
|
is StateAck.NewPlayback -> {
|
||||||
val rawQueue = stateHolder.resolveQueue()
|
val rawQueue = stateHolder.resolveQueue()
|
||||||
stateMirror =
|
stateMirror =
|
||||||
|
|||||||
@@ -47,6 +47,9 @@ class FakePlaybackStateHolder : PlaybackStateHolder {
|
|||||||
/** Every [StateAck.PlayNext] receipt handed to this holder, in order. */
|
/** Every [StateAck.PlayNext] receipt handed to this holder, in order. */
|
||||||
val insertions = mutableListOf<StateAck.PlayNext>()
|
val insertions = mutableListOf<StateAck.PlayNext>()
|
||||||
|
|
||||||
|
/** Every [StateAck.ShuffleDeck] receipt handed to this holder, in order. */
|
||||||
|
val shuffles = mutableListOf<StateAck.ShuffleDeck>()
|
||||||
|
|
||||||
var rawQueue = RawQueue.nil()
|
var rawQueue = RawQueue.nil()
|
||||||
private set
|
private set
|
||||||
|
|
||||||
@@ -225,6 +228,18 @@ class FakePlaybackStateHolder : PlaybackStateHolder {
|
|||||||
manager?.ack(this, StateAck.QueueReordered)
|
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 handleDeferred(action: DeferredPlayback) = false
|
||||||
|
|
||||||
override fun applySavedState(
|
override fun applySavedState(
|
||||||
|
|||||||
@@ -255,6 +255,58 @@ class PlaybackStateManagerImplTest {
|
|||||||
assertEquals(heap[3], manager.currentSong)
|
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<List<Song>>()
|
||||||
|
manager.addListener(
|
||||||
|
object : PlaybackStateManager.Listener {
|
||||||
|
override fun onQueueChanged(queue: List<Song>, 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
|
* 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.
|
* rest of the deck survives, matching the heap's own realignment.
|
||||||
|
|||||||
Reference in New Issue
Block a user