library: scaffold-aware pull-to-refresh box
- Commit
205241c5fa38e4996fa1f386d3506a1c686ed1fd- Author
- vayun-mathur <[email protected]>
- When
- 2026-10-09 19:53 UTC (1 month ago)
- Parents
c59c4a4
Diff
appstore/src/main/java/com/vayunmathur/appstore/ui/UpdatesPage.kt
diff --git a/appstore/src/main/java/com/vayunmathur/appstore/ui/UpdatesPage.kt b/appstore/src/main/java/com/vayunmathur/appstore/ui/UpdatesPage.ktindex 810ccb7ae1..7d6a36b747 100644--- a/appstore/src/main/java/com/vayunmathur/appstore/ui/UpdatesPage.kt+++ b/appstore/src/main/java/com/vayunmathur/appstore/ui/UpdatesPage.kt@@ -36,7 +36,7 @@ import com.vayunmathur.library.ui.IconCheck import com.vayunmathur.library.ui.IconDownload import com.vayunmathur.library.ui.MaterialTheme import com.vayunmathur.library.ui.OutlinedButton-import com.vayunmathur.library.ui.PullToRefreshBox+import com.vayunmathur.library.ui.AppPullToRefreshBox import com.vayunmathur.library.ui.Text import com.vayunmathur.library.ui.appBarScrollBehavior @@ -69,12 +69,14 @@ fun UpdatesScreen( }, scrollBehavior = appBarScrollBehavior(), ) { padding ->- PullToRefreshBox(+ AppPullToRefreshBox( isRefreshing = state.isChecking, onRefresh = { actions.checkForUpdates() },- modifier = Modifier.fillMaxSize().padding(padding),+ modifier = Modifier.fillMaxSize(),+ // Below the top app bar: the scaffold's top inset is the exact clearance.+ topClearance = padding.calculateTopPadding(), ) {- Column(Modifier.fillMaxSize()) {+ Column(Modifier.fillMaxSize().padding(padding)) { Row( Modifier.fillMaxWidth().padding(horizontal = 16.dp, vertical = 8.dp), horizontalArrangement = Arrangement.spacedBy(8.dp),
email/src/main/java/com/vayunmathur/email/ui/MessageListScreen.kt
diff --git a/email/src/main/java/com/vayunmathur/email/ui/MessageListScreen.kt b/email/src/main/java/com/vayunmathur/email/ui/MessageListScreen.ktindex 806c012c60..01de4b9aaf 100644--- a/email/src/main/java/com/vayunmathur/email/ui/MessageListScreen.kt+++ b/email/src/main/java/com/vayunmathur/email/ui/MessageListScreen.kt@@ -211,7 +211,14 @@ fun MessageListScreen( "email-subject-${msg.accountEmail}-${msg.threadId ?: msg.id}" } }- com.vayunmathur.library.ui.PullToRefreshBox(isRefreshing = state.isSyncing, onRefresh = { actions.refresh(context) }, modifier = Modifier.fillMaxSize()) {+ com.vayunmathur.library.ui.AppPullToRefreshBox(+ isRefreshing = state.isSyncing,+ onRefresh = { actions.refresh(context) },+ modifier = Modifier.fillMaxSize(),+ // The box sits below the top bar, sync bar, and filter-chip row,+ // so only a small clearance is needed; the slot stays centered.+ topClearance = 8.dp,+ ) { if (state.messages.isEmpty() && state.searchQuery.isEmpty()) { Box(modifier = Modifier.fillMaxSize()) { Text(text = stringResource(R.string.no_messages_found_pull_down_to_refresh), modifier = Modifier.align(Alignment.Center))
library/ui/src/main/java/com/vayunmathur/library/ui/AppPullToRefreshBox.kt
diff --git a/library/ui/src/main/java/com/vayunmathur/library/ui/AppPullToRefreshBox.kt b/library/ui/src/main/java/com/vayunmathur/library/ui/AppPullToRefreshBox.ktnew file mode 100644index 0000000000..338e464a80--- /dev/null+++ b/library/ui/src/main/java/com/vayunmathur/library/ui/AppPullToRefreshBox.kt@@ -0,0 +1,63 @@+@file:OptIn(+ androidx.compose.material3.ExperimentalMaterial3Api::class,+ androidx.compose.material3.ExperimentalMaterial3ExpressiveApi::class,+)++package com.vayunmathur.library.ui++import androidx.compose.foundation.layout.Box+import androidx.compose.foundation.layout.BoxScope+import androidx.compose.foundation.layout.fillMaxWidth+import androidx.compose.foundation.layout.padding+import androidx.compose.runtime.Composable+import androidx.compose.ui.Alignment+import androidx.compose.ui.Modifier+import androidx.compose.ui.unit.Dp+import androidx.compose.ui.unit.dp++/**+ * Scaffold-aware pull-to-refresh container.+ *+ * A thin fix over [PullToRefreshBox] for screens hosted in [AppScaffold] (or a raw+ * `Scaffold` with a top bar): the indicator slot is a `fillMaxWidth` box with centered+ * content rather than a wrap-content `Box.align`, so the spinner is horizontally centered+ * regardless of how the content measures (grids, pinch-zoom layers), and [topClearance]+ * drops it below the top bar / floating search bar the M3 default top-center slot would+ * otherwise sit under.+ *+ * The caller owns content insets (the scaffold's `PaddingValues` go on the content, not+ * on [modifier]) while this box owns only the indicator offset, so the two cannot+ * double-offset. Pass a larger [topClearance] when extra in-content rows (filter chips,+ * action rows) sit above the refreshable area.+ *+ * @param topClearance distance from the top of [modifier] to the indicator. Defaults to+ * the standard small top-bar height; weather's floating search bar uses the same value.+ */+@Composable+fun AppPullToRefreshBox(+ isRefreshing: Boolean,+ onRefresh: () -> Unit,+ modifier: Modifier = Modifier,+ topClearance: Dp = 64.dp,+ content: @Composable BoxScope.() -> Unit,+) {+ val state = androidx.compose.material3.pulltorefresh.rememberPullToRefreshState()+ androidx.compose.material3.pulltorefresh.PullToRefreshBox(+ isRefreshing = isRefreshing,+ onRefresh = onRefresh,+ modifier = modifier,+ state = state,+ indicator = {+ Box(+ modifier = Modifier.align(Alignment.TopCenter).fillMaxWidth().padding(top = topClearance),+ contentAlignment = Alignment.TopCenter,+ ) {+ androidx.compose.material3.pulltorefresh.PullToRefreshDefaults.LoadingIndicator(+ state = state,+ isRefreshing = isRefreshing,+ )+ }+ },+ content = content,+ )+}
photos/src/main/java/com/vayunmathur/photos/ui/AlbumDetailScreen.kt
diff --git a/photos/src/main/java/com/vayunmathur/photos/ui/AlbumDetailScreen.kt b/photos/src/main/java/com/vayunmathur/photos/ui/AlbumDetailScreen.ktindex 570819e9c8..7e1670afc8 100644--- a/photos/src/main/java/com/vayunmathur/photos/ui/AlbumDetailScreen.kt+++ b/photos/src/main/java/com/vayunmathur/photos/ui/AlbumDetailScreen.kt@@ -22,7 +22,7 @@ import com.vayunmathur.library.ui.IconImage import com.vayunmathur.library.ui.IconMoreVert import com.vayunmathur.library.ui.MaterialTheme import com.vayunmathur.library.ui.OutlinedTextField-import com.vayunmathur.library.ui.PullToRefreshBox+import com.vayunmathur.library.ui.AppPullToRefreshBox import com.vayunmathur.library.ui.Text import com.vayunmathur.library.ui.TextButton import com.vayunmathur.library.ui.appBarScrollBehavior@@ -151,14 +151,17 @@ fun AlbumDetailScreen( modifier = Modifier.fillMaxSize().padding(padding), ) } else {- PullToRefreshBox(+ AppPullToRefreshBox( isRefreshing = isRefreshing, onRefresh = onRefresh,- modifier = Modifier.fillMaxSize().padding(padding),+ modifier = Modifier.fillMaxSize(),+ // Below the top app bar: the scaffold's top inset is the exact clearance.+ topClearance = padding.calculateTopPadding(), ) { Box( modifier = Modifier .fillMaxSize()+ .padding(padding) .pinchToZoomColumns({ columnCount }, { columnCount = it }) ) { LazyVerticalGrid(
photos/src/main/java/com/vayunmathur/photos/ui/GalleryScreen.kt
diff --git a/photos/src/main/java/com/vayunmathur/photos/ui/GalleryScreen.kt b/photos/src/main/java/com/vayunmathur/photos/ui/GalleryScreen.ktindex 601abcce98..e299b28397 100644--- a/photos/src/main/java/com/vayunmathur/photos/ui/GalleryScreen.kt+++ b/photos/src/main/java/com/vayunmathur/photos/ui/GalleryScreen.kt@@ -139,7 +139,7 @@ fun GalleryScreen( // RAW SCAFFOLD EXCEPTION: top bar is an expandable SearchBar (with in-bar search // results) swapped for a selection TopAppBar, and the body is a pinch-to-zoom photo- // grid wrapped in PullToRefreshBox. Neither AppScaffold (title/actions only) nor+ // grid wrapped in AppPullToRefreshBox. Neither AppScaffold (title/actions only) nor // LazyListScaffold (LazyColumn body) can represent this without changing the UI. Scaffold( topBar = {@@ -270,14 +270,17 @@ fun GalleryScreen( }, bottomBar = { if (!isSelectionMode) NavigationBar(Route.Gallery, backStack) } ) { paddingValues ->- com.vayunmathur.library.ui.PullToRefreshBox(+ com.vayunmathur.library.ui.AppPullToRefreshBox( isRefreshing = state.isRefreshing, onRefresh = { actions.runSync() },- modifier = Modifier.fillMaxSize().padding(paddingValues),+ modifier = Modifier.fillMaxSize(),+ // Below the top search bar: the scaffold's top inset is the exact clearance.+ topClearance = paddingValues.calculateTopPadding(), ) { Box( modifier = Modifier .fillMaxSize()+ .padding(paddingValues) .pinchToZoomColumns({ columnCount }, { columnCount = it }) ) { LazyVerticalGrid(
weather/src/main/java/com/vayunmathur/weather/ui/ForecastColumn.kt
diff --git a/weather/src/main/java/com/vayunmathur/weather/ui/ForecastColumn.kt b/weather/src/main/java/com/vayunmathur/weather/ui/ForecastColumn.ktindex c433b65bb1..c055e8a342 100644--- a/weather/src/main/java/com/vayunmathur/weather/ui/ForecastColumn.kt+++ b/weather/src/main/java/com/vayunmathur/weather/ui/ForecastColumn.kt@@ -12,7 +12,7 @@ import androidx.compose.foundation.verticalScroll import com.vayunmathur.library.ui.ExperimentalMaterial3Api import com.vayunmathur.library.ui.LoadingIndicator import com.vayunmathur.library.ui.MaterialTheme-import com.vayunmathur.library.ui.PullToRefreshBox+import com.vayunmathur.library.ui.AppPullToRefreshBox import com.vayunmathur.library.ui.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect@@ -62,16 +62,15 @@ internal fun ForecastColumn( val selected = state.selected val scrollState = rememberScrollState() - PullToRefreshBox(+ AppPullToRefreshBox( // No pull indicator on first load: the centered LoadingIndicator below // already covers it, and the two spinners stack. Refreshes still show it. isRefreshing = state.refreshing && forecast != null, onRefresh = { actions.refreshAll(force = true) }, modifier = Modifier.fillMaxSize(),- // Same spot as the first-load LoadingIndicator (TopCenter + 64.dp):- // the default top-center slot overlaps the floating search bar.- indicatorAlignment = Alignment.TopCenter,- indicatorPadding = PaddingValues(top = 64.dp),+ // Same spot as the first-load LoadingIndicator below (64.dp clears the+ // floating search bar); the indicator slot itself is always centered.+ topClearance = 64.dp, ) { Column(modifier = Modifier.fillMaxSize().verticalScroll(scrollState)) { MainSearchBar(