Modern Apps git

← Modern-Apps.git

games-wordmaker: competitive lobby is a route (#nav)

Commit
60cecbe81f1578ec7ea9e98561c31f0ec3d690bc
Author
Vayun Mathur <[email protected]>
When
2026-11-16 21:32 UTC (19 days ago)
Parents
ad279ac
Replace the loader's competitive lobby-versus-board mode swap
with a destination: Route.CompetitiveLobby, shown while the mode
is competitive and no level is active, swapped with Route.Game by
Navigation's route sync.

The ViewModel keeps the mode and the competitive session
(competitiveActive, result, deadline); syncWordMakerRoute projects
them onto the stack as a reset rather than a push — lobby and board
are alternatives for the same slot, as the loader arm was, so Back
never steps from a live board into the lobby or vice versa.
Destinations pushed over either (game center, settings) are left
alone and re-synced when popped back to. WordMakerGameLoader keeps
only its loading/error/board arms inside the Game destination.

Tests: the new WordMakerRouteSyncTest pins the swap for
competitive/casual/daily and the pushed-destination case;
GameLoaderPageDeepTest's lobby-to-game flow now composes the real
Navigation so it exercises the route swap end to end. (One full-suite
run hit a pre-existing Dispatchers.Main concurrency flake in
WordMakerViewModelTest; the module reran green.)

Verified on devvm21958 (no push):
- ./gradlew :games:wordmaker:compileDevKotlin
  :games:wordmaker:testDevUnitTest — green

Diff

games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/Navigation.kt
diff --git a/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/Navigation.kt b/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/Navigation.ktindex ce7a4016fa..7647dc7adb 100644--- a/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/Navigation.kt+++ b/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/Navigation.kt@@ -1,11 +1,16 @@ package com.vayunmathur.games.wordmaker  import androidx.compose.runtime.Composable+import androidx.compose.runtime.LaunchedEffect+import androidx.compose.runtime.collectAsState+import androidx.compose.runtime.getValue import androidx.compose.runtime.produceState import androidx.compose.ui.platform.LocalContext+import com.vayunmathur.games.wordmaker.data.GameMode import com.vayunmathur.games.wordmaker.data.LevelDataStore import com.vayunmathur.games.wordmaker.platform.WordMakerAchievementsManager import com.vayunmathur.games.wordmaker.platform.WordMakerViewModel+import com.vayunmathur.games.wordmaker.ui.CompetitiveLobbyPage import com.vayunmathur.games.wordmaker.ui.SettingsPage import com.vayunmathur.games.wordmaker.ui.WordMakerGameLoader import com.vayunmathur.library.ui.GameCenterScreen@@ -13,6 +18,7 @@ import com.vayunmathur.library.util.AchievementsManager import com.vayunmathur.library.util.FullscreenPage import com.vayunmathur.library.util.GameHubSessionHook import com.vayunmathur.library.util.MainNavigation+import com.vayunmathur.library.util.NavBackStack import com.vayunmathur.library.util.openSettingsIfRequested import com.vayunmathur.library.util.rememberNavBackStack import kotlinx.coroutines.Dispatchers@@ -22,11 +28,19 @@ import kotlinx.coroutines.withContext fun Navigation(viewModel: WordMakerViewModel) {     val backStack = rememberNavBackStack<Route>(Route.Game)     backStack.openSettingsIfRequested(Route.Settings)+    WordMakerRouteEffect(backStack, viewModel)     GameHubSessionHook("wordmaker", "Wordmaker")     MainNavigation(backStack) {         entry<Route.Game>(metadata = FullscreenPage()) {             WordMakerGameLoader(backStack, viewModel)         }+        entry<Route.CompetitiveLobby>(metadata = FullscreenPage()) {+            CompetitiveLobbyPage(+                viewModel = viewModel,+                onOpenGameCenter = { backStack.add(Route.GameCenter) },+                onOpenSettings = { backStack.add(Route.Settings) },+            )+        }         entry<Route.GameCenter> {             val achievementsManager = rememberAchievementsManager(viewModel.levelDataStore)             if (achievementsManager != null) {@@ -40,6 +54,46 @@ fun Navigation(viewModel: WordMakerViewModel) {     } } +/**+ * Swaps the board and the competitive lobby as destinations.+ *+ * The mode and the competitive session stay in the ViewModel; this+ * effect projects them onto the stack. Competitive with no level+ * active is [Route.CompetitiveLobby], everything else is [Route.Game].+ * The swap is a reset, not a push: the two are alternatives for the same+ * slot (as the loader's mode swap was), so Back never steps from a live+ * board into the lobby or vice versa. Destinations pushed over either+ * (game center, settings) are left alone and re-synced on pop.+ */+@Composable+private fun WordMakerRouteEffect(backStack: NavBackStack<Route>, viewModel: WordMakerViewModel) {+    val gameMode by viewModel.gameMode.collectAsState()+    val competitiveActive by viewModel.competitiveActive.collectAsState()+    val top = backStack.backStack.lastOrNull()+    LaunchedEffect(gameMode, competitiveActive, top) {+        syncWordMakerRoute(backStack, gameMode, competitiveActive)+    }+}++/**+ * The mode-to-stack projection behind [WordMakerRouteEffect], kept free+ * of composition so unit tests can drive it with a plain [NavBackStack].+ */+internal fun syncWordMakerRoute(+    backStack: NavBackStack<Route>,+    gameMode: GameMode,+    competitiveActive: Boolean,+) {+    val target: Route =+        if (gameMode == GameMode.COMPETITIVE && !competitiveActive) Route.CompetitiveLobby+        else Route.Game+    when (backStack.backStack.lastOrNull()) {+        target -> Unit+        Route.Game, Route.CompetitiveLobby -> backStack.reset(target)+        else -> Unit+    }+}+ @Composable fun rememberAchievementsManager(levelDataStore: LevelDataStore): AchievementsManager? {     val context = LocalContext.current
games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/Route.kt
diff --git a/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/Route.kt b/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/Route.ktindex 8ebd7c24ed..9ba0c1b391 100644--- a/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/Route.kt+++ b/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/Route.kt@@ -7,6 +7,13 @@ import kotlinx.serialization.Serializable sealed interface Route : NavKey {     @Serializable     data object Game : Route+    /**+     * The between-levels competitive lobby. Shown instead of [Game] while+     * the mode is competitive and no level is active; starting a level+     * swaps the stack back to [Game] (see Navigation's route sync).+     */+    @Serializable+    data object CompetitiveLobby : Route     @Serializable     data object GameCenter : Route     @Serializable
games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/WordMakerGameLoader.kt
diff --git a/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/WordMakerGameLoader.kt b/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/WordMakerGameLoader.ktindex 402dd2c5b6..76bb162427 100644--- a/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/WordMakerGameLoader.kt+++ b/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/WordMakerGameLoader.kt@@ -23,7 +23,6 @@ fun WordMakerGameLoader(backStack: NavBackStack<Route>, viewModel: WordMakerView     val crosswordData by viewModel.crosswordData.collectAsState()     val error by viewModel.error.collectAsState()     val gameMode by viewModel.gameMode.collectAsState()-    val competitiveActive by viewModel.competitiveActive.collectAsState()      val achievementsManager = rememberAchievementsManager(viewModel.levelDataStore)     if (achievementsManager == null) {@@ -40,14 +39,9 @@ fun WordMakerGameLoader(backStack: NavBackStack<Route>, viewModel: WordMakerView      Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {         when {-            gameMode == GameMode.COMPETITIVE && !competitiveActive -> {-                CompetitiveLobbyPage(-                    viewModel = viewModel,-                    onOpenGameCenter = { backStack.add(Route.GameCenter) },-                    onOpenSettings = { backStack.add(Route.Settings) }-                )-            }-+            // The competitive lobby is Route.CompetitiveLobby, swapped with+            // this board destination by Navigation's route sync; the arms+            // below are this destination's own loading/error/content.             // Inside the normal scaffold (mode chooser, settings) with a Retry button, rather             // than a bare line of text the player could not get away from.             error != null -> {
games/wordmaker/src/test/java/com/vayunmathur/games/wordmaker/WordMakerRouteSyncTest.kt
diff --git a/games/wordmaker/src/test/java/com/vayunmathur/games/wordmaker/WordMakerRouteSyncTest.kt b/games/wordmaker/src/test/java/com/vayunmathur/games/wordmaker/WordMakerRouteSyncTest.ktnew file mode 100644index 0000000000..a583107625--- /dev/null+++ b/games/wordmaker/src/test/java/com/vayunmathur/games/wordmaker/WordMakerRouteSyncTest.kt@@ -0,0 +1,51 @@+package com.vayunmathur.games.wordmaker++import com.vayunmathur.games.wordmaker.data.GameMode+import com.vayunmathur.library.util.NavBackStack+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import kotlin.test.Test+import kotlin.test.assertEquals++/**+ * The competitive lobby is a destination, not a loader arm:+ * [syncWordMakerRoute] swaps board and lobby with a reset (they are+ * alternatives for the same slot), and leaves destinations pushed over+ * either untouched.+ */+@RunWith(RobolectricTestRunner::class)+class WordMakerRouteSyncTest {++    @Test+    fun `competitive without an active level shows the lobby`() {+        val stack = NavBackStack(arrayOf<Route>(Route.Game))+        syncWordMakerRoute(stack, GameMode.COMPETITIVE, competitiveActive = false)+        assertEquals(listOf<Route>(Route.CompetitiveLobby), stack.backStack)+    }++    @Test+    fun `starting a competitive level swaps back to the board`() {+        val stack = NavBackStack(arrayOf<Route>(Route.CompetitiveLobby))+        syncWordMakerRoute(stack, GameMode.COMPETITIVE, competitiveActive = true)+        assertEquals(listOf<Route>(Route.Game), stack.backStack)+    }++    @Test+    fun `casual and daily always show the board`() {+        val stack = NavBackStack(arrayOf<Route>(Route.CompetitiveLobby))+        syncWordMakerRoute(stack, GameMode.CASUAL, competitiveActive = false)+        assertEquals(listOf<Route>(Route.Game), stack.backStack)+        syncWordMakerRoute(stack, GameMode.DAILY, competitiveActive = false)+        assertEquals(listOf<Route>(Route.Game), stack.backStack)+    }++    @Test+    fun `destinations pushed over the board or lobby are left alone`() {+        val stack = NavBackStack(arrayOf<Route>(Route.CompetitiveLobby, Route.Settings))+        syncWordMakerRoute(stack, GameMode.CASUAL, competitiveActive = false)+        assertEquals(listOf(Route.CompetitiveLobby, Route.Settings), stack.backStack)+        stack.pop()+        syncWordMakerRoute(stack, GameMode.CASUAL, competitiveActive = false)+        assertEquals(listOf<Route>(Route.Game), stack.backStack)+    }+}
games/wordmaker/src/test/java/com/vayunmathur/games/wordmaker/ui/GameLoaderPageDeepTest.kt
diff --git a/games/wordmaker/src/test/java/com/vayunmathur/games/wordmaker/ui/GameLoaderPageDeepTest.kt b/games/wordmaker/src/test/java/com/vayunmathur/games/wordmaker/ui/GameLoaderPageDeepTest.ktindex 2fc082d1eb..bc167a8c95 100644--- a/games/wordmaker/src/test/java/com/vayunmathur/games/wordmaker/ui/GameLoaderPageDeepTest.kt+++ b/games/wordmaker/src/test/java/com/vayunmathur/games/wordmaker/ui/GameLoaderPageDeepTest.kt@@ -131,8 +131,7 @@ class GameLoaderPageDeepTest {         pollUntil { vm.gameMode.value == GameMode.COMPETITIVE }         rule.setContent {             DynamicTheme {-                val backStack = rememberNavBackStack<Route>(Route.Game)-                WordMakerGameLoader(backStack, vm)+                com.vayunmathur.games.wordmaker.Navigation(vm)             }         }         rule.onNodeWithText("Start").assertIsDisplayed()