games-hub: tabs are sibling routes (#nav)
- Commit
69334d3b1c471ff27bee2e03669d8eeb0f9a901d- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-16 20:00 UTC (19 days ago)
- Parents
7c2e05a
Replace MainRoute.Main+pager with sibling MainRoute.Dashboard/Games/Achievements/Profile/Yuu (SiblingPage(); Dashboard/Games also ListPage()); route-backed TabbedPagerScaffold hosts MainNavigation. Cross-tab animateScrollToPage jumps become backStack.reset(Games/Profile). Start stack is Dashboard; Yuu stays DEV_BUILD-only; detail/editor routes unchanged. Verified: ./gradlew :games:hub:testDevUnitTest :games:hub:compileDevKotlin green.
Diff
games/hub/src/main/java/com/vayunmathur/games/hub/MainActivity.kt
diff --git a/games/hub/src/main/java/com/vayunmathur/games/hub/MainActivity.kt b/games/hub/src/main/java/com/vayunmathur/games/hub/MainActivity.ktindex 95ac4bf74d..8a62998f4a 100644--- a/games/hub/src/main/java/com/vayunmathur/games/hub/MainActivity.kt+++ b/games/hub/src/main/java/com/vayunmathur/games/hub/MainActivity.kt@@ -1,10 +1,8 @@ package com.vayunmathur.games.hub import androidx.activity.viewModels-import androidx.compose.foundation.pager.rememberPagerState import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue-import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.ui.res.stringResource import androidx.lifecycle.compose.collectAsStateWithLifecycle import com.vayunmathur.games.hub.ui.components.AvatarIcon@@ -13,11 +11,9 @@ import com.vayunmathur.library.ui.IconDashboard import com.vayunmathur.library.ui.IconEmoji import com.vayunmathur.library.ui.IconEmojiEvents import com.vayunmathur.library.ui.IconSportsEsports-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.games.hub.data.GamesHubRepository-import com.vayunmathur.games.hub.BuildConfig import com.vayunmathur.games.hub.ui.screens.AchievementsScreen import com.vayunmathur.games.hub.ui.screens.ActivityFeedScreen import com.vayunmathur.games.hub.ui.screens.DashboardPage@@ -35,10 +31,9 @@ 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.NavKey+import com.vayunmathur.library.util.SiblingPage import com.vayunmathur.library.util.rememberNavBackStack-import kotlinx.coroutines.launch import kotlinx.serialization.Serializable /** Main Activity: a ma activity implementation for main activity. */@@ -57,8 +52,11 @@ class MainActivity : MaActivity() { /** Contract for main route. */ @Serializable sealed interface MainRoute : NavKey {- /** Singleton holding settings state and operations. */- @Serializable data object Main : MainRoute+ @Serializable data object Dashboard : MainRoute+ @Serializable data object Games : MainRoute+ @Serializable data object Achievements : MainRoute+ @Serializable data object Profile : MainRoute+ @Serializable data object Yuu : MainRoute @Serializable data class GameDetail(val gameId: String) : MainRoute @Serializable data class AchievementsForGame(val gameId: String) : MainRoute @Serializable data object Activity : MainRoute@@ -73,106 +71,104 @@ sealed interface MainRoute : NavKey { fun HubNavigation( viewModel: GameHubViewModel ) {- val backStack = rememberNavBackStack<MainRoute>(MainRoute.Main)-- MainNavigation(backStack) {- entry<MainRoute.Main>(metadata = ListPage()) {- HubTabs(viewModel = viewModel, backStack = backStack)- }- entry<MainRoute.GameDetail>(metadata = ListDetailPage() + MorphPage()) { route ->- GameDetailScreen(- gameId = route.gameId,- viewModel = viewModel,- backStack = backStack- )- }- entry<MainRoute.AchievementsForGame> { route ->- AchievementsScreen(viewModel = viewModel, initialGameFilter = route.gameId)- }- entry<MainRoute.Activity> {- ActivityFeedScreen(- viewModel = viewModel,- backStack = backStack,- onGameClick = { gid -> backStack.add(MainRoute.GameDetail(gid)) }- )- }- entry<MainRoute.Settings> {- SettingsScreen(- viewModel = viewModel,- backStack = backStack- )- }- entry<MainRoute.PlayOnTv> {- PlayOnTvScreen(backStack = backStack)- }- entry<MainRoute.YuuEditorNew> {- YuuEditorPage(- viewModel = viewModel,- editingId = null,- onDismiss = { backStack.pop() },- )- }- entry<MainRoute.YuuEditorEdit> { route ->- YuuEditorPage(- viewModel = viewModel,- editingId = route.yuuId,- onDismiss = { backStack.pop() },- )- }- }-}--@Composable-private fun HubTabs(- viewModel: GameHubViewModel,- backStack: NavBackStack<MainRoute>,-) {- val pagerState = rememberPagerState(pageCount = { if (BuildConfig.DEV_BUILD) 5 else 4 })- val scope = rememberCoroutineScope()+ // Dashboard is the start tab (the pager's page 0), so a fresh or+ // deep-linked launch with no tab named lands where it always did.+ val backStack = rememberNavBackStack<MainRoute>(MainRoute.Dashboard) val profile by viewModel.profileFlow.collectAsStateWithLifecycle()- // Home and Games are adjacent pages showing the same games (recentlyPlayed is a subset of the- // full list), and the pager composes both while a swipe is in flight - so "hub-game-<id>" would- // have two live origins. settledPage rather than currentPage: currentPage flips at the halfway- // point of a scroll, which would leave both pages claiming the key mid-swipe.- val tabs = listOf(- PagerTab(stringResource(R.string.tab_home), { IconDashboard() }) {- DashboardPage(- viewModel = viewModel,- onGameClick = { gameId -> backStack.add(MainRoute.GameDetail(gameId)) },- onProfileClick = { scope.launch { pagerState.animateScrollToPage(3) } },- onActivityClick = { backStack.add(MainRoute.Activity) },- onGamesClick = { scope.launch { pagerState.animateScrollToPage(1) } },- onControllerClick = { backStack.add(MainRoute.PlayOnTv) },- ownsGameMorphKeys = pagerState.settledPage == 0- )- },- PagerTab(stringResource(R.string.tab_games), { IconSportsEsports() }) {- GamesListPage(- viewModel = viewModel,- onGameClick = { gameId -> backStack.add(MainRoute.GameDetail(gameId)) },- ownsGameMorphKeys = pagerState.settledPage == 1- )- },- PagerTab(stringResource(R.string.tab_achievements), { IconEmojiEvents() }) {- AchievementsScreen(viewModel = viewModel)- },- PagerTab(- stringResource(R.string.tab_profile),- { AvatarIcon(symbol = profileTabAvatarSymbol(profile?.avatarSymbol)) },- ) {- ProfilePage(viewModel = viewModel)- },++ // Home and Games show the same games (recentlyPlayed is a subset of the+ // full list), so "hub-game-<id>" would have two live origins if both+ // tabs were composed at once. The pager avoided that with settledPage;+ // sibling routes are never composed together outside a tab-switch+ // crossfade, and only the settled tab is on top, so each tab entry+ // owns its morph keys (the default) with no pager page to compare.+ val tabs = buildList<RouteTab<MainRoute>> {+ add(RouteTab(MainRoute.Dashboard, stringResource(R.string.tab_home)) { IconDashboard() })+ add(RouteTab(MainRoute.Games, stringResource(R.string.tab_games)) { IconSportsEsports() })+ add(RouteTab(MainRoute.Achievements, stringResource(R.string.tab_achievements)) { IconEmojiEvents() })+ add(RouteTab(MainRoute.Profile, stringResource(R.string.tab_profile)) {+ AvatarIcon(symbol = profileTabAvatarSymbol(profile?.avatarSymbol))+ }) // Dev-only: the Yuu avatar plaza is still experimental. R8 strips the // block from release builds via the repo-wide BuildConfig.DEV_BUILD flag. if (BuildConfig.DEV_BUILD) {- PagerTab(stringResource(R.string.tab_yuu), { IconEmoji() }) {- YuuPage(+ add(RouteTab(MainRoute.Yuu, stringResource(R.string.tab_yuu)) { IconEmoji() })+ }+ }++ TabbedPagerScaffold(backStack = backStack, tabs = tabs) {+ MainNavigation(backStack) {+ entry<MainRoute.Dashboard>(metadata = ListPage() + SiblingPage()) {+ DashboardPage( viewModel = viewModel,- onCreateYuu = { backStack.add(MainRoute.YuuEditorNew) },- onEditYuu = { id -> backStack.add(MainRoute.YuuEditorEdit(id)) },+ onGameClick = { gameId -> backStack.add(MainRoute.GameDetail(gameId)) },+ onProfileClick = { backStack.reset(MainRoute.Profile) },+ onActivityClick = { backStack.add(MainRoute.Activity) },+ onGamesClick = { backStack.reset(MainRoute.Games) },+ onControllerClick = { backStack.add(MainRoute.PlayOnTv) }, ) }- } else null,- ).filterNotNull()- TabbedPagerScaffold(tabs = tabs, pagerState = pagerState, tabStyle = TabStyle.BottomNav)+ entry<MainRoute.Games>(metadata = ListPage() + SiblingPage()) {+ GamesListPage(+ viewModel = viewModel,+ onGameClick = { gameId -> backStack.add(MainRoute.GameDetail(gameId)) },+ )+ }+ entry<MainRoute.Achievements>(metadata = SiblingPage()) {+ AchievementsScreen(viewModel = viewModel)+ }+ entry<MainRoute.Profile>(metadata = SiblingPage()) {+ ProfilePage(viewModel = viewModel)+ }+ if (BuildConfig.DEV_BUILD) {+ entry<MainRoute.Yuu>(metadata = SiblingPage()) {+ YuuPage(+ viewModel = viewModel,+ onCreateYuu = { backStack.add(MainRoute.YuuEditorNew) },+ onEditYuu = { id -> backStack.add(MainRoute.YuuEditorEdit(id)) },+ )+ }+ }+ entry<MainRoute.GameDetail>(metadata = ListDetailPage() + MorphPage()) { route ->+ GameDetailScreen(+ gameId = route.gameId,+ viewModel = viewModel,+ backStack = backStack+ )+ }+ entry<MainRoute.AchievementsForGame> { route ->+ AchievementsScreen(viewModel = viewModel, initialGameFilter = route.gameId)+ }+ entry<MainRoute.Activity> {+ ActivityFeedScreen(+ viewModel = viewModel,+ backStack = backStack,+ onGameClick = { gid -> backStack.add(MainRoute.GameDetail(gid)) }+ )+ }+ entry<MainRoute.Settings> {+ SettingsScreen(+ viewModel = viewModel,+ backStack = backStack+ )+ }+ entry<MainRoute.PlayOnTv> {+ PlayOnTvScreen(backStack = backStack)+ }+ entry<MainRoute.YuuEditorNew> {+ YuuEditorPage(+ viewModel = viewModel,+ editingId = null,+ onDismiss = { backStack.pop() },+ )+ }+ entry<MainRoute.YuuEditorEdit> { route ->+ YuuEditorPage(+ viewModel = viewModel,+ editingId = route.yuuId,+ onDismiss = { backStack.pop() },+ )+ }+ }+ } }
games/hub/src/test/java/com/vayunmathur/games/hub/ui/HubNavigationCoverageTest.kt
diff --git a/games/hub/src/test/java/com/vayunmathur/games/hub/ui/HubNavigationCoverageTest.kt b/games/hub/src/test/java/com/vayunmathur/games/hub/ui/HubNavigationCoverageTest.ktindex 69a006fe37..260260f2c0 100644--- a/games/hub/src/test/java/com/vayunmathur/games/hub/ui/HubNavigationCoverageTest.kt+++ b/games/hub/src/test/java/com/vayunmathur/games/hub/ui/HubNavigationCoverageTest.kt@@ -55,7 +55,7 @@ class HubNavigationCoverageTest { } @Test fun playOnTvScreenShowsDiscovery() {- val stack = NavBackStack<MainRoute>(arrayOf(MainRoute.Main, MainRoute.PlayOnTv))+ val stack = NavBackStack<MainRoute>(arrayOf(MainRoute.Dashboard, MainRoute.PlayOnTv)) rule.setContent { PlayOnTvScreen(stack) } rule.waitForIdle() // Discovery state renders (no hosts on the test network): reaching@@ -66,7 +66,7 @@ class HubNavigationCoverageTest { @Test fun mainRouteValuesAreDistinct() { // Singleton routes must stay singletons: navigation equality depends // on it, and the detail routes carry their game id verbatim.- assertEquals(MainRoute.Main, MainRoute.Main)+ assertEquals(MainRoute.Dashboard, MainRoute.Dashboard) assertEquals(MainRoute.PlayOnTv, MainRoute.PlayOnTv) assertEquals("chess", MainRoute.GameDetail("chess").gameId) assertEquals("chess", MainRoute.AchievementsForGame("chess").gameId)
games/hub/src/test/java/com/vayunmathur/games/hub/ui/HubScreensPass3Test.kt
diff --git a/games/hub/src/test/java/com/vayunmathur/games/hub/ui/HubScreensPass3Test.kt b/games/hub/src/test/java/com/vayunmathur/games/hub/ui/HubScreensPass3Test.ktindex c22d5ad2a4..9bbd33099a 100644--- a/games/hub/src/test/java/com/vayunmathur/games/hub/ui/HubScreensPass3Test.kt+++ b/games/hub/src/test/java/com/vayunmathur/games/hub/ui/HubScreensPass3Test.kt@@ -142,7 +142,7 @@ class HubScreensPass3Test { @Test fun gameDetailShowsStatsAchievementsAndSessions() { seedLibrary()- val stack = NavBackStack<MainRoute>(arrayOf(MainRoute.Main))+ val stack = NavBackStack<MainRoute>(arrayOf(MainRoute.Dashboard)) rule.setContent { DynamicTheme { GameDetailScreen("chess", vm, stack) } } rule.onAllNodesWithText("Chess")[0].assertIsDisplayed() rule.onNodeWithText("Board game").assertIsDisplayed()@@ -155,7 +155,7 @@ class HubScreensPass3Test { @Test fun gameDetailMissingGameShowsNotFound() {- val stack = NavBackStack<MainRoute>(arrayOf(MainRoute.Main))+ val stack = NavBackStack<MainRoute>(arrayOf(MainRoute.Dashboard)) rule.setContent { DynamicTheme { GameDetailScreen("no-such-game", vm, stack) } } rule.onNodeWithText("Game not found").assertIsDisplayed() }@@ -171,7 +171,7 @@ class HubScreensPass3Test { @Test fun settingsClearCacheConfirmsAndClears() { seedLibrary()- val stack = NavBackStack<MainRoute>(arrayOf(MainRoute.Main, MainRoute.Settings))+ val stack = NavBackStack<MainRoute>(arrayOf(MainRoute.Dashboard, MainRoute.Settings)) rule.setContent { DynamicTheme { SettingsScreen(vm, stack) } } rule.onNodeWithText("Settings").assertIsDisplayed() rule.onNodeWithText("Clear cache").performClick()