Skip to content

Commit

Permalink
fix drag scroll exclusion
Browse files Browse the repository at this point in the history
  • Loading branch information
Calvin-LL committed May 13, 2024
1 parent 71c0973 commit 3563c55
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -405,13 +405,7 @@ open class ReorderableLazyCollectionState<out T> internal constructor(
internal fun onDrag(offset: Offset) {
draggingItemDraggedDelta += offset

if (!onMoveStateMutex.tryLock()) return

val draggingItem = draggingItemLayoutInfo
if (draggingItem == null) {
onMoveStateMutex.unlock()
return
}
val draggingItem = draggingItemLayoutInfo ?: return
// how far the dragging item is from the original position
val dragOffset = draggingItemOffset.reverseAxisIfNecessary()
.reverseAxisWithLayoutDirectionIfLazyVerticalStaggeredGridRtlFix()
Expand All @@ -421,20 +415,6 @@ open class ReorderableLazyCollectionState<out T> internal constructor(
scrollThresholdPadding
)

if (!scroller.isScrolling) {
val draggingItemRect = Rect(startOffset, endOffset)
// find a target item to move with
val targetItem = findTargetItem(draggingItemRect) {
it.index != draggingItem.index
}
if (targetItem != null) {
scope.launch {
moveItems(draggingItem, targetItem)
}
}
}
onMoveStateMutex.unlock()

// the distance from the top or left of the list to the center of the dragging item handle
val handleOffset =
when (state.layoutInfo.reverseLayout ||
Expand All @@ -453,7 +433,7 @@ open class ReorderableLazyCollectionState<out T> internal constructor(
val distanceFromEnd = (contentEndOffset - handleOffset.getAxis(orientation))
.coerceAtLeast(0f)

if (distanceFromStart < scrollThreshold) {
val isScrollingStarted = if (distanceFromStart < scrollThreshold) {
scroller.start(
Scroller.Direction.BACKWARD,
getScrollSpeedMultiplier(distanceFromStart),
Expand Down Expand Up @@ -492,7 +472,26 @@ open class ReorderableLazyCollectionState<out T> internal constructor(
)
} else {
scroller.tryStop()
false
}

if (!onMoveStateMutex.tryLock()) return
if (!scroller.isScrolling && !isScrollingStarted) {
val draggingItemRect = Rect(startOffset, endOffset)
// find a target item to move with
val targetItem = findTargetItem(
draggingItemRect,
items = state.layoutInfo.visibleItemsInfo,
) {
it.index != draggingItem.index
}
if (targetItem != null) {
scope.launch {
moveItems(draggingItem, targetItem)
}
}
}
onMoveStateMutex.unlock()
}

// keep dragging item in visible area to prevent it from disappearing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class Scroller internal constructor(

private var programmaticScrollJob: Job? = null
val isScrolling: Boolean
get() = programmaticScrollJob != null
get() = programmaticScrollJob?.isActive == true

private val scrollInfoChannel = Channel<ScrollInfo>(Channel.CONFLATED)

Expand All @@ -143,8 +143,8 @@ class Scroller internal constructor(
speedMultiplier: Float = 1f,
maxScrollDistanceProvider: () -> Float = { Float.MAX_VALUE },
onScroll: suspend () -> Unit = {},
) {
if (!canScroll(direction)) return
): Boolean {
if (!canScroll(direction)) return false

if (programmaticScrollJob == null) {
programmaticScrollJob = scope.launch {
Expand All @@ -156,6 +156,7 @@ class Scroller internal constructor(
ScrollInfo(direction, speedMultiplier, maxScrollDistanceProvider, onScroll)

scrollInfoChannel.trySend(scrollInfo)
return true
}

private suspend fun scrollLoop() {
Expand Down

0 comments on commit 3563c55

Please sign in to comment.