Android: typed nav routes for Documents and Ensemble
The last two string-route graphs move to @Serializable routes like the rest of the app: Documents' sealed DocumentsDestination (with its fully qualified navArgument/NavType, against the style rule) and Ensemble's EnsembleRoute. The String overload of AnimatedNavHost then has no caller and goes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
655d9442dd
commit
c381b349e4
3 files changed
+24
-58
No files matched your search
+14
-23
@@ -5,43 +5,34 @@ import androidx.navigation.NavHostController
|
||||
import androidx.navigation.compose.NavHost
|
||||
import androidx.navigation.compose.composable
|
||||
import androidx.navigation.compose.rememberNavController
|
||||
import androidx.navigation.toRoute
|
||||
import com.gregshuflin.synchronicity.documents.ui.DocumentDetailScreen
|
||||
import com.gregshuflin.synchronicity.documents.ui.DocumentsScreen as DocumentsContentScreen
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable private object DocumentListRoute
|
||||
|
||||
@Serializable private data class DocumentDetailRoute(val documentId: String)
|
||||
|
||||
@Composable
|
||||
fun DocumentsScreen(
|
||||
navController: NavHostController = rememberNavController(),
|
||||
onBackToMain: () -> Unit,
|
||||
) {
|
||||
NavHost(navController = navController, startDestination = DocumentsDestination.List.route) {
|
||||
composable(DocumentsDestination.List.route) {
|
||||
NavHost(navController = navController, startDestination = DocumentListRoute) {
|
||||
composable<DocumentListRoute> {
|
||||
DocumentsContentScreen(
|
||||
onBack = onBackToMain,
|
||||
onDocumentClick = { documentId ->
|
||||
navController.navigate(DocumentsDestination.Detail.createRoute(documentId))
|
||||
navController.navigate(DocumentDetailRoute(documentId))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
composable(
|
||||
route = DocumentsDestination.Detail.route,
|
||||
arguments =
|
||||
listOf(
|
||||
androidx.navigation.navArgument("documentId") {
|
||||
type = androidx.navigation.NavType.StringType
|
||||
}
|
||||
),
|
||||
) { backStackEntry ->
|
||||
val documentId = backStackEntry.arguments?.getString("documentId") ?: return@composable
|
||||
DocumentDetailScreen(documentId = documentId, onBack = { navController.popBackStack() })
|
||||
composable<DocumentDetailRoute> { backStackEntry ->
|
||||
DocumentDetailScreen(
|
||||
documentId = backStackEntry.toRoute<DocumentDetailRoute>().documentId,
|
||||
onBack = { navController.popBackStack() },
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sealed class DocumentsDestination(val route: String) {
|
||||
object List : DocumentsDestination("documents_list")
|
||||
|
||||
object Detail : DocumentsDestination("document_detail/{documentId}") {
|
||||
fun createRoute(documentId: String) = "document_detail/$documentId"
|
||||
}
|
||||
}
|
||||
@@ -23,10 +23,9 @@ import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.text.font.FontStyle
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.navigation.NavType
|
||||
import androidx.navigation.compose.composable
|
||||
import androidx.navigation.compose.rememberNavController
|
||||
import androidx.navigation.navArgument
|
||||
import androidx.navigation.toRoute
|
||||
import com.gregshuflin.synchronicity.NativeLib
|
||||
import com.gregshuflin.synchronicity.NodeReset
|
||||
import com.gregshuflin.synchronicity.OnStoreChange
|
||||
@@ -44,6 +43,7 @@ import com.gregshuflin.synchronicity.userMessage
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import kotlinx.serialization.Serializable
|
||||
import uniffi.synchronicity.ChangeChannel
|
||||
import uniffi.synchronicity.EnsembleMemberInfo
|
||||
import uniffi.synchronicity.EnsembleStatus
|
||||
@@ -59,12 +59,9 @@ import uniffi.synchronicity.revocationRevokedAt
|
||||
import uniffi.synchronicity.revocationShowRevoked
|
||||
import uniffi.synchronicity.shortId
|
||||
|
||||
private object EnsembleRoute {
|
||||
const val NODES = "nodes"
|
||||
const val NODE = "node/{nodeId}"
|
||||
@Serializable private object EnsembleNodesRoute
|
||||
|
||||
fun node(nodeIdHex: String) = "node/$nodeIdHex"
|
||||
}
|
||||
@Serializable private data class EnsembleNodeRoute(val nodeIdHex: String)
|
||||
|
||||
/**
|
||||
* Entry point for the Ensemble agent. Hosts an internal nav graph so the nodes list can push a
|
||||
@@ -163,10 +160,10 @@ fun EnsembleScreen(
|
||||
|
||||
AnimatedNavHost(
|
||||
navController = navController,
|
||||
startDestination = EnsembleRoute.NODES,
|
||||
startDestination = EnsembleNodesRoute,
|
||||
modifier = modifier,
|
||||
) {
|
||||
composable(EnsembleRoute.NODES) {
|
||||
composable<EnsembleNodesRoute> {
|
||||
EnsembleNodesScreen(
|
||||
nativeLib = nativeLib,
|
||||
screenData = screenData,
|
||||
@@ -180,17 +177,14 @@ fun EnsembleScreen(
|
||||
onPing = { nodeIdHex -> launchPing(scope, nativeLib, nodeIdHex, pingStates) },
|
||||
onSync = { nodeIdHex -> launchSync(scope, nativeLib, nodeIdHex, syncStates, zone) },
|
||||
onNodeSelect = { nodeIdHex ->
|
||||
navController.navigate(EnsembleRoute.node(nodeIdHex))
|
||||
navController.navigate(EnsembleNodeRoute(nodeIdHex))
|
||||
},
|
||||
onRetire = onRetire,
|
||||
applySafePadding = applySafePadding,
|
||||
)
|
||||
}
|
||||
composable(
|
||||
route = EnsembleRoute.NODE,
|
||||
arguments = listOf(navArgument("nodeId") { type = NavType.StringType }),
|
||||
) { backStackEntry ->
|
||||
val nodeIdHex = backStackEntry.arguments?.getString("nodeId") ?: return@composable
|
||||
composable<EnsembleNodeRoute> { backStackEntry ->
|
||||
val nodeIdHex = backStackEntry.toRoute<EnsembleNodeRoute>().nodeIdHex
|
||||
val member = screenData?.members?.find { it.nodeIdHex == nodeIdHex }
|
||||
NodeScreen(
|
||||
nativeLib = nativeLib,
|
||||
|
||||
@@ -35,28 +35,9 @@ private val popExit: AnimatedContentTransitionScope<NavBackStackEntry>.() -> Exi
|
||||
/**
|
||||
* [NavHost] wrapper that applies the app's standard slide transitions. Prefer this over [NavHost]
|
||||
* directly so every navigation graph — the root graph and each agent's nested graph — animates
|
||||
* consistently. This overload takes a string-route start destination.
|
||||
* consistently. Routes are `@Serializable` types, never strings.
|
||||
*/
|
||||
@Composable
|
||||
fun AnimatedNavHost(
|
||||
navController: NavHostController,
|
||||
startDestination: String,
|
||||
modifier: Modifier = Modifier,
|
||||
builder: NavGraphBuilder.() -> Unit,
|
||||
) =
|
||||
NavHost(
|
||||
navController = navController,
|
||||
startDestination = startDestination,
|
||||
modifier = modifier,
|
||||
enterTransition = enter,
|
||||
exitTransition = exit,
|
||||
popEnterTransition = popEnter,
|
||||
popExitTransition = popExit,
|
||||
builder = builder,
|
||||
)
|
||||
|
||||
/** Type-safe-route overload of [AnimatedNavHost]. */
|
||||
@Composable
|
||||
fun AnimatedNavHost(
|
||||
navController: NavHostController,
|
||||
startDestination: Any,
|
||||
|
||||
Reference in new issue
Block a user