Validate deck-shuffle order splice; fall back to full repin

This commit is contained in:
jay
2026-11-28 16:35:37 -05:00
parent 501f8934f0
commit 72bbef3891
@@ -297,20 +297,38 @@ class ExoPlaybackStateHolder(
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.
// Extract the block, permute it, 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() })
player.addMediaItems(current + 1, block.shuffled().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.
// keeps its sequence. A disturbed order (moves under shuffle leave the shuffle
// order stale) can walk short or with duplicates, so the splice is validated and
// otherwise falls back to repinning the whole order.
val timelineSize = player.mediaItemCount
val rest =
playOrder
.filterNot { it == anchor || it in current + 1..current + deckSize }
.distinct()
val order =
listOf(anchor) + (current + 1 until current + 1 + deckSize).toList() + tail
player.setShuffleOrder(BetterShuffleOrder(order.toIntArray()))
listOf(anchor) + (current + 1 until current + 1 + deckSize).toList() + rest
val valid =
order.size == timelineSize &&
order.all { it in 0 until timelineSize } &&
order.toSet().size == timelineSize
if (valid) {
player.setShuffleOrder(BetterShuffleOrder(order.toIntArray()))
} else {
L.w("Deck shuffle order splice invalid; repinning the whole order")
player.setShuffleOrder(
BetterShuffleOrder.createDeckPinned(timelineSize, anchor, deckSize)
)
}
}
playbackManager.ack(this, ack)
deferSave()