Skip to content

Native Android client for Spotify, built with Jetpack Compose

Immersive queue: a row dragged far up or down is no longer put back when the list scrolls

kota-zoa78af541afaa
diff --git a/base/app/src/main/java/com/music/spotui/ui/immersive/lyrics/ImmersiveQueuePanel.kt b/base/app/src/main/java/com/music/spotui/ui/immersive/lyrics/ImmersiveQueuePanel.kt
index f9b32e3b..854c15c0 100644
--- a/base/app/src/main/java/com/music/spotui/ui/immersive/lyrics/ImmersiveQueuePanel.kt
+++ b/base/app/src/main/java/com/music/spotui/ui/immersive/lyrics/ImmersiveQueuePanel.kt
@@ -207,8 +207,9 @@ fun ImmersiveQueuePanel(modifier: Modifier = Modifier, contentPadding: PaddingVa
while (true) {
withFrameNanos { }
val info = listState.layoutInfo
- val row = info.visibleItemsInfo.firstOrNull { it.key == drag.key } ?: continue
- val y = row.offset + row.size / 2f + drag.offset
+ // The finger's place, not the row's slot: scrolling moves the slot (out of sight when far)
+ // and never the finger.
+ val y = drag.center ?: continue
val step = QueueDragMath.edgeStep(y, info.viewportSize.height.toFloat(), edge, maxStep)
if (step != 0f) drag.offset += listState.scrollBy(step)
}
@@ -330,6 +331,13 @@ private fun shownRows(queue: SongPlayer.EngineQueue, pending: QueueDragMath.Pend
private class QueueDrag {
var key by mutableStateOf<String?>(null)
var offset by mutableFloatStateOf(0f)
+ /**
+ * The dragged row's centre in the list's viewport: where it was taken plus how far the finger
+ * moved since, whatever the list scrolled. Where it hovers and the edge scroll are worked out
+ * from it: from the row's slot, they were lost once the slot left the screen (the list
+ * scrolling under a row dragged far), and the drag ended with the row back in its old place.
+ */
+ var center by mutableStateOf<Float?>(null)
/**
* Just dropped. The rows are drawn where they were (the dragged one under the finger, the others
* stepped aside) and the list takes the new order in the same frame, so no row has to move: a
@@ -351,8 +359,7 @@ private fun hoverOf(listState: LazyListState, rows: List<QRow>, drag: QueueDrag)
val from = rows.indexOfFirst { it.key == key }
if (from < 0) return null
val visible = listState.layoutInfo.visibleItemsInfo.associateBy { it.key }
- val me = visible[key] ?: return null
- val center = me.offset + me.size / 2f + drag.offset
+ val center = drag.center ?: return null
// A row out of sight is above the dragged one's centre if it comes before it in the list.
val mids = rows.filter { it.index != from }.map { r ->
visible[r.key]?.let { it.offset + it.size / 2f } ?: if (r.index < from) Float.NEGATIVE_INFINITY else Float.POSITIVE_INFINITY
@@ -410,6 +417,13 @@ private fun LazyListScope.queueSection(
val item = row.item
val rowPx = with(LocalDensity.current) { Immersive.RowHeight.toPx() }
val dragged = key == drag.key
+ // Kept composed while dragged: its slot scrolled out of sight, the list would drop the row
+ // and its gesture with it (see QueueDrag.center).
+ val pinnable = androidx.compose.ui.layout.LocalPinnableContainer.current
+ androidx.compose.runtime.DisposableEffect(dragged, pinnable) {
+ val handle = if (dragged) pinnable?.pin() else null
+ onDispose { handle?.release() }
+ }
// Rows the dragged one passes step aside by one slot.
val shift = hover()?.let { h -> if (dragged) 0 else QueueDragMath.step(row.index, h.from, h.slot) } ?: 0
val dropNow by rememberUpdatedState(onDrop)
@@ -482,7 +496,10 @@ private fun LazyListScope.queueSection(
.padding(8.dp)
.pointerInput(key) {
detectDragGestures(
- onDragStart = { drag.key = key; drag.offset = 0f },
+ onDragStart = {
+ drag.key = key; drag.offset = 0f
+ drag.center = offsetOf(key)?.let { it + rowPx / 2f }
+ },
onDragEnd = {
// Worked out while the row is still where it was dropped.
if (drag.key == key) {
@@ -497,11 +514,13 @@ private fun LazyListScope.queueSection(
}
drag.key = null
drag.offset = 0f
+ drag.center = null
},
- onDragCancel = { drag.key = null; drag.offset = 0f },
+ onDragCancel = { drag.key = null; drag.offset = 0f; drag.center = null },
onDrag = { change, amount ->
change.consume()
drag.offset += amount.y
+ drag.center = drag.center?.plus(amount.y)
},
)
},