Skip to content

Commit

Permalink
Update ReorderableLazyListState.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
aclassen committed Jun 16, 2022
1 parent e5c6260 commit 144c6e3
Showing 1 changed file with 19 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -81,7 +75,14 @@ class ReorderableLazyListState(
canDragOver: ((index: ItemPosition) -> Boolean)? = null,
onDragEnd: ((startIndex: Int, endIndex: Int) -> (Unit))? = null,
dragCancelledAnimation: DragCancelledAnimation = SpringDragCancelledAnimation()
) : ReorderableState<LazyListItemInfo>(scope, maxScrollPerFrame, onMove, canDragOver, onDragEnd, dragCancelledAnimation) {
) : ReorderableState<LazyListItemInfo>(
scope,
maxScrollPerFrame,
onMove,
canDragOver,
onDragEnd,
dragCancelledAnimation
) {
override val isVerticalScroll: Boolean
get() = listState.layoutInfo.orientation == Orientation.Vertical
override val LazyListItemInfo.left: Int
Expand Down Expand Up @@ -145,7 +146,12 @@ class ReorderableLazyListState(
super.findTargets(x, 0, selected)
}

override fun chooseDropItem(draggedItemInfo: LazyListItemInfo?, items: List<LazyListItemInfo>, curX: Int, curY: Int) =
override fun chooseDropItem(
draggedItemInfo: LazyListItemInfo?,
items: List<LazyListItemInfo>,
curX: Int,
curY: Int
) =
if (isVerticalScroll) {
super.chooseDropItem(draggedItemInfo, items, 0, curY)
} else {
Expand Down

0 comments on commit 144c6e3

Please sign in to comment.