Skip to content

Move current song to front of queue when shuffle is enabled#33

Open
lostf1sh wants to merge 1 commit into
mainfrom
fix-shuffle-index-32
Open

Move current song to front of queue when shuffle is enabled#33
lostf1sh wants to merge 1 commit into
mainfrom
fix-shuffle-index-32

Conversation

@lostf1sh

@lostf1sh lostf1sh commented Jul 1, 2026

Copy link
Copy Markdown
Collaborator

Fixes #32

Root cause

Toggling shuffle mid-playback built the shuffled queue with the current song anchored at its old index (buildAnchoredShuffleQueueSuspending without startAtZero). Playing at index 3 meant three random songs were shuffled into slots 0–2 — behind the playhead. Forward playback never reached them, and pressing prev walked into songs that were never actually played, exactly as reported. Every other shuffle entry point (Home, playlist, artist, daily mix) already passes startAtZero = true; only two paths were outliers.

Changes

  • PlaybackStateHolder.toggleShuffle (enable path): builds the shuffled queue with startAtZero = true and applies it via a new replacePlayerQueueWithCurrentFirst() helper — removes the items before the playing item, then replaces the tail with the shuffled remainder. Two player calls regardless of queue size, no playback interruption. This also replaces the old BULK_REPLACE_THRESHOLD branching for this path (the small-queue variant issued one moveMediaItem IPC per song). The fallback full-replace and the unshuffle path (restore original order at original index) are unchanged.
  • PlayerViewModel.playSongs (persistent-shuffle branch): had the same stranded-songs flaw when tapping a song mid-list with persistent shuffle on; now also starts the shuffled queue at zero.
  • PlaybackStateHolderTest: regression test driving the real toggleShuffle against a stateful fake player, asserting the playing song moves to index 0, keeps playing, and no songs are lost.

Testing

  • ./gradlew :app:compileDebugKotlin — passes
  • ./gradlew :app:testDebugUnitTest (full suite, incl. new regression test) — passes

Toggling shuffle mid-playback anchored the current song at its old
index, shuffling random songs into the slots before the playhead.
Forward playback never reached those songs and prev walked into
tracks that were never played.

Shuffle-on now places the current song at index 0 (matching every
other shuffle entry point) and applies the new order by removing the
items before the playing item and replacing the tail — two player
calls for any queue size, without interrupting playback. The
persistent-shuffle branch of playSongs had the same flaw and now also
starts the shuffled queue at zero.

Fixes #32
@lostf1sh lostf1sh force-pushed the fix-shuffle-index-32 branch from 3f3eb4f to 624243c Compare July 1, 2026 23:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: playback shuffle index

1 participant