Skip to content

Commit

Permalink
LazyList reverseLayout support
Browse files Browse the repository at this point in the history
  • Loading branch information
aclassen committed Jun 14, 2022
1 parent f9976de commit e5c6260
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 11 deletions.
6 changes: 3 additions & 3 deletions android/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ plugins {
}

dependencies {
implementation("org.burnoutcrew.composereorderable:reorderable:0.9.0")
implementation("androidx.compose.runtime:runtime:1.2.0-beta02")
implementation("androidx.compose.material:material:1.2.0-beta02")
implementation(project(":reorderable"))
implementation("androidx.compose.runtime:runtime:1.2.0-beta03")
implementation("androidx.compose.material:material:1.2.0-beta03")
implementation("androidx.activity:activity-compose:1.4.0")
implementation("com.google.android.material:material:1.6.1")
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1")
Expand Down
2 changes: 1 addition & 1 deletion desktop/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ kotlin {
sourceSets {
val jvmMain by getting {
dependencies {
implementation("org.burnoutcrew.composereorderable:reorderable:0.9.0")
implementation(project(":reorderable"))
implementation(compose.desktop.currentOs)
}
}
Expand Down
2 changes: 1 addition & 1 deletion reorderable/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
}

group = "org.burnoutcrew.composereorderable"
version = "0.9.0"
version = "0.9.1"

kotlin {
jvm()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.burnoutcrew.reorderable

import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.gestures.ScrollableState
import androidx.compose.foundation.gestures.scrollBy
import androidx.compose.foundation.lazy.LazyListItemInfo
import androidx.compose.foundation.lazy.LazyListState
Expand All @@ -25,7 +26,9 @@ import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.CoroutineScope

Expand All @@ -50,13 +53,24 @@ fun rememberReorderableLazyListState(
.collect { state.onDrag(0, 0) }
}

Scroller(state, listState, run {
var reverseDirection = !listState.layoutInfo.reverseLayout
if (LocalLayoutDirection.current == LayoutDirection.Rtl && 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) {
while (true) {
val diff = state.scrollChannel.receive()
listState.scrollBy(diff)
listState.scrollBy(diff * direction)
}
}
return state
}

class ReorderableLazyListState(
Expand All @@ -71,13 +85,29 @@ class ReorderableLazyListState(
override val isVerticalScroll: Boolean
get() = listState.layoutInfo.orientation == Orientation.Vertical
override val LazyListItemInfo.left: Int
get() = if (isVerticalScroll) 0 else offset
get() = when {
isVerticalScroll -> 0
listState.layoutInfo.reverseLayout -> listState.layoutInfo.viewportSize.width - offset - size
else -> offset
}
override val LazyListItemInfo.top: Int
get() = if (isVerticalScroll) offset else 0
get() = when {
!isVerticalScroll -> 0
listState.layoutInfo.reverseLayout -> listState.layoutInfo.viewportSize.height - offset - size
else -> offset
}
override val LazyListItemInfo.right: Int
get() = if (isVerticalScroll) 0 else offset + size
get() = when {
isVerticalScroll -> 0
listState.layoutInfo.reverseLayout -> listState.layoutInfo.viewportSize.width - offset
else -> offset + size
}
override val LazyListItemInfo.bottom: Int
get() = if (isVerticalScroll) offset + size else 0
get() = when {
!isVerticalScroll -> 0
listState.layoutInfo.reverseLayout -> listState.layoutInfo.viewportSize.height - offset
else -> offset + size
}
override val LazyListItemInfo.width: Int
get() = if (isVerticalScroll) 0 else size
override val LazyListItemInfo.height: Int
Expand Down

0 comments on commit e5c6260

Please sign in to comment.