playback: notify listeners only after deck bookkeeping settles

This commit is contained in:
jay
2026-11-12 14:55:17 -05:00
parent a8e898d5e9
commit 23135fe80d
2 changed files with 72 additions and 13 deletions
@@ -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,
)
}
}
}
@@ -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<List<Song>>()
val observedCurrent = mutableListOf<Boolean>()
manager.addListener(
object : PlaybackStateManager.Listener {
override fun onQueueChanged(queue: List<Song>, 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.