Last active
October 25, 2022 15:31
-
-
Save Astroa7m/48c5935efde186b8a55e2506610c84f5 to your computer and use it in GitHub Desktop.
Creating navigation graph and app screens
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@OptIn(ExperimentalLifecycleComposeApi::class) | |
@Composable | |
private fun App() { | |
val db = remember { UserDatabase.getUserDatabase(this) } | |
val viewModel = | |
viewModel<MainViewModel>(factory = MainViewModel.Companion.MainViewModelFactory(db)) | |
val navController = rememberNavController() | |
val users by viewModel.users.collectAsStateWithLifecycle() | |
NavHost(navController = navController, startDestination = Destinations.Home.route) { | |
// main screen | |
composable( | |
route = Destinations.Home.route | |
) { | |
HomeScreen( | |
users = users, | |
onUserClicked = { id -> | |
navController.navigate( | |
Destinations.Details.setAndGetArgumentRoute( | |
id | |
) | |
) | |
} | |
) | |
} | |
// details screen | |
composable( | |
route = Destinations.Details.route, | |
deepLinks = listOf( | |
navDeepLink { | |
val c = this@MainActivity | |
val scheme = c.getString(R.string.scheme) | |
val host = c.getString(R.string.host) | |
val path = c.getString(R.string.path_to_existed_user) | |
val uri = "$scheme://$host$path?userId={userId}" | |
uriPattern = uri | |
} | |
), | |
arguments = listOf( | |
navArgument("userId") { | |
this.type = NavType.IntType | |
} | |
) | |
) { navBackStackEntry -> | |
val userId = navBackStackEntry.arguments?.getInt("userId") | |
val user = users.find { it.id == userId } | |
DetailsScreen( | |
user = user, | |
onDismiss = { }, | |
onAddNewUserClicked = { | |
}, | |
onSharing = { | |
} | |
) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment