From 2bab0c5b2e084e9da55768afbf050460fb1084d0 Mon Sep 17 00:00:00 2001 From: Jingwei Date: Tue, 17 Oct 2023 11:27:21 -0700 Subject: [PATCH] Make default saver for LazyListState private (#1600) --- redwood-lazylayout-compose/build.gradle | 2 +- .../lazylayout/compose/LazyListState.kt | 27 +++++++++---------- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/redwood-lazylayout-compose/build.gradle b/redwood-lazylayout-compose/build.gradle index e62143e566..71304440d5 100644 --- a/redwood-lazylayout-compose/build.gradle +++ b/redwood-lazylayout-compose/build.gradle @@ -13,7 +13,7 @@ kotlin { sourceSets { commonMain { dependencies { - api libs.jetbrains.compose.runtime.saveable + implementation libs.jetbrains.compose.runtime.saveable api projects.redwoodLazylayoutWidget } } diff --git a/redwood-lazylayout-compose/src/commonMain/kotlin/app/cash/redwood/lazylayout/compose/LazyListState.kt b/redwood-lazylayout-compose/src/commonMain/kotlin/app/cash/redwood/lazylayout/compose/LazyListState.kt index ba42c36949..6f1a2d73d7 100644 --- a/redwood-lazylayout-compose/src/commonMain/kotlin/app/cash/redwood/lazylayout/compose/LazyListState.kt +++ b/redwood-lazylayout-compose/src/commonMain/kotlin/app/cash/redwood/lazylayout/compose/LazyListState.kt @@ -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 = Saver( + save = { it.firstVisibleItemIndex }, + restore = { + LazyListState().apply { + restoreIndex(it) + } + }, +) + public class LazyListState { + /** We only restore the scroll position once. */ private var hasRestoredScrollPosition = false @@ -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 = Saver( - save = { it.firstVisibleItemIndex }, - restore = { - LazyListState().apply { - restoreIndex(it) - } - }, - ) - } }