queue: update the drawer with precise per-change instructions
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* Copyright (c) 2026 Auxio Project
|
||||
* DeckRowDiff.kt is part of Auxio.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.oxycblt.auxio.playback.queue
|
||||
|
||||
import org.oxycblt.auxio.list.adapter.UpdateInstructions
|
||||
import org.oxycblt.musikr.Song
|
||||
|
||||
/**
|
||||
* Compute the minimal [UpdateInstructions] transforming the queue drawer's rows from [old] to
|
||||
* [new]. Returns null when nothing changed. Falls back to [UpdateInstructions.Replace] from the
|
||||
* first divergence when no single add, remove, or move explains the change.
|
||||
*/
|
||||
internal fun diffDeckRows(old: List<Song>, new: List<Song>): UpdateInstructions? {
|
||||
val oldUids = old.map { it.uid }
|
||||
val newUids = new.map { it.uid }
|
||||
if (oldUids == newUids) {
|
||||
return null
|
||||
}
|
||||
|
||||
// Common prefix and suffix frame the changed window.
|
||||
var prefix = 0
|
||||
while (prefix < minOf(oldUids.size, newUids.size) && oldUids[prefix] == newUids[prefix]) {
|
||||
prefix++
|
||||
}
|
||||
var suffix = 0
|
||||
while (suffix < minOf(oldUids.size, newUids.size) - prefix &&
|
||||
oldUids[oldUids.size - 1 - suffix] == newUids[newUids.size - 1 - suffix]
|
||||
) {
|
||||
suffix++
|
||||
}
|
||||
|
||||
val oldWindow = oldUids.subList(prefix, oldUids.size - suffix)
|
||||
val newWindow = newUids.subList(prefix, newUids.size - suffix)
|
||||
return when {
|
||||
// Pure addition or removal.
|
||||
oldWindow.isEmpty() -> UpdateInstructions.Add(prefix, newWindow.size)
|
||||
newWindow.isEmpty() -> UpdateInstructions.Remove(prefix, oldWindow.size)
|
||||
// A single element moved within the window.
|
||||
oldWindow.size == newWindow.size -> {
|
||||
val move = findSingleMove(oldWindow, newWindow)
|
||||
if (move != null) {
|
||||
UpdateInstructions.Move(prefix + move.first, prefix + move.second)
|
||||
} else {
|
||||
UpdateInstructions.Replace(prefix)
|
||||
}
|
||||
}
|
||||
else -> UpdateInstructions.Replace(prefix)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the single element move transforming [old] into [new], as its indices in each list. Null
|
||||
* when no single move explains the difference (including replacements, which equalize on removal
|
||||
* without the element actually moving).
|
||||
*/
|
||||
private fun <T> findSingleMove(old: List<T>, new: List<T>): Pair<Int, Int>? {
|
||||
for (from in old.indices) {
|
||||
val remainder = old.toMutableList().apply { removeAt(from) }
|
||||
for (to in new.indices) {
|
||||
if (old[from] == new[to] && new.toMutableList().apply { removeAt(to) } == remainder) {
|
||||
return from to to
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
@@ -90,7 +90,7 @@ class QueueAdapter(private val listener: EditClickListListener<Song>) :
|
||||
// TODO: Don't update when the index has not moved.
|
||||
if (currentIndex < lastIndex) {
|
||||
L.d("Moved backwards, must update items above last index")
|
||||
val since = if (incremental) currentIndex else 0
|
||||
val since = (if (incremental) currentIndex else 0).coerceAtLeast(0)
|
||||
notifyItemRangeChanged(
|
||||
since,
|
||||
lastIndex - since + 1,
|
||||
@@ -98,12 +98,15 @@ class QueueAdapter(private val listener: EditClickListListener<Song>) :
|
||||
)
|
||||
} else if (currentIndex > lastIndex) {
|
||||
L.d("Moved forwards, update items after index")
|
||||
val since = if (incremental) lastIndex else 0
|
||||
val since = (if (incremental) lastIndex else 0).coerceAtLeast(0)
|
||||
notifyItemRangeChanged(
|
||||
since,
|
||||
currentIndex - since + 1,
|
||||
PAYLOAD_UPDATE_POSITION,
|
||||
)
|
||||
} else if (!incremental && currentIndex >= 0) {
|
||||
// A list operation replaced the playing row's occupant without moving the index.
|
||||
notifyItemChanged(currentIndex, PAYLOAD_UPDATE_POSITION)
|
||||
}
|
||||
|
||||
val lastIsPlaying = this.isPlaying
|
||||
|
||||
@@ -89,9 +89,9 @@ class QueueFragment : ViewBindingFragment<FragmentQueueBinding>(), EditClickList
|
||||
super.onDestroyBinding(binding)
|
||||
touchHelper = null
|
||||
binding.queueRecycler.adapter = null
|
||||
// Avoid possible race conditions that could cause a bad instruction to be consumed
|
||||
// during list initialization and crash the app. Could happen if the user is fast enough.
|
||||
queueModel.queueInstructions.consume()
|
||||
// Discard any updates accumulated while the view was gone; a recreated view rebuilds
|
||||
// from the current rows.
|
||||
queueModel.drainQueueUpdates()
|
||||
}
|
||||
|
||||
override fun onClick(item: Song, viewHolder: RecyclerView.ViewHolder) {
|
||||
@@ -112,9 +112,9 @@ class QueueFragment : ViewBindingFragment<FragmentQueueBinding>(), EditClickList
|
||||
private fun updateQueue(queue: List<Song>, index: Int, isPlaying: Boolean) {
|
||||
val binding = requireBinding()
|
||||
|
||||
val instructions = queueModel.queueInstructions.consume()
|
||||
queueAdapter.update(queue, instructions)
|
||||
queueAdapter.setPosition(index, isPlaying, instructions == null)
|
||||
val updates = queueModel.drainQueueUpdates()
|
||||
updates.forEach { (rows, instructions) -> queueAdapter.update(rows, instructions) }
|
||||
queueAdapter.setPosition(index, isPlaying, updates.isEmpty())
|
||||
binding.queueEmpty.isVisible = queue.isEmpty()
|
||||
|
||||
// If requested, scroll to a new item (occurs when the index moves)
|
||||
|
||||
@@ -45,9 +45,22 @@ class QueueViewModel @Inject constructor(private val playbackManager: PlaybackSt
|
||||
private val _queue = MutableStateFlow(listOf<Song>())
|
||||
/** The drawer rows: the playing deck song (if any) followed by the waiting deck songs. */
|
||||
val queue: StateFlow<List<Song>> = _queue
|
||||
private val _queueInstructions = MutableEvent<UpdateInstructions>()
|
||||
/** Instructions for how to update [queue] in the UI. */
|
||||
val queueInstructions: Event<UpdateInstructions> = _queueInstructions
|
||||
|
||||
// Manager callbacks can arrive on any thread; the fragment drains on the main thread.
|
||||
private val pendingLock = Any()
|
||||
private val pendingUpdates = ArrayDeque<Pair<List<Song>, UpdateInstructions>>()
|
||||
|
||||
/**
|
||||
* Drain the row updates accumulated since the last drain, as (rows, instructions) pairs in
|
||||
* order. Each pair is a complete intermediate state, so applying them sequentially is safe
|
||||
* even when flow emissions were conflated.
|
||||
*/
|
||||
fun drainQueueUpdates(): List<Pair<List<Song>, UpdateInstructions>> =
|
||||
synchronized(pendingLock) {
|
||||
val drained = pendingUpdates.toList()
|
||||
pendingUpdates.clear()
|
||||
drained
|
||||
}
|
||||
|
||||
/** Controls whether the queue should be force-scrolled to a particular location. */
|
||||
val scrollTo: Event<Int>
|
||||
@@ -156,8 +169,13 @@ class QueueViewModel @Inject constructor(private val playbackManager: PlaybackSt
|
||||
|
||||
/** Republish the drawer rows from the manager's deck state. */
|
||||
private fun republish() {
|
||||
_queue.value = listOfNotNull(playbackManager.deckCurrent) + playbackManager.deck
|
||||
val rows = listOfNotNull(playbackManager.deckCurrent) + playbackManager.deck
|
||||
val instructions = diffDeckRows(_queue.value, rows)
|
||||
if (instructions != null) {
|
||||
// Complete (rows, instruction) pairs chain correctly under flow conflation.
|
||||
synchronized(pendingLock) { pendingUpdates += rows to instructions }
|
||||
}
|
||||
_queue.value = rows
|
||||
index.value = if (playbackManager.deckCurrent != null) 0 else -1
|
||||
_queueInstructions.put(UpdateInstructions.Replace(0))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
* Copyright (c) 2026 Auxio Project
|
||||
* DeckRowDiffTest.kt is part of Auxio.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.oxycblt.auxio.playback.queue
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Test
|
||||
import org.oxycblt.auxio.list.adapter.UpdateInstructions
|
||||
import org.oxycblt.auxio.playback.state.fakeSong
|
||||
|
||||
/**
|
||||
* The queue drawer renders deck rows; every update must carry the minimal [UpdateInstructions]
|
||||
* for the change, or the list flashes (full replaces) and ghosts (desynced drags). See the D9
|
||||
* device round.
|
||||
*/
|
||||
class DeckRowDiffTest {
|
||||
|
||||
@Test
|
||||
fun `identical rows produce no instruction`() {
|
||||
val rows = listOf(fakeSong(), fakeSong())
|
||||
assertNull(diffDeckRows(rows, rows.toList()))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `appending rows produces an Add`() {
|
||||
val old = listOf(fakeSong(), fakeSong(), fakeSong())
|
||||
val new = old + fakeSong() + fakeSong()
|
||||
assertEquals(UpdateInstructions.Add(at = 3, size = 2), diffDeckRows(old, new))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `the deck advancing drops row 0 with a Remove`() {
|
||||
val old = listOf(fakeSong(), fakeSong(), fakeSong())
|
||||
val new = old.drop(1)
|
||||
assertEquals(UpdateInstructions.Remove(at = 0, size = 1), diffDeckRows(old, new))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `removing an interior row produces a Remove`() {
|
||||
val old = listOf(fakeSong(), fakeSong(), fakeSong())
|
||||
val new = listOf(old[0], old[2])
|
||||
assertEquals(UpdateInstructions.Remove(at = 1, size = 1), diffDeckRows(old, new))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `emptying the rows produces a Remove`() {
|
||||
val old = listOf(fakeSong(), fakeSong())
|
||||
assertEquals(UpdateInstructions.Remove(at = 0, size = 2), diffDeckRows(old, emptyList()))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `an end drag produces a Move`() {
|
||||
val old = listOf(fakeSong(), fakeSong(), fakeSong())
|
||||
val new = listOf(old[1], old[2], old[0])
|
||||
assertEquals(UpdateInstructions.Move(from = 0, to = 2), diffDeckRows(old, new))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `an adjacent drag produces a Move`() {
|
||||
val old = listOf(fakeSong(), fakeSong(), fakeSong())
|
||||
val new = listOf(old[0], old[2], old[1])
|
||||
assertEquals(UpdateInstructions.Move(from = 1, to = 2), diffDeckRows(old, new))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a replacement produces a Replace from the first divergence`() {
|
||||
val old = listOf(fakeSong(), fakeSong(), fakeSong())
|
||||
val new = listOf(old[0], fakeSong(), old[2])
|
||||
assertEquals(UpdateInstructions.Replace(from = 1), diffDeckRows(old, new))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a wholesale change produces a Replace`() {
|
||||
val old = listOf(fakeSong(), fakeSong())
|
||||
val new = listOf(fakeSong(), fakeSong(), fakeSong())
|
||||
assertEquals(UpdateInstructions.Replace(from = 0), diffDeckRows(old, new))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user