1 /*
<lambda>null2  * Copyright 2020 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  *      http://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 androidx.compose.material.navigation.samples
18 
19 import androidx.annotation.Sampled
20 import androidx.compose.foundation.layout.Column
21 import androidx.compose.foundation.layout.fillMaxSize
22 import androidx.compose.foundation.layout.fillMaxWidth
23 import androidx.compose.material.Button
24 import androidx.compose.material.Text
25 import androidx.compose.material.navigation.ModalBottomSheetLayout
26 import androidx.compose.material.navigation.bottomSheet
27 import androidx.compose.material.navigation.rememberBottomSheetNavigator
28 import androidx.compose.runtime.Composable
29 import androidx.compose.ui.Alignment
30 import androidx.compose.ui.Modifier
31 import androidx.navigation.compose.NavHost
32 import androidx.navigation.compose.composable
33 import androidx.navigation.compose.rememberNavController
34 import java.util.UUID
35 
36 private object Destinations {
37     const val Home = "HOME"
38     const val Feed = "FEED"
39     const val Sheet = "SHEET"
40 }
41 
42 @Sampled
43 @Composable
BottomSheetNavDemonull44 fun BottomSheetNavDemo() {
45     val bottomSheetNavigator = rememberBottomSheetNavigator()
46     val navController = rememberNavController(bottomSheetNavigator)
47 
48     ModalBottomSheetLayout(bottomSheetNavigator) {
49         NavHost(navController, Destinations.Home) {
50             composable(Destinations.Home) {
51                 HomeScreen(
52                     showSheet = {
53                         navController.navigate(Destinations.Sheet + "?arg=From Home Screen")
54                     },
55                     showFeed = { navController.navigate(Destinations.Feed) }
56                 )
57             }
58             composable(Destinations.Feed) { Text("Feed!") }
59             bottomSheet(Destinations.Sheet + "?arg={arg}") { backstackEntry ->
60                 val arg = backstackEntry.arguments?.getString("arg") ?: "Missing argument :("
61                 BottomSheet(
62                     showFeed = { navController.navigate(Destinations.Feed) },
63                     showAnotherSheet = {
64                         navController.navigate(Destinations.Sheet + "?arg=${UUID.randomUUID()}")
65                     },
66                     arg = arg
67                 )
68             }
69         }
70     }
71 }
72 
73 @Composable
HomeScreennull74 private fun HomeScreen(showSheet: () -> Unit, showFeed: () -> Unit) {
75     Column(Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally) {
76         Text("Body")
77         Button(onClick = showSheet) { Text("Show sheet!") }
78         Button(onClick = showFeed) { Text("Navigate to Feed") }
79     }
80 }
81 
82 @Composable
BottomSheetnull83 private fun BottomSheet(showFeed: () -> Unit, showAnotherSheet: () -> Unit, arg: String) {
84     Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) {
85         Text("Sheet with arg: $arg")
86         Button(onClick = showFeed) { Text("Click me to navigate!") }
87         Button(onClick = showAnotherSheet) { Text("Click me to show another sheet!") }
88     }
89 }
90