playback: never lose pager commands to event overwrite
This commit is contained in:
@@ -347,11 +347,9 @@ class PlaybackPanelFragment :
|
||||
// but only on some devices.
|
||||
val binding = binding ?: return
|
||||
|
||||
val command = playbackModel.pagerCommand.consume()
|
||||
L.d(
|
||||
"Pager: applying queue size ${queue.queue.size}, index ${queue.index}, command $command, pager at ${binding.playbackPager.currentItem}"
|
||||
)
|
||||
if (command == null) {
|
||||
val commands = playbackModel.drainPagerCommands()
|
||||
L.d("Pager: applying ${commands.size} command(s), pager at ${binding.playbackPager.currentItem}")
|
||||
if (commands.isEmpty()) {
|
||||
// This probably shouldn't happen in practice, as QueueViewModel directly
|
||||
// attaches to PlaybackStateManager and will basically always initialize
|
||||
// with a command as a result.
|
||||
@@ -363,24 +361,28 @@ class PlaybackPanelFragment :
|
||||
return
|
||||
}
|
||||
|
||||
if (command.update != null) {
|
||||
// queue needs to be updated.
|
||||
coverPagerAdapter.update(queue.queue, command.update)
|
||||
}
|
||||
|
||||
if (command.scroll != null) {
|
||||
// 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
|
||||
// Apply every command in order; each carries the queue state it was made against, so a
|
||||
// burst (like a drain's removal plus scroll) cannot lose its intermediate step.
|
||||
for ((commandQueue, command) in commands) {
|
||||
if (command.update != null) {
|
||||
// queue needs to be updated.
|
||||
coverPagerAdapter.update(commandQueue.queue, command.update)
|
||||
}
|
||||
if (command.update == null && abs(delta) == 1) {
|
||||
binding.playbackPager.smoothScrollByPageTo(command.scroll)
|
||||
} else {
|
||||
binding.playbackPager.setCurrentItem(command.scroll, false)
|
||||
|
||||
if (command.scroll != null) {
|
||||
// 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
|
||||
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. */
|
||||
val pagerQueue: StateFlow<PagerQueue> = _pagerQueue
|
||||
|
||||
/** Specialized ViewPager2-friendly queue commands */
|
||||
val pagerCommand: Event<PagerCommand>
|
||||
field = MutableEvent<PagerCommand>()
|
||||
// Manager callbacks can arrive on any thread; the panel drains on the main thread.
|
||||
private val pagerLock = Any()
|
||||
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
|
||||
@@ -138,12 +158,10 @@ constructor(
|
||||
|
||||
override fun onIndexMoved(index: Int) {
|
||||
L.d("Index moved, updating current song")
|
||||
L.d("Pager: command scroll to $index")
|
||||
positionDs.value = playbackManager.progression.calculateElapsedPositionMs().msToDs()
|
||||
song.value = playbackManager.currentSong
|
||||
|
||||
pagerCommand.put(PagerCommand(update = null, scroll = index))
|
||||
_pagerQueue.value = _pagerQueue.value.copy(index = index)
|
||||
updatePagerQueue(_pagerQueue.value.queue, index, PagerCommand(update = null, scroll = index))
|
||||
}
|
||||
|
||||
override fun onQueueChanged(queue: List<Song>, index: Int, change: QueueChange) {
|
||||
@@ -153,22 +171,21 @@ constructor(
|
||||
song.value = playbackManager.currentSong
|
||||
}
|
||||
|
||||
pagerCommand.put(
|
||||
updatePagerQueue(
|
||||
queue,
|
||||
index,
|
||||
PagerCommand(
|
||||
update = change.instructions,
|
||||
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) {
|
||||
L.d("Queue completely changed, updating current song")
|
||||
this.isShuffled.value = isShuffled
|
||||
|
||||
pagerCommand.put(PagerCommand(update = UpdateInstructions.Replace(0), scroll = index))
|
||||
_pagerQueue.value = PagerQueue(queue = queue, index = index)
|
||||
updatePagerQueue(queue, index, PagerCommand(update = UpdateInstructions.Replace(0), scroll = index))
|
||||
}
|
||||
|
||||
override fun onNewPlayback(
|
||||
@@ -182,8 +199,7 @@ constructor(
|
||||
_parent.value = parent
|
||||
this.isShuffled.value = isShuffled
|
||||
|
||||
pagerCommand.put(PagerCommand(update = UpdateInstructions.Replace(0), scroll = index))
|
||||
_pagerQueue.value = PagerQueue(queue = queue, index = index)
|
||||
updatePagerQueue(queue, index, PagerCommand(update = UpdateInstructions.Replace(0), scroll = index))
|
||||
}
|
||||
|
||||
override fun onProgressionChanged(progression: Progression) {
|
||||
|
||||
Reference in New Issue
Block a user