-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMainMenuSheet.kt
More file actions
161 lines (149 loc) · 5.39 KB
/
MainMenuSheet.kt
File metadata and controls
161 lines (149 loc) · 5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
* DepNav -- department navigator.
* Copyright (C) 2022 Timofei Pushkin
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package ru.spbu.depnav.ui.component
import android.annotation.SuppressLint
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Info
import androidx.compose.material.icons.rounded.Settings
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.ModalDrawerSheet
import androidx.compose.material3.NavigationDrawerItem
import androidx.compose.material3.NavigationDrawerItemDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import ru.spbu.depnav.R
import ru.spbu.depnav.ui.viewmodel.AvailableMap
import androidx.compose.ui.platform.LocalResources
// https://m3.material.io/components/navigation-drawer/specs#368147de-9661-4a28-9fc1-ce2f8c9eac40
private val ITEM_HEIGHT = 56.dp
private val ITEM_HORIZONTAL_PADDING = 28.dp
private val DIVIDER_VERTICAL_PADDING = 16.dp
private val ICON_SIZE = 24.dp
/** [ModalDrawerSheet] for the main menu. */
@Composable
fun MainMenuSheet(
selectedMapId: Int?,
availableMaps: Collection<AvailableMap>,
onMapSelected: (Int) -> Unit,
onSettingsClick: () -> Unit,
onMapLegendClick: () -> Unit
) {
ModalDrawerSheet {
AppTitle()
MapItems(selectedMapId, availableMaps, onMapSelected)
HorizontalDivider(
modifier = Modifier.padding(
horizontal = ITEM_HORIZONTAL_PADDING,
vertical = DIVIDER_VERTICAL_PADDING
)
)
MiscItem(
icon = {
Icon(
Icons.Rounded.Settings,
contentDescription = stringResource(R.string.label_open_settings)
)
},
labelText = stringResource(R.string.settings),
onClick = onSettingsClick
)
MiscItem(
icon = {
Icon(
Icons.Rounded.Info,
contentDescription = stringResource(R.string.label_open_map_legend)
)
},
labelText = stringResource(R.string.map_legend),
onClick = onMapLegendClick
)
}
}
@Composable
private fun AppTitle() {
Box(
contentAlignment = Alignment.CenterStart,
modifier = Modifier
.height(ITEM_HEIGHT)
.padding(horizontal = ITEM_HORIZONTAL_PADDING)
) {
Text(stringResource(R.string.app_name), style = MaterialTheme.typography.titleMedium)
}
}
@Composable
private fun ColumnScope.MapItems(
selectedMapId: Int?,
availableMaps: Collection<AvailableMap>,
onMapSelected: (Int) -> Unit
) {
AnimatedVisibility(visible = availableMaps.isNotEmpty()) {
Column {
for ((mapId, internalMapName, mapTitle) in availableMaps) {
NavigationDrawerItem(
icon = {
MapLogo(
internalMapName,
contentDescription = "${stringResource(R.string.label_open_map)} $mapTitle"
)
},
label = { Text(mapTitle) },
selected = mapId == selectedMapId,
onClick = { onMapSelected(mapId) },
modifier = Modifier.padding(NavigationDrawerItemDefaults.ItemPadding)
)
}
}
}
}
@Composable
private fun MapLogo(internalMapName: String, contentDescription: String) {
@SuppressLint("DiscouragedApi")
val logoId = LocalResources.current.getIdentifier(
"logo_${internalMapName.replace('-', '_')}", "drawable", LocalContext.current.packageName
)
Icon(
painterResource(logoId),
contentDescription,
modifier = Modifier.size(ICON_SIZE)
)
}
@Composable
private fun MiscItem(icon: @Composable () -> Unit, labelText: String, onClick: () -> Unit) {
NavigationDrawerItem(
icon = icon,
label = { Text(labelText) },
selected = false,
onClick = onClick,
modifier = Modifier.padding(NavigationDrawerItemDefaults.ItemPadding)
)
}