1 /*
<lambda>null2 * Copyright 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.google.accompanist.sample.navigation.material
18
19 import android.os.Bundle
20 import androidx.activity.ComponentActivity
21 import androidx.activity.compose.setContent
22 import androidx.compose.foundation.layout.Column
23 import androidx.compose.foundation.layout.fillMaxSize
24 import androidx.compose.foundation.layout.fillMaxWidth
25 import androidx.compose.material.Button
26 import androidx.compose.material.Text
27 import androidx.compose.runtime.Composable
28 import androidx.compose.ui.Alignment
29 import androidx.compose.ui.Modifier
30 import androidx.navigation.compose.NavHost
31 import androidx.navigation.compose.composable
32 import androidx.navigation.compose.rememberNavController
33 import com.google.accompanist.navigation.material.ExperimentalMaterialNavigationApi
34 import com.google.accompanist.navigation.material.ModalBottomSheetLayout
35 import com.google.accompanist.navigation.material.bottomSheet
36 import com.google.accompanist.navigation.material.rememberBottomSheetNavigator
37 import com.google.accompanist.sample.AccompanistSampleTheme
38 import java.util.UUID
39
40 class BottomSheetNavSample : ComponentActivity() {
41 override fun onCreate(savedInstanceState: Bundle?) {
42 super.onCreate(savedInstanceState)
43
44 setContent {
45 AccompanistSampleTheme {
46 BottomSheetNavDemo()
47 }
48 }
49 }
50 }
51
52 private object Destinations {
53 const val Home = "HOME"
54 const val Feed = "FEED"
55 const val Sheet = "SHEET"
56 }
57
58 @OptIn(ExperimentalMaterialNavigationApi::class)
59 @Composable
BottomSheetNavDemonull60 fun BottomSheetNavDemo() {
61 val bottomSheetNavigator = rememberBottomSheetNavigator()
62 val navController = rememberNavController(bottomSheetNavigator)
63
64 ModalBottomSheetLayout(bottomSheetNavigator) {
65 NavHost(navController, Destinations.Home) {
66 composable(Destinations.Home) {
67 HomeScreen(
68 showSheet = {
69 navController.navigate(Destinations.Sheet + "?arg=From Home Screen")
70 },
71 showFeed = { navController.navigate(Destinations.Feed) }
72 )
73 }
74 composable(Destinations.Feed) { Text("Feed!") }
75 bottomSheet(Destinations.Sheet + "?arg={arg}") { backstackEntry ->
76 val arg = backstackEntry.arguments?.getString("arg") ?: "Missing argument :("
77 BottomSheet(
78 showFeed = { navController.navigate(Destinations.Feed) },
79 showAnotherSheet = {
80 navController.navigate(Destinations.Sheet + "?arg=${UUID.randomUUID()}")
81 },
82 arg = arg
83 )
84 }
85 }
86 }
87 }
88
89 @Composable
HomeScreennull90 private fun HomeScreen(showSheet: () -> Unit, showFeed: () -> Unit) {
91 Column(Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally) {
92 Text("Body")
93 Button(onClick = showSheet) {
94 Text("Show sheet!")
95 }
96 Button(onClick = showFeed) {
97 Text("Navigate to Feed")
98 }
99 }
100 }
101
102 @Composable
BottomSheetnull103 private fun BottomSheet(showFeed: () -> Unit, showAnotherSheet: () -> Unit, arg: String) {
104 Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) {
105 Text("Sheet with arg: $arg")
106 Button(onClick = showFeed) {
107 Text("Click me to navigate!")
108 }
109 Button(onClick = showAnotherSheet) {
110 Text("Click me to show another sheet!")
111 }
112 }
113 }
114