5 Commits
Author SHA1 Message Date
jay 9971419dc4 chore: release v4.1.5-fork.6
Build / build (push) Successful in 6m10s
Release / release (push) Successful in 7m9s
2026-11-28 16:36:37 -05:00
jay 72bbef3891 Validate deck-shuffle order splice; fall back to full repin 2026-11-28 16:35:37 -05:00
jay 501f8934f0 Add queue drawer deck-shuffle button 2026-11-28 16:17:07 -05:00
jay 6001741e10 Pin deck-shuffle diff behavior to a wholesale Replace 2026-11-28 16:14:19 -05:00
jay 1eb490d209 Add deck shuffle to the playback manager 2026-11-28 16:11:38 -05:00
11 changed files with 213 additions and 4 deletions
+2 -2
View File
@@ -16,8 +16,8 @@ android {
defaultConfig {
applicationId namespace
versionName "4.1.5-fork.5"
versionCode 80
versionName "4.1.5-fork.6"
versionCode 81
minSdk min_sdk
// randomly flickers between deprecated and required depending on what mood my gradle is in
@@ -131,6 +131,7 @@ class MainFragment :
// --- QUEUE SHEET SETUP ---
binding.queueClear.setOnClickListener { queueModel.clearQueue() }
binding.queueShuffle.setOnClickListener { queueModel.shuffleDeck() }
elevationNormal = binding.context.getDimen(MR.dimen.m3_sys_elevation_level1)
@@ -234,7 +235,7 @@ class MainFragment :
collectImmediately(listModel.selected, selectionBackCallback::invalidateEnabled)
collectImmediately(playbackModel.song, ::updateSong)
collectImmediately(playbackModel.openPanel.flow, ::handlePanel)
collectImmediately(queueModel.queue, ::updateQueueClearButton)
collectImmediately(queueModel.queue, ::updateQueueButtons)
}
override fun onStart() {
@@ -656,8 +657,11 @@ class MainFragment :
}
}
private fun updateQueueClearButton(queue: List<Song>) {
private fun updateQueueButtons(queue: List<Song>) {
requireBinding().queueClear.isVisible = queue.isNotEmpty()
// Shuffling fewer than two waiting entries cannot change anything.
val waiting = queue.size - if (queueModel.index.value == 0) 1 else 0
requireBinding().queueShuffle.isVisible = waiting >= 2
}
private fun tryClosePlaybackPanel() {
@@ -167,6 +167,12 @@ class QueueViewModel @Inject constructor(private val playbackManager: PlaybackSt
playbackManager.clearQueue()
}
/** Randomly reorder the waiting deck songs; the playing row keeps its place. */
fun shuffleDeck() {
L.d("Shuffling the deck")
playbackManager.shuffleDeck()
}
/** Republish the drawer rows from the manager's deck state. */
private fun republish() {
val rows = listOfNotNull(playbackManager.deckCurrent) + playbackManager.deck
@@ -287,6 +287,53 @@ 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]
// 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.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 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() + 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()
}
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
@@ -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
@@ -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 =
@@ -114,6 +114,14 @@
android:gravity="center_vertical"
android:orientation="horizontal">
<com.google.android.material.button.MaterialButton
android:id="@+id/queue_shuffle"
style="@style/Widget.Auxio.Button.TextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/lbl_shuffle"
android:visibility="gone" />
<TextView
android:layout_width="0dp"
android:layout_height="64dp"
+11
View File
@@ -126,6 +126,17 @@
app:layout_constraintEnd_toEndOf="@+id/queue_handle"
app:layout_constraintStart_toStartOf="parent" />
<com.google.android.material.button.MaterialButton
android:id="@+id/queue_shuffle"
style="@style/Widget.Auxio.Button.TextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/lbl_shuffle"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.button.MaterialButton
android:id="@+id/queue_clear"
style="@style/Widget.Auxio.Button.TextButton"
@@ -78,6 +78,15 @@ class DeckRowDiffTest {
assertEquals(UpdateInstructions.Move(from = 1, to = 2), diffDeckRows(old, new))
}
@Test
fun `a deck shuffle permutation produces a Replace from the first divergence`() {
// A multi-element permutation is no single move; the drawer must re-render the
// shuffled region wholesale, which is coherent for a deliberate shuffle.
val old = listOf(fakeSong(), fakeSong(), fakeSong())
val new = old.reversed()
assertEquals(UpdateInstructions.Replace(from = 0), diffDeckRows(old, new))
}
@Test
fun `a replacement produces a Replace from the first divergence`() {
val old = listOf(fakeSong(), fakeSong(), fakeSong())
@@ -47,6 +47,9 @@ class FakePlaybackStateHolder : PlaybackStateHolder {
/** Every [StateAck.PlayNext] receipt handed to this holder, in order. */
val insertions = mutableListOf<StateAck.PlayNext>()
/** Every [StateAck.ShuffleDeck] receipt handed to this holder, in order. */
val shuffles = mutableListOf<StateAck.ShuffleDeck>()
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(
@@ -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<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
* rest of the deck survives, matching the heap's own realignment.