From f2035d047fea6a7d2793b607a91a81c627615b35 Mon Sep 17 00:00:00 2001 From: Jake Roseman <122034773+jakeroseman@users.noreply.github.com> Date: Wed, 26 Jul 2023 14:50:44 +0100 Subject: [PATCH] Navigation (#130) * Adding landing screen with navigation destinations. * Apply Spotless * Updating the navigation to use an enum class that enumerated all possible destinations. They go on to populate a LazyColumn in NavigationButtons. * Apply Spotless * Updating the navigation to use an enum class that enumerated all possible destinations. They go on to populate a LazyColumn in NavigationButtons. * Removing when block from NavigationItems. * Apply Spotless --------- Co-authored-by: jakeroseman --- .../snippets/LandingScreen/LandingScreen.kt | 90 +++++++++++++++++++ .../compose/snippets/SnippetsActivity.kt | 27 ++++-- .../snippets/navigation/Destination.kt | 22 +++++ 3 files changed, 131 insertions(+), 8 deletions(-) create mode 100644 compose/snippets/src/main/java/com/example/compose/snippets/LandingScreen/LandingScreen.kt create mode 100644 compose/snippets/src/main/java/com/example/compose/snippets/navigation/Destination.kt diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/LandingScreen/LandingScreen.kt b/compose/snippets/src/main/java/com/example/compose/snippets/LandingScreen/LandingScreen.kt new file mode 100644 index 00000000..2d4dbf64 --- /dev/null +++ b/compose/snippets/src/main/java/com/example/compose/snippets/LandingScreen/LandingScreen.kt @@ -0,0 +1,90 @@ +/* + * Copyright 2023 The Android Open Source Project + * + * 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.example.compose.snippets.LandingScreen + +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.foundation.lazy.items +import androidx.compose.material3.Button +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.text.TextStyle +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.text.style.TextAlign +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import com.example.compose.snippets.navigation.Destination + +@Composable +fun LandingScreen( + navigate: (Destination) -> Unit +) { + Column( + modifier = Modifier + .padding(16.dp) + .fillMaxSize(), + verticalArrangement = Arrangement.spacedBy(24.dp), + ) { + Text( + modifier = Modifier.fillMaxWidth(), + style = TextStyle( + fontSize = 24.sp, + fontWeight = FontWeight.Bold, + textAlign = TextAlign.Center, + ), + text = "Android snippets", + ) + Text( + text = "Use the following buttons to view a selection of the snippets used in the Android documentation." + ) + NavigationItems { navigate(it) } + } +} + +@Composable +fun NavigationItems(navigate: (Destination) -> Unit) { + LazyColumn( + modifier = Modifier + .padding(16.dp) + .fillMaxSize(), + verticalArrangement = Arrangement.spacedBy(8.dp), + horizontalAlignment = Alignment.CenterHorizontally, + ) { + items(Destination.values().toList()) { destination -> + NavigationItem(destination) { + navigate( + destination + ) + } + } + } +} + +@Composable +fun NavigationItem(destination: Destination, onClick: () -> Unit) { + Button( + onClick = { onClick() } + ) { + Text(destination.title) + } +} diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/SnippetsActivity.kt b/compose/snippets/src/main/java/com/example/compose/snippets/SnippetsActivity.kt index 085558fb..330a5c04 100644 --- a/compose/snippets/src/main/java/com/example/compose/snippets/SnippetsActivity.kt +++ b/compose/snippets/src/main/java/com/example/compose/snippets/SnippetsActivity.kt @@ -19,12 +19,17 @@ package com.example.compose.snippets import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent -import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Surface import androidx.compose.ui.Modifier +import androidx.navigation.compose.NavHost +import androidx.navigation.compose.composable +import androidx.navigation.compose.rememberNavController +import com.example.compose.snippets.LandingScreen.LandingScreen +import com.example.compose.snippets.graphics.BrushExamplesScreen import com.example.compose.snippets.images.ImageExamplesScreen +import com.example.compose.snippets.navigation.Destination import com.example.compose.snippets.ui.theme.SnippetsTheme class SnippetsActivity : ComponentActivity() { @@ -32,18 +37,24 @@ class SnippetsActivity : ComponentActivity() { super.onCreate(savedInstanceState) setContent { SnippetsTheme { + val navController = rememberNavController() // A surface container using the 'background' color from the theme Surface( modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background ) { - // TODO - We should put these in different navigation destinations - Column( - modifier = Modifier - .fillMaxSize() - ) { - // BrushExamplesScreen() - ImageExamplesScreen() + NavHost(navController, startDestination = "LandingScreen") { + composable("LandingScreen") { + LandingScreen { navController.navigate(it.route) } + } + Destination.values().forEach { destination -> + composable(destination.route) { + when (destination) { + Destination.BrushExamples -> BrushExamplesScreen() + Destination.ImageExamples -> ImageExamplesScreen() + } + } + } } } } diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/navigation/Destination.kt b/compose/snippets/src/main/java/com/example/compose/snippets/navigation/Destination.kt new file mode 100644 index 00000000..b4bb39ad --- /dev/null +++ b/compose/snippets/src/main/java/com/example/compose/snippets/navigation/Destination.kt @@ -0,0 +1,22 @@ +/* + * Copyright 2023 The Android Open Source Project + * + * 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.example.compose.snippets.navigation + +enum class Destination(val route: String, val title: String) { + BrushExamples("brushExamples", "Brush Examples"), + ImageExamples("imageExamples", "Image Examples"), +}