From 6213d5ac15c696a1e005313ee10ac03a840160a3 Mon Sep 17 00:00:00 2001 From: Thomas Andres Gomez Date: Tue, 1 Apr 2025 17:29:39 +0200 Subject: [PATCH] Add a navigation layer into the GameMaster screens. --- .../kotlin/com/pixelized/desktop/lwa/App.kt | 3 +- .../repository/alteration/AlterationStore.kt | 4 +- .../lwa/repository/campaign/CampaignStore.kt | 2 +- .../characterSheet/CharacterSheetStore.kt | 4 +- .../gamemaster/GameMasterDestination.kt | 30 +++++++++++++ .../ui/screen/gamemaster/GameMasterNavHost.kt | 42 +++++++++++++++++++ .../ui/screen/gamemaster/GameMasterScreen.kt | 41 +++++++----------- .../alteration/edit/GMAlterationEditPage.kt | 1 + 8 files changed, 93 insertions(+), 34 deletions(-) create mode 100644 composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/ui/navigation/screen/destination/gamemaster/GameMasterDestination.kt create mode 100644 composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/ui/screen/gamemaster/GameMasterNavHost.kt diff --git a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/App.kt b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/App.kt index 93dd0e0..acd03fc 100644 --- a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/App.kt +++ b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/App.kt @@ -52,6 +52,7 @@ import com.pixelized.desktop.lwa.ui.navigation.window.destination.RollHistoryWin import com.pixelized.desktop.lwa.ui.navigation.window.rememberMaxWindowHeight import com.pixelized.desktop.lwa.ui.overlay.roll.RollHostState import com.pixelized.desktop.lwa.ui.screen.characterSheet.CharacterSheetMainNavHost +import com.pixelized.desktop.lwa.ui.screen.gamemaster.GameMasterNavHost import com.pixelized.desktop.lwa.ui.screen.gamemaster.GameMasterScreen import com.pixelized.desktop.lwa.ui.screen.rollhistory.RollHistoryPage import com.pixelized.desktop.lwa.ui.theme.LwaTheme @@ -184,7 +185,7 @@ private fun WindowsHandler( is RollHistoryWindow -> RollHistoryPage() is GameMasterWindow -> LwaScaffold { - GameMasterScreen() + GameMasterNavHost() } } } diff --git a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/alteration/AlterationStore.kt b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/alteration/AlterationStore.kt index 74ed229..708f579 100644 --- a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/alteration/AlterationStore.kt +++ b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/alteration/AlterationStore.kt @@ -15,7 +15,7 @@ import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch class AlterationStore( - private val networkRepository: NetworkRepository, + private val network: NetworkRepository, private val factory: AlterationJsonFactory, private val client: LwaClient, ) { @@ -26,7 +26,7 @@ class AlterationStore( val scope = CoroutineScope(Dispatchers.IO + Job()) // data update through WebSocket. scope.launch { - networkRepository.data.collect(::handleMessage) + network.data.collect(::handleMessage) } } diff --git a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/campaign/CampaignStore.kt b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/campaign/CampaignStore.kt index 74adc1c..402211f 100644 --- a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/campaign/CampaignStore.kt +++ b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/campaign/CampaignStore.kt @@ -16,9 +16,9 @@ import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch class CampaignStore( - private val client: LwaClient, private val network: NetworkRepository, private val factory: CampaignJsonFactory, + private val client: LwaClient, ) { private val campaignFlow = MutableStateFlow(value = Campaign.empty()) diff --git a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/characterSheet/CharacterSheetStore.kt b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/characterSheet/CharacterSheetStore.kt index 29c4635..163c44d 100644 --- a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/characterSheet/CharacterSheetStore.kt +++ b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/characterSheet/CharacterSheetStore.kt @@ -19,9 +19,9 @@ import kotlinx.coroutines.launch class CharacterSheetStore( private val alterationStore: AlterationStore, - private val client: LwaClient, private val network: NetworkRepository, private val factory: CharacterSheetJsonFactory, + private val client: LwaClient, ) { private val _previewFlow = MutableStateFlow>(value = emptyList()) val previewFlow: StateFlow> get() = _previewFlow @@ -88,7 +88,6 @@ class CharacterSheetStore( } } - // TODO check crash @Throws suspend fun updateCharacterSheet( sheet: CharacterSheet, @@ -100,7 +99,6 @@ class CharacterSheetStore( } } - // TODO check crash @Throws suspend fun deleteCharacterSheet( characterSheetId: String, diff --git a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/ui/navigation/screen/destination/gamemaster/GameMasterDestination.kt b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/ui/navigation/screen/destination/gamemaster/GameMasterDestination.kt new file mode 100644 index 0000000..43968cd --- /dev/null +++ b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/ui/navigation/screen/destination/gamemaster/GameMasterDestination.kt @@ -0,0 +1,30 @@ +package com.pixelized.desktop.lwa.ui.navigation.screen.destination.gamemaster + +import androidx.compose.runtime.Stable +import androidx.navigation.NavGraphBuilder +import androidx.navigation.NavHostController +import androidx.navigation.compose.composable +import com.pixelized.desktop.lwa.ui.screen.gamemaster.GameMasterScreen + +@Stable +object GameMasterDestination { + private const val ROUTE = "GameMasterMain" + + fun baseRoute() = ROUTE + + @Stable + fun navigationRoute() = ROUTE +} + +fun NavGraphBuilder.composableGameMasterMainPage() { + composable( + route = GameMasterDestination.baseRoute(), + ) { + GameMasterScreen() + } +} + +fun NavHostController.navigateToGameMasterMainPage() { + val route = GameMasterDestination.navigationRoute() + navigate(route = route) +} \ No newline at end of file diff --git a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/ui/screen/gamemaster/GameMasterNavHost.kt b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/ui/screen/gamemaster/GameMasterNavHost.kt new file mode 100644 index 0000000..8b8b056 --- /dev/null +++ b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/ui/screen/gamemaster/GameMasterNavHost.kt @@ -0,0 +1,42 @@ +package com.pixelized.desktop.lwa.ui.screen.gamemaster + +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.material.Surface +import androidx.compose.runtime.Composable +import androidx.compose.runtime.CompositionLocalProvider +import androidx.compose.runtime.compositionLocalOf +import androidx.compose.ui.Modifier +import androidx.navigation.NavHostController +import androidx.navigation.compose.NavHost +import androidx.navigation.compose.rememberNavController +import com.pixelized.desktop.lwa.ui.navigation.screen.LocalScreenController +import com.pixelized.desktop.lwa.ui.navigation.screen.destination.gamemaster.GameMasterDestination +import com.pixelized.desktop.lwa.ui.navigation.screen.destination.gamemaster.composableGameMasterAlterationEditPage +import com.pixelized.desktop.lwa.ui.navigation.screen.destination.gamemaster.composableGameMasterMainPage + +val LocalGMScreenController = compositionLocalOf { + error("GameMaster NavHost controller is not yet ready") +} + +@Composable +fun GameMasterNavHost() { + val controller = rememberNavController() + + CompositionLocalProvider( + LocalScreenController provides controller, + LocalGMScreenController provides rememberNavController(), + ) { + Surface( + modifier = Modifier.fillMaxSize() + ) { + NavHost( + modifier = Modifier.fillMaxSize(), + navController = controller, + startDestination = GameMasterDestination.navigationRoute(), + ) { + composableGameMasterMainPage() + composableGameMasterAlterationEditPage() + } + } + } +} \ No newline at end of file diff --git a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/ui/screen/gamemaster/GameMasterScreen.kt b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/ui/screen/gamemaster/GameMasterScreen.kt index 8b7b6ea..abd0c70 100644 --- a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/ui/screen/gamemaster/GameMasterScreen.kt +++ b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/ui/screen/gamemaster/GameMasterScreen.kt @@ -17,7 +17,6 @@ import androidx.compose.material.Switch import androidx.compose.material.Text import androidx.compose.material.TopAppBar import androidx.compose.runtime.Composable -import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.runtime.State import androidx.compose.runtime.collectAsState import androidx.compose.ui.Alignment @@ -28,7 +27,6 @@ import androidx.compose.ui.unit.dp import androidx.navigation.NavHostController import androidx.navigation.compose.NavHost import androidx.navigation.compose.rememberNavController -import com.pixelized.desktop.lwa.ui.navigation.screen.LocalScreenController import com.pixelized.desktop.lwa.ui.navigation.screen.destination.gamemaster.GMActionDestination import com.pixelized.desktop.lwa.ui.navigation.screen.destination.gamemaster.composableGameMasterActionPage import com.pixelized.desktop.lwa.ui.navigation.screen.destination.gamemaster.composableGameMasterAlterationEditPage @@ -52,35 +50,24 @@ import org.koin.compose.viewmodel.koinViewModel @Composable fun GameMasterScreen( gameMasterViewModel: GameMasterViewModel = koinViewModel(), - + controller: NavHostController = LocalGMScreenController.current, ) { - val screen = rememberNavController() val gameMaster = gameMasterViewModel.isGameMaster.collectAsState() - CompositionLocalProvider( - LocalScreenController provides screen, - ) { - Surface( - modifier = Modifier.fillMaxSize() - ) { - Box { - GameMasterContent( - modifier = Modifier.fillMaxSize(), - controller = screen, - gameMaster = gameMaster, - onGameMaster = gameMasterViewModel::onGameMaster, - onTab = { - when (it) { - GMTabUio.Actions -> screen.navigateToGameMasterActionPage() - GMTabUio.Characters -> screen.navigateToGameMasterCharacterPage() - GMTabUio.Alterations -> screen.navigateToGameMasterAlterationPage() - GMTabUio.Objects -> screen.navigateToGameMasterObjectPage() - } - }, - ) + GameMasterContent( + modifier = Modifier.fillMaxSize(), + controller = controller, + gameMaster = gameMaster, + onGameMaster = gameMasterViewModel::onGameMaster, + onTab = { + when (it) { + GMTabUio.Actions -> controller.navigateToGameMasterActionPage() + GMTabUio.Characters -> controller.navigateToGameMasterCharacterPage() + GMTabUio.Alterations -> controller.navigateToGameMasterAlterationPage() + GMTabUio.Objects -> controller.navigateToGameMasterObjectPage() } - } - } + }, + ) } @Composable diff --git a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/ui/screen/gamemaster/alteration/edit/GMAlterationEditPage.kt b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/ui/screen/gamemaster/alteration/edit/GMAlterationEditPage.kt index d0f2099..8d6c051 100644 --- a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/ui/screen/gamemaster/alteration/edit/GMAlterationEditPage.kt +++ b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/ui/screen/gamemaster/alteration/edit/GMAlterationEditPage.kt @@ -40,6 +40,7 @@ import com.pixelized.desktop.lwa.ui.composable.key.KeyHandler import com.pixelized.desktop.lwa.ui.composable.textfield.LwaTextField import com.pixelized.desktop.lwa.ui.composable.textfield.LwaTextFieldUio import com.pixelized.desktop.lwa.ui.navigation.screen.LocalScreenController +import com.pixelized.desktop.lwa.ui.screen.gamemaster.LocalGMScreenController import com.pixelized.desktop.lwa.ui.theme.color.component.LwaButtonColors import com.pixelized.desktop.lwa.ui.theme.lwa import kotlinx.coroutines.flow.MutableStateFlow