diff --git a/app/src/main/java/com/google/samples/apps/sunflower/compose/Screen.kt b/app/src/main/java/com/google/samples/apps/sunflower/compose/Screen.kt new file mode 100644 index 000000000..4d34d4f1c --- /dev/null +++ b/app/src/main/java/com/google/samples/apps/sunflower/compose/Screen.kt @@ -0,0 +1,47 @@ +/* + * Copyright 2023 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.samples.apps.sunflower.compose + +import androidx.navigation.NamedNavArgument +import androidx.navigation.NavType +import androidx.navigation.navArgument + +sealed class Screen( + val route: String, + val navArguments: List = emptyList() +) { + data object Home : Screen("home") + + data object PlantDetail : Screen( + route = "plantDetail/{plantId}", + navArguments = listOf(navArgument("plantId") { + type = NavType.StringType + }) + ) { + fun createRoute(plantId: String) = "plantDetail/${plantId}" + } + + data object Gallery : Screen( + route = "gallery/{plantName}", + navArguments = listOf(navArgument("plantName") { + type = NavType.StringType + }) + ) { + fun createRoute(plantName: String) = "gallery/${plantName}" + + } +} \ No newline at end of file diff --git a/app/src/main/java/com/google/samples/apps/sunflower/compose/SunflowerApp.kt b/app/src/main/java/com/google/samples/apps/sunflower/compose/SunflowerApp.kt index f59541972..859907fdf 100644 --- a/app/src/main/java/com/google/samples/apps/sunflower/compose/SunflowerApp.kt +++ b/app/src/main/java/com/google/samples/apps/sunflower/compose/SunflowerApp.kt @@ -23,11 +23,9 @@ import androidx.compose.runtime.Composable import androidx.compose.ui.platform.LocalContext import androidx.core.app.ShareCompat import androidx.navigation.NavHostController -import androidx.navigation.NavType import androidx.navigation.compose.NavHost import androidx.navigation.compose.composable import androidx.navigation.compose.rememberNavController -import androidx.navigation.navArgument import com.google.samples.apps.sunflower.R import com.google.samples.apps.sunflower.compose.gallery.GalleryScreen import com.google.samples.apps.sunflower.compose.home.HomeScreen @@ -46,19 +44,21 @@ fun SunFlowerNavHost( navController: NavHostController ) { val activity = (LocalContext.current as Activity) - NavHost(navController = navController, startDestination = "home") { - composable("home") { + NavHost(navController = navController, startDestination = Screen.Home.route) { + composable(route = Screen.Home.route) { HomeScreen( onPlantClick = { - navController.navigate("plantDetail/${it.plantId}") + navController.navigate( + Screen.PlantDetail.createRoute( + plantId = it.plantId + ) + ) } ) } composable( - "plantDetail/{plantId}", - arguments = listOf(navArgument("plantId") { - type = NavType.StringType - }) + route = Screen.PlantDetail.route, + arguments = Screen.PlantDetail.navArguments ) { PlantDetailsScreen( onBackClick = { navController.navigateUp() }, @@ -66,15 +66,17 @@ fun SunFlowerNavHost( createShareIntent(activity, it) }, onGalleryClick = { - navController.navigate("gallery/${it.name}") + navController.navigate( + Screen.Gallery.createRoute( + plantName = it.name + ) + ) } ) } composable( - "gallery/{plantName}", - arguments = listOf(navArgument("plantName") { - type = NavType.StringType - }) + route = Screen.Gallery.route, + arguments = Screen.Gallery.navArguments ) { GalleryScreen( onPhotoClick = {