web: swipe between text, image and news results
- Commit
84fdea4d406cde615c490b40f845686356f5c246- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-06 03:54 UTC (6 hours ago)
- Parents
00aa6b6
MA Search results split one query into three peer kinds - Text, Images, News - behind a TabRow and a when(kind), with one shared response slot guarded by a tab-switch race check so a slow Text reply could not overwrite News. Make each kind a page of the shared TabbedPagerScaffold in PrimaryTabRow style. Text and News move into a per-kind SearchKindResults that owns its response, loading, failure and pagination state keyed by query and kind, so pages keep their results and list positions while swiping, and the race the shared slot needed is gone by construction. Images keeps its own paginated fetch (SearchImageResults) as the middle page. Query stays in the top bar, result taps still record history through onOpenResult, and nothing about routes or the browser chrome changes. The scaffold's top-level pull-to-refresh flag is dropped; each page shows the same empty loading box and error retry it already showed on first load. Verified: :web:lint, :web:lintVitalRelease and :web:testDevUnitTest all pass.
Diff
web/src/main/java/com/vayunmathur/web/ui/SearchResultsPage.kt
diff --git a/web/src/main/java/com/vayunmathur/web/ui/SearchResultsPage.kt b/web/src/main/java/com/vayunmathur/web/ui/SearchResultsPage.ktindex 02a87dd77a..8f89eb9087 100644--- a/web/src/main/java/com/vayunmathur/web/ui/SearchResultsPage.kt+++ b/web/src/main/java/com/vayunmathur/web/ui/SearchResultsPage.kt@@ -21,15 +21,16 @@ import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.dp-import com.vayunmathur.library.ui.AppScaffold+import com.vayunmathur.library.ui.PagerTab+import com.vayunmathur.library.ui.TabStyle+import com.vayunmathur.library.ui.TabbedPagerScaffold+import com.vayunmathur.library.ui.TopAppBar import com.vayunmathur.library.ui.Button import com.vayunmathur.library.ui.DesktopMaxWidthContainer import com.vayunmathur.library.ui.EmptyState import com.vayunmathur.library.ui.friendlyError import com.vayunmathur.library.ui.ExperimentalMaterial3Api import com.vayunmathur.library.ui.MaterialTheme-import com.vayunmathur.library.ui.Tab-import com.vayunmathur.library.ui.TabRow import com.vayunmathur.library.ui.Text import com.vayunmathur.library.ui.rememberMessenger import com.vayunmathur.web.R@@ -52,47 +53,75 @@ import kotlinx.coroutines.launch * is not a visit, and logging every keystroke-confirmed query would pollute * history with non-pages. */+/**+ * Text, images and news as three swipeable peer pages for one query. Each+ * page owns its fetch state, so swiping (or tapping) never flashes another+ * kind's results and the old shared-slot tab-switch race is gone by+ * construction. Explicit page->kind order below (never+ * `WebSearchKind.entries[page]`: a future enum reorder must not silently+ * mismatch pages).+ */ @OptIn(ExperimentalMaterial3Api::class) @Composable fun SearchResultsPage( query: String, onOpenResult: (String) -> Unit, modifier: Modifier = Modifier,+) {+ TabbedPagerScaffold(+ modifier = modifier,+ tabStyle = TabStyle.PrimaryTabRow,+ topBar = { TopAppBar(title = { Text(query) }) },+ tabs = listOf(+ PagerTab(stringResource(R.string.search_tab_text)) {+ DesktopMaxWidthContainer {+ SearchKindResults(query, WebSearchKind.TEXT, onOpenResult)+ }+ },+ PagerTab(stringResource(R.string.search_tab_images)) {+ DesktopMaxWidthContainer {+ // SearchImageResults owns its own paginated fetch (the grid needs+ // append semantics the shared response does not have).+ SearchImageResults(query = query, onOpenResult = onOpenResult)+ }+ },+ PagerTab(stringResource(R.string.search_tab_news)) {+ DesktopMaxWidthContainer {+ SearchKindResults(query, WebSearchKind.NEWS, onOpenResult)+ }+ },+ ),+ )+}++/** One text-like result kind (TEXT or NEWS) with its own fetch state. */+@Composable+private fun SearchKindResults(+ query: String,+ kind: WebSearchKind,+ onOpenResult: (String) -> Unit, ) { val context = LocalContext.current val searchFailedText = stringResource(R.string.search_failed) val messenger = rememberMessenger() val scope = rememberCoroutineScope()- var selectedTab by remember { mutableIntStateOf(0) }- // Explicit tab->kind order (never WebSearchKind.entries[selectedTab]: a- // future enum reorder must not silently mismatch tabs).- val kind = when (selectedTab) {- 1 -> WebSearchKind.IMAGES- 2 -> WebSearchKind.NEWS- else -> WebSearchKind.TEXT- }- var response by remember(query) { mutableStateOf<WebSearchResponse?>(null) }- var loading by remember(query) { mutableStateOf(true) }- var failure by remember(query) { mutableStateOf<String?>(null) }- var page by remember(query) { mutableIntStateOf(1) }+ var response by remember(query, kind) { mutableStateOf<WebSearchResponse?>(null) }+ var loading by remember(query, kind) { mutableStateOf(true) }+ var failure by remember(query, kind) { mutableStateOf<String?>(null) }+ var page by remember(query, kind) { mutableIntStateOf(1) } fun load(requestedPage: Int) {- // Guard against the tab-switch race: capture the kind at request time- // and drop completions that arrive after the user moved on, so a slow- // TEXT reply cannot overwrite the NEWS response (or vice versa).- val requestKind = kind loading = true failure = null scope.launch { runCatching { WebSearchRepository.search( query = query,- kind = requestKind,+ kind = kind, safeSearch = ContentFilters.filterSearchResults(context), page = requestedPage, ) }.onSuccess {- if (requestKind != kind) return@onSuccess response = if (requestedPage > 1 && response?.query == it.query) { appendPage(response, it) } else {@@ -101,7 +130,6 @@ fun SearchResultsPage( page = requestedPage loading = false }.onFailure { e ->- if (requestKind != kind) return@onFailure failure = context.friendlyError(e) loading = false messenger.show(searchFailedText)@@ -109,82 +137,40 @@ fun SearchResultsPage( } } - LaunchedEffect(query, selectedTab) {- // SearchImageResults owns its own paginated fetch (the grid needs- // append semantics the shared response does not have). Clear the shared- // slot so switching tabs never flashes the previous kind's results.- if (kind == WebSearchKind.IMAGES) {- loading = false- failure = null- response = null- } else {- response = null- load(1)- }- }+ LaunchedEffect(query, kind) { load(1) } - AppScaffold(- title = query,- modifier = modifier,- isRefreshing = loading,- ) { paddingValues ->- DesktopMaxWidthContainer(modifier = Modifier.padding(paddingValues)) {- Column(Modifier.fillMaxSize()) {- TabRow(selectedTabIndex = selectedTab) {- Tab(- selected = selectedTab == 0,- onClick = { selectedTab = 0 },- text = { Text(stringResource(R.string.search_tab_text)) },+ Box(Modifier.fillMaxSize()) {+ when {+ loading && response == null -> Box(Modifier.fillMaxSize())++ failure != null && response == null -> SearchErrorState(+ message = failure.orEmpty(),+ onRetry = { load(1) },+ )++ else -> {+ val resp = response+ // Degraded server reply: lists are empty and an+ // upstream error is set.+ if (resp?.error != null && resp.text.isNullOrEmpty() &&+ resp.images.isNullOrEmpty() && resp.news.isNullOrEmpty()+ ) {+ SearchErrorState(+ message = resp.error.orEmpty(),+ onRetry = { load(1) }, )- Tab(- selected = selectedTab == 1,- onClick = { selectedTab = 1 },- text = { Text(stringResource(R.string.search_tab_images)) },+ } else when (kind) {+ WebSearchKind.TEXT -> SearchTextResults(+ items = resp?.text.orEmpty(),+ loadingMore = loading,+ onOpenResult = onOpenResult,+ onLoadMore = { load(page + 1) }, )- Tab(- selected = selectedTab == 2,- onClick = { selectedTab = 2 },- text = { Text(stringResource(R.string.search_tab_news)) },+ WebSearchKind.NEWS -> SearchNewsResults(+ items = resp?.news.orEmpty(),+ onOpenResult = onOpenResult, )- }- Box(Modifier.fillMaxSize().weight(1f)) {- when {- loading && response == null -> Box(Modifier.fillMaxSize())-- failure != null && response == null -> SearchErrorState(- message = failure.orEmpty(),- onRetry = { load(1) },- )-- else -> {- val resp = response- // Degraded server reply: lists are empty and an- // upstream error is set.- if (resp?.error != null && resp.text.isNullOrEmpty() &&- resp.images.isNullOrEmpty() && resp.news.isNullOrEmpty()- ) {- SearchErrorState(- message = resp.error.orEmpty(),- onRetry = { load(1) },- )- } else when (kind) {- WebSearchKind.TEXT -> SearchTextResults(- items = resp?.text.orEmpty(),- loadingMore = loading,- onOpenResult = onOpenResult,- onLoadMore = { load(page + 1) },- )- WebSearchKind.IMAGES -> SearchImageResults(- query = query,- onOpenResult = onOpenResult,- )- WebSearchKind.NEWS -> SearchNewsResults(- items = resp?.news.orEmpty(),- onOpenResult = onOpenResult,- )- }- }- }+ WebSearchKind.IMAGES -> Box(Modifier.fillMaxSize()) } } }