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

Commit

Permalink
Separate navigation route and args Issue#863
Browse files Browse the repository at this point in the history
  • Loading branch information
sahilsk3333 committed Nov 16, 2023
1 parent 5dd259b commit 7a5ed9a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@ 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
import com.google.samples.apps.sunflower.compose.plantdetail.PlantDetailsScreen
import com.google.samples.apps.sunflower.utilities.Screen

@Composable
fun SunflowerApp() {
Expand All @@ -46,35 +45,39 @@ 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.passPlantId(
plantId = it.plantId
)
)
}
)
}
composable(
"plantDetail/{plantId}",
arguments = listOf(navArgument("plantId") {
type = NavType.StringType
})
route = Screen.PlantDetail.route,
arguments = Screen.PlantDetail.arguments
) {
PlantDetailsScreen(
onBackClick = { navController.navigateUp() },
onShareClick = {
createShareIntent(activity, it)
},
onGalleryClick = {
navController.navigate("gallery/${it.name}")
navController.navigate(
Screen.Gallery.passPlantName(
plantName = it.name
)
)
}
)
}
composable(
"gallery/{plantName}",
arguments = listOf(navArgument("plantName") {
type = NavType.StringType
})
route = Screen.Gallery.route,
arguments = Screen.Gallery.arguments
) {
GalleryScreen(
onPhotoClick = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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.utilities

import androidx.navigation.NavType
import androidx.navigation.navArgument

sealed class Screen(val route: String) {
data object Home : Screen("home")
data object PlantDetail : Screen("plantDetail/{plantId}") {
fun passPlantId(plantId: String) = "plantDetail/${plantId}"
val arguments = listOf(navArgument("plantId") {
type = NavType.StringType
})

}

data object Gallery : Screen("gallery/{plantName}") {
fun passPlantName(plantName: String) = "gallery/${plantName}"
val arguments = listOf(navArgument("plantName") {
type = NavType.StringType
})

}
}

0 comments on commit 7a5ed9a

Please sign in to comment.