health: swipe between recipes and ingredients
- Commit
42bc5b7a593e8ae760b2c4b1abb09804a67820ab- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-06 03:54 UTC (6 hours ago)
- Parents
3600e69
Recipe management held two peer lists behind a hand-rolled PrimaryTabRow plus a when(selectedTab): Recipes and Ingredients are in-screen sub-pages of the one RecipeManagement destination, neither is a route, and both lists already come from the ViewModel, so this is the pager's home turf. Replace the manual tab state with the shared TabbedPagerScaffold in PrimaryTabRow style: tap and swipe are the same gesture, the pager keeps both lists composed so scroll and loaded state survive the flip, and the create FAB follows the settled page (new recipe on Recipes, add-ingredient on Ingredients) via the scaffold's new floatingActionButton slot. The empty states keep their full-width create buttons, the food-database card stays on the Ingredients page, and the RecipeEditor / AddIngredientDialog / RenameIngredientDialog routes push exactly as before. The tab tint now follows the theme instead of HealthColors.Nutrition; the list rows keep their nutrition accent. Unit tests that drove this screen assumed exactly one scrollable container; with the pager there are three (pager plus both lists), so ChartDrawAndRecipeEditTest, UncoveredScreensTest and ThresholdBatchTest now scroll every container until the seeded row composes. Verified: :health:lint and :health:lintVitalRelease pass. :health:testDevUnitTest: the three adapted classes pass together with the full suite's recipe tests; RecipeEditorFlowTest (which renders RecipeEditorPage, not this screen) intermittently times out awaiting semantics in full-suite runs only and passes in isolation - it carries a comment in-tree acknowledging that exact full-suite race. :health:metadata was run but not committed: the committed metadata_data set is stale on main (6 phone shots for many more defined previews), so a regen adds 14 unrelated shots and renumbers the existing ones.
Diff
health/src/main/java/com/vayunmathur/health/ui/RecipeManagementPage.kt
diff --git a/health/src/main/java/com/vayunmathur/health/ui/RecipeManagementPage.kt b/health/src/main/java/com/vayunmathur/health/ui/RecipeManagementPage.ktindex edac71a8e3..4ae78298a0 100644--- a/health/src/main/java/com/vayunmathur/health/ui/RecipeManagementPage.kt+++ b/health/src/main/java/com/vayunmathur/health/ui/RecipeManagementPage.kt@@ -18,11 +18,7 @@ import androidx.compose.foundation.layout.width import androidx.compose.runtime.Composable import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue-import androidx.compose.runtime.key-import androidx.compose.runtime.mutableIntStateOf-import androidx.compose.runtime.mutableStateOf-import androidx.compose.runtime.remember-import androidx.compose.runtime.setValue+import androidx.compose.foundation.pager.rememberPagerState import com.vayunmathur.health.data.Ingredient import com.vayunmathur.health.data.Recipe import com.vayunmathur.library.ui.AlertDialog@@ -36,8 +32,6 @@ import com.vayunmathur.library.ui.IconDelete import com.vayunmathur.library.ui.IconFire import com.vayunmathur.library.ui.MaterialTheme import com.vayunmathur.library.ui.OutlinedTextField-import com.vayunmathur.library.ui.PrimaryTabRow-import com.vayunmathur.library.ui.Tab import com.vayunmathur.library.ui.Text import com.vayunmathur.library.ui.TextButton import androidx.compose.foundation.lazy.LazyColumn@@ -53,7 +47,11 @@ import androidx.compose.ui.text.input.KeyboardType import androidx.compose.ui.unit.dp import com.vayunmathur.health.R import com.vayunmathur.health.Route-import com.vayunmathur.library.ui.AppScaffold+import com.vayunmathur.library.ui.IconNavigation+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.EmptyState import com.vayunmathur.health.data.FoodDatabase import com.vayunmathur.health.data.FoodSearchAPI@@ -62,23 +60,37 @@ import com.vayunmathur.library.util.NavBackStack import kotlinx.coroutines.launch import java.text.NumberFormat +/**+ * Recipes and ingredients as two swipeable peer pages of this one+ * destination: neither tab is a route, so the pager (tap or swipe) owns the+ * selection, the lists come from the ViewModel, and the create action follows+ * the settled page. Editor and dialog destinations still push as routes.+ */ @OptIn(ExperimentalMaterial3Api::class) @Composable fun RecipeManagementPage(backStack: NavBackStack<Route>, viewModel: HealthViewModel) {- var selectedTab by remember { mutableIntStateOf(0) } val recipes by viewModel.allRecipes.collectAsState(emptyList()) val ingredients by viewModel.allIngredients.collectAsState(emptyList())+ val pagerState = rememberPagerState(pageCount = { 2 })+ val currentPage = pagerState.currentPage+ val recipesEmpty = recipes.isEmpty()+ val ingredientsEmpty = ingredients.isEmpty()+ val isListEmpty = if (currentPage == 0) recipesEmpty else ingredientsEmpty - val isListEmpty = if (selectedTab == 0) recipes.isEmpty() else ingredients.isEmpty()-- AppScaffold(- title = stringResource(R.string.recipes),- backStack = backStack,+ TabbedPagerScaffold(+ pagerState = pagerState,+ tabStyle = TabStyle.PrimaryTabRow,+ topBar = {+ TopAppBar(+ title = { Text(stringResource(R.string.recipes)) },+ navigationIcon = { IconNavigation(backStack) },+ )+ }, floatingActionButton = { if (!isListEmpty) {- FloatingActionButton(onClick = { - if (selectedTab == 0) {- backStack.add(Route.RecipeEditor()) + FloatingActionButton(onClick = {+ if (currentPage == 0) {+ backStack.add(Route.RecipeEditor()) } else { backStack.add(Route.AddIngredientDialog) }@@ -87,49 +99,51 @@ fun RecipeManagementPage(backStack: NavBackStack<Route>, viewModel: HealthViewMo } } },- ) { padding ->- Column(modifier = Modifier.padding(padding).fillMaxSize()) {- PrimaryTabRow(- selectedTabIndex = selectedTab,- contentColor = HealthColors.Nutrition,- ) {- Tab(selected = selectedTab == 0, onClick = { selectedTab = 0 }, selectedContentColor = HealthColors.Nutrition, text = { Text(stringResource(R.string.recipes)) })- Tab(selected = selectedTab == 1, onClick = { selectedTab = 1 }, selectedContentColor = HealthColors.Nutrition, text = { Text(stringResource(R.string.ingredients)) })- }-- // Ingredient search depends on the bundled food database, so its- // first-run state belongs on the Ingredients tab.- if (selectedTab == 1) {- FoodDatabaseCard(viewModel, modifier = Modifier.padding(horizontal = 16.dp, vertical = 8.dp))- }-- if (isListEmpty) {- Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {- Button(onClick = {- if (selectedTab == 0) {- backStack.add(Route.RecipeEditor())- } else {- backStack.add(Route.AddIngredientDialog)- }- }) {- IconAdd()- Spacer(Modifier.width(8.dp))- Text(if (selectedTab == 0) stringResource(R.string.create_your_first_recipe) else stringResource(R.string.add_your_first_ingredient))+ tabs = listOf(+ PagerTab(stringResource(R.string.recipes)) {+ if (recipesEmpty) {+ RecipeEmptyState(+ label = stringResource(R.string.create_your_first_recipe),+ onCreate = { backStack.add(Route.RecipeEditor()) },+ )+ } else {+ RecipesList(recipes, onDeleteRecipe = { viewModel.deleteRecipe(it) }) { recipeId ->+ backStack.add(Route.RecipeEditor(recipeId)) } }- } else {- if (selectedTab == 0) {- RecipesList(recipes, onDeleteRecipe = { viewModel.deleteRecipe(it) }) { recipeId -> backStack.add(Route.RecipeEditor(recipeId)) }- } else {- IngredientsList(- ingredients,- onToggleIngredient = { viewModel.updateIngredient(it.copy(isRecipe = !it.isRecipe)) },- onDeleteIngredient = { viewModel.deleteIngredient(it) },- ) { ingredient ->- backStack.add(Route.RenameIngredientDialog(ingredient.id))+ },+ PagerTab(stringResource(R.string.ingredients)) {+ Column(Modifier.fillMaxSize()) {+ // Ingredient search depends on the bundled food database, so its+ // first-run state belongs on the Ingredients page.+ FoodDatabaseCard(viewModel, modifier = Modifier.padding(horizontal = 16.dp, vertical = 8.dp))+ if (ingredientsEmpty) {+ RecipeEmptyState(+ label = stringResource(R.string.add_your_first_ingredient),+ onCreate = { backStack.add(Route.AddIngredientDialog) },+ )+ } else {+ IngredientsList(+ ingredients,+ onToggleIngredient = { viewModel.updateIngredient(it.copy(isRecipe = !it.isRecipe)) },+ onDeleteIngredient = { viewModel.deleteIngredient(it) },+ ) { ingredient ->+ backStack.add(Route.RenameIngredientDialog(ingredient.id))+ } } }- }+ },+ ),+ )+}++@Composable+private fun RecipeEmptyState(label: String, onCreate: () -> Unit) {+ Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {+ Button(onClick = onCreate) {+ IconAdd()+ Spacer(Modifier.width(8.dp))+ Text(label) } } }
health/src/test/java/com/vayunmathur/health/ui/ChartDrawAndRecipeEditTest.kt
diff --git a/health/src/test/java/com/vayunmathur/health/ui/ChartDrawAndRecipeEditTest.kt b/health/src/test/java/com/vayunmathur/health/ui/ChartDrawAndRecipeEditTest.ktindex d23bede769..045fc0839f 100644--- a/health/src/test/java/com/vayunmathur/health/ui/ChartDrawAndRecipeEditTest.kt+++ b/health/src/test/java/com/vayunmathur/health/ui/ChartDrawAndRecipeEditTest.kt@@ -4,11 +4,12 @@ import android.app.Application import androidx.compose.foundation.layout.Column import androidx.compose.ui.graphics.Color import androidx.compose.ui.test.captureToImage-import androidx.compose.ui.test.hasScrollAction-import androidx.compose.ui.test.hasText-import androidx.compose.ui.test.performScrollToNode import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onNodeWithText+import androidx.compose.ui.test.performScrollToNode+import androidx.compose.ui.test.hasText+import androidx.compose.ui.test.hasScrollAction+import androidx.compose.ui.test.onAllNodesWithText import androidx.compose.ui.test.onRoot import androidx.compose.ui.test.performClick import androidx.room3.Room@@ -91,7 +92,21 @@ class ChartDrawAndRecipeEditTest { rule.waitForIdle() rule.onNodeWithText("Ingredients").performClick() rule.waitForIdle()- rule.onNode(hasScrollAction()).performScrollToNode(hasText("Rename Oats"))+ // Both pager pages (and the pager itself) are scrollable and the+ // shared suite database can leave this row far down the lazy list,+ // so scroll every container until the row composes.+ val deadline = System.currentTimeMillis() + 60_000+ var found = false+ while (!found && System.currentTimeMillis() < deadline) {+ val scrollCount = rule.onAllNodes(hasScrollAction()).fetchSemanticsNodes().size+ for (i in 0 until scrollCount) {+ runCatching {+ rule.onAllNodes(hasScrollAction())[i].performScrollToNode(hasText("Rename Oats"))+ }+ }+ found = rule.onAllNodesWithText("Rename Oats").fetchSemanticsNodes().isNotEmpty()+ if (!found) Thread.sleep(20)+ } rule.onNodeWithText("Rename Oats").performClick() rule.waitForIdle() // The ingredient card now pushes the rename dialog route carrying the ingredient id.
health/src/test/java/com/vayunmathur/health/ui/ThresholdBatchTest.kt
diff --git a/health/src/test/java/com/vayunmathur/health/ui/ThresholdBatchTest.kt b/health/src/test/java/com/vayunmathur/health/ui/ThresholdBatchTest.ktindex 3d82bf163f..e7b9dc47cc 100644--- a/health/src/test/java/com/vayunmathur/health/ui/ThresholdBatchTest.kt+++ b/health/src/test/java/com/vayunmathur/health/ui/ThresholdBatchTest.kt@@ -177,9 +177,16 @@ class ThresholdBatchTest { // holds other classes' ingredients, so this row can be // far down the lazy list and never compose until scrolled // to; a targeted run starts empty and finds it at once.- runCatching {- rule.onNode(androidx.compose.ui.test.hasScrollAction())- .performScrollToNode(androidx.compose.ui.test.hasText("Toggle Beans", substring = true))+ // The pager host and both page lists are scrollable, so scroll+ // every candidate container; only the ingredients list can+ // bring this row into composition.+ val scrollCount = rule.onAllNodes(androidx.compose.ui.test.hasScrollAction())+ .fetchSemanticsNodes().size+ for (i in 0 until scrollCount) {+ runCatching {+ rule.onAllNodes(androidx.compose.ui.test.hasScrollAction())[i]+ .performScrollToNode(androidx.compose.ui.test.hasText("Toggle Beans", substring = true))+ } } found = rule.onAllNodesWithText("Toggle Beans", substring = true) .fetchSemanticsNodes().isNotEmpty()
health/src/test/java/com/vayunmathur/health/ui/UncoveredScreensTest.kt
diff --git a/health/src/test/java/com/vayunmathur/health/ui/UncoveredScreensTest.kt b/health/src/test/java/com/vayunmathur/health/ui/UncoveredScreensTest.ktindex 3553f3946b..2c193471f4 100644--- a/health/src/test/java/com/vayunmathur/health/ui/UncoveredScreensTest.kt+++ b/health/src/test/java/com/vayunmathur/health/ui/UncoveredScreensTest.kt@@ -3,11 +3,12 @@ package com.vayunmathur.health.ui import android.app.Application import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.ui.test.assertIsDisplayed-import androidx.compose.ui.test.hasScrollAction-import androidx.compose.ui.test.hasText-import androidx.compose.ui.test.performScrollToNode import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onNodeWithText+import androidx.compose.ui.test.performScrollToNode+import androidx.compose.ui.test.hasText+import androidx.compose.ui.test.hasScrollAction+import androidx.compose.ui.test.onAllNodesWithText import androidx.compose.ui.test.performClick import androidx.compose.ui.test.performTextInput import androidx.room3.Room@@ -143,7 +144,21 @@ class UncoveredScreensTest { val backStack = NavBackStack<Route>(arrayOf(Route.Today, Route.RecipeManagement)) rule.setContent { RecipeManagementPage(backStack, vm()) } rule.waitForIdle()- rule.onNode(hasScrollAction()).performScrollToNode(hasText("Screen Salad"))+ // Both pager pages (and the pager itself) are scrollable and the+ // shared suite database can leave this row far down the lazy list,+ // so scroll every container until the row composes.+ val deadline = System.currentTimeMillis() + 60_000+ var found = false+ while (!found && System.currentTimeMillis() < deadline) {+ val scrollCount = rule.onAllNodes(hasScrollAction()).fetchSemanticsNodes().size+ for (i in 0 until scrollCount) {+ runCatching {+ rule.onAllNodes(hasScrollAction())[i].performScrollToNode(hasText("Screen Salad"))+ }+ }+ found = rule.onAllNodesWithText("Screen Salad").fetchSemanticsNodes().isNotEmpty()+ if (!found) Thread.sleep(20)+ } rule.onNodeWithText("Screen Salad").assertExists() rule.onNodeWithText("Ingredients").performClick() rule.waitForIdle()