From adaaa153b667a790a9400ff93d53f0b3231a7070 Mon Sep 17 00:00:00 2001 From: Sahil Khan <85223122+sahilsk3333@users.noreply.github.com> Date: Wed, 15 Nov 2023 17:24:15 +0530 Subject: [PATCH 1/4] Replaced TopAppBar with CenterAlignedTopAppBar, Fix issue #857 App title moves --- .../google/samples/apps/sunflower/compose/home/HomeScreen.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/google/samples/apps/sunflower/compose/home/HomeScreen.kt b/app/src/main/java/com/google/samples/apps/sunflower/compose/home/HomeScreen.kt index d7b6400e2..5b274d74a 100644 --- a/app/src/main/java/com/google/samples/apps/sunflower/compose/home/HomeScreen.kt +++ b/app/src/main/java/com/google/samples/apps/sunflower/compose/home/HomeScreen.kt @@ -30,6 +30,7 @@ import androidx.compose.foundation.layout.statusBarsPadding import androidx.compose.foundation.pager.HorizontalPager import androidx.compose.foundation.pager.PagerState import androidx.compose.foundation.pager.rememberPagerState +import androidx.compose.material3.CenterAlignedTopAppBar import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.Icon import androidx.compose.material3.IconButton @@ -38,7 +39,6 @@ import androidx.compose.material3.Scaffold import androidx.compose.material3.Tab import androidx.compose.material3.TabRow import androidx.compose.material3.Text -import androidx.compose.material3.TopAppBar import androidx.compose.material3.TopAppBarDefaults import androidx.compose.material3.TopAppBarScrollBehavior import androidx.compose.runtime.Composable @@ -169,7 +169,7 @@ private fun HomeTopAppBar( scrollBehavior: TopAppBarScrollBehavior, modifier: Modifier = Modifier ) { - TopAppBar( + CenterAlignedTopAppBar( title = { Row( Modifier.fillMaxWidth(), From 7a5ed9a67667df1717508572a54e4faf376bb9fa Mon Sep 17 00:00:00 2001 From: Sahil Khan <85223122+sahilsk3333@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:29:05 +0530 Subject: [PATCH 2/4] Separate navigation route and args Issue#863 --- .../apps/sunflower/compose/SunflowerApp.kt | 31 ++++++++------- .../apps/sunflower/utilities/Screen.kt | 39 +++++++++++++++++++ 2 files changed, 56 insertions(+), 14 deletions(-) create mode 100644 app/src/main/java/com/google/samples/apps/sunflower/utilities/Screen.kt 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..e04437861 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,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() { @@ -46,19 +45,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.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() }, @@ -66,15 +67,17 @@ fun SunFlowerNavHost( 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 = { diff --git a/app/src/main/java/com/google/samples/apps/sunflower/utilities/Screen.kt b/app/src/main/java/com/google/samples/apps/sunflower/utilities/Screen.kt new file mode 100644 index 000000000..392da4961 --- /dev/null +++ b/app/src/main/java/com/google/samples/apps/sunflower/utilities/Screen.kt @@ -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 + }) + + } +} \ No newline at end of file From 84c29e0330baf20cc506af3c5856f6d879522d25 Mon Sep 17 00:00:00 2001 From: Sahil Khan <85223122+sahilsk3333@users.noreply.github.com> Date: Fri, 17 Nov 2023 21:18:06 +0530 Subject: [PATCH 3/4] moved navArguments to Screen class constructor --- .../apps/sunflower/compose/SunflowerApp.kt | 8 +++---- .../apps/sunflower/utilities/Screen.kt | 24 ++++++++++++------- 2 files changed, 20 insertions(+), 12 deletions(-) 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 e04437861..627b93677 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 @@ -50,7 +50,7 @@ fun SunFlowerNavHost( HomeScreen( onPlantClick = { navController.navigate( - Screen.PlantDetail.passPlantId( + Screen.PlantDetail.createRoute( plantId = it.plantId ) ) @@ -59,7 +59,7 @@ fun SunFlowerNavHost( } composable( route = Screen.PlantDetail.route, - arguments = Screen.PlantDetail.arguments + arguments = Screen.PlantDetail.navArguments ) { PlantDetailsScreen( onBackClick = { navController.navigateUp() }, @@ -68,7 +68,7 @@ fun SunFlowerNavHost( }, onGalleryClick = { navController.navigate( - Screen.Gallery.passPlantName( + Screen.Gallery.createRoute( plantName = it.name ) ) @@ -77,7 +77,7 @@ fun SunFlowerNavHost( } composable( route = Screen.Gallery.route, - arguments = Screen.Gallery.arguments + arguments = Screen.Gallery.navArguments ) { GalleryScreen( onPhotoClick = { diff --git a/app/src/main/java/com/google/samples/apps/sunflower/utilities/Screen.kt b/app/src/main/java/com/google/samples/apps/sunflower/utilities/Screen.kt index 392da4961..4d32fb82d 100644 --- a/app/src/main/java/com/google/samples/apps/sunflower/utilities/Screen.kt +++ b/app/src/main/java/com/google/samples/apps/sunflower/utilities/Screen.kt @@ -16,24 +16,32 @@ package com.google.samples.apps.sunflower.utilities +import androidx.navigation.NamedNavArgument import androidx.navigation.NavType import androidx.navigation.navArgument -sealed class Screen(val route: String) { +sealed class Screen( + val route: String, + val navArguments: List = emptyList() +) { data object Home : Screen("home") - data object PlantDetail : Screen("plantDetail/{plantId}") { - fun passPlantId(plantId: String) = "plantDetail/${plantId}" - val arguments = listOf(navArgument("plantId") { + + data object PlantDetail : Screen( + route = "plantDetail/{plantId}", + navArguments = listOf(navArgument("plantId") { type = NavType.StringType }) - + ) { + fun createRoute(plantId: String) = "plantDetail/${plantId}" } - data object Gallery : Screen("gallery/{plantName}") { - fun passPlantName(plantName: String) = "gallery/${plantName}" - val arguments = listOf(navArgument("plantName") { + 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 From e6a9ea1eaa4a23d1fa76eb7d4179a09670dfce7b Mon Sep 17 00:00:00 2001 From: Sahil Khan <85223122+sahilsk3333@users.noreply.github.com> Date: Tue, 21 Nov 2023 08:21:53 +0530 Subject: [PATCH 4/4] Moved Screen class to compose package --- .../samples/apps/sunflower/{utilities => compose}/Screen.kt | 2 +- .../com/google/samples/apps/sunflower/compose/SunflowerApp.kt | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) rename app/src/main/java/com/google/samples/apps/sunflower/{utilities => compose}/Screen.kt (96%) diff --git a/app/src/main/java/com/google/samples/apps/sunflower/utilities/Screen.kt b/app/src/main/java/com/google/samples/apps/sunflower/compose/Screen.kt similarity index 96% rename from app/src/main/java/com/google/samples/apps/sunflower/utilities/Screen.kt rename to app/src/main/java/com/google/samples/apps/sunflower/compose/Screen.kt index 4d32fb82d..4d34d4f1c 100644 --- a/app/src/main/java/com/google/samples/apps/sunflower/utilities/Screen.kt +++ b/app/src/main/java/com/google/samples/apps/sunflower/compose/Screen.kt @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.google.samples.apps.sunflower.utilities +package com.google.samples.apps.sunflower.compose import androidx.navigation.NamedNavArgument import androidx.navigation.NavType 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 627b93677..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 @@ -30,7 +30,6 @@ 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() {