Pablo
03/20/2024, 11:46 AMNavHost(startDestination = "profile/{userId}") {
composable("profile/{userId}")
}
But how to work with enums for the routers? in previous codelabs and docu they told you that you must use enums for the routes:
NavHost(startDestination = Screen.HomeScreen.name) {
composable(Screen.HomeScreen.name)
}
How to pass parameters correctly using enums for the routes? How to add that userId parameter if you use an enum for the route?Stylianos Gakis
03/20/2024, 11:56 AMPablo
03/20/2024, 12:07 PMPablo
03/20/2024, 12:07 PMPablo
03/20/2024, 12:07 PMPablo
03/20/2024, 12:07 PMPablo
03/20/2024, 12:08 PMcomposable(route = Screen.PlacesScreen.name+"/{selectedCategory}")
Pablo
03/20/2024, 12:08 PMStylianos Gakis
03/20/2024, 12:12 PMcomposable(route = Screen.PlacesScreen.name+"/{selectedCategory}")
suggestion does not add the right arguments to the route as you you can see in the first link
arguments = listOf(navArgument(TOPIC_ID_ARG) { type = NavType.StringType }),
Also something that you will need to do anyway, you can’t go around that.
Both option 1 and 2 have all those problems covered.
So it’s up to you to decide what you want to do now that you know what you need to think about 😊Pablo
03/20/2024, 12:14 PMPablo
03/20/2024, 12:14 PMStylianos Gakis
03/20/2024, 12:15 PMnavigation-compose-typed
is by far the simplest think you can do for this. All you’ll need to do is change the imports to use the right function from inside that library, add the serialization plugin, and mark your destinations are serializable. That’s it.Stylianos Gakis
03/20/2024, 12:20 PMPablo
03/20/2024, 2:07 PMPablo
03/20/2024, 2:07 PMPablo
03/20/2024, 2:07 PMclass UserViewModel(
savedStateHandle: SavedStateHandle,
private val userInfoRepository: UserInfoRepository
) : ViewModel() {
private val userId: String = checkNotNull(savedStateHandle["userId"])
private val userInfo: Flow<UserInfo> = userInfoRepository.getUserInfo(userId)
}
Stylianos Gakis
03/20/2024, 2:08 PMPablo
03/20/2024, 2:08 PMPablo
03/20/2024, 2:08 PMStylianos Gakis
03/20/2024, 2:09 PMlink/blah/3
for route link/blah/{someId}
then it's 3
Pablo
03/20/2024, 2:10 PMPablo
03/20/2024, 2:11 PMStylianos Gakis
03/20/2024, 2:12 PMPablo
03/20/2024, 2:12 PMprivate val userInfoRepository: UserInfoRepository
Pablo
03/20/2024, 2:13 PMStylianos Gakis
03/20/2024, 2:14 PMviewModels
function. Again, not magic.Stylianos Gakis
03/20/2024, 2:15 PMwhy in that sample they are passing a second parameter on the viewmodel? how and where is set that parameter and why is not a livedata, remember etc... parameter?private val userInfoRepository: UserInfoRepository
You will probably benefit from reading the docs a bit https://developer.android.com/topic/architecture
Pablo
03/20/2024, 2:15 PMStylianos Gakis
03/20/2024, 2:16 PMPablo
03/20/2024, 2:18 PMPablo
03/20/2024, 2:18 PMPablo
03/20/2024, 2:19 PMPablo
03/20/2024, 2:19 PMPablo
03/20/2024, 2:19 PMStylianos Gakis
03/20/2024, 2:20 PMStylianos Gakis
03/20/2024, 2:21 PMgetUserInfo
function from the repo using the parameter which has come from the route parameters. That's it.Pablo
03/20/2024, 2:22 PMStylianos Gakis
03/20/2024, 2:24 PMPablo
03/20/2024, 2:28 PMViewModelProvider.Factory
but they are not showing itPablo
03/20/2024, 2:28 PMPablo
03/20/2024, 2:29 PMStylianos Gakis
03/20/2024, 2:33 PMPablo
03/20/2024, 2:53 PMPablo
03/20/2024, 2:53 PMPablo
03/20/2024, 2:53 PMStylianos Gakis
03/20/2024, 2:53 PMPablo
03/20/2024, 2:53 PM