|
| 1 | +/* |
| 2 | + * Designed and developed by Cavin |
| 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 | +package com.cavin.material3expressivecatalog.components.widenavigationrail |
| 17 | + |
| 18 | +import android.annotation.SuppressLint |
| 19 | +import android.app.Activity |
| 20 | +import android.content.pm.ActivityInfo |
| 21 | +import androidx.compose.foundation.layout.Column |
| 22 | +import androidx.compose.foundation.layout.Row |
| 23 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 24 | +import androidx.compose.foundation.layout.padding |
| 25 | +import androidx.compose.material.icons.Icons |
| 26 | +import androidx.compose.material.icons.automirrored.filled.MenuOpen |
| 27 | +import androidx.compose.material.icons.filled.Favorite |
| 28 | +import androidx.compose.material.icons.filled.Home |
| 29 | +import androidx.compose.material.icons.filled.Menu |
| 30 | +import androidx.compose.material.icons.filled.Star |
| 31 | +import androidx.compose.material.icons.outlined.FavoriteBorder |
| 32 | +import androidx.compose.material.icons.outlined.Home |
| 33 | +import androidx.compose.material.icons.outlined.StarBorder |
| 34 | +import androidx.compose.material3.Icon |
| 35 | +import androidx.compose.material3.IconButton |
| 36 | +import androidx.compose.material3.Text |
| 37 | +import androidx.compose.material3.WideNavigationRail |
| 38 | +import androidx.compose.material3.WideNavigationRailItem |
| 39 | +import androidx.compose.material3.WideNavigationRailValue |
| 40 | +import androidx.compose.material3.rememberWideNavigationRailState |
| 41 | +import androidx.compose.runtime.Composable |
| 42 | +import androidx.compose.runtime.DisposableEffect |
| 43 | +import androidx.compose.runtime.getValue |
| 44 | +import androidx.compose.runtime.mutableIntStateOf |
| 45 | +import androidx.compose.runtime.remember |
| 46 | +import androidx.compose.runtime.rememberCoroutineScope |
| 47 | +import androidx.compose.runtime.setValue |
| 48 | +import androidx.compose.ui.Modifier |
| 49 | +import androidx.compose.ui.platform.LocalContext |
| 50 | +import androidx.compose.ui.semantics.semantics |
| 51 | +import androidx.compose.ui.semantics.stateDescription |
| 52 | +import androidx.compose.ui.unit.dp |
| 53 | +import kotlinx.coroutines.launch |
| 54 | + |
| 55 | +@SuppressLint("SourceLockedOrientationActivity") |
| 56 | +@Composable |
| 57 | +fun WideNavigationRail() { |
| 58 | + var selectedItem by remember { mutableIntStateOf(0) } |
| 59 | + val items = listOf("Home", "Search", "Settings") |
| 60 | + val selectedIcons = listOf(Icons.Filled.Home, Icons.Filled.Favorite, Icons.Filled.Star) |
| 61 | + val unselectedIcons = |
| 62 | + listOf(Icons.Outlined.Home, Icons.Outlined.FavoriteBorder, Icons.Outlined.StarBorder) |
| 63 | + val state = rememberWideNavigationRailState() |
| 64 | + val scope = rememberCoroutineScope() |
| 65 | + |
| 66 | + Row(Modifier.fillMaxWidth()) { |
| 67 | + WideNavigationRail( |
| 68 | + state = state, |
| 69 | + header = { |
| 70 | + IconButton( |
| 71 | + modifier = |
| 72 | + Modifier |
| 73 | + .padding(start = 24.dp) |
| 74 | + .semantics { |
| 75 | + // The button must announce the expanded or collapsed state of the rail |
| 76 | + // for accessibility. |
| 77 | + stateDescription = |
| 78 | + if (state.currentValue == WideNavigationRailValue.Expanded) { |
| 79 | + "Expanded" |
| 80 | + } else { |
| 81 | + "Collapsed" |
| 82 | + } |
| 83 | + }, |
| 84 | + onClick = { |
| 85 | + scope.launch { |
| 86 | + if (state.targetValue == WideNavigationRailValue.Expanded) { |
| 87 | + state.collapse() |
| 88 | + } else { |
| 89 | + state.expand() |
| 90 | + } |
| 91 | + } |
| 92 | + }, |
| 93 | + ) { |
| 94 | + if (state.targetValue == WideNavigationRailValue.Expanded) { |
| 95 | + Icon( |
| 96 | + Icons.AutoMirrored.Filled.MenuOpen, |
| 97 | + "Collapse rail", |
| 98 | + ) |
| 99 | + } else { |
| 100 | + Icon(Icons.Filled.Menu, "Expand rail") |
| 101 | + } |
| 102 | + } |
| 103 | + }, |
| 104 | + ) { |
| 105 | + items.forEachIndexed { index, item -> |
| 106 | + WideNavigationRailItem( |
| 107 | + railExpanded = state.targetValue == WideNavigationRailValue.Expanded, |
| 108 | + icon = { |
| 109 | + val imageVector = |
| 110 | + if (selectedItem == index) { |
| 111 | + selectedIcons[index] |
| 112 | + } else { |
| 113 | + unselectedIcons[index] |
| 114 | + } |
| 115 | + Icon(imageVector = imageVector, contentDescription = null) |
| 116 | + }, |
| 117 | + label = { Text(item) }, |
| 118 | + selected = selectedItem == index, |
| 119 | + onClick = { selectedItem = index }, |
| 120 | + ) |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + val textString = |
| 125 | + if (state.currentValue == WideNavigationRailValue.Expanded) { |
| 126 | + "Expanded" |
| 127 | + } else { |
| 128 | + "Collapsed" |
| 129 | + } |
| 130 | + Column { |
| 131 | + Text(modifier = Modifier.padding(16.dp), text = "Is animating: " + state.isAnimating) |
| 132 | + Text(modifier = Modifier.padding(16.dp), text = "The rail is $textString.") |
| 133 | + Text( |
| 134 | + modifier = Modifier.padding(16.dp), |
| 135 | + text = |
| 136 | + "Note: The orientation of this demo has been locked to portrait mode, because" + |
| 137 | + " landscape mode may result in a compact height in certain devices. For" + |
| 138 | + " any compact screen dimensions, use a Navigation Bar instead.", |
| 139 | + ) |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | +// Lock the orientation for this demo as the navigation rail may look cut off in landscape in |
| 144 | +// smaller screens. |
| 145 | + val context = LocalContext.current |
| 146 | + DisposableEffect(context) { |
| 147 | + (context as? Activity)?.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT |
| 148 | + onDispose { |
| 149 | + (context as? Activity)?.requestedOrientation = |
| 150 | + ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments