Skip to content

Commit

Permalink
Navigation (#130)
Browse files Browse the repository at this point in the history
* 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 <jakeroseman@users.noreply.github.com>
  • Loading branch information
jakeroseman and jakeroseman authored Jul 26, 2023
1 parent 0210c41 commit f2035d0
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,42 @@ 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() {
override fun onCreate(savedInstanceState: Bundle?) {
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()
}
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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"),
}

0 comments on commit f2035d0

Please sign in to comment.