From 23135fe80db716a0ed16104a0cc9f2af6a56b31a Mon Sep 17 00:00:00 2001 From: Jay Date: Thu, 12 Nov 2026 14:55:17 -0500 Subject: [PATCH] playback: notify listeners only after deck bookkeeping settles --- .../playback/state/PlaybackStateManager.kt | 43 +++++++++++++------ .../state/PlaybackStateManagerImplTest.kt | 42 ++++++++++++++++++ 2 files changed, 72 insertions(+), 13 deletions(-) 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 55b592418..43727b867 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 @@ -556,9 +556,8 @@ class PlaybackStateManagerImpl @Inject constructor() : PlaybackStateManager { } else { val stateHolder = stateHolder ?: return L.d("Adding ${songs.size} songs to start of queue") + // The ack joins the songs to the deck, so that listeners observe the final state. stateHolder.playNext(songs, StateAck.PlayNext(stateMirror.index + 1, songs.size)) - // Play-next joins the front of the deck. - deck = songs + deck } } @@ -583,8 +582,8 @@ class PlaybackStateManagerImpl @Inject constructor() : PlaybackStateManager { val stateHolder = stateHolder ?: return val at = stateMirror.index + 1 + deck.size L.d("Adding ${songs.size} songs at deck end $at") + // The ack grows the deck, so that listeners observe the final state. stateHolder.playAfter(songs, StateAck.PlayNext(at, songs.size)) - deck = deck + songs } } @@ -632,17 +631,16 @@ class PlaybackStateManagerImpl @Inject constructor() : PlaybackStateManager { stateHolder.goto(stateMirror.index) return } - // Pull the target out of the waiting deck, re-insert it directly behind the current - // song, and jump to it. The rest of the deck keeps its place and order; a displaced - // playing deck song drains in the ack, a displaced heap song passes by behind the - // playhead. + // Pull the target to the front of the deck block and step into it. The move ack keeps + // the rest of the deck in order behind it; the goto ack drains a displaced playing deck + // song and marks the target as the playing deck song. val deckIndex = if (deckCurrent != null) row - 1 else row - val target = deck[deckIndex] val targetAt = stateMirror.index + 1 + deckIndex - stateHolder.remove(targetAt, StateAck.Remove(targetAt)) - stateHolder.playAfter(listOf(target), StateAck.PlayNext(stateMirror.index + 1, 1)) - stateHolder.goto(stateMirror.index + 1) - deckCurrent = target + val frontAt = stateMirror.index + 1 + if (deckIndex > 0) { + stateHolder.move(targetAt, frontAt, StateAck.Move(targetAt, frontAt)) + } + stateHolder.goto(frontAt) } @Synchronized @@ -746,7 +744,6 @@ class PlaybackStateManagerImpl @Inject constructor() : PlaybackStateManager { val departedAt = stateMirror.index val rawQueue = stateHolder.resolveQueue() stateMirror = stateMirror.copy(index = rawQueue.resolveIndex(), rawQueue = rawQueue) - listeners.forEach { it.onIndexMoved(stateMirror.index) } // Leaving a deck song drains it: a different song is playing now. A rewind // acks without departing and drains nothing. if (departed != null && departed !== currentSong && departed === deckCurrent) { @@ -758,12 +755,21 @@ class PlaybackStateManagerImpl @Inject constructor() : PlaybackStateManager { deckCurrent = deck[0] deck = deck.drop(1) } + // Notify only after the deck bookkeeping settles, so listeners observe the + // final state. + listeners.forEach { it.onIndexMoved(stateMirror.index) } } is StateAck.PlayNext -> { val rawQueue = stateHolder.resolveQueue() val change = QueueChange(QueueChange.Type.MAPPING, UpdateInstructions.Add(ack.at, ack.size)) stateMirror = stateMirror.copy(queue = rawQueue.resolveSongs(), rawQueue = rawQueue) + // The inserted songs join the deck before anyone is told, so listeners observe + // the final state. The insert lands at the deck's end (playAfter) or front + // (playNext). + val block = stateMirror.queue.subList(ack.at, ack.at + ack.size) + val deckInsertAt = (ack.at - (stateMirror.index + 1)).coerceIn(0, deck.size) + deck = deck.toMutableList().apply { addAll(deckInsertAt, block) } listeners.forEach { it.onQueueChanged(stateMirror.queue, stateMirror.index, change) } @@ -1025,5 +1031,16 @@ class PlaybackStateManagerImpl @Inject constructor() : PlaybackStateManager { deckCurrent = if (savedState.deckCurrent) currentSong else null isInitialized = true + + // The deck restore ran after the holder's NewPlayback ack; re-notify with the final + // state so listeners observe the restored deck. + listeners.forEach { + it.onNewPlayback( + savedState.parent, + stateMirror.queue, + stateMirror.index, + stateMirror.rawQueue.isShuffled, + ) + } } } 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 7ccae0de5..8e36a71c0 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 @@ -19,10 +19,52 @@ package org.oxycblt.auxio.playback.state import org.junit.Assert.assertEquals +import org.oxycblt.musikr.Song import org.junit.Assert.assertSame import org.junit.Test class PlaybackStateManagerImplTest { + /** + * Regression (D9): listeners must observe the final deck state at notification time. The + * deck bookkeeping used to run after notifying, leaving the UI one update behind and the + * playing row unhighlighted. + */ + @Test + fun `listeners observe the final deck state at notification time`() { + val manager = PlaybackStateManagerImpl() + val holder = FakePlaybackStateHolder() + holder.manager = manager + manager.registerStateHolder(holder) + + // Record the deck state observed at each callback. + val observedDecks = mutableListOf>() + val observedCurrent = mutableListOf() + manager.addListener( + object : PlaybackStateManager.Listener { + override fun onQueueChanged(queue: List, index: Int, change: QueueChange) { + observedDecks += manager.deck.toList() + } + + override fun onIndexMoved(index: Int) { + observedCurrent += manager.deckCurrent != null + } + } + ) + + // Playing index 2 of a 10-song queue; queue two songs in separate actions. + val heap = List(10) { fakeSong() } + manager.play(PlaybackCommandFactoryImpl.PlaybackCommandImpl(heap[2], null, heap, false)) + manager.playAfter(listOf(fakeSong())) + manager.playAfter(listOf(fakeSong())) + + // The second action's callback observes both deck entries, not the pre-append state. + assertEquals(2, observedDecks.last().size) + + // Entering the deck: the callback observes the playing song as deck-sourced. + holder.moveTo(3) + assertEquals(true, observedCurrent.last()) + } + /** * Drag-reordering deck rows reorders the waiting entries; drags involving the playing row * are refused.