From 144c6e3fdffa64959b6b9c92fff08ffc75ae3760 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=20Cla=C3=9Fen?= Date: Thu, 16 Jun 2022 15:46:39 +0200 Subject: [PATCH] Update ReorderableLazyListState.kt --- .../reorderable/ReorderableLazyListState.kt | 32 +++++++++++-------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/reorderable/src/commonMain/kotlin/org/burnoutcrew/reorderable/ReorderableLazyListState.kt b/reorderable/src/commonMain/kotlin/org/burnoutcrew/reorderable/ReorderableLazyListState.kt index f79b5cc..cde660e 100644 --- a/reorderable/src/commonMain/kotlin/org/burnoutcrew/reorderable/ReorderableLazyListState.kt +++ b/reorderable/src/commonMain/kotlin/org/burnoutcrew/reorderable/ReorderableLazyListState.kt @@ -47,30 +47,24 @@ fun rememberReorderableLazyListState( val state = remember(listState) { ReorderableLazyListState(listState, scope, maxScroll, onMove, canDragOver, onDragEnd, dragCancelledAnimation) } - + val isRtl = LocalLayoutDirection.current == LayoutDirection.Rtl LaunchedEffect(state) { state.visibleItemsChanged() .collect { state.onDrag(0, 0) } } - Scroller(state, listState, run { + LaunchedEffect(state) { var reverseDirection = !listState.layoutInfo.reverseLayout - if (LocalLayoutDirection.current == LayoutDirection.Rtl && listState.layoutInfo.orientation != Orientation.Vertical) { + if (isRtl && listState.layoutInfo.orientation != Orientation.Vertical) { reverseDirection = !reverseDirection } - if (reverseDirection) -1f else 1f - }) - return state -} - -@Composable -private fun Scroller(state: ReorderableState<*>, listState: ScrollableState, direction: Float) { - LaunchedEffect(state) { + val direction = if (reverseDirection) 1f else -1f while (true) { val diff = state.scrollChannel.receive() listState.scrollBy(diff * direction) } } + return state } class ReorderableLazyListState( @@ -81,7 +75,14 @@ class ReorderableLazyListState( canDragOver: ((index: ItemPosition) -> Boolean)? = null, onDragEnd: ((startIndex: Int, endIndex: Int) -> (Unit))? = null, dragCancelledAnimation: DragCancelledAnimation = SpringDragCancelledAnimation() -) : ReorderableState(scope, maxScrollPerFrame, onMove, canDragOver, onDragEnd, dragCancelledAnimation) { +) : ReorderableState( + scope, + maxScrollPerFrame, + onMove, + canDragOver, + onDragEnd, + dragCancelledAnimation +) { override val isVerticalScroll: Boolean get() = listState.layoutInfo.orientation == Orientation.Vertical override val LazyListItemInfo.left: Int @@ -145,7 +146,12 @@ class ReorderableLazyListState( super.findTargets(x, 0, selected) } - override fun chooseDropItem(draggedItemInfo: LazyListItemInfo?, items: List, curX: Int, curY: Int) = + override fun chooseDropItem( + draggedItemInfo: LazyListItemInfo?, + items: List, + curX: Int, + curY: Int + ) = if (isVerticalScroll) { super.chooseDropItem(draggedItemInfo, items, 0, curY) } else {