From 454521b41bedf36742f98a8000a9c63f4650ea61 Mon Sep 17 00:00:00 2001 From: Calvin Liang Date: Thu, 12 Sep 2024 13:53:15 -0700 Subject: [PATCH] fix temporary item offset after onMove not reset in some conditions fix #50 --- .../calvin/reorderable/ReorderableLazyCollection.kt | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/reorderable/src/commonMain/kotlin/sh/calvin/reorderable/ReorderableLazyCollection.kt b/reorderable/src/commonMain/kotlin/sh/calvin/reorderable/ReorderableLazyCollection.kt index 9ede9f9..3f0594f 100644 --- a/reorderable/src/commonMain/kotlin/sh/calvin/reorderable/ReorderableLazyCollection.kt +++ b/reorderable/src/commonMain/kotlin/sh/calvin/reorderable/ReorderableLazyCollection.kt @@ -294,7 +294,7 @@ open class ReorderableLazyCollectionState internal constructor( // visibleItemsInfo doesn't update immediately after onMove, draggingItemLayoutInfo.item may be outdated for a short time. // not a clean solution, but it works. - private var draggingItemTargetIndex by mutableStateOf(null) + private var oldDraggingItemIndex by mutableStateOf(null) private var predictedDraggingItemOffset by mutableStateOf(null) private val draggingItemLayoutInfo: LazyCollectionItemInfo? @@ -304,8 +304,8 @@ open class ReorderableLazyCollectionState internal constructor( internal val draggingItemOffset: Offset get() = (draggingItemLayoutInfo?.let { val offset = - if (it.index == draggingItemTargetIndex || draggingItemTargetIndex == null) { - draggingItemTargetIndex = null + if (it.index != oldDraggingItemIndex || oldDraggingItemIndex == null) { + oldDraggingItemIndex = null predictedDraggingItemOffset = null it.offset } else { @@ -398,7 +398,7 @@ open class ReorderableLazyCollectionState internal constructor( draggingItemKey = null draggingItemInitialOffset = previousDraggingItemInitialOffset ?: IntOffset.Zero scroller.tryStop() - draggingItemTargetIndex = null + oldDraggingItemIndex = null predictedDraggingItemOffset = null } @@ -630,6 +630,8 @@ open class ReorderableLazyCollectionState internal constructor( try { onMoveStateMutex.withLock { + oldDraggingItemIndex = draggingItem.index + scope.(onMoveState.value)(draggingItem.data, targetItem.data) predictedDraggingItemOffset = if (targetItem.index > draggingItem.index) { @@ -637,13 +639,14 @@ open class ReorderableLazyCollectionState internal constructor( } else { targetItem.offset } - draggingItemTargetIndex = targetItem.index withTimeout(MoveItemsLayoutInfoUpdateMaxWaitDuration) { // the first result from layoutInfoFlow is the current layoutInfo // the second result is the updated layoutInfo layoutInfoFlow.take(2).collect() } + + predictedDraggingItemOffset = null } } catch (e: CancellationException) { // do nothing