Modern Apps git

← Modern-Apps.git

vpn: tabs are sibling routes (#nav)

Commit
c5353e4e09708b657a903c7006f71139e6d1dd8e
Author
Vayun Mathur <[email protected]>
When
2026-11-16 20:00 UTC (19 days ago)
Parents
80d1ecd
Replace Route.Main(initialTab)+rememberPagerState(3) with sibling Route.Tunnels/Logging(tab)/Settings registered with SiblingPage() in the route-backed TabbedPagerScaffold; tab selection resets the stack. Logging sub-tabs stay in-screen state (rememberSaveable tabIndex mirrored into Route.Logging(tab) via setLast). Deep link ACTION_APPLICATION_PREFERENCES becomes start route Route.Settings, otherwise Tunnels.

Verified: ./gradlew :vpn:testDevUnitTest :vpn:compileDevKotlin green.

Diff

vpn/src/main/java/com/vayunmathur/vpn/Navigation.kt
diff --git a/vpn/src/main/java/com/vayunmathur/vpn/Navigation.kt b/vpn/src/main/java/com/vayunmathur/vpn/Navigation.ktindex 0241ef50be..749bfe9c71 100644--- a/vpn/src/main/java/com/vayunmathur/vpn/Navigation.kt+++ b/vpn/src/main/java/com/vayunmathur/vpn/Navigation.kt@@ -3,29 +3,20 @@ package com.vayunmathur.vpn import android.content.Intent import androidx.activity.compose.LocalActivity import androidx.compose.runtime.Composable-import com.vayunmathur.library.util.ListDetailPage-import com.vayunmathur.library.util.ListPage-import com.vayunmathur.library.util.MainNavigation-import com.vayunmathur.library.util.MorphPage import com.vayunmathur.library.util.rememberNavBackStack import com.vayunmathur.vpn.platform.VpnViewModel-import com.vayunmathur.vpn.ui.BypassListPage-import com.vayunmathur.vpn.ui.ConfigDetailPage import com.vayunmathur.vpn.ui.VpnTabs  @Composable fun Navigation(vm: VpnViewModel) {-    // Land on the Settings tab when opened from the system App Info page.+    // Land on the Settings tab when opened from the system App Info page: the+    // deep-linked tab is the stack's start route, not an initial pager page.     val activity = LocalActivity.current-    val startTab = if (activity?.intent?.action == Intent.ACTION_APPLICATION_PREFERENCES) {-        2+    val startRoute: Route = if (activity?.intent?.action == Intent.ACTION_APPLICATION_PREFERENCES) {+        Route.Settings     } else {-        0-    }-    val backStack = rememberNavBackStack<Route>(Route.Main(startTab))-    MainNavigation(backStack) {-        entry<Route.Main>(metadata = ListPage()) { VpnTabs(backStack, vm, it.initialTab) }-        entry<Route.Detail>(metadata = ListDetailPage() + MorphPage()) { ConfigDetailPage(backStack, vm, it.id) }-        entry<Route.BypassList>(metadata = ListDetailPage()) { BypassListPage(backStack) }+        Route.Tunnels     }+    val backStack = rememberNavBackStack<Route>(startRoute)+    VpnTabs(backStack, vm) }
vpn/src/main/java/com/vayunmathur/vpn/Route.kt
diff --git a/vpn/src/main/java/com/vayunmathur/vpn/Route.kt b/vpn/src/main/java/com/vayunmathur/vpn/Route.ktindex b7583b8df0..b7b9e39372 100644--- a/vpn/src/main/java/com/vayunmathur/vpn/Route.kt+++ b/vpn/src/main/java/com/vayunmathur/vpn/Route.kt@@ -6,9 +6,22 @@ import kotlinx.serialization.Serializable /** Contract for route. */ @Serializable sealed interface Route : NavKey {-    /** Immutable value describing main; carries initialTab. */+    /** The Tunnels tab: a sibling destination switched with `backStack.reset`. */     @Serializable-    data class Main(val initialTab: Int = 0) : Route+    data object Tunnels : Route++    /**+     * The Logging tab: a sibling destination switched with `backStack.reset`.+     * [tab] is the in-screen leaderboard tab the Logging screen last showed, so+     * leaving and returning to Logging restores it; the sub-tabs themselves stay+     * in-screen state of the Logging destination (see `LoggingContent`).+     */+    @Serializable+    data class Logging(val tab: Int = 0) : Route++    /** The Settings tab: a sibling destination switched with `backStack.reset`. */+    @Serializable+    data object Settings : Route      /** Immutable value describing detail; carries id. */     @Serializable
vpn/src/main/java/com/vayunmathur/vpn/ui/LoggingContent.kt
diff --git a/vpn/src/main/java/com/vayunmathur/vpn/ui/LoggingContent.kt b/vpn/src/main/java/com/vayunmathur/vpn/ui/LoggingContent.ktindex 2c62fa9259..cf6c944bb8 100644--- a/vpn/src/main/java/com/vayunmathur/vpn/ui/LoggingContent.kt+++ b/vpn/src/main/java/com/vayunmathur/vpn/ui/LoggingContent.kt@@ -18,6 +18,7 @@ import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableIntStateOf 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@@ -64,9 +65,13 @@ fun LoggingContent(      * given tab can be captured without driving the UI to get there.      */     initialTab: Int = 0,+    /** Notified when the in-screen leaderboard tab changes, so the host can mirror it. */+    onTabChange: (Int) -> Unit = {}, ) {     var showDeleteDialog by remember { mutableStateOf(false) }-    var tabIndex by remember { mutableIntStateOf(initialTab) }+    // In-screen sub-tabs of the Logging destination: saveable so the chosen+    // leaderboard survives rotation/process recreation, not routed destinations.+    var tabIndex by rememberSaveable { mutableIntStateOf(initialTab) }      AppScaffold(         title = "",@@ -82,7 +87,7 @@ fun LoggingContent(                 TABS.forEachIndexed { i, label ->                     Tab(                         selected = tabIndex == i,-                        onClick = { tabIndex = i },+                        onClick = { tabIndex = i; onTabChange(i) },                         text = { Text(label, maxLines = 1, fontSize = 13.sp) },                     )                 }
vpn/src/main/java/com/vayunmathur/vpn/ui/LoggingPage.kt
diff --git a/vpn/src/main/java/com/vayunmathur/vpn/ui/LoggingPage.kt b/vpn/src/main/java/com/vayunmathur/vpn/ui/LoggingPage.ktindex dadf9dfd16..667119bc9a 100644--- a/vpn/src/main/java/com/vayunmathur/vpn/ui/LoggingPage.kt+++ b/vpn/src/main/java/com/vayunmathur/vpn/ui/LoggingPage.kt@@ -6,7 +6,11 @@ import androidx.compose.runtime.getValue import com.vayunmathur.vpn.platform.VpnViewModel  @Composable-fun LoggingPage(vm: VpnViewModel) {+fun LoggingPage(+    vm: VpnViewModel,+    initialTab: Int = 0,+    onTabChange: (Int) -> Unit = {},+) {     val topApps by vm.topAppsFlow.collectAsState()     val domainsByCount by vm.domainsByCountFlow.collectAsState()     val domainsByBytes by vm.domainsByBytesFlow.collectAsState()@@ -16,5 +20,7 @@ fun LoggingPage(vm: VpnViewModel) {         domainsByCount = domainsByCount,         domainsByBytes = domainsByBytes,         onDeleteAllLogs = { vm.deleteAllLogs() },+        initialTab = initialTab,+        onTabChange = onTabChange,     ) }
vpn/src/main/java/com/vayunmathur/vpn/ui/VpnTabs.kt
diff --git a/vpn/src/main/java/com/vayunmathur/vpn/ui/VpnTabs.kt b/vpn/src/main/java/com/vayunmathur/vpn/ui/VpnTabs.ktindex 54169a7fc3..75487fc8d0 100644--- a/vpn/src/main/java/com/vayunmathur/vpn/ui/VpnTabs.kt+++ b/vpn/src/main/java/com/vayunmathur/vpn/ui/VpnTabs.kt@@ -1,25 +1,74 @@ package com.vayunmathur.vpn.ui -import androidx.compose.foundation.pager.rememberPagerState import androidx.compose.runtime.Composable+import androidx.compose.runtime.LaunchedEffect+import androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableIntStateOf+import androidx.compose.runtime.saveable.rememberSaveable+import androidx.compose.runtime.setValue import com.vayunmathur.library.ui.IconDashboard import com.vayunmathur.library.ui.IconHistory import com.vayunmathur.library.ui.IconSettings-import com.vayunmathur.library.ui.PagerTab-import com.vayunmathur.library.ui.TabStyle+import com.vayunmathur.library.ui.RouteTab import com.vayunmathur.library.ui.TabbedPagerScaffold+import com.vayunmathur.library.util.ListDetailPage+import com.vayunmathur.library.util.ListPage+import com.vayunmathur.library.util.MainNavigation+import com.vayunmathur.library.util.MorphPage import com.vayunmathur.library.util.NavBackStack+import com.vayunmathur.library.util.SiblingPage import com.vayunmathur.vpn.Route import com.vayunmathur.vpn.platform.VpnViewModel -/** The three top-level tabs, hosted in a swipeable pager. Detail and BypassList push on top. */+/**+ * The three top-level tabs as sibling routes. The route-backed+ * [TabbedPagerScaffold] owns the tab bar / rail chrome and switches tabs with+ * `backStack.reset`, so each tab is a real destination (deep link, restore and+ * back see it) instead of a pager page inside one `Route.Main`. Detail and+ * BypassList push on top of whichever tab is showing.+ *+ * The Logging leaderboard tab is in-screen state of the Logging destination;+ * it is mirrored into [Route.Logging] (and remembered here for the tab's+ * target route) so returning to Logging restores the leaderboard last viewed.+ */ @Composable-fun VpnTabs(backStack: NavBackStack<Route>, vm: VpnViewModel, initialTab: Int) {-    val pagerState = rememberPagerState(initialPage = initialTab, pageCount = { 3 })-    val tabs = listOf(-        PagerTab("Tunnels", { IconDashboard() }) { ConfigListPage(backStack, vm) },-        PagerTab("Logging", { IconHistory() }) { LoggingPage(vm) },-        PagerTab("Settings", { IconSettings() }) { SettingsPage(backStack) },+fun VpnTabs(backStack: NavBackStack<Route>, vm: VpnViewModel) {+    var loggingTab by rememberSaveable {+        mutableIntStateOf((backStack.last() as? Route.Logging)?.tab ?: 0)+    }+    // A restored or deep-linked Logging route carries its tab; keep the tab+    // target in sync when the stack top is Logging with a different tab.+    val topLoggingTab = (backStack.last() as? Route.Logging)?.tab+    LaunchedEffect(topLoggingTab) {+        if (topLoggingTab != null && topLoggingTab != loggingTab) loggingTab = topLoggingTab+    }+    val tabs = listOf<RouteTab<Route>>(+        RouteTab(Route.Tunnels, "Tunnels") { IconDashboard() },+        RouteTab(Route.Logging(loggingTab), "Logging") { IconHistory() },+        RouteTab(Route.Settings, "Settings") { IconSettings() },     )-    TabbedPagerScaffold(tabs = tabs, pagerState = pagerState, tabStyle = TabStyle.BottomNav)+    TabbedPagerScaffold(backStack = backStack, tabs = tabs) {+        MainNavigation(backStack) {+            entry<Route.Tunnels>(metadata = SiblingPage() + ListPage()) {+                ConfigListPage(backStack, vm)+            }+            entry<Route.Logging>(metadata = SiblingPage()) {+                LoggingPage(+                    vm,+                    initialTab = it.tab,+                    onTabChange = { tab ->+                        loggingTab = tab+                        if (backStack.last() != Route.Logging(tab)) {+                            backStack.setLast(Route.Logging(tab))+                        }+                    },+                )+            }+            entry<Route.Settings>(metadata = SiblingPage()) { SettingsPage(backStack) }+            entry<Route.Detail>(metadata = ListDetailPage() + MorphPage()) {+                ConfigDetailPage(backStack, vm, it.id)+            }+            entry<Route.BypassList>(metadata = ListDetailPage()) { BypassListPage(backStack) }+        }+    } }
vpn/src/screenshotTest/kotlin/com/vayunmathur/vpn/ui/MetadataPreviews.kt
diff --git a/vpn/src/screenshotTest/kotlin/com/vayunmathur/vpn/ui/MetadataPreviews.kt b/vpn/src/screenshotTest/kotlin/com/vayunmathur/vpn/ui/MetadataPreviews.ktindex e53c081bb2..9246113d64 100644--- a/vpn/src/screenshotTest/kotlin/com/vayunmathur/vpn/ui/MetadataPreviews.kt+++ b/vpn/src/screenshotTest/kotlin/com/vayunmathur/vpn/ui/MetadataPreviews.kt@@ -4,7 +4,6 @@ import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.fillMaxHeight import androidx.compose.foundation.layout.fillMaxSize-import androidx.compose.foundation.pager.rememberPagerState import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import androidx.compose.ui.tooling.preview.Preview@@ -13,8 +12,7 @@ import com.vayunmathur.library.ui.DynamicTheme import com.vayunmathur.library.ui.IconDashboard import com.vayunmathur.library.ui.IconHistory import com.vayunmathur.library.ui.IconSettings-import com.vayunmathur.library.ui.PagerTab-import com.vayunmathur.library.ui.TabStyle+import com.vayunmathur.library.ui.RouteTab import com.vayunmathur.library.ui.TabbedPagerScaffold import com.vayunmathur.library.util.NavBackStack import com.vayunmathur.vpn.Route@@ -160,31 +158,33 @@ class MetadataPreviews {     @Composable     private fun ChromePreview(selectedTab: Int, loggingTab: Int = 0) {         DynamicTheme(darkTheme = true) {-            val tabs = listOf(-                PagerTab("Tunnels", { IconDashboard() }) {-                    ConfigListContent(-                        configs = tunnels,-                        connectingId = 1L,-                        activeId = 1L,-                    )-                },-                PagerTab("Logging", { IconHistory() }) {-                    LoggingContent(+            val startRoute: Route = when (selectedTab) {+                1 -> Route.Logging(loggingTab)+                2 -> Route.Settings+                else -> Route.Tunnels+            }+            val backStack = NavBackStack(arrayOf(startRoute))+            val tabs = listOf<RouteTab<Route>>(+                RouteTab(Route.Tunnels, "Tunnels") { IconDashboard() },+                RouteTab(Route.Logging(loggingTab), "Logging") { IconHistory() },+                RouteTab(Route.Settings, "Settings") { IconSettings() },+            )+            TabbedPagerScaffold(backStack = backStack, tabs = tabs) { route ->+                when (route) {+                    is Route.Logging -> LoggingContent(                         topApps = topApps,                         domainsByCount = domainsByCount,                         domainsByBytes = domainsByBytes,                         initialTab = loggingTab,                     )-                },-                PagerTab("Settings", { IconSettings() }) {-                    SettingsPage(backStack = NavBackStack(arrayOf(Route.Main())))-                },-            )-            TabbedPagerScaffold(-                tabs = tabs,-                pagerState = rememberPagerState(initialPage = selectedTab, pageCount = { tabs.size }),-                tabStyle = TabStyle.BottomNav,-            )+                    Route.Settings -> SettingsPage(backStack = backStack)+                    else -> ConfigListContent(+                        configs = tunnels,+                        connectingId = 1L,+                        activeId = 1L,+                    )+                }+            }         }     } }
vpn/src/test/java/com/vayunmathur/vpn/ui/FinalVpnBatchTest.kt
diff --git a/vpn/src/test/java/com/vayunmathur/vpn/ui/FinalVpnBatchTest.kt b/vpn/src/test/java/com/vayunmathur/vpn/ui/FinalVpnBatchTest.ktindex 587d656487..e890b30a3f 100644--- a/vpn/src/test/java/com/vayunmathur/vpn/ui/FinalVpnBatchTest.kt+++ b/vpn/src/test/java/com/vayunmathur/vpn/ui/FinalVpnBatchTest.kt@@ -43,8 +43,12 @@ class FinalVpnBatchTest {      @Test fun routeSerializationRoundTrip() {         val json = Json-        val main = json.decodeFromString(Route.serializer(), json.encodeToString(Route.serializer(), Route.Main(2)))-        assertEquals(Route.Main(2), main)+        val logging = json.decodeFromString(Route.serializer(), json.encodeToString(Route.serializer(), Route.Logging(2)))+        assertEquals(Route.Logging(2), logging)+        val tunnels = json.decodeFromString(Route.serializer(), json.encodeToString(Route.serializer(), Route.Tunnels))+        assertEquals(Route.Tunnels, tunnels)+        val settings = json.decodeFromString(Route.serializer(), json.encodeToString(Route.serializer(), Route.Settings))+        assertEquals(Route.Settings, settings)         val detail = json.decodeFromString(Route.serializer(), json.encodeToString(Route.serializer(), Route.Detail(9)))         assertEquals(Route.Detail(9), detail)         val bypass = json.decodeFromString(Route.serializer(), json.encodeToString(Route.serializer(), Route.BypassList))
vpn/src/test/java/com/vayunmathur/vpn/ui/MainActivityAndBypassSwitchTest.kt
diff --git a/vpn/src/test/java/com/vayunmathur/vpn/ui/MainActivityAndBypassSwitchTest.kt b/vpn/src/test/java/com/vayunmathur/vpn/ui/MainActivityAndBypassSwitchTest.ktindex 629bc92215..a1df0fc32a 100644--- a/vpn/src/test/java/com/vayunmathur/vpn/ui/MainActivityAndBypassSwitchTest.kt+++ b/vpn/src/test/java/com/vayunmathur/vpn/ui/MainActivityAndBypassSwitchTest.kt@@ -50,7 +50,7 @@ class MainActivityAndBypassSwitchTest {         }         rule.setContent {             DynamicTheme {-                val bs = rememberNavBackStack<Route>(Route.Main())+                val bs = rememberNavBackStack<Route>(Route.Tunnels)                 BypassListPage(bs)             }         }
vpn/src/test/java/com/vayunmathur/vpn/ui/NavigationAndPagesTest.kt
diff --git a/vpn/src/test/java/com/vayunmathur/vpn/ui/NavigationAndPagesTest.kt b/vpn/src/test/java/com/vayunmathur/vpn/ui/NavigationAndPagesTest.ktindex 91ac00e9a2..c0e1ffab7a 100644--- a/vpn/src/test/java/com/vayunmathur/vpn/ui/NavigationAndPagesTest.kt+++ b/vpn/src/test/java/com/vayunmathur/vpn/ui/NavigationAndPagesTest.kt@@ -83,7 +83,7 @@ class NavigationAndPagesTest {         rule.setContent {             CompositionLocalProvider(LocalActivityResultRegistryOwner provides fakeOwner(Uri.fromFile(conf))) {                 DynamicTheme {-                    val bs = com.vayunmathur.library.util.rememberNavBackStack<com.vayunmathur.vpn.Route>(com.vayunmathur.vpn.Route.Main())+                    val bs = com.vayunmathur.library.util.rememberNavBackStack<com.vayunmathur.vpn.Route>(com.vayunmathur.vpn.Route.Tunnels)                     com.vayunmathur.vpn.ui.ConfigListPage(bs, vm)                 }             }@@ -105,7 +105,7 @@ class NavigationAndPagesTest {         val vm = VpnViewModel(app, dao, VpnViewModelTest.FakeLogDao())         rule.setContent {             DynamicTheme {-                val bs = com.vayunmathur.library.util.rememberNavBackStack<com.vayunmathur.vpn.Route>(com.vayunmathur.vpn.Route.Main())+                val bs = com.vayunmathur.library.util.rememberNavBackStack<com.vayunmathur.vpn.Route>(com.vayunmathur.vpn.Route.Tunnels)                 ConfigListPage(bs, vm)             }         }
vpn/src/test/java/com/vayunmathur/vpn/ui/PageCallbacksTest.kt
diff --git a/vpn/src/test/java/com/vayunmathur/vpn/ui/PageCallbacksTest.kt b/vpn/src/test/java/com/vayunmathur/vpn/ui/PageCallbacksTest.ktindex 1ea1735d1e..b8c85ab4b9 100644--- a/vpn/src/test/java/com/vayunmathur/vpn/ui/PageCallbacksTest.kt+++ b/vpn/src/test/java/com/vayunmathur/vpn/ui/PageCallbacksTest.kt@@ -40,7 +40,7 @@ class PageCallbacksTest {         val vm = VpnViewModel(app, dao, VpnViewModelTest.FakeLogDao())         rule.setContent {             DynamicTheme {-                val bs = rememberNavBackStack<Route>(Route.Main())+                val bs = rememberNavBackStack<Route>(Route.Tunnels)                 ConfigListPage(bs, vm)             }         }
vpn/src/test/java/com/vayunmathur/vpn/ui/VpnComposeScreensTest.kt
diff --git a/vpn/src/test/java/com/vayunmathur/vpn/ui/VpnComposeScreensTest.kt b/vpn/src/test/java/com/vayunmathur/vpn/ui/VpnComposeScreensTest.ktindex f96848e853..28f1cfb1f1 100644--- a/vpn/src/test/java/com/vayunmathur/vpn/ui/VpnComposeScreensTest.kt+++ b/vpn/src/test/java/com/vayunmathur/vpn/ui/VpnComposeScreensTest.kt@@ -127,7 +127,7 @@ class VpnComposeScreensTest {         val vm = vm(dao)         rule.setContent {             DynamicTheme {-                val bs = rememberNavBackStack<Route>(Route.Main())+                val bs = rememberNavBackStack<Route>(Route.Tunnels)                 ConfigDetailPage(bs, vm, 7)             }         }@@ -145,7 +145,7 @@ class VpnComposeScreensTest {     @Test fun settingsPageRows() {         rule.setContent {             DynamicTheme {-                val bs = rememberNavBackStack<Route>(Route.Main())+                val bs = rememberNavBackStack<Route>(Route.Tunnels)                 SettingsPage(bs)             }         }@@ -156,7 +156,7 @@ class VpnComposeScreensTest {     @Test fun bypassListPageLoadsApps() {         rule.setContent {             DynamicTheme {-                val bs = rememberNavBackStack<Route>(Route.Main())+                val bs = rememberNavBackStack<Route>(Route.Tunnels)                 BypassListPage(bs)             }         }@@ -172,8 +172,8 @@ class VpnComposeScreensTest {         val vm = vm()         rule.setContent {             DynamicTheme {-                val bs = rememberNavBackStack<Route>(Route.Main())-                VpnTabs(bs, vm, 0)+                val bs = rememberNavBackStack<Route>(Route.Tunnels)+                VpnTabs(bs, vm)             }         }         rule.waitForIdle()@@ -181,7 +181,9 @@ class VpnComposeScreensTest {     }      @Test fun routeKeys() {-        assertEquals(Route.Main(2), Route.Main(initialTab = 2))+        assertEquals(Route.Logging(2), Route.Logging(tab = 2))+        assertEquals(Route.Tunnels, Route.Tunnels)+        assertEquals(Route.Settings, Route.Settings)         assertEquals(Route.Detail(5), Route.Detail(id = 5))         assertEquals(Route.BypassList, Route.BypassList)     }