playback: never lose pager commands to event overwrite

This commit is contained in:
jay
2026-11-12 15:43:43 -05:00
parent 642a3add27
commit 1b5134f854
2 changed files with 54 additions and 36 deletions
@@ -347,11 +347,9 @@ class PlaybackPanelFragment :
// but only on some devices. // but only on some devices.
val binding = binding ?: return val binding = binding ?: return
val command = playbackModel.pagerCommand.consume() val commands = playbackModel.drainPagerCommands()
L.d( L.d("Pager: applying ${commands.size} command(s), pager at ${binding.playbackPager.currentItem}")
"Pager: applying queue size ${queue.queue.size}, index ${queue.index}, command $command, pager at ${binding.playbackPager.currentItem}" if (commands.isEmpty()) {
)
if (command == null) {
// This probably shouldn't happen in practice, as QueueViewModel directly // This probably shouldn't happen in practice, as QueueViewModel directly
// attaches to PlaybackStateManager and will basically always initialize // attaches to PlaybackStateManager and will basically always initialize
// with a command as a result. // with a command as a result.
@@ -363,24 +361,28 @@ class PlaybackPanelFragment :
return return
} }
if (command.update != null) { // Apply every command in order; each carries the queue state it was made against, so a
// queue needs to be updated. // burst (like a drain's removal plus scroll) cannot lose its intermediate step.
coverPagerAdapter.update(queue.queue, command.update) for ((commandQueue, command) in commands) {
} if (command.update != null) {
// queue needs to be updated.
if (command.scroll != null) { coverPagerAdapter.update(commandQueue.queue, command.update)
// we need to scroll, however the smooth scroll only really looks best
// when we are only doing next/prev due to various factors. better to
// just not animate on outright gotos or queue updates
val delta = binding.playbackPager.currentItem - command.scroll
if (delta == 0) {
// user scroll, carry on
return
} }
if (command.update == null && abs(delta) == 1) {
binding.playbackPager.smoothScrollByPageTo(command.scroll) if (command.scroll != null) {
} else { // we need to scroll, however the smooth scroll only really looks best
binding.playbackPager.setCurrentItem(command.scroll, false) // when we are only doing next/prev due to various factors. better to
// just not animate on outright gotos or queue updates
val delta = binding.playbackPager.currentItem - command.scroll
if (delta == 0) {
// user scroll, carry on
continue
}
if (command.update == null && abs(delta) == 1) {
binding.playbackPager.smoothScrollByPageTo(command.scroll)
} else {
binding.playbackPager.setCurrentItem(command.scroll, false)
}
} }
} }
} }
@@ -109,9 +109,29 @@ constructor(
/** The current queue in a special bundled format suitable for the cover ViewPager2. */ /** The current queue in a special bundled format suitable for the cover ViewPager2. */
val pagerQueue: StateFlow<PagerQueue> = _pagerQueue val pagerQueue: StateFlow<PagerQueue> = _pagerQueue
/** Specialized ViewPager2-friendly queue commands */ // Manager callbacks can arrive on any thread; the panel drains on the main thread.
val pagerCommand: Event<PagerCommand> private val pagerLock = Any()
field = MutableEvent<PagerCommand>() private val pendingPagerCommands = ArrayDeque<Pair<PagerQueue, PagerCommand>>()
/**
* Drain the pager commands accumulated since the last drain, as (queue, command) pairs in
* order. Each pair is a complete intermediate state, so applying them sequentially is safe
* even when a single event (like a drain) produces several commands in one burst.
*/
fun drainPagerCommands(): List<Pair<PagerQueue, PagerCommand>> =
synchronized(pagerLock) {
val drained = pendingPagerCommands.toList()
pendingPagerCommands.clear()
drained
}
/** Update the pager's queue and enqueue the command describing the change. */
private fun updatePagerQueue(queue: List<Song>, index: Int, command: PagerCommand) {
val pagerQueue = PagerQueue(queue, index)
L.d("Pager: enqueue $command, queue size ${queue.size}, index $index")
synchronized(pagerLock) { pendingPagerCommands += pagerQueue to command }
_pagerQueue.value = pagerQueue
}
/** /**
* A [PlaybackDecision] command that is awaiting a view capable of responding to it. Null if * A [PlaybackDecision] command that is awaiting a view capable of responding to it. Null if
@@ -138,12 +158,10 @@ constructor(
override fun onIndexMoved(index: Int) { override fun onIndexMoved(index: Int) {
L.d("Index moved, updating current song") L.d("Index moved, updating current song")
L.d("Pager: command scroll to $index")
positionDs.value = playbackManager.progression.calculateElapsedPositionMs().msToDs() positionDs.value = playbackManager.progression.calculateElapsedPositionMs().msToDs()
song.value = playbackManager.currentSong song.value = playbackManager.currentSong
pagerCommand.put(PagerCommand(update = null, scroll = index)) updatePagerQueue(_pagerQueue.value.queue, index, PagerCommand(update = null, scroll = index))
_pagerQueue.value = _pagerQueue.value.copy(index = index)
} }
override fun onQueueChanged(queue: List<Song>, index: Int, change: QueueChange) { override fun onQueueChanged(queue: List<Song>, index: Int, change: QueueChange) {
@@ -153,22 +171,21 @@ constructor(
song.value = playbackManager.currentSong song.value = playbackManager.currentSong
} }
pagerCommand.put( updatePagerQueue(
queue,
index,
PagerCommand( PagerCommand(
update = change.instructions, update = change.instructions,
scroll = index.takeIf { change.type != QueueChange.Type.MAPPING }, scroll = index.takeIf { change.type != QueueChange.Type.MAPPING },
) ),
) )
L.d("Pager: command ${change.instructions}, scroll ${index.takeIf { change.type != QueueChange.Type.MAPPING }}, queue size ${queue.size}")
_pagerQueue.value = PagerQueue(queue = queue, index = index)
} }
override fun onQueueReordered(queue: List<Song>, index: Int, isShuffled: Boolean) { override fun onQueueReordered(queue: List<Song>, index: Int, isShuffled: Boolean) {
L.d("Queue completely changed, updating current song") L.d("Queue completely changed, updating current song")
this.isShuffled.value = isShuffled this.isShuffled.value = isShuffled
pagerCommand.put(PagerCommand(update = UpdateInstructions.Replace(0), scroll = index)) updatePagerQueue(queue, index, PagerCommand(update = UpdateInstructions.Replace(0), scroll = index))
_pagerQueue.value = PagerQueue(queue = queue, index = index)
} }
override fun onNewPlayback( override fun onNewPlayback(
@@ -182,8 +199,7 @@ constructor(
_parent.value = parent _parent.value = parent
this.isShuffled.value = isShuffled this.isShuffled.value = isShuffled
pagerCommand.put(PagerCommand(update = UpdateInstructions.Replace(0), scroll = index)) updatePagerQueue(queue, index, PagerCommand(update = UpdateInstructions.Replace(0), scroll = index))
_pagerQueue.value = PagerQueue(queue = queue, index = index)
} }
override fun onProgressionChanged(progression: Progression) { override fun onProgressionChanged(progression: Progression) {