shop: tabs are sibling routes (#nav)
- Commit
402af98c606ddf129345e560f19d9a0297c8830d- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-16 20:00 UTC (19 days ago)
- Parents
69334d3
Delete the pager inside Route.Search (ShopTabs.kt removed) and push the already-declared Route.Cart/Route.Orders plus new Route.Profile as siblings alongside Search, all SiblingPage() in route-backed TabbedPagerScaffold hosting MainNavigation. VM pendingRoute/LaunchedEffect handoff deleted: deep links use direct backStack reset/add calls from Navigation/MainActivity. Profile-gate row untouched. Verified: ./gradlew :shop:testDevUnitTest :shop:compileDevKotlin green (21 test classes).
Diff
shop/src/main/java/com/vayunmathur/shop/MainActivity.kt
diff --git a/shop/src/main/java/com/vayunmathur/shop/MainActivity.kt b/shop/src/main/java/com/vayunmathur/shop/MainActivity.ktindex cbe0542560..79c53e830c 100644--- a/shop/src/main/java/com/vayunmathur/shop/MainActivity.kt+++ b/shop/src/main/java/com/vayunmathur/shop/MainActivity.kt@@ -3,6 +3,10 @@ package com.vayunmathur.shop import android.content.Intent import androidx.activity.viewModels import androidx.compose.runtime.Composable+import androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableIntStateOf+import androidx.compose.runtime.mutableStateOf+import androidx.compose.runtime.setValue import com.vayunmathur.library.ui.MaActivity import com.vayunmathur.shop.platform.AppInit import com.vayunmathur.shop.platform.ShopViewModel@@ -11,6 +15,18 @@ import com.vayunmathur.shop.platform.ShopViewModel class MainActivity : MaActivity() { private val viewModel: ShopViewModel by viewModels() + /**+ * Counts Orders deep-links rather than latching a flag: the notification can be+ * tapped again after the user has moved to another tab, and an unchanged flag+ * would not re-navigate. Navigation turns a new count into a direct+ * `backStack.reset(Route.Orders)` (and into the start route on a cold start).+ */+ private var ordersRequest by mutableIntStateOf(0)++ /** Latest merchant link to open, sequenced so re-delivering the same URL re-navigates. */+ private var linkRequest by mutableStateOf<Pair<Int, String>?>(null)+ private var linkSequence = 0+ override fun onCreateBeforeContent() { // Trust-bundle warm-up off the main thread; screens await AppInit.awaitReady(). AppInit.start(this)@@ -29,11 +45,11 @@ class MainActivity : MaActivity() { private fun handleLink(intent: Intent?) { if (intent?.action == ACTION_SHOW_ORDERS) {- viewModel.showOrders()+ ordersRequest++ return } if (intent?.action != Intent.ACTION_VIEW) return- intent.dataString?.let(viewModel::openLink)+ intent.dataString?.let { linkRequest = ++linkSequence to it } } /** Shared constants and helpers for MainActivity. */@@ -43,6 +59,6 @@ class MainActivity : MaActivity() { @Composable override fun Content() {- Navigation(viewModel)+ Navigation(viewModel, ordersRequest = ordersRequest, linkRequest = linkRequest) } }
shop/src/main/java/com/vayunmathur/shop/Navigation.kt
diff --git a/shop/src/main/java/com/vayunmathur/shop/Navigation.kt b/shop/src/main/java/com/vayunmathur/shop/Navigation.ktindex b192e4b6ff..dc26c52f2a 100644--- a/shop/src/main/java/com/vayunmathur/shop/Navigation.kt+++ b/shop/src/main/java/com/vayunmathur/shop/Navigation.kt@@ -1,9 +1,23 @@ package com.vayunmathur.shop +import android.Manifest+import android.content.pm.PackageManager+import android.os.Build+import androidx.activity.compose.rememberLauncherForActivityResult+import androidx.activity.result.contract.ActivityResultContracts import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.ui.platform.LocalContext+import androidx.compose.ui.res.stringResource+import androidx.core.content.ContextCompat+import com.vayunmathur.library.ui.IconPerson+import com.vayunmathur.library.ui.IconReceipt+import com.vayunmathur.library.ui.IconSearch+import com.vayunmathur.library.ui.IconShoppingCart+import com.vayunmathur.library.ui.RouteTab+import com.vayunmathur.library.ui.TabbedPagerScaffold import com.vayunmathur.library.util.MainNavigation+import com.vayunmathur.library.util.SiblingPage import com.vayunmathur.library.util.rememberNavBackStack import com.vayunmathur.shop.platform.ShopViewModel import com.vayunmathur.shop.ui.CartScreen@@ -11,10 +25,14 @@ import com.vayunmathur.shop.ui.EmbeddedCheckoutScreen import com.vayunmathur.shop.ui.OrdersScreen import com.vayunmathur.shop.ui.ProductScreen import com.vayunmathur.shop.ui.ProfileScreen-import com.vayunmathur.shop.ui.ShopTabs+import com.vayunmathur.shop.ui.SearchScreen @Composable-fun Navigation(viewModel: ShopViewModel) {+fun Navigation(+ viewModel: ShopViewModel,+ ordersRequest: Int = 0,+ linkRequest: Pair<Int, String>? = null,+) { val context = LocalContext.current LaunchedEffect(viewModel) { viewModel.observeProfile(context) } if (!viewModel.profileLoaded) return@@ -22,40 +40,79 @@ fun Navigation(viewModel: ShopViewModel) { ProfileScreen(viewModel, required = true) return }- val backStack = rememberNavBackStack<Route>(Route.Search)- LaunchedEffect(viewModel.pendingRoute) {- val route = viewModel.pendingRoute ?: return@LaunchedEffect- if (route == Route.Search) backStack.reset(Route.Search) else backStack.reset(Route.Search, route)- viewModel.consumePendingRoute()+ // Deep-link initial tab: a cold-start Orders link starts on Orders, not Search.+ val backStack = rememberNavBackStack<Route>(if (ordersRequest > 0) Route.Orders else Route.Search)+ // A later Orders link: direct sibling reset (no ViewModel route handoff).+ LaunchedEffect(ordersRequest) {+ if (ordersRequest > 0 && backStack.last() != Route.Orders) backStack.reset(Route.Orders) }- MainNavigation(backStack) {- entry<Route.Search> {- ShopTabs(- viewModel = viewModel,- onProductClick = { variantId, platformId, shopDomain ->- backStack.add(Route.ProductDetail(variantId, platformId, shopDomain))- },- onPayInApp = { backStack.add(Route.EmbeddedCheckout) },- )+ // Merchant links resolve in the ViewModel, which calls back with the destination;+ // this is the direct backStack call that replaces the old pendingRoute effect.+ LaunchedEffect(linkRequest) {+ val url = linkRequest?.second ?: return@LaunchedEffect+ viewModel.openLink(url) { route ->+ if (route is Route.ProductDetail) backStack.reset(Route.Search, route) else backStack.reset(route) }- entry<Route.ProductDetail> { route ->- ProductScreen(- viewModel = viewModel,- variantId = route.variantId,- platformId = route.platformId,- shopDomain = route.shopDomain,- onBack = { backStack.pop() },- onPayInApp = { backStack.add(Route.EmbeddedCheckout) },- )- }- entry<Route.Cart> {- CartScreen(viewModel, onPayInApp = { backStack.add(Route.EmbeddedCheckout) })+ }+ LaunchedEffect(viewModel) {+ viewModel.loadCart(context)+ viewModel.loadOrders(context)+ }+ val notificationPermission = rememberLauncherForActivityResult(ActivityResultContracts.RequestPermission()) {}+ LaunchedEffect(viewModel.notificationPermissionRequest) {+ if (viewModel.notificationPermissionRequest > 0 &&+ Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU &&+ ContextCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) !=+ PackageManager.PERMISSION_GRANTED+ ) {+ notificationPermission.launch(Manifest.permission.POST_NOTIFICATIONS) }- entry<Route.EmbeddedCheckout> {- EmbeddedCheckoutScreen(viewModel = viewModel, onClose = {- if (backStack.backStack.lastOrNull() == Route.EmbeddedCheckout) backStack.pop()- })+ }+ val tabs = listOf<RouteTab<Route>>(+ RouteTab(Route.Search, stringResource(R.string.search)) { IconSearch() },+ RouteTab(Route.Cart, stringResource(R.string.cart)) { IconShoppingCart() },+ RouteTab(Route.Orders, stringResource(R.string.orders)) { IconReceipt() },+ RouteTab(Route.Profile, "Profile") { IconPerson() },+ )+ // Route-backed tabs: selecting a tab resets the stack to that sibling route.+ // MainNavigation is hosted in the content slot, so detail/checkout destinations+ // pushed over a tab render here with no tab selected, and each entry keeps its+ // own rememberSaveable state under its route.+ TabbedPagerScaffold(backStack = backStack, tabs = tabs) {+ MainNavigation(backStack) {+ entry<Route.Search>(metadata = SiblingPage()) {+ SearchScreen(+ viewModel = viewModel,+ onProductClick = { variantId, platformId, shopDomain ->+ backStack.add(Route.ProductDetail(variantId, platformId, shopDomain))+ },+ )+ }+ entry<Route.Cart>(metadata = SiblingPage()) {+ CartScreen(viewModel, onPayInApp = { backStack.add(Route.EmbeddedCheckout) })+ }+ entry<Route.Orders>(metadata = SiblingPage()) {+ LaunchedEffect(Unit) { viewModel.refreshTracking(context) }+ OrdersScreen(viewModel = viewModel)+ }+ entry<Route.Profile>(metadata = SiblingPage()) {+ ProfileScreen(viewModel, required = false)+ }+ entry<Route.ProductDetail> { route ->+ ProductScreen(+ viewModel = viewModel,+ variantId = route.variantId,+ platformId = route.platformId,+ shopDomain = route.shopDomain,+ onBack = { backStack.pop() },+ onPayInApp = { backStack.add(Route.EmbeddedCheckout) },+ )+ }+ entry<Route.EmbeddedCheckout> {+ EmbeddedCheckoutScreen(viewModel = viewModel, onClose = {+ if (backStack.backStack.lastOrNull() == Route.EmbeddedCheckout) backStack.pop()+ })+ } }- entry<Route.Orders> { OrdersScreen(viewModel = viewModel) } } }
shop/src/main/java/com/vayunmathur/shop/Route.kt
diff --git a/shop/src/main/java/com/vayunmathur/shop/Route.kt b/shop/src/main/java/com/vayunmathur/shop/Route.ktindex 69741130c8..42a6754f62 100644--- a/shop/src/main/java/com/vayunmathur/shop/Route.kt+++ b/shop/src/main/java/com/vayunmathur/shop/Route.kt@@ -26,6 +26,10 @@ sealed interface Route : NavKey { @Serializable data object Orders : Route + /** Singleton holding profile state and operations. */+ @Serializable+ data object Profile : Route+ /** Request (with its secret-bearing URL) lives in ShopViewModel, never in saved state. */ @Serializable data object EmbeddedCheckout : Route
shop/src/main/java/com/vayunmathur/shop/platform/ShopViewModel.kt
diff --git a/shop/src/main/java/com/vayunmathur/shop/platform/ShopViewModel.kt b/shop/src/main/java/com/vayunmathur/shop/platform/ShopViewModel.ktindex bce23ac4be..a449e57d28 100644--- a/shop/src/main/java/com/vayunmathur/shop/platform/ShopViewModel.kt+++ b/shop/src/main/java/com/vayunmathur/shop/platform/ShopViewModel.kt@@ -47,15 +47,11 @@ class ShopViewModel( private set var focusSearchRequest by mutableStateOf(0) private set- var focusOrdersRequest by mutableIntStateOf(0)- private set var refreshingTracking by mutableStateOf(false) private set /** Set after an order is placed so the UI can ask for the delivery-notification permission. */ var notificationPermissionRequest by mutableIntStateOf(0) private set- var pendingRoute by mutableStateOf<Route?>(null)- private set var linkedProducts by mutableStateOf<List<UcpProduct>>(emptyList()) private set var launchLinkHandled = false@@ -150,8 +146,6 @@ class ShopViewModel( fun updateQuery(value: String) { query = value } - fun consumePendingRoute() { pendingRoute = null }- fun clearScope() { scopeDomain = null scopeName = null@@ -159,8 +153,13 @@ class ShopViewModel( search() } - /** Opens a merchant web link: product pages in-app, anything else as a search scoped to that store. */- fun openLink(url: String) {+ /**+ * Opens a merchant web link: product pages in-app, anything else as a search scoped to that store.+ * Navigation is the caller's job: [navigate] is invoked directly with the destination+ * (a product detail, or [Route.Search] for a scoped search) instead of parking a route+ * in this ViewModel for a UI effect to pick up.+ */+ fun openLink(url: String, navigate: (Route) -> Unit = {}) { val link = MerchantLinks.parse(url) ?: return viewModelScope.launch { AppInit.awaitReady()@@ -174,27 +173,27 @@ class ShopViewModel( onSuccess = { product -> val variant = product.variants.firstOrNull() if (variant == null) {- scopedSearch(domain, name, link.handle.replace('-', ' '))+ scopedSearch(domain, name, link.handle.replace('-', ' '), navigate) } else { linkedProducts = (linkedProducts + product).takeLast(LINKED_PRODUCT_LIMIT) val publisher = variant.shopDomain.ifBlank { product.shopDomain }- pendingRoute = Route.ProductDetail(variant.id, product.platformId, publisher)+ navigate(Route.ProductDetail(variant.id, product.platformId, publisher)) } },- onFailure = { scopedSearch(domain, name, link.handle.replace('-', ' ')) },+ onFailure = { scopedSearch(domain, name, link.handle.replace('-', ' '), navigate) }, )- is MerchantLink.Search -> scopedSearch(domain, name, link.query)- is MerchantLink.Merchant -> scopedSearch(domain, name, "")+ is MerchantLink.Search -> scopedSearch(domain, name, link.query, navigate)+ is MerchantLink.Merchant -> scopedSearch(domain, name, "", navigate) } } } - private fun scopedSearch(domain: String, name: String, scopedQuery: String) {+ private fun scopedSearch(domain: String, name: String, scopedQuery: String, navigate: (Route) -> Unit) { scopeDomain = domain scopeName = name query = scopedQuery results = emptyList()- pendingRoute = Route.Search+ navigate(Route.Search) focusSearchRequest++ if (scopedQuery.isNotBlank()) search() }@@ -210,8 +209,6 @@ class ShopViewModel( } } - fun showOrders() { focusOrdersRequest++ }- fun refreshTracking(context: Context) { if (refreshingTracking) return refreshingTracking = true
shop/src/main/java/com/vayunmathur/shop/ui/ProfileScreen.kt
diff --git a/shop/src/main/java/com/vayunmathur/shop/ui/ProfileScreen.kt b/shop/src/main/java/com/vayunmathur/shop/ui/ProfileScreen.ktindex 2b89462088..7d4745343c 100644--- a/shop/src/main/java/com/vayunmathur/shop/ui/ProfileScreen.kt+++ b/shop/src/main/java/com/vayunmathur/shop/ui/ProfileScreen.kt@@ -10,7 +10,8 @@ import androidx.compose.foundation.text.KeyboardOptions import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf-import androidx.compose.runtime.remember+import androidx.compose.runtime.saveable.listSaver+import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.text.input.KeyboardType@@ -25,12 +26,28 @@ import com.vayunmathur.library.ui.rememberMessenger import com.vayunmathur.shop.data.ShopProfile import com.vayunmathur.shop.platform.ShopViewModel +/** Draft edits survive tab switches and process recreation, so the draft is saveable state. */+private val ShopProfileSaver = listSaver<ShopProfile, String>(+ save = {+ listOf(+ it.firstName, it.lastName, it.email, it.phone, it.street, it.extendedAddress,+ it.city, it.region, it.country, it.postalCode,+ )+ },+ restore = {+ ShopProfile(+ firstName = it[0], lastName = it[1], email = it[2], phone = it[3], street = it[4],+ extendedAddress = it[5], city = it[6], region = it[7], country = it[8], postalCode = it[9],+ )+ },+)+ /** Buyer profile. With [required] it is the first-run gate and the rest of the app stays locked until saved. */ @Composable fun ProfileScreen(viewModel: ShopViewModel, required: Boolean) { val messenger = rememberMessenger() val saved = viewModel.profile- var draft by remember(saved) { mutableStateOf(saved ?: ShopProfile()) }+ var draft by rememberSaveable(saved, stateSaver = ShopProfileSaver) { mutableStateOf(saved ?: ShopProfile()) } val error = draft.validationError() AppScaffold(title = "Profile", scrollBehavior = appBarScrollBehavior()) { padding -> LazyColumn(
shop/src/main/java/com/vayunmathur/shop/ui/SearchScreen.kt
diff --git a/shop/src/main/java/com/vayunmathur/shop/ui/SearchScreen.kt b/shop/src/main/java/com/vayunmathur/shop/ui/SearchScreen.ktindex c03fb9d1db..f9618a810c 100644--- a/shop/src/main/java/com/vayunmathur/shop/ui/SearchScreen.kt+++ b/shop/src/main/java/com/vayunmathur/shop/ui/SearchScreen.kt@@ -22,6 +22,7 @@ import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember+import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier@@ -68,7 +69,7 @@ fun SearchScreen( viewModel.dismissSearchError() } }- var showLinkPrompt by remember { mutableStateOf(LinkDefaults.shouldPrompt(context)) }+ var showLinkPrompt by rememberSaveable { mutableStateOf(LinkDefaults.shouldPrompt(context)) } SearchContent( query = viewModel.query, onQueryChange = viewModel::updateQuery,
shop/src/main/java/com/vayunmathur/shop/ui/ShopTabs.kt
diff --git a/shop/src/main/java/com/vayunmathur/shop/ui/ShopTabs.kt b/shop/src/main/java/com/vayunmathur/shop/ui/ShopTabs.ktdeleted file mode 100644index 63475ded10..0000000000--- a/shop/src/main/java/com/vayunmathur/shop/ui/ShopTabs.kt+++ /dev/null@@ -1,69 +0,0 @@-package com.vayunmathur.shop.ui--import android.Manifest-import android.content.pm.PackageManager-import android.os.Build-import androidx.activity.compose.rememberLauncherForActivityResult-import androidx.activity.result.contract.ActivityResultContracts-import androidx.compose.foundation.pager.rememberPagerState-import androidx.compose.runtime.Composable-import androidx.compose.runtime.LaunchedEffect-import androidx.compose.ui.platform.LocalContext-import androidx.compose.ui.res.stringResource-import androidx.core.content.ContextCompat-import com.vayunmathur.library.ui.IconPerson-import com.vayunmathur.library.ui.IconReceipt-import com.vayunmathur.library.ui.IconSearch-import com.vayunmathur.library.ui.IconShoppingCart-import com.vayunmathur.library.ui.PagerTab-import com.vayunmathur.library.ui.TabStyle-import com.vayunmathur.library.ui.TabbedPagerScaffold-import com.vayunmathur.shop.R-import com.vayunmathur.shop.platform.ShopViewModel--private const val ORDERS_PAGE = 2--@Composable-fun ShopTabs(viewModel: ShopViewModel, onProductClick: (String, String, String) -> Unit, onPayInApp: () -> Unit) {- val context = LocalContext.current- LaunchedEffect(Unit) {- viewModel.loadCart(context)- viewModel.loadOrders(context)- }- val pagerState = rememberPagerState(initialPage = 0, pageCount = { 4 })- // A scoped search opened from a merchant link always lands on the Search tab.- LaunchedEffect(viewModel.focusSearchRequest) {- if (viewModel.focusSearchRequest > 0) pagerState.animateScrollToPage(0)- }- LaunchedEffect(viewModel.focusOrdersRequest) {- if (viewModel.focusOrdersRequest > 0) pagerState.scrollToPage(ORDERS_PAGE)- }- LaunchedEffect(pagerState.currentPage) {- if (pagerState.currentPage == ORDERS_PAGE) viewModel.refreshTracking(context)- }- val notificationPermission = rememberLauncherForActivityResult(ActivityResultContracts.RequestPermission()) {}- LaunchedEffect(viewModel.notificationPermissionRequest) {- if (viewModel.notificationPermissionRequest > 0 &&- Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU &&- ContextCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) !=- PackageManager.PERMISSION_GRANTED- ) {- notificationPermission.launch(Manifest.permission.POST_NOTIFICATIONS)- }- }- val tabs = listOf(- PagerTab(stringResource(R.string.search), { IconSearch() }) {- SearchScreen(viewModel, onProductClick)- },- PagerTab(stringResource(R.string.cart), { IconShoppingCart() }) {- CartScreen(viewModel, onPayInApp = onPayInApp)- },- PagerTab(stringResource(R.string.orders), { IconReceipt() }) {- OrdersScreen(viewModel)- },- PagerTab("Profile", { IconPerson() }) {- ProfileScreen(viewModel, required = false)- },- )- TabbedPagerScaffold(tabs = tabs, pagerState = pagerState, tabStyle = TabStyle.BottomNav)-}
shop/src/test/java/com/vayunmathur/shop/platform/ShopPlatformCoverageTest.kt
diff --git a/shop/src/test/java/com/vayunmathur/shop/platform/ShopPlatformCoverageTest.kt b/shop/src/test/java/com/vayunmathur/shop/platform/ShopPlatformCoverageTest.ktindex 7933a79a13..8a2f42e88c 100644--- a/shop/src/test/java/com/vayunmathur/shop/platform/ShopPlatformCoverageTest.kt+++ b/shop/src/test/java/com/vayunmathur/shop/platform/ShopPlatformCoverageTest.kt@@ -107,10 +107,7 @@ class ShopPlatformCoverageTest { // Invalid profile -> null inputs setState(vm, "profile", ShopProfile()) assertNull(vm.checkoutInputs(state)); assertFalse(vm.profileComplete)- // showOrders / consumePendingRoute / updateQuery- vm.showOrders(); assertTrue(vm.focusOrdersRequest > 0) vm.updateQuery("shoes"); assertEquals("shoes", vm.query)- vm.consumePendingRoute(); assertNull(vm.pendingRoute) vm.closeEmbeddedCheckout(); assertNull(vm.embeddedCheckout) } }
shop/src/test/java/com/vayunmathur/shop/platform/ShopViewModelCoverage2Test.kt
diff --git a/shop/src/test/java/com/vayunmathur/shop/platform/ShopViewModelCoverage2Test.kt b/shop/src/test/java/com/vayunmathur/shop/platform/ShopViewModelCoverage2Test.ktindex 43d8f64a70..9614ad37fa 100644--- a/shop/src/test/java/com/vayunmathur/shop/platform/ShopViewModelCoverage2Test.kt+++ b/shop/src/test/java/com/vayunmathur/shop/platform/ShopViewModelCoverage2Test.kt@@ -130,7 +130,7 @@ class ShopViewModelCoverage2Test { await { vm.scopeDomain == "shoes.example" } assertEquals("trail shoes", vm.query); assertEquals("shoes.example", vm.scopeName) vm.openLink("https://shoes.example/products/no-variants")- // product failure path falls back to scoped search with handle words; pendingRoute Search+ // product failure path falls back to scoped search with handle words; navigate(Search) await { vm.query == "no variants" } }
shop/src/test/java/com/vayunmathur/shop/platform/ShopViewModelTest.kt
diff --git a/shop/src/test/java/com/vayunmathur/shop/platform/ShopViewModelTest.kt b/shop/src/test/java/com/vayunmathur/shop/platform/ShopViewModelTest.ktindex 635059539c..c5fbb7d9ad 100644--- a/shop/src/test/java/com/vayunmathur/shop/platform/ShopViewModelTest.kt+++ b/shop/src/test/java/com/vayunmathur/shop/platform/ShopViewModelTest.kt@@ -177,19 +177,19 @@ class ShopViewModelTest { fun openLink_productSuccessRoutesToDetailAndCapsLinked() { backend.merchant = MerchantListing("m1", "Cool Store", "cool.example", "https://cool.example") backend.productResult = Result.success(product())- vm.openLink("https://cool.example/products/trail-shoes")- await { vm.pendingRoute != null }- assertEquals(Route.ProductDetail("v1", "shopify", "cool.example"), vm.pendingRoute)+ val routes = mutableListOf<Route>()+ vm.openLink("https://cool.example/products/trail-shoes") { routes += it }+ await { routes.isNotEmpty() }+ assertEquals(listOf<Route>(Route.ProductDetail("v1", "shopify", "cool.example")), routes) assertEquals(1, vm.linkedProducts.size)- vm.consumePendingRoute()- assertNull(vm.pendingRoute) } @Test fun openLink_productFailureFallsBackToScopedSearch() { backend.productResult = Result.failure(IllegalStateException("404"))- vm.openLink("https://unknown.example/products/red-shoes")- await { vm.pendingRoute == Route.Search }+ val routes = mutableListOf<Route>()+ vm.openLink("https://unknown.example/products/red-shoes") { routes += it }+ await { routes == listOf(Route.Search) } assertEquals("unknown.example", vm.scopeDomain) assertEquals("red shoes", vm.query) assertTrue(vm.focusSearchRequest > 0)@@ -197,11 +197,12 @@ class ShopViewModelTest { @Test fun openLink_merchantAndInvalidUrls() {- vm.openLink("not a url")- assertNull(vm.pendingRoute)+ val routes = mutableListOf<Route>()+ vm.openLink("not a url") { routes += it }+ assertTrue(routes.isEmpty()) backend.merchant = MerchantListing("m1", "Kitchen Co", "kitchen.example", "https://kitchen.example")- vm.openLink("https://kitchen.example/collections/all")- await { vm.pendingRoute == Route.Search }+ vm.openLink("https://kitchen.example/collections/all") { routes += it }+ await { routes == listOf(Route.Search) } assertEquals("kitchen.example", vm.scopeDomain) assertEquals("Kitchen Co", vm.scopeName) }@@ -261,8 +262,6 @@ class ShopViewModelTest { vm2.loadOrders(context) await { !vm2.refreshingTracking } assertTrue(vm2.orders.isEmpty())- vm2.showOrders()- assertTrue(vm2.focusOrdersRequest > 0) } @Test
shop/src/test/java/com/vayunmathur/shop/ui/ShopScreensCoverage2Test.kt
diff --git a/shop/src/test/java/com/vayunmathur/shop/ui/ShopScreensCoverage2Test.kt b/shop/src/test/java/com/vayunmathur/shop/ui/ShopScreensCoverage2Test.ktindex 53a4296ebc..77f1f4d042 100644--- a/shop/src/test/java/com/vayunmathur/shop/ui/ShopScreensCoverage2Test.kt+++ b/shop/src/test/java/com/vayunmathur/shop/ui/ShopScreensCoverage2Test.kt@@ -108,6 +108,36 @@ class ShopScreensCoverage2Test { composeRule.waitForIdle() composeRule.onNodeWithText(str(R.string.no_products_found)).assertIsDisplayed() composeRule.onNodeWithText(str(R.string.cart)).assertIsDisplayed()+ // Sibling tabs are routes: tapping a tab resets the stack to that tab.+ composeRule.onNode(+ androidx.compose.ui.test.hasText(str(R.string.cart)) and androidx.compose.ui.test.hasClickAction(),+ ).performClick()+ composeRule.waitForIdle()+ composeRule.onNodeWithText(str(R.string.cart_empty)).assertIsDisplayed()+ composeRule.onNode(+ androidx.compose.ui.test.hasText(str(R.string.orders)) and androidx.compose.ui.test.hasClickAction(),+ ).performClick()+ composeRule.waitForIdle()+ composeRule.onNodeWithText(str(R.string.no_orders_yet)).assertIsDisplayed()+ composeRule.onNode(+ androidx.compose.ui.test.hasText("Profile") and androidx.compose.ui.test.hasClickAction(),+ ).performClick()+ composeRule.waitForIdle()+ composeRule.onNodeWithText("First name").assertIsDisplayed()+ composeRule.onNode(+ androidx.compose.ui.test.hasText(str(R.string.search)) and androidx.compose.ui.test.hasClickAction(),+ ).performClick()+ composeRule.waitForIdle()+ composeRule.onNodeWithText(str(R.string.no_products_found)).assertIsDisplayed()+ }++ @Test fun navigation_ordersRequestStartsOnOrdersTab() {+ val vm = ShopViewModel(); stubProfileRepository(vm)+ setVmField(vm, "profile", ShopProfile(firstName="Ada", lastName="L", email="[email protected]", phone="+14155550123"))+ setVmField(vm, "profileLoaded", true)+ composeRule.setContent { Navigation(vm, ordersRequest = 1) }+ composeRule.waitForIdle()+ composeRule.onNodeWithText(str(R.string.no_orders_yet)).assertIsDisplayed() } @Test fun productScreen_liveRefreshVerifiesAndAddsToCart() {
shop/src/test/java/com/vayunmathur/shop/ui/ShopScreensTest.kt
diff --git a/shop/src/test/java/com/vayunmathur/shop/ui/ShopScreensTest.kt b/shop/src/test/java/com/vayunmathur/shop/ui/ShopScreensTest.ktindex 9d24f7d433..4ceb78f9a9 100644--- a/shop/src/test/java/com/vayunmathur/shop/ui/ShopScreensTest.kt+++ b/shop/src/test/java/com/vayunmathur/shop/ui/ShopScreensTest.kt@@ -255,18 +255,6 @@ class ShopScreensTest { assertEquals(1, closed) } - @Test- fun shopTabs_rendersSearchTabAndBottomTabs() {- val vm = ShopViewModel()- composeRule.setContent {- ShopTabs(vm, onProductClick = { _, _, _ -> }, onPayInApp = {})- }- composeRule.waitForIdle()- composeRule.onNodeWithText(str(R.string.no_products_found)).assertIsDisplayed()- composeRule.onNodeWithText(str(R.string.cart)).assertIsDisplayed()- composeRule.onNodeWithText(str(R.string.orders)).assertIsDisplayed()- }- @Test fun cartScreenWrapper_showsVmCartAndRemoveEmptiesIt() { val vm = ShopViewModel()