1616package org.burnoutcrew.reorderable
1717
1818import androidx.compose.foundation.gestures.Orientation
19+ import androidx.compose.foundation.gestures.ScrollableState
1920import androidx.compose.foundation.gestures.scrollBy
2021import androidx.compose.foundation.lazy.LazyListItemInfo
2122import androidx.compose.foundation.lazy.LazyListState
@@ -25,7 +26,9 @@ import androidx.compose.runtime.LaunchedEffect
2526import androidx.compose.runtime.remember
2627import androidx.compose.runtime.rememberCoroutineScope
2728import androidx.compose.ui.platform.LocalDensity
29+ import androidx.compose.ui.platform.LocalLayoutDirection
2830import androidx.compose.ui.unit.Dp
31+ import androidx.compose.ui.unit.LayoutDirection
2932import androidx.compose.ui.unit.dp
3033import kotlinx.coroutines.CoroutineScope
3134
@@ -50,13 +53,24 @@ fun rememberReorderableLazyListState(
5053 .collect { state.onDrag(0 , 0 ) }
5154 }
5255
56+ Scroller (state, listState, run {
57+ var reverseDirection = ! listState.layoutInfo.reverseLayout
58+ if (LocalLayoutDirection .current == LayoutDirection .Rtl && listState.layoutInfo.orientation != Orientation .Vertical ) {
59+ reverseDirection = ! reverseDirection
60+ }
61+ if (reverseDirection) - 1f else 1f
62+ })
63+ return state
64+ }
65+
66+ @Composable
67+ private fun Scroller (state : ReorderableState <* >, listState : ScrollableState , direction : Float ) {
5368 LaunchedEffect (state) {
5469 while (true ) {
5570 val diff = state.scrollChannel.receive()
56- listState.scrollBy(diff)
71+ listState.scrollBy(diff * direction )
5772 }
5873 }
59- return state
6074}
6175
6276class ReorderableLazyListState (
@@ -71,13 +85,29 @@ class ReorderableLazyListState(
7185 override val isVerticalScroll: Boolean
7286 get() = listState.layoutInfo.orientation == Orientation .Vertical
7387 override val LazyListItemInfo .left: Int
74- get() = if (isVerticalScroll) 0 else offset
88+ get() = when {
89+ isVerticalScroll -> 0
90+ listState.layoutInfo.reverseLayout -> listState.layoutInfo.viewportSize.width - offset - size
91+ else -> offset
92+ }
7593 override val LazyListItemInfo .top: Int
76- get() = if (isVerticalScroll) offset else 0
94+ get() = when {
95+ ! isVerticalScroll -> 0
96+ listState.layoutInfo.reverseLayout -> listState.layoutInfo.viewportSize.height - offset - size
97+ else -> offset
98+ }
7799 override val LazyListItemInfo .right: Int
78- get() = if (isVerticalScroll) 0 else offset + size
100+ get() = when {
101+ isVerticalScroll -> 0
102+ listState.layoutInfo.reverseLayout -> listState.layoutInfo.viewportSize.width - offset
103+ else -> offset + size
104+ }
79105 override val LazyListItemInfo .bottom: Int
80- get() = if (isVerticalScroll) offset + size else 0
106+ get() = when {
107+ ! isVerticalScroll -> 0
108+ listState.layoutInfo.reverseLayout -> listState.layoutInfo.viewportSize.height - offset
109+ else -> offset + size
110+ }
81111 override val LazyListItemInfo .width: Int
82112 get() = if (isVerticalScroll) 0 else size
83113 override val LazyListItemInfo .height: Int
0 commit comments