Skip to content

Commit

Permalink
fix temporary item offset after onMove not reset in some conditions
Browse files Browse the repository at this point in the history
fix #50
  • Loading branch information
Calvin-LL committed Sep 12, 2024
1 parent a02d7ce commit 454521b
Showing 1 changed file with 8 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ open class ReorderableLazyCollectionState<out T> 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<Int?>(null)
private var oldDraggingItemIndex by mutableStateOf<Int?>(null)
private var predictedDraggingItemOffset by mutableStateOf<IntOffset?>(null)

private val draggingItemLayoutInfo: LazyCollectionItemInfo<T>?
Expand All @@ -304,8 +304,8 @@ open class ReorderableLazyCollectionState<out T> 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 {
Expand Down Expand Up @@ -398,7 +398,7 @@ open class ReorderableLazyCollectionState<out T> internal constructor(
draggingItemKey = null
draggingItemInitialOffset = previousDraggingItemInitialOffset ?: IntOffset.Zero
scroller.tryStop()
draggingItemTargetIndex = null
oldDraggingItemIndex = null
predictedDraggingItemOffset = null
}

Expand Down Expand Up @@ -630,20 +630,23 @@ open class ReorderableLazyCollectionState<out T> internal constructor(

try {
onMoveStateMutex.withLock {
oldDraggingItemIndex = draggingItem.index

scope.(onMoveState.value)(draggingItem.data, targetItem.data)

predictedDraggingItemOffset = if (targetItem.index > draggingItem.index) {
(targetItem.offset + targetItem.size) - draggingItem.size
} 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
Expand Down

0 comments on commit 454521b

Please sign in to comment.