library: pull-to-refresh no longer restarts scaffold content when the gesture toggles
- Commit
37cc1efec94d9c889aef8e068d3fa221a3a67900- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 12:15 UTC (5 days ago)
- Parents
df93895
ScaffoldRefreshBox composed content through two structurally
different branches (PullToRefreshBox when refreshEnabled
&& onRefresh != null, a plain Box otherwise), so flipping
either input at runtime moved content into a different
composition group and disposed every remember inside it —
scroll positions and local UI state silently reset when a
screen enabled refresh or gained an onRefresh handler.
One unconditional PullToRefreshBox now gates only the
gesture via its enabled parameter; programmatic isRefreshing
still shows the indicator with the gesture disabled. No
public API change. Regression tests flip refreshEnabled and
onRefresh=null->{} after setting a counter: content state
survived post-fix (failed pre-fix). DynamicTheme, messenger
and icon mappings were audited and cleared. Verified:
:library:ui:testDebugUnitTest 1101 tests pass, lint and
detekt BUILD SUCCESSFUL, :notes:compileDevKotlin consumer
compiles; ktlintCheck fails only on the module's
pre-existing backlog.Diff
library/ui/src/main/java/com/vayunmathur/library/ui/ScaffoldRefresh.kt
diff --git a/library/ui/src/main/java/com/vayunmathur/library/ui/ScaffoldRefresh.kt b/library/ui/src/main/java/com/vayunmathur/library/ui/ScaffoldRefresh.ktindex cb09c30f67..1b9941f4f4 100644--- a/library/ui/src/main/java/com/vayunmathur/library/ui/ScaffoldRefresh.kt+++ b/library/ui/src/main/java/com/vayunmathur/library/ui/ScaffoldRefresh.kt@@ -31,6 +31,9 @@ import androidx.compose.ui.Modifier * The gesture is only wired when [refreshEnabled] is true and [onRefresh] is * non-null; in every other case the box still reflects a programmatic * [isRefreshing] with the same top indicator, it just cannot be pulled.+ * The box is composed unconditionally with only its `enabled` flag changing,+ * so toggling the gesture (or a refresh callback arriving) never restarts+ * the content and loses its remembered state. * * [padding] is the scaffold's content padding: it is forwarded to [content] * untouched (screens keep owning how they apply it) and only its top inset@@ -44,48 +47,28 @@ internal fun ScaffoldRefreshBox( padding: PaddingValues, content: @Composable (PaddingValues) -> Unit, ) {- if (refreshEnabled && onRefresh != null) {- val state = androidx.compose.material3.pulltorefresh.rememberPullToRefreshState()- androidx.compose.material3.pulltorefresh.PullToRefreshBox(- isRefreshing = isRefreshing,- onRefresh = onRefresh,- modifier = Modifier.fillMaxSize(),- state = state,- indicator = {- Box(- modifier = Modifier- .align(Alignment.TopCenter)- .fillMaxWidth()- .padding(top = padding.calculateTopPadding()),- contentAlignment = Alignment.TopCenter,- ) {- androidx.compose.material3.pulltorefresh.PullToRefreshDefaults.LoadingIndicator(- state = state,- isRefreshing = isRefreshing,- )- }- },- ) {- content(padding)- }- } else {- Box(Modifier.fillMaxSize()) {- content(padding)- if (isRefreshing) {- val state = androidx.compose.material3.pulltorefresh.rememberPullToRefreshState()- Box(- modifier = Modifier- .align(Alignment.TopCenter)- .fillMaxWidth()- .padding(top = padding.calculateTopPadding()),- contentAlignment = Alignment.TopCenter,- ) {- androidx.compose.material3.pulltorefresh.PullToRefreshDefaults.LoadingIndicator(- state = state,- isRefreshing = true,- )- }+ val state = androidx.compose.material3.pulltorefresh.rememberPullToRefreshState()+ androidx.compose.material3.pulltorefresh.PullToRefreshBox(+ isRefreshing = isRefreshing,+ onRefresh = onRefresh ?: {},+ modifier = Modifier.fillMaxSize(),+ state = state,+ enabled = refreshEnabled && onRefresh != null,+ indicator = {+ Box(+ modifier = Modifier+ .align(Alignment.TopCenter)+ .fillMaxWidth()+ .padding(top = padding.calculateTopPadding()),+ contentAlignment = Alignment.TopCenter,+ ) {+ androidx.compose.material3.pulltorefresh.PullToRefreshDefaults.LoadingIndicator(+ state = state,+ isRefreshing = isRefreshing,+ ) }- }+ },+ ) {+ content(padding) } }
library/ui/src/test/java/com/vayunmathur/library/ui/ScaffoldRefreshStatePreservationTest.kt
diff --git a/library/ui/src/test/java/com/vayunmathur/library/ui/ScaffoldRefreshStatePreservationTest.kt b/library/ui/src/test/java/com/vayunmathur/library/ui/ScaffoldRefreshStatePreservationTest.ktnew file mode 100644index 0000000000..b824dff5b3--- /dev/null+++ b/library/ui/src/test/java/com/vayunmathur/library/ui/ScaffoldRefreshStatePreservationTest.kt@@ -0,0 +1,83 @@+package com.vayunmathur.library.ui++import androidx.compose.foundation.layout.Column+import androidx.compose.foundation.layout.padding+import androidx.compose.material3.Text+import androidx.compose.ui.Modifier+import androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableIntStateOf+import androidx.compose.runtime.mutableStateOf+import androidx.compose.runtime.remember+import androidx.compose.runtime.setValue+import androidx.compose.ui.test.junit4.createComposeRule+import androidx.compose.ui.test.onNodeWithText+import androidx.compose.ui.test.performClick+import kotlin.test.Test+import org.junit.Rule+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner++/**+ * Regression: changing whether the pull gesture is available must not+ * dispose the scaffold content. A screen that starts a programmatic+ * refresh with the gesture disabled (or with no refresh callback yet)+ * and later enables the gesture keeps its scroll and other remembered+ * content state; switching the refresh container's structure would+ * restart the content and lose it.+ */+@RunWith(RobolectricTestRunner::class)+class ScaffoldRefreshStatePreservationTest {++ @get:Rule+ val rule = createComposeRule()++ @Test fun refreshEnabledTogglePreservesContentState() {+ val refreshEnabled = mutableStateOf(false)+ rule.setContent {+ AppScaffold(+ title = "Inbox",+ isRefreshing = true,+ onRefresh = {},+ refreshEnabled = refreshEnabled.value,+ ) { padding ->+ Column(Modifier.padding(padding)) {+ var count by remember { mutableIntStateOf(0) }+ Text("Count $count")+ Button(onClick = { count++ }) { Text("Increment") }+ }+ }+ }+ rule.onNodeWithText("Increment").performClick()+ rule.onNodeWithText("Count 1").assertExists()++ rule.runOnUiThread { refreshEnabled.value = true }+ rule.waitForIdle()++ rule.onNodeWithText("Count 1").assertExists()+ }++ @Test fun refreshCallbackArrivalPreservesContentState() {+ val onRefresh = mutableStateOf<(() -> Unit)?>(null)+ rule.setContent {+ AppScaffold(+ title = "Inbox",+ isRefreshing = false,+ onRefresh = onRefresh.value,+ refreshEnabled = true,+ ) { padding ->+ Column(Modifier.padding(padding)) {+ var count by remember { mutableIntStateOf(0) }+ Text("Count $count")+ Button(onClick = { count++ }) { Text("Increment") }+ }+ }+ }+ rule.onNodeWithText("Increment").performClick()+ rule.onNodeWithText("Count 1").assertExists()++ rule.runOnUiThread { onRefresh.value = {} }+ rule.waitForIdle()++ rule.onNodeWithText("Count 1").assertExists()+ }+}