Skip to content

Commit e5c6260

Browse files
committed
LazyList reverseLayout support
1 parent f9976de commit e5c6260

File tree

4 files changed

+41
-11
lines changed

4 files changed

+41
-11
lines changed

android/build.gradle.kts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ plugins {
55
}
66

77
dependencies {
8-
implementation("org.burnoutcrew.composereorderable:reorderable:0.9.0")
9-
implementation("androidx.compose.runtime:runtime:1.2.0-beta02")
10-
implementation("androidx.compose.material:material:1.2.0-beta02")
8+
implementation(project(":reorderable"))
9+
implementation("androidx.compose.runtime:runtime:1.2.0-beta03")
10+
implementation("androidx.compose.material:material:1.2.0-beta03")
1111
implementation("androidx.activity:activity-compose:1.4.0")
1212
implementation("com.google.android.material:material:1.6.1")
1313
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1")

desktop/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ kotlin {
1515
sourceSets {
1616
val jvmMain by getting {
1717
dependencies {
18-
implementation("org.burnoutcrew.composereorderable:reorderable:0.9.0")
18+
implementation(project(":reorderable"))
1919
implementation(compose.desktop.currentOs)
2020
}
2121
}

reorderable/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ plugins {
99
}
1010

1111
group = "org.burnoutcrew.composereorderable"
12-
version = "0.9.0"
12+
version = "0.9.1"
1313

1414
kotlin {
1515
jvm()

reorderable/src/commonMain/kotlin/org/burnoutcrew/reorderable/ReorderableLazyListState.kt

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.burnoutcrew.reorderable
1717

1818
import androidx.compose.foundation.gestures.Orientation
19+
import androidx.compose.foundation.gestures.ScrollableState
1920
import androidx.compose.foundation.gestures.scrollBy
2021
import androidx.compose.foundation.lazy.LazyListItemInfo
2122
import androidx.compose.foundation.lazy.LazyListState
@@ -25,7 +26,9 @@ import androidx.compose.runtime.LaunchedEffect
2526
import androidx.compose.runtime.remember
2627
import androidx.compose.runtime.rememberCoroutineScope
2728
import androidx.compose.ui.platform.LocalDensity
29+
import androidx.compose.ui.platform.LocalLayoutDirection
2830
import androidx.compose.ui.unit.Dp
31+
import androidx.compose.ui.unit.LayoutDirection
2932
import androidx.compose.ui.unit.dp
3033
import 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

6276
class 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

Comments
 (0)