1 /*
<lambda>null2  * Copyright 2023 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 @file:Suppress("DEPRECATION") // Suppress for WindowWidthSizeClass
18 
19 package androidx.compose.material3.adaptive.navigationsuite.samples
20 
21 import androidx.annotation.Sampled
22 import androidx.compose.foundation.layout.Arrangement
23 import androidx.compose.foundation.layout.Column
24 import androidx.compose.foundation.layout.fillMaxWidth
25 import androidx.compose.foundation.layout.padding
26 import androidx.compose.material.icons.Icons
27 import androidx.compose.material.icons.filled.Favorite
28 import androidx.compose.material.icons.outlined.FavoriteBorder
29 import androidx.compose.material3.Button
30 import androidx.compose.material3.ExperimentalMaterial3ExpressiveApi
31 import androidx.compose.material3.Icon
32 import androidx.compose.material3.Text
33 import androidx.compose.material3.adaptive.currentWindowAdaptiveInfo
34 import androidx.compose.material3.adaptive.navigationsuite.NavigationSuiteItem
35 import androidx.compose.material3.adaptive.navigationsuite.NavigationSuiteScaffold
36 import androidx.compose.material3.adaptive.navigationsuite.NavigationSuiteScaffoldDefaults
37 import androidx.compose.material3.adaptive.navigationsuite.NavigationSuiteType
38 import androidx.compose.material3.adaptive.navigationsuite.rememberNavigationSuiteScaffoldState
39 import androidx.compose.runtime.Composable
40 import androidx.compose.runtime.getValue
41 import androidx.compose.runtime.mutableIntStateOf
42 import androidx.compose.runtime.remember
43 import androidx.compose.runtime.rememberCoroutineScope
44 import androidx.compose.runtime.setValue
45 import androidx.compose.ui.Alignment
46 import androidx.compose.ui.Modifier
47 import androidx.compose.ui.text.style.TextAlign
48 import androidx.compose.ui.tooling.preview.Preview
49 import androidx.compose.ui.unit.dp
50 import androidx.window.core.layout.WindowHeightSizeClass
51 import androidx.window.core.layout.WindowWidthSizeClass
52 import kotlinx.coroutines.launch
53 
54 @OptIn(ExperimentalMaterial3ExpressiveApi::class)
55 @Preview
56 @Sampled
57 @Composable
58 fun NavigationSuiteScaffoldSample() {
59     var selectedItem by remember { mutableIntStateOf(0) }
60     val navItems = listOf("Songs", "Artists", "Playlists")
61     val navSuiteType =
62         NavigationSuiteScaffoldDefaults.navigationSuiteType(currentWindowAdaptiveInfo())
63     val state = rememberNavigationSuiteScaffoldState()
64     val scope = rememberCoroutineScope()
65 
66     NavigationSuiteScaffold(
67         state = state,
68         navigationItems = {
69             navItems.forEachIndexed { index, navItem ->
70                 NavigationSuiteItem(
71                     icon = {
72                         Icon(
73                             if (selectedItem == index) Icons.Filled.Favorite
74                             else Icons.Outlined.FavoriteBorder,
75                             contentDescription = null
76                         )
77                     },
78                     label = { Text(navItem) },
79                     selected = selectedItem == index,
80                     onClick = { selectedItem = index }
81                 )
82             }
83         }
84     ) {
85         // Screen content.
86         Column(
87             modifier = Modifier.fillMaxWidth(),
88             horizontalAlignment = Alignment.CenterHorizontally
89         ) {
90             Text(
91                 modifier = Modifier.padding(16.dp),
92                 text =
93                     "Current NavigationSuiteType: $navSuiteType\n" +
94                         "Visibility: ${state.currentValue}",
95                 textAlign = TextAlign.Center
96             )
97             Button(onClick = { scope.launch { state.toggle() } }) {
98                 Text("Hide/show navigation component")
99             }
100         }
101     }
102 }
103 
104 @OptIn(ExperimentalMaterial3ExpressiveApi::class)
105 @Preview
106 @Sampled
107 @Composable
108 @Suppress("DEPRECATION") // WindowWidthSizeClass is deprecated
NavigationSuiteScaffoldCustomConfigSamplenull109 fun NavigationSuiteScaffoldCustomConfigSample() {
110     var selectedItem by remember { mutableIntStateOf(0) }
111     val navItems = listOf("Songs", "Artists", "Playlists")
112     // Custom configuration that shows a wide navigation rail in small/medium width screens, an
113     // expanded wide navigation rail in expanded width screens, and a short navigation bar in small
114     // height screens.
115     val navSuiteType =
116         with(currentWindowAdaptiveInfo()) {
117             if (
118                 windowSizeClass.windowWidthSizeClass == WindowWidthSizeClass.COMPACT ||
119                     windowSizeClass.windowWidthSizeClass == WindowWidthSizeClass.MEDIUM
120             ) {
121                 NavigationSuiteType.WideNavigationRailCollapsed
122             } else if (windowSizeClass.windowHeightSizeClass == WindowHeightSizeClass.COMPACT) {
123                 NavigationSuiteType.ShortNavigationBarMedium
124             } else if (windowSizeClass.windowWidthSizeClass == WindowWidthSizeClass.EXPANDED) {
125                 NavigationSuiteType.WideNavigationRailExpanded
126             } else {
127                 NavigationSuiteScaffoldDefaults.navigationSuiteType(currentWindowAdaptiveInfo())
128             }
129         }
130     val state = rememberNavigationSuiteScaffoldState()
131     val scope = rememberCoroutineScope()
132 
133     NavigationSuiteScaffold(
134         navigationSuiteType = navSuiteType,
135         state = state,
136         navigationItemVerticalArrangement = Arrangement.Center,
137         navigationItems = {
138             navItems.forEachIndexed { index, navItem ->
139                 NavigationSuiteItem(
140                     navigationSuiteType = navSuiteType,
141                     icon = {
142                         Icon(
143                             if (selectedItem == index) Icons.Filled.Favorite
144                             else Icons.Outlined.FavoriteBorder,
145                             contentDescription = null
146                         )
147                     },
148                     label = { Text(navItem) },
149                     selected = selectedItem == index,
150                     onClick = { selectedItem = index }
151                 )
152             }
153         }
154     ) {
155         // Screen content.
156         Column(
157             modifier = Modifier.fillMaxWidth(),
158             horizontalAlignment = Alignment.CenterHorizontally
159         ) {
160             Text(
161                 modifier = Modifier.padding(16.dp),
162                 text =
163                     "Current NavigationSuiteType: $navSuiteType\n" +
164                         "Visibility: ${state.currentValue}",
165                 textAlign = TextAlign.Center
166             )
167             Button(onClick = { scope.launch { state.toggle() } }) {
168                 Text("Hide/show navigation component")
169             }
170         }
171     }
172 }
173