Skip to content
This repository was archived by the owner on Aug 2, 2024. It is now read-only.

Commit 7a5ed9a

Browse files
committed
Separate navigation route and args Issue#863
1 parent 5dd259b commit 7a5ed9a

File tree

2 files changed

+56
-14
lines changed

2 files changed

+56
-14
lines changed

app/src/main/java/com/google/samples/apps/sunflower/compose/SunflowerApp.kt

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,14 @@ import androidx.compose.runtime.Composable
2323
import androidx.compose.ui.platform.LocalContext
2424
import androidx.core.app.ShareCompat
2525
import androidx.navigation.NavHostController
26-
import androidx.navigation.NavType
2726
import androidx.navigation.compose.NavHost
2827
import androidx.navigation.compose.composable
2928
import androidx.navigation.compose.rememberNavController
30-
import androidx.navigation.navArgument
3129
import com.google.samples.apps.sunflower.R
3230
import com.google.samples.apps.sunflower.compose.gallery.GalleryScreen
3331
import com.google.samples.apps.sunflower.compose.home.HomeScreen
3432
import com.google.samples.apps.sunflower.compose.plantdetail.PlantDetailsScreen
33+
import com.google.samples.apps.sunflower.utilities.Screen
3534

3635
@Composable
3736
fun SunflowerApp() {
@@ -46,35 +45,39 @@ fun SunFlowerNavHost(
4645
navController: NavHostController
4746
) {
4847
val activity = (LocalContext.current as Activity)
49-
NavHost(navController = navController, startDestination = "home") {
50-
composable("home") {
48+
NavHost(navController = navController, startDestination = Screen.Home.route) {
49+
composable(route = Screen.Home.route) {
5150
HomeScreen(
5251
onPlantClick = {
53-
navController.navigate("plantDetail/${it.plantId}")
52+
navController.navigate(
53+
Screen.PlantDetail.passPlantId(
54+
plantId = it.plantId
55+
)
56+
)
5457
}
5558
)
5659
}
5760
composable(
58-
"plantDetail/{plantId}",
59-
arguments = listOf(navArgument("plantId") {
60-
type = NavType.StringType
61-
})
61+
route = Screen.PlantDetail.route,
62+
arguments = Screen.PlantDetail.arguments
6263
) {
6364
PlantDetailsScreen(
6465
onBackClick = { navController.navigateUp() },
6566
onShareClick = {
6667
createShareIntent(activity, it)
6768
},
6869
onGalleryClick = {
69-
navController.navigate("gallery/${it.name}")
70+
navController.navigate(
71+
Screen.Gallery.passPlantName(
72+
plantName = it.name
73+
)
74+
)
7075
}
7176
)
7277
}
7378
composable(
74-
"gallery/{plantName}",
75-
arguments = listOf(navArgument("plantName") {
76-
type = NavType.StringType
77-
})
79+
route = Screen.Gallery.route,
80+
arguments = Screen.Gallery.arguments
7881
) {
7982
GalleryScreen(
8083
onPhotoClick = {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2023 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.samples.apps.sunflower.utilities
18+
19+
import androidx.navigation.NavType
20+
import androidx.navigation.navArgument
21+
22+
sealed class Screen(val route: String) {
23+
data object Home : Screen("home")
24+
data object PlantDetail : Screen("plantDetail/{plantId}") {
25+
fun passPlantId(plantId: String) = "plantDetail/${plantId}"
26+
val arguments = listOf(navArgument("plantId") {
27+
type = NavType.StringType
28+
})
29+
30+
}
31+
32+
data object Gallery : Screen("gallery/{plantName}") {
33+
fun passPlantName(plantName: String) = "gallery/${plantName}"
34+
val arguments = listOf(navArgument("plantName") {
35+
type = NavType.StringType
36+
})
37+
38+
}
39+
}

0 commit comments

Comments
 (0)