|
| 1 | +package org.nsh07.nsh07.ui |
| 2 | + |
| 3 | +import androidx.compose.animation.AnimatedContent |
| 4 | +import androidx.compose.animation.Crossfade |
| 5 | +import androidx.compose.animation.core.animateFloatAsState |
| 6 | +import androidx.compose.animation.fadeIn |
| 7 | +import androidx.compose.animation.fadeOut |
| 8 | +import androidx.compose.animation.togetherWith |
| 9 | +import androidx.compose.foundation.layout.Row |
| 10 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 11 | +import androidx.compose.foundation.layout.padding |
| 12 | +import androidx.compose.material3.Icon |
| 13 | +import androidx.compose.material3.IconButton |
| 14 | +import androidx.compose.material3.MaterialTheme.typography |
| 15 | +import androidx.compose.material3.ModalWideNavigationRail |
| 16 | +import androidx.compose.material3.Text |
| 17 | +import androidx.compose.material3.WideNavigationRail |
| 18 | +import androidx.compose.material3.WideNavigationRailItem |
| 19 | +import androidx.compose.material3.WideNavigationRailValue |
| 20 | +import androidx.compose.material3.rememberWideNavigationRailState |
| 21 | +import androidx.compose.material3.windowsizeclass.ExperimentalMaterial3WindowSizeClassApi |
| 22 | +import androidx.compose.material3.windowsizeclass.WindowWidthSizeClass |
| 23 | +import androidx.compose.material3.windowsizeclass.calculateWindowSizeClass |
| 24 | +import androidx.compose.runtime.Composable |
| 25 | +import androidx.compose.runtime.LaunchedEffect |
| 26 | +import androidx.compose.runtime.getValue |
| 27 | +import androidx.compose.runtime.mutableStateOf |
| 28 | +import androidx.compose.runtime.remember |
| 29 | +import androidx.compose.runtime.rememberCoroutineScope |
| 30 | +import androidx.compose.runtime.setValue |
| 31 | +import androidx.compose.ui.Modifier |
| 32 | +import androidx.compose.ui.draw.rotate |
| 33 | +import androidx.compose.ui.semantics.semantics |
| 34 | +import androidx.compose.ui.semantics.stateDescription |
| 35 | +import androidx.compose.ui.unit.dp |
| 36 | +import androidx.compose.ui.util.fastForEach |
| 37 | +import kotlinx.coroutines.launch |
| 38 | +import nsh07.composeapp.generated.resources.Res |
| 39 | +import nsh07.composeapp.generated.resources.filled_home |
| 40 | +import nsh07.composeapp.generated.resources.filled_info |
| 41 | +import nsh07.composeapp.generated.resources.menu |
| 42 | +import nsh07.composeapp.generated.resources.menu_open |
| 43 | +import nsh07.composeapp.generated.resources.outline_home |
| 44 | +import nsh07.composeapp.generated.resources.outline_info |
| 45 | +import org.jetbrains.compose.resources.painterResource |
| 46 | +import org.nsh07.nsh07.ui.about.AboutScreen |
| 47 | +import org.nsh07.nsh07.ui.home.HomeScreen |
| 48 | + |
| 49 | +@OptIn(ExperimentalMaterial3WindowSizeClassApi::class) |
| 50 | +@Composable |
| 51 | +fun AppScreen(modifier: Modifier = Modifier) { |
| 52 | + var currentPortfolioScreen by remember { mutableStateOf(PortfolioScreen.HOME) } |
| 53 | + |
| 54 | + val windowSizeClass = calculateWindowSizeClass() |
| 55 | + val state = rememberWideNavigationRailState(WideNavigationRailValue.Collapsed) |
| 56 | + val scope = rememberCoroutineScope() |
| 57 | + |
| 58 | + val railItems = remember { |
| 59 | + listOf( |
| 60 | + RailItem( |
| 61 | + "Home", |
| 62 | + Res.drawable.outline_home, |
| 63 | + Res.drawable.filled_home, |
| 64 | + PortfolioScreen.HOME |
| 65 | + ), |
| 66 | + RailItem( |
| 67 | + "About me", |
| 68 | + Res.drawable.outline_info, |
| 69 | + Res.drawable.filled_info, |
| 70 | + PortfolioScreen.ABOUT |
| 71 | + ), |
| 72 | + ) |
| 73 | + } |
| 74 | + |
| 75 | + Row(modifier.fillMaxWidth()) { |
| 76 | + when (windowSizeClass.widthSizeClass) { |
| 77 | + WindowWidthSizeClass.Expanded -> { |
| 78 | + LaunchedEffect(Unit) { |
| 79 | + state.expand() |
| 80 | + } |
| 81 | + WideNavigationRail( |
| 82 | + state = state |
| 83 | + ) { |
| 84 | + railItems.fastForEach { |
| 85 | + val selected = it.portfolioScreen == currentPortfolioScreen |
| 86 | + WideNavigationRailItem( |
| 87 | + selected = selected, |
| 88 | + onClick = { currentPortfolioScreen = it.portfolioScreen }, |
| 89 | + icon = { |
| 90 | + Crossfade(selected) { targetState -> |
| 91 | + when (targetState) { |
| 92 | + true -> Icon(painterResource(it.selectedIcon), null) |
| 93 | + else -> Icon(painterResource(it.unselectedIcon), null) |
| 94 | + } |
| 95 | + } |
| 96 | + }, |
| 97 | + label = { Text(it.name, style = typography.labelLarge) }, |
| 98 | + railExpanded = state.targetValue == WideNavigationRailValue.Expanded |
| 99 | + ) |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + WindowWidthSizeClass.Medium -> { |
| 105 | + WideNavigationRail( |
| 106 | + state = state, |
| 107 | + header = { |
| 108 | + val rotation by animateFloatAsState(if (state.targetValue == WideNavigationRailValue.Expanded) 0f else 180f) |
| 109 | + IconButton( |
| 110 | + modifier = |
| 111 | + Modifier |
| 112 | + .padding(start = 24.dp) |
| 113 | + .rotate(rotation) |
| 114 | + .semantics { |
| 115 | + stateDescription = |
| 116 | + if (state.currentValue == WideNavigationRailValue.Expanded) { |
| 117 | + "Expanded" |
| 118 | + } else { |
| 119 | + "Collapsed" |
| 120 | + } |
| 121 | + }, |
| 122 | + onClick = { |
| 123 | + scope.launch { |
| 124 | + if (state.targetValue == WideNavigationRailValue.Expanded) |
| 125 | + state.collapse() |
| 126 | + else state.expand() |
| 127 | + } |
| 128 | + }, |
| 129 | + ) { |
| 130 | + Crossfade(state.targetValue) { |
| 131 | + when (it) { |
| 132 | + WideNavigationRailValue.Expanded -> Icon( |
| 133 | + painterResource(Res.drawable.menu_open), |
| 134 | + "Collapse rail" |
| 135 | + ) |
| 136 | + |
| 137 | + WideNavigationRailValue.Collapsed -> Icon( |
| 138 | + painterResource(Res.drawable.menu), |
| 139 | + "Expand rail" |
| 140 | + ) |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + ) { |
| 146 | + railItems.fastForEach { |
| 147 | + val selected = it.portfolioScreen == currentPortfolioScreen |
| 148 | + WideNavigationRailItem( |
| 149 | + selected = selected, |
| 150 | + onClick = { |
| 151 | + scope.launch { |
| 152 | + currentPortfolioScreen = it.portfolioScreen |
| 153 | + state.collapse() |
| 154 | + } |
| 155 | + }, |
| 156 | + icon = { |
| 157 | + Crossfade(selected) { targetState -> |
| 158 | + when (targetState) { |
| 159 | + true -> Icon(painterResource(it.selectedIcon), null) |
| 160 | + else -> Icon(painterResource(it.unselectedIcon), null) |
| 161 | + } |
| 162 | + } |
| 163 | + }, |
| 164 | + label = { Text(it.name, style = typography.labelLarge) }, |
| 165 | + railExpanded = state.targetValue == WideNavigationRailValue.Expanded |
| 166 | + ) |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + WindowWidthSizeClass.Compact -> { |
| 172 | + ModalWideNavigationRail( |
| 173 | + state = state, |
| 174 | + hideOnCollapse = true |
| 175 | + ) { |
| 176 | + railItems.fastForEach { |
| 177 | + val selected = it.portfolioScreen == currentPortfolioScreen |
| 178 | + WideNavigationRailItem( |
| 179 | + selected = selected, |
| 180 | + onClick = { |
| 181 | + scope.launch { |
| 182 | + currentPortfolioScreen = it.portfolioScreen |
| 183 | + state.collapse() |
| 184 | + } |
| 185 | + }, |
| 186 | + icon = { |
| 187 | + Crossfade(selected) { targetState -> |
| 188 | + when (targetState) { |
| 189 | + true -> Icon(painterResource(it.selectedIcon), null) |
| 190 | + else -> Icon(painterResource(it.unselectedIcon), null) |
| 191 | + } |
| 192 | + } |
| 193 | + }, |
| 194 | + label = { Text(it.name, style = typography.labelLarge) }, |
| 195 | + railExpanded = state.targetValue == WideNavigationRailValue.Expanded |
| 196 | + ) |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + AnimatedContent( |
| 203 | + currentPortfolioScreen, |
| 204 | + transitionSpec = { fadeIn().togetherWith(fadeOut()) } |
| 205 | + ) { |
| 206 | + when (it) { |
| 207 | + PortfolioScreen.HOME -> HomeScreen( |
| 208 | + expandRail = { scope.launch { state.expand() } }, |
| 209 | + windowWidthClass = windowSizeClass.widthSizeClass |
| 210 | + ) |
| 211 | + |
| 212 | + PortfolioScreen.ABOUT -> AboutScreen( |
| 213 | + expandRail = { scope.launch { state.expand() } }, |
| 214 | + windowWidthClass = windowSizeClass.widthSizeClass |
| 215 | + ) |
| 216 | + |
| 217 | + PortfolioScreen.PROJECTS -> {} |
| 218 | + } |
| 219 | + } |
| 220 | + } |
| 221 | +} |
0 commit comments