queue: optimize only-position index moves

This commit is contained in:
Alexander Capehart
2026-11-01 16:17:43 -07:00
parent 4044f62dfb
commit 1c0e29e9b1
2 changed files with 23 additions and 6 deletions
@@ -80,7 +80,7 @@ class QueueAdapter(private val listener: EditClickListListener<Song>) :
* @param index The position of the currently playing item in the queue.
* @param isPlaying Whether playback is ongoing or paused.
*/
fun setPosition(index: Int, isPlaying: Boolean) {
fun setPosition(index: Int, isPlaying: Boolean, incremental: Boolean) {
L.d("Updating index")
val lastIndex = currentIndex
currentIndex = index
@@ -91,13 +91,28 @@ 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")
notifyItemRangeChanged(0, lastIndex + 1, PAYLOAD_UPDATE_POSITION)
} else {
val since = if (incremental) currentIndex else 0
notifyItemRangeChanged(
since,
lastIndex - since + 1,
PAYLOAD_UPDATE_POSITION
)
} else if (currentIndex > lastIndex) {
L.d("Moved forwards, update items after index")
notifyItemRangeChanged(0, currentIndex + 1, PAYLOAD_UPDATE_POSITION)
val since = if (incremental) lastIndex else 0
notifyItemRangeChanged(
since,
currentIndex - since + 1,
PAYLOAD_UPDATE_POSITION
)
}
val lastIsPlaying = this.isPlaying
this.isPlaying = isPlaying
if (this.isPlaying != lastIsPlaying) {
L.d("Changed play state")
notifyItemChanged(currentIndex)
}
}
private companion object {
@@ -30,6 +30,7 @@ import dagger.hilt.android.AndroidEntryPoint
import kotlin.math.min
import org.oxycblt.auxio.databinding.FragmentQueueBinding
import org.oxycblt.auxio.list.EditClickListListener
import org.oxycblt.auxio.list.adapter.UpdateInstructions
import org.oxycblt.auxio.playback.PlaybackViewModel
import org.oxycblt.auxio.ui.ViewBindingFragment
import org.oxycblt.auxio.util.collectImmediately
@@ -111,8 +112,9 @@ class QueueFragment : ViewBindingFragment<FragmentQueueBinding>(), EditClickList
private fun updateQueue(queue: List<Song>, index: Int, isPlaying: Boolean) {
val binding = requireBinding()
queueAdapter.update(queue, queueModel.queueInstructions.consume())
queueAdapter.setPosition(index, isPlaying)
val instructions = queueModel.queueInstructions.consume()
queueAdapter.update(queue, instructions)
queueAdapter.setPosition(index, isPlaying, instructions == null)
// If requested, scroll to a new item (occurs when the index moves)
val scrollTo = queueModel.scrollTo.consume()