Modern Apps git

← Modern-Apps.git

shop: make profile gate Route.ProfileSetup graph root (#nav)

Commit
dd2a5b7c913f91a7029c2310cb3d4172f25a29e4
Author
Vayun Mathur <[email protected]>
When
2026-11-16 20:13 UTC (19 days ago)
Parents
4978b0d
Navigation returned ProfileScreen(required=true) before the back
stack existed whenever the buyer profile was incomplete, so the
gate screen lived outside the navigation graph. Route.ProfileSetup
is now the graph root when the profile is not complete
(shopStartRoute), and a LaunchedEffect resets to Route.Search
once the profile loads complete and ProfileSetup is still on the
stack (applyShopProfileGate) — reset, so Back never returns to
the gate, and guarded so later Profile-tab edits never reset the
user's stack. The ProfileSetup entry renders nothing until the
profile loads, preserving the previous blank-until-loaded
behaviour with no flash of an empty form.

Deep links arriving while gated are preserved: the pending-route
effect no longer consumes a route while the profile is
incomplete, and re-runs when the gate lifts so the route lands
after the reset to Search, as before.

The ShopTabs pager and pendingRoute mechanism are otherwise
unchanged. New ShopProfileGateTest covers start-route selection,
the gate reset and its idempotence, and pending-route deferral;
route serialization tests cover ProfileSetup, and a compose test
flips the profile valid mid-composition and asserts the tabs
appear.

Verified: ./gradlew :shop:compileDevKotlin :shop:testDevUnitTest

Diff

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 dc26c52f2a..1409ec5fef 100644--- a/shop/src/main/java/com/vayunmathur/shop/Navigation.kt+++ b/shop/src/main/java/com/vayunmathur/shop/Navigation.kt@@ -17,6 +17,7 @@ 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.NavBackStack import com.vayunmathur.library.util.SiblingPage import com.vayunmathur.library.util.rememberNavBackStack import com.vayunmathur.shop.platform.ShopViewModel@@ -27,6 +28,28 @@ import com.vayunmathur.shop.ui.ProductScreen import com.vayunmathur.shop.ui.ProfileScreen import com.vayunmathur.shop.ui.SearchScreen +/** Graph root for the current profile state: the gate until the profile validates, then Search. */+internal fun shopStartRoute(profileComplete: Boolean): Route =+    if (profileComplete) Route.Search else Route.ProfileSetup++/**+ * Lifts the profile gate. Only fires once the profile has loaded and validates,+ * and only while [Route.ProfileSetup] is still on the stack — later edits via+ * the Profile tab (which also keep the profile complete) must not reset the+ * user's stack. reset() rather than add(): there is no going back to the gate.+ * Returns true when the stack was reset.+ */+internal fun applyShopProfileGate(+    backStack: NavBackStack<Route>,+    profileLoaded: Boolean,+    profileComplete: Boolean,+): Boolean {+    if (!profileLoaded || !profileComplete) return false+    if (Route.ProfileSetup !in backStack.backStack) return false+    backStack.reset(Route.Search)+    return true+}+ @Composable fun Navigation(     viewModel: ShopViewModel,@@ -35,21 +58,31 @@ fun Navigation( ) {     val context = LocalContext.current     LaunchedEffect(viewModel) { viewModel.observeProfile(context) }-    if (!viewModel.profileLoaded) return-    if (!viewModel.profileComplete) {-        ProfileScreen(viewModel, required = true)-        return+    // The gate is a graph root, not an early return: before the profile loads+    // it is not known to be complete, so the stack starts at ProfileSetup and+    // the gate effect below lifts it to Search once a complete profile loads+    // (or is saved from the gate itself). A user whose profile is already+    // complete at first composition starts at Search directly.+    val backStack = rememberNavBackStack<Route>(shopStartRoute(viewModel.profileComplete))+    LaunchedEffect(viewModel.profileLoaded, viewModel.profileComplete) {+        applyShopProfileGate(backStack, viewModel.profileLoaded, viewModel.profileComplete)     }-    // 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)+    // While the gate is up the reset is deferred — the gate effect lifts to+    // Search first and this effect re-runs on the stack change.+    LaunchedEffect(ordersRequest, backStack.backStack.lastOrNull()) {+        if (ordersRequest > 0 && backStack.last() != Route.Orders &&+            backStack.backStack.lastOrNull() != Route.ProfileSetup+        ) {+            backStack.reset(Route.Orders)+        }     }     // 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) {+    // While gated, the link is left unconsumed and applies after the gate lifts.+    LaunchedEffect(linkRequest, backStack.backStack.lastOrNull()) {         val url = linkRequest?.second ?: return@LaunchedEffect+        if (backStack.backStack.lastOrNull() == Route.ProfileSetup) return@LaunchedEffect         viewModel.openLink(url) { route ->             if (route is Route.ProductDetail) backStack.reset(Route.Search, route) else backStack.reset(route)         }@@ -68,6 +101,11 @@ fun Navigation(             notificationPermission.launch(Manifest.permission.POST_NOTIFICATIONS)         }     }+    if (backStack.backStack.lastOrNull() == Route.ProfileSetup) {+        // Gate up: no tab bar — the required profile form is the whole graph.+        MainNavigation(backStack) { shopEntries(backStack, viewModel, context) }+        return+    }     val tabs = listOf<RouteTab<Route>>(         RouteTab(Route.Search, stringResource(R.string.search)) { IconSearch() },         RouteTab(Route.Cart, stringResource(R.string.cart)) { IconShoppingCart() },@@ -79,40 +117,52 @@ fun Navigation(     // 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()-                })-            }-        }+        MainNavigation(backStack) { shopEntries(backStack, viewModel, context) }+    }+}++private fun com.vayunmathur.library.util.EntryProviderScope<Route>.shopEntries(+    backStack: NavBackStack<Route>,+    viewModel: ShopViewModel,+    context: android.content.Context,+) {+    entry<Route.ProfileSetup> {+        // Still inside the graph while loading: nothing renders until the+        // repository emits, matching the previous blank-until-loaded gate,+        // then the same required profile form as before.+        if (viewModel.profileLoaded) ProfileScreen(viewModel, required = true)+    }+    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()+        })     } }
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 42a6754f62..81128eb9de 100644--- a/shop/src/main/java/com/vayunmathur/shop/Route.kt+++ b/shop/src/main/java/com/vayunmathur/shop/Route.kt@@ -6,6 +6,15 @@ import kotlinx.serialization.Serializable /** Contract for route. */ @Serializable sealed interface Route : NavKey {+    /**+     * First-run profile gate. The graph root while the saved profile is+     * incomplete; replaced by [Search] via reset once it validates, so Back+     * can never return to the gate. Later profile edits stay in the Profile+     * tab inside [Search] and never use this route.+     */+    @Serializable+    data object ProfileSetup : Route+     /** Singleton holding search state and operations. */     @Serializable     data object Search : Route
shop/src/test/java/com/vayunmathur/shop/ShopProfileGateTest.kt
diff --git a/shop/src/test/java/com/vayunmathur/shop/ShopProfileGateTest.kt b/shop/src/test/java/com/vayunmathur/shop/ShopProfileGateTest.ktnew file mode 100644index 0000000000..a0838cfb15--- /dev/null+++ b/shop/src/test/java/com/vayunmathur/shop/ShopProfileGateTest.kt@@ -0,0 +1,68 @@+package com.vayunmathur.shop++import com.vayunmathur.library.util.NavBackStack+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertFalse+import kotlin.test.assertTrue+import kotlinx.serialization.encodeToString+import kotlinx.serialization.json.Json++/** Pure route-level tests for the shop profile gate (no Compose / Android). */+class ShopProfileGateTest {+    private fun stackOf(vararg routes: Route) = NavBackStack<Route>(arrayOf(*routes))++    @Test fun startRouteIsProfileSetupUntilCompleteThenSearch() {+        assertEquals(Route.ProfileSetup, shopStartRoute(profileComplete = false))+        assertEquals(Route.Search, shopStartRoute(profileComplete = true))+    }++    @Test fun profileSetupSerializesAsRoute() {+        val json = Json { ignoreUnknownKeys = true }+        assertEquals(+            Route.ProfileSetup,+            json.decodeFromString<Route>(json.encodeToString<Route>(Route.ProfileSetup)),+        )+    }++    @Test fun gateDoesNotResetBeforeLoadOrWhileIncomplete() {+        val stack = stackOf(Route.ProfileSetup)+        assertFalse(applyShopProfileGate(stack, profileLoaded = false, profileComplete = false))+        assertFalse(applyShopProfileGate(stack, profileLoaded = true, profileComplete = false))+        assertEquals(listOf(Route.ProfileSetup), stack.backStack)+    }++    @Test fun gateResetsProfileSetupToSearchOnceComplete() {+        val stack = stackOf(Route.ProfileSetup)+        assertTrue(applyShopProfileGate(stack, profileLoaded = true, profileComplete = true))+        assertEquals(listOf(Route.Search), stack.backStack)+        // Idempotent: the lifted stack is left alone on later recompositions.+        assertFalse(applyShopProfileGate(stack, profileLoaded = true, profileComplete = true))+        assertEquals(listOf(Route.Search), stack.backStack)+    }++    @Test fun gateDoesNotResetSearchStackWhenEditingProfileViaTab() {+        val stack = stackOf(Route.Search, Route.ProductDetail("v1", "shopify", "s.example"))+        assertFalse(applyShopProfileGate(stack, profileLoaded = true, profileComplete = true))+        assertEquals(listOf(Route.Search, Route.ProductDetail("v1", "shopify", "s.example")), stack.backStack)+    }++    @Test fun pendingRouteWaitsForGateThenAppliesAfterReset() {+        val stack = stackOf(Route.ProfileSetup)+        val pending = Route.ProductDetail("v1", "shopify", "cool.example")+        // Arrived while gated: not applied, not consumed.+        assertFalse(applyShopPendingRoute(stack, pending, profileComplete = false))+        assertEquals(listOf(Route.ProfileSetup), stack.backStack)+        // Gate lifts, then the same pending route applies on top of Search.+        assertTrue(applyShopProfileGate(stack, profileLoaded = true, profileComplete = true))+        assertTrue(applyShopPendingRoute(stack, pending, profileComplete = true))+        assertEquals(listOf(Route.Search, pending), stack.backStack)+    }++    @Test fun pendingSearchRouteResetsToSearchRoot() {+        val stack = stackOf(Route.Search, Route.Cart)+        assertTrue(applyShopPendingRoute(stack, Route.Search, profileComplete = true))+        assertEquals(listOf(Route.Search), stack.backStack)+        assertFalse(applyShopPendingRoute(stack, null, profileComplete = true))+    }+}
shop/src/test/java/com/vayunmathur/shop/data/ShopDataCoverageTest.kt
diff --git a/shop/src/test/java/com/vayunmathur/shop/data/ShopDataCoverageTest.kt b/shop/src/test/java/com/vayunmathur/shop/data/ShopDataCoverageTest.ktindex 08bcfd2049..103cf918e4 100644--- a/shop/src/test/java/com/vayunmathur/shop/data/ShopDataCoverageTest.kt+++ b/shop/src/test/java/com/vayunmathur/shop/data/ShopDataCoverageTest.kt@@ -54,6 +54,7 @@ class ShopDataCoverageTest {     }      @Test fun routeEqualityAndSerialization() {+        assertEquals(Route.ProfileSetup, Route.ProfileSetup)         assertEquals(Route.Search, Route.Search); assertEquals(Route.Cart, Route.Cart)         assertEquals(Route.Orders, Route.Orders); assertEquals(Route.EmbeddedCheckout, Route.EmbeddedCheckout)         val detail = Route.ProductDetail("v1","shopify","s.example")@@ -64,5 +65,6 @@ class ShopDataCoverageTest {         val encoded = json.encodeToString<Route>(detail)         assertEquals(detail, json.decodeFromString<Route>(encoded))         assertEquals(Route.Search, json.decodeFromString<Route>(json.encodeToString<Route>(Route.Search)))+        assertEquals(Route.ProfileSetup, json.decodeFromString<Route>(json.encodeToString<Route>(Route.ProfileSetup)))     } }
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 77f1f4d042..cc261374eb 100644--- a/shop/src/test/java/com/vayunmathur/shop/ui/ShopScreensCoverage2Test.kt+++ b/shop/src/test/java/com/vayunmathur/shop/ui/ShopScreensCoverage2Test.kt@@ -100,6 +100,20 @@ class ShopScreensCoverage2Test {         composeRule.onNodeWithText("Add your details to start shopping.").assertIsDisplayed()     } +    @Test fun navigation_gateResetsToSearchWhenProfileCompletes() {+        val vm = ShopViewModel(); stubProfileRepository(vm)+        setVmField(vm, "profileLoaded", true)+        composeRule.setContent { Navigation(vm) }+        composeRule.waitForIdle()+        composeRule.onNodeWithText("Add your details to start shopping.").assertIsDisplayed()+        // Saving a valid profile (as the gate's Continue does via the repository+        // flow) flips profileComplete; the gate root must reset to Search.+        setVmField(vm, "profile", ShopProfile(firstName="Ada", lastName="L", email="[email protected]", phone="+14155550123"))+        composeRule.waitForIdle()+        composeRule.onNodeWithText(str(R.string.no_products_found)).assertIsDisplayed()+        composeRule.onNodeWithText("Add your details to start shopping.").assertDoesNotExist()+    }+     @Test fun navigation_completeProfileShowsSearchTabs() {         val vm = ShopViewModel(); stubProfileRepository(vm)         setVmField(vm, "profile", ShopProfile(firstName="Ada", lastName="L", email="[email protected]", phone="+14155550123"))