|
| 1 | +package com.kelsos.mbrc.app |
| 2 | + |
| 3 | +import androidx.compose.foundation.Image |
| 4 | +import androidx.compose.foundation.layout.Arrangement |
| 5 | +import androidx.compose.foundation.layout.Column |
| 6 | +import androidx.compose.foundation.layout.Row |
| 7 | +import androidx.compose.foundation.layout.Spacer |
| 8 | +import androidx.compose.foundation.layout.fillMaxSize |
| 9 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 10 | +import androidx.compose.foundation.layout.padding |
| 11 | +import androidx.compose.foundation.layout.width |
| 12 | +import androidx.compose.material.Divider |
| 13 | +import androidx.compose.material.MaterialTheme |
| 14 | +import androidx.compose.material.Surface |
| 15 | +import androidx.compose.material.Text |
| 16 | +import androidx.compose.material.TextButton |
| 17 | +import androidx.compose.material.icons.Icons |
| 18 | +import androidx.compose.material.icons.filled.Close |
| 19 | +import androidx.compose.material.icons.filled.Help |
| 20 | +import androidx.compose.material.icons.filled.Home |
| 21 | +import androidx.compose.material.icons.filled.LibraryMusic |
| 22 | +import androidx.compose.material.icons.filled.QueueMusic |
| 23 | +import androidx.compose.material.icons.filled.Radio |
| 24 | +import androidx.compose.material.icons.filled.Settings |
| 25 | +import androidx.compose.material.icons.filled.Speaker |
| 26 | +import androidx.compose.material.icons.filled.ViewHeadline |
| 27 | +import androidx.compose.material.icons.filled.ViewList |
| 28 | +import androidx.compose.runtime.Composable |
| 29 | +import androidx.compose.ui.Alignment |
| 30 | +import androidx.compose.ui.Modifier |
| 31 | +import androidx.compose.ui.graphics.Color |
| 32 | +import androidx.compose.ui.graphics.ColorFilter |
| 33 | +import androidx.compose.ui.graphics.vector.ImageVector |
| 34 | +import androidx.compose.ui.res.stringResource |
| 35 | +import androidx.compose.ui.tooling.preview.Preview |
| 36 | +import androidx.compose.ui.unit.dp |
| 37 | +import com.kelsos.mbrc.R |
| 38 | +import com.kelsos.mbrc.theme.RemoteTheme |
| 39 | + |
| 40 | +@Composable |
| 41 | +fun AppDrawer( |
| 42 | + currentRoute: String, |
| 43 | + navigateTo: (destination: Destination) -> Unit, |
| 44 | + closeDrawer: () -> Unit |
| 45 | +) { |
| 46 | + Column(modifier = Modifier.fillMaxSize()) { |
| 47 | + DrawerButton( |
| 48 | + icon = Icons.Filled.Home, |
| 49 | + label = stringResource(id = R.string.nav_home), |
| 50 | + isSelected = Destination.Home.matches(currentRoute), |
| 51 | + action = { navigateTo(Destination.Home) } |
| 52 | + ) |
| 53 | + DrawerButton( |
| 54 | + icon = Icons.Filled.LibraryMusic, |
| 55 | + label = stringResource(id = R.string.nav_library), |
| 56 | + isSelected = Destination.Library.matches(currentRoute), |
| 57 | + action = { navigateTo(Destination.Library) } |
| 58 | + ) |
| 59 | + DrawerButton( |
| 60 | + icon = Icons.Filled.ViewList, |
| 61 | + label = stringResource(id = R.string.nav_now_playing), |
| 62 | + isSelected = Destination.NowPlaying.matches(currentRoute), |
| 63 | + action = { navigateTo(Destination.NowPlaying) } |
| 64 | + ) |
| 65 | + DrawerButton( |
| 66 | + icon = Icons.Filled.QueueMusic, |
| 67 | + label = stringResource(id = R.string.nav_playlists), |
| 68 | + isSelected = Destination.Playlists.matches(currentRoute), |
| 69 | + action = { navigateTo(Destination.Playlists) } |
| 70 | + ) |
| 71 | + DrawerButton( |
| 72 | + icon = Icons.Filled.Radio, |
| 73 | + label = stringResource(id = R.string.nav_radio), |
| 74 | + isSelected = Destination.Radio.matches(currentRoute), |
| 75 | + action = { navigateTo(Destination.Radio) } |
| 76 | + ) |
| 77 | + DrawerButton( |
| 78 | + icon = Icons.Filled.ViewHeadline, |
| 79 | + label = stringResource(id = R.string.nav_lyrics), |
| 80 | + isSelected = Destination.Lyrics.matches(currentRoute), |
| 81 | + action = { navigateTo(Destination.Lyrics) } |
| 82 | + ) |
| 83 | + |
| 84 | + Divider(modifier = Modifier.padding(vertical = 8.dp)) |
| 85 | + |
| 86 | + Text( |
| 87 | + text = stringResource(id = R.string.nav_option_title), |
| 88 | + color = MaterialTheme.colors.onSurface.copy(alpha = 0.6f), |
| 89 | + style = MaterialTheme.typography.subtitle1, |
| 90 | + modifier = Modifier.padding(horizontal = 16.dp) |
| 91 | + ) |
| 92 | + |
| 93 | + DrawerButton( |
| 94 | + icon = Icons.Filled.Speaker, |
| 95 | + label = stringResource(id = R.string.nav_output), |
| 96 | + isSelected = Destination.OutputSelection.matches(currentRoute), |
| 97 | + action = { navigateTo(Destination.OutputSelection) } |
| 98 | + ) |
| 99 | + |
| 100 | + DrawerButton( |
| 101 | + icon = Icons.Filled.Settings, |
| 102 | + label = stringResource(id = R.string.nav_settings), |
| 103 | + isSelected = Destination.Settings.matches(currentRoute), |
| 104 | + action = { navigateTo(Destination.Settings) } |
| 105 | + ) |
| 106 | + |
| 107 | + DrawerButton( |
| 108 | + icon = Icons.Filled.Help, |
| 109 | + label = stringResource(id = R.string.nav_help), |
| 110 | + isSelected = Destination.Help.matches(currentRoute), |
| 111 | + action = { navigateTo(Destination.Help) } |
| 112 | + ) |
| 113 | + |
| 114 | + DrawerButton( |
| 115 | + icon = Icons.Filled.Close, |
| 116 | + label = stringResource(id = R.string.nav_exit), |
| 117 | + isSelected = false, |
| 118 | + action = { TODO("implement exit") } |
| 119 | + ) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +@Composable |
| 124 | +private fun DrawerButton( |
| 125 | + icon: ImageVector, |
| 126 | + label: String, |
| 127 | + isSelected: Boolean, |
| 128 | + action: () -> Unit, |
| 129 | + modifier: Modifier = Modifier |
| 130 | +) { |
| 131 | + val colors = MaterialTheme.colors |
| 132 | + val imageAlpha = if (isSelected) { |
| 133 | + 1f |
| 134 | + } else { |
| 135 | + 0.6f |
| 136 | + } |
| 137 | + val textIconColor = if (isSelected) { |
| 138 | + colors.primary |
| 139 | + } else { |
| 140 | + colors.onSurface.copy(alpha = 0.6f) |
| 141 | + } |
| 142 | + val backgroundColor = if (isSelected) { |
| 143 | + colors.primary.copy(alpha = 0.12f) |
| 144 | + } else { |
| 145 | + Color.Transparent |
| 146 | + } |
| 147 | + |
| 148 | + val surfaceModifier = modifier |
| 149 | + .padding(start = 8.dp, top = 8.dp, end = 8.dp) |
| 150 | + .fillMaxWidth() |
| 151 | + Surface( |
| 152 | + modifier = surfaceModifier, |
| 153 | + color = backgroundColor, |
| 154 | + shape = MaterialTheme.shapes.small |
| 155 | + ) { |
| 156 | + TextButton( |
| 157 | + onClick = action, |
| 158 | + modifier = Modifier.fillMaxWidth() |
| 159 | + ) { |
| 160 | + Row( |
| 161 | + horizontalArrangement = Arrangement.Start, |
| 162 | + verticalAlignment = Alignment.CenterVertically, |
| 163 | + modifier = Modifier.fillMaxWidth() |
| 164 | + ) { |
| 165 | + Image( |
| 166 | + imageVector = icon, |
| 167 | + contentDescription = null, // decorative |
| 168 | + colorFilter = ColorFilter.tint(textIconColor), |
| 169 | + alpha = imageAlpha |
| 170 | + ) |
| 171 | + Spacer(Modifier.width(16.dp)) |
| 172 | + Text( |
| 173 | + text = label, |
| 174 | + style = MaterialTheme.typography.body2, |
| 175 | + color = textIconColor |
| 176 | + ) |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +@Preview |
| 183 | +@Composable |
| 184 | +fun RemoteDrawerPreview() { |
| 185 | + RemoteTheme { |
| 186 | + AppDrawer(Destination.Home.route, {}, {}) |
| 187 | + } |
| 188 | +} |
0 commit comments