Skip to content

Commit

Permalink
Make default saver for LazyListState private (#1600)
Browse files Browse the repository at this point in the history
  • Loading branch information
jingwei99 authored Oct 17, 2023
1 parent d0ab57e commit 2bab0c5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
2 changes: 1 addition & 1 deletion redwood-lazylayout-compose/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ kotlin {
sourceSets {
commonMain {
dependencies {
api libs.jetbrains.compose.runtime.saveable
implementation libs.jetbrains.compose.runtime.saveable
api projects.redwoodLazylayoutWidget
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,23 @@ import androidx.compose.runtime.setValue

@Composable
public fun rememberLazyListState(): LazyListState {
return rememberSaveable(saver = LazyListState.Saver) {
return rememberSaveable(saver = saver) {
LazyListState()
}
}

/** The default [Saver] implementation for [LazyListState]. */
private val saver: Saver<LazyListState, *> = Saver(
save = { it.firstVisibleItemIndex },
restore = {
LazyListState().apply {
restoreIndex(it)
}
},
)

public class LazyListState {

/** We only restore the scroll position once. */
private var hasRestoredScrollPosition = false

Expand Down Expand Up @@ -71,18 +82,4 @@ public class LazyListState {
public fun onScrolled(firstVisibleItemIndex: Int) {
this.firstVisibleItemIndex = firstVisibleItemIndex
}

public companion object {
/**
* The default [Saver] implementation for [LazyListState].
*/
public val Saver: Saver<LazyListState, *> = Saver(
save = { it.firstVisibleItemIndex },
restore = {
LazyListState().apply {
restoreIndex(it)
}
},
)
}
}

0 comments on commit 2bab0c5

Please sign in to comment.