|
| 1 | +/* |
| 2 | + * Copyright 2024 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 | +@file:OptIn(ExperimentalMaterial3AdaptiveNavigationSuiteApi::class) |
| 18 | + |
| 19 | +package com.example.compose.snippets.adaptivelayouts |
| 20 | + |
| 21 | +import androidx.annotation.StringRes |
| 22 | +import androidx.compose.material.icons.Icons |
| 23 | +import androidx.compose.material.icons.filled.AccountBox |
| 24 | +import androidx.compose.material.icons.filled.Favorite |
| 25 | +import androidx.compose.material.icons.filled.Home |
| 26 | +import androidx.compose.material.icons.filled.ShoppingCart |
| 27 | +import androidx.compose.material3.Icon |
| 28 | +import androidx.compose.material3.MaterialTheme |
| 29 | +import androidx.compose.material3.NavigationBarItemDefaults |
| 30 | +import androidx.compose.material3.Text |
| 31 | +import androidx.compose.material3.adaptive.currentWindowAdaptiveInfo |
| 32 | +import androidx.compose.material3.adaptive.navigationsuite.ExperimentalMaterial3AdaptiveNavigationSuiteApi |
| 33 | +import androidx.compose.material3.adaptive.navigationsuite.NavigationSuiteDefaults |
| 34 | +import androidx.compose.material3.adaptive.navigationsuite.NavigationSuiteScaffold |
| 35 | +import androidx.compose.material3.adaptive.navigationsuite.NavigationSuiteScaffoldDefaults |
| 36 | +import androidx.compose.material3.adaptive.navigationsuite.NavigationSuiteType |
| 37 | +import androidx.compose.runtime.Composable |
| 38 | +import androidx.compose.runtime.getValue |
| 39 | +import androidx.compose.runtime.mutableStateOf |
| 40 | +import androidx.compose.runtime.saveable.rememberSaveable |
| 41 | +import androidx.compose.runtime.setValue |
| 42 | +import androidx.compose.ui.graphics.Color |
| 43 | +import androidx.compose.ui.graphics.vector.ImageVector |
| 44 | +import androidx.compose.ui.res.stringResource |
| 45 | +import androidx.window.core.layout.WindowWidthSizeClass |
| 46 | +import com.example.compose.snippets.R |
| 47 | + |
| 48 | +// [START android_compose_adaptivelayouts_sample_navigation_suite_scaffold_destinations] |
| 49 | +enum class AppDestinations( |
| 50 | + @StringRes val label: Int, |
| 51 | + val icon: ImageVector, |
| 52 | + @StringRes val contentDescription: Int |
| 53 | +) { |
| 54 | + HOME(R.string.home, Icons.Default.Home, R.string.home), |
| 55 | + FAVORITES(R.string.favorites, Icons.Default.Favorite, R.string.favorites), |
| 56 | + SHOPPING(R.string.shopping, Icons.Default.ShoppingCart, R.string.shopping), |
| 57 | + PROFILE(R.string.profile, Icons.Default.AccountBox, R.string.profile), |
| 58 | +} |
| 59 | +// [END android_compose_adaptivelayouts_sample_navigation_suite_scaffold_destinations] |
| 60 | + |
| 61 | +@Composable |
| 62 | +fun SampleNavigationSuiteScaffoldParts() { |
| 63 | + // [START android_compose_adaptivelayouts_sample_navigation_suite_scaffold_remember] |
| 64 | + var currentDestination by rememberSaveable { mutableStateOf(AppDestinations.HOME) } |
| 65 | + // [END android_compose_adaptivelayouts_sample_navigation_suite_scaffold_remember] |
| 66 | + |
| 67 | + // [START android_compose_adaptivelayouts_sample_navigation_suite_scaffold_items] |
| 68 | + NavigationSuiteScaffold( |
| 69 | + navigationSuiteItems = { |
| 70 | + AppDestinations.entries.forEach { |
| 71 | + item( |
| 72 | + icon = { |
| 73 | + Icon( |
| 74 | + it.icon, |
| 75 | + contentDescription = stringResource(it.contentDescription) |
| 76 | + ) |
| 77 | + }, |
| 78 | + label = { Text(stringResource(it.label)) }, |
| 79 | + selected = it == currentDestination, |
| 80 | + onClick = { currentDestination = it } |
| 81 | + ) |
| 82 | + } |
| 83 | + } |
| 84 | + ) { |
| 85 | + // TODO: Destination content. |
| 86 | + } |
| 87 | + // [END android_compose_adaptivelayouts_sample_navigation_suite_scaffold_items] |
| 88 | + |
| 89 | + // [START android_compose_adaptivelayouts_sample_navigation_suite_scaffold_content] |
| 90 | + NavigationSuiteScaffold( |
| 91 | + navigationSuiteItems = { /*...*/ } |
| 92 | + ) { |
| 93 | + // Destination content. |
| 94 | + when (currentDestination) { |
| 95 | + AppDestinations.HOME -> HomeDestination() |
| 96 | + AppDestinations.FAVORITES -> FavoritesDestination() |
| 97 | + AppDestinations.SHOPPING -> ShoppingDestination() |
| 98 | + AppDestinations.PROFILE -> ProfileDestination() |
| 99 | + } |
| 100 | + } |
| 101 | + // [END android_compose_adaptivelayouts_sample_navigation_suite_scaffold_content] |
| 102 | +} |
| 103 | + |
| 104 | +@Composable |
| 105 | +fun SampleNavigationSuiteScaffoldColors() { |
| 106 | + var currentDestination by rememberSaveable { mutableStateOf(AppDestinations.HOME) } |
| 107 | + |
| 108 | + // [START android_compose_adaptivelayouts_sample_navigation_suite_scaffold_container_color] |
| 109 | + NavigationSuiteScaffold( |
| 110 | + navigationSuiteItems = { /* ... */ }, |
| 111 | + containerColor = MaterialTheme.colorScheme.primary, |
| 112 | + contentColor = MaterialTheme.colorScheme.onPrimary, |
| 113 | + ) { |
| 114 | + // Content... |
| 115 | + } |
| 116 | + // [END android_compose_adaptivelayouts_sample_navigation_suite_scaffold_container_color] |
| 117 | + |
| 118 | + // [START android_compose_adaptivelayouts_sample_navigation_suite_scaffold_suite_colors] |
| 119 | + NavigationSuiteScaffold( |
| 120 | + navigationSuiteItems = { /* ... */ }, |
| 121 | + navigationSuiteColors = NavigationSuiteDefaults.colors( |
| 122 | + navigationBarContainerColor = Color.Transparent, |
| 123 | + ) |
| 124 | + ) { |
| 125 | + // Content... |
| 126 | + } |
| 127 | + // [END android_compose_adaptivelayouts_sample_navigation_suite_scaffold_suite_colors] |
| 128 | + |
| 129 | + // [START android_compose_adaptivelayouts_sample_navigation_suite_scaffold_item_colors] |
| 130 | + val myNavigationSuiteItemColors = NavigationSuiteDefaults.itemColors( |
| 131 | + navigationBarItemColors = NavigationBarItemDefaults.colors( |
| 132 | + indicatorColor = MaterialTheme.colorScheme.primaryContainer, |
| 133 | + selectedIconColor = MaterialTheme.colorScheme.onPrimaryContainer |
| 134 | + ), |
| 135 | + ) |
| 136 | + |
| 137 | + NavigationSuiteScaffold( |
| 138 | + navigationSuiteItems = { |
| 139 | + AppDestinations.entries.forEach { |
| 140 | + item( |
| 141 | + icon = { |
| 142 | + Icon( |
| 143 | + it.icon, |
| 144 | + contentDescription = stringResource(it.contentDescription) |
| 145 | + ) |
| 146 | + }, |
| 147 | + label = { Text(stringResource(it.label)) }, |
| 148 | + selected = it == currentDestination, |
| 149 | + onClick = { currentDestination = it }, |
| 150 | + colors = myNavigationSuiteItemColors, |
| 151 | + ) |
| 152 | + } |
| 153 | + }, |
| 154 | + ) { |
| 155 | + // Content... |
| 156 | + } |
| 157 | + // [END android_compose_adaptivelayouts_sample_navigation_suite_scaffold_item_colors] |
| 158 | +} |
| 159 | + |
| 160 | +@Composable |
| 161 | +fun SampleNavigationSuiteScaffoldCustomType() { |
| 162 | + // [START android_compose_adaptivelayouts_sample_navigation_suite_scaffold_layout_type] |
| 163 | + val adaptiveInfo = currentWindowAdaptiveInfo() |
| 164 | + val customNavSuiteType = with(adaptiveInfo) { |
| 165 | + if (windowSizeClass.windowWidthSizeClass == WindowWidthSizeClass.EXPANDED) { |
| 166 | + NavigationSuiteType.NavigationDrawer |
| 167 | + } else { |
| 168 | + NavigationSuiteScaffoldDefaults.calculateFromAdaptiveInfo(adaptiveInfo) |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + NavigationSuiteScaffold( |
| 173 | + navigationSuiteItems = { /* ... */ }, |
| 174 | + layoutType = customNavSuiteType, |
| 175 | + ) { |
| 176 | + // Content... |
| 177 | + } |
| 178 | + // [END android_compose_adaptivelayouts_sample_navigation_suite_scaffold_layout_type] |
| 179 | +} |
| 180 | + |
| 181 | +@Composable |
| 182 | +fun HomeDestination() {} |
| 183 | + |
| 184 | +@Composable |
| 185 | +fun FavoritesDestination() {} |
| 186 | + |
| 187 | +@Composable |
| 188 | +fun ShoppingDestination() {} |
| 189 | + |
| 190 | +@Composable |
| 191 | +fun ProfileDestination() {} |
0 commit comments