-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDrawerMenu.kt
More file actions
323 lines (301 loc) · 10.3 KB
/
DrawerMenu.kt
File metadata and controls
323 lines (301 loc) · 10.3 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package to.bitkit.ui.components
import androidx.annotation.DrawableRes
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.slideInHorizontally
import androidx.compose.animation.slideOutHorizontally
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.systemBarsPadding
import androidx.compose.foundation.layout.width
import androidx.compose.material3.DrawerState
import androidx.compose.material3.DrawerValue
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.material3.rememberDrawerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.zIndex
import androidx.navigation.NavController
import androidx.navigation.NavDestination.Companion.hasRoute
import androidx.navigation.compose.rememberNavController
import kotlinx.coroutines.launch
import to.bitkit.R
import to.bitkit.ui.Routes
import to.bitkit.ui.navigateTo
import to.bitkit.ui.navigateToHome
import to.bitkit.ui.shared.modifiers.clickableAlpha
import to.bitkit.ui.shared.util.blockPointerInputPassthrough
import to.bitkit.ui.theme.AppThemeSurface
import to.bitkit.ui.theme.Colors
import to.bitkit.ui.theme.InterFontFamily
private inline fun <reified T : Any> NavController.navigateIfNotCurrent(route: T) {
if (currentBackStackEntry?.destination?.hasRoute<T>() != true) {
navigateTo(route)
}
}
private const val Z_INDEX_SCRIM = 10f
private const val Z_INDEX_MENU = 11f
private val bgScrim = Colors.Black50
private val drawerBg = Colors.Brand
private val drawerWidth = 200.dp
@Composable
fun DrawerMenu(
drawerState: DrawerState,
rootNavController: NavController,
hasSeenWidgetsIntro: Boolean,
hasSeenShopIntro: Boolean,
modifier: Modifier = Modifier,
) {
val scope = rememberCoroutineScope()
Scrim(
visible = drawerState.currentValue == DrawerValue.Open,
onClick = {
scope.launch {
drawerState.close()
}
},
modifier = Modifier
.fillMaxSize()
.zIndex(Z_INDEX_SCRIM)
)
AnimatedVisibility(
visible = drawerState.currentValue == DrawerValue.Open,
enter = slideInHorizontally(
initialOffsetX = { it }
),
exit = slideOutHorizontally(
targetOffsetX = { it }
),
modifier = modifier.then(
Modifier
.fillMaxHeight()
.zIndex(Z_INDEX_MENU)
.blockPointerInputPassthrough()
)
) {
Menu(
rootNavController = rootNavController,
drawerState = drawerState,
onClickAddWidget = {
if (!hasSeenWidgetsIntro) {
rootNavController.navigateIfNotCurrent(Routes.WidgetsIntro)
} else {
rootNavController.navigateIfNotCurrent(Routes.AddWidget)
}
},
onClickShop = {
if (!hasSeenShopIntro) {
rootNavController.navigateIfNotCurrent(Routes.ShopIntro)
} else {
rootNavController.navigateIfNotCurrent(Routes.ShopDiscover)
}
},
)
}
}
@Composable
private fun Menu(
rootNavController: NavController,
drawerState: DrawerState,
onClickAddWidget: () -> Unit,
onClickShop: () -> Unit,
) {
val scope = rememberCoroutineScope()
Column(
modifier = Modifier
.width(drawerWidth)
.fillMaxHeight()
.background(drawerBg)
.systemBarsPadding()
) {
VerticalSpacer(16.dp)
DrawerItem(
label = stringResource(R.string.wallet__drawer__wallet),
iconRes = R.drawable.ic_coins,
onClick = {
val isInHome = rootNavController.currentBackStackEntry?.destination?.hasRoute<Routes.Home>() ?: false
if (!isInHome) rootNavController.navigateToHome()
scope.launch { drawerState.close() }
},
modifier = Modifier.testTag("DrawerWallet")
)
DrawerItem(
label = stringResource(R.string.wallet__drawer__activity),
iconRes = R.drawable.ic_heartbeat,
onClick = {
rootNavController.navigateIfNotCurrent(Routes.AllActivity)
scope.launch { drawerState.close() }
},
modifier = Modifier.testTag("DrawerActivity")
)
DrawerItem(
label = stringResource(R.string.wallet__drawer__contacts),
iconRes = R.drawable.ic_users,
onClick = {
rootNavController.navigateIfNotCurrent(Routes.Contacts)
scope.launch { drawerState.close() }
},
modifier = Modifier.testTag("DrawerContacts")
)
DrawerItem(
label = stringResource(R.string.wallet__drawer__profile),
iconRes = R.drawable.ic_user_square,
onClick = {
rootNavController.navigateIfNotCurrent(Routes.Profile)
scope.launch { drawerState.close() }
},
modifier = Modifier.testTag("DrawerProfile")
)
DrawerItem(
label = stringResource(R.string.wallet__drawer__widgets),
iconRes = R.drawable.ic_stack,
onClick = {
onClickAddWidget()
scope.launch { drawerState.close() }
},
modifier = Modifier.testTag("DrawerWidgets")
)
DrawerItem(
label = stringResource(R.string.wallet__drawer__shop),
iconRes = R.drawable.ic_store_front,
onClick = {
onClickShop()
scope.launch { drawerState.close() }
},
modifier = Modifier.testTag("DrawerShop")
)
DrawerItem(
label = stringResource(R.string.wallet__drawer__support),
iconRes = R.drawable.ic_chats_circle,
onClick = {
rootNavController.navigateIfNotCurrent(Routes.Support)
scope.launch { drawerState.close() }
},
modifier = Modifier.testTag("DrawerSupport")
)
DrawerItem(
label = stringResource(R.string.wallet__drawer__settings),
iconRes = R.drawable.ic_settings,
onClick = {
rootNavController.navigateIfNotCurrent(Routes.Settings)
scope.launch { drawerState.close() }
},
modifier = Modifier.testTag("DrawerSettings")
)
FillHeight()
Box(
contentAlignment = Alignment.Center,
modifier = Modifier
.fillMaxWidth()
.clickableAlpha {
rootNavController.navigateIfNotCurrent(Routes.AppStatus)
scope.launch { drawerState.close() }
}
) {
AppStatus(
showText = true,
showReady = true,
color = Colors.Black,
modifier = Modifier
.padding(vertical = 16.dp)
.testTag("DrawerAppStatus")
)
}
}
}
@Composable
private fun Scrim(
visible: Boolean,
onClick: () -> Unit,
modifier: Modifier = Modifier,
) {
AnimatedVisibility(
visible = visible,
modifier = modifier
) {
Box(
modifier = Modifier
.fillMaxSize()
.background(bgScrim)
.clickableAlpha(pressedAlpha = 1f, onClick = onClick)
)
}
}
@Composable
private fun DrawerItem(
label: String,
@DrawableRes iconRes: Int,
modifier: Modifier = Modifier,
onClick: (() -> Unit)? = null,
) {
Column(
modifier = modifier
.clickableAlpha(onClick = onClick)
.padding(horizontal = 16.dp)
) {
VerticalSpacer(16.dp)
Row(
horizontalArrangement = Arrangement.spacedBy(12.dp),
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier.fillMaxWidth()
) {
Icon(
painter = painterResource(iconRes),
contentDescription = null,
modifier = Modifier.size(24.dp)
)
Text(
text = label.uppercase(),
style = TextStyle(
fontWeight = FontWeight.Black,
fontSize = 24.sp,
lineHeight = 24.sp,
letterSpacing = (-1).sp,
fontFamily = InterFontFamily,
color = Colors.White,
),
maxLines = 1,
overflow = TextOverflow.Ellipsis,
modifier = Modifier.weight(1f)
)
}
VerticalSpacer(16.dp)
HorizontalDivider()
}
}
@Preview(showSystemUi = true)
@Composable
private fun Preview() {
AppThemeSurface {
val navController = rememberNavController()
Box {
DrawerMenu(
rootNavController = navController,
drawerState = rememberDrawerState(initialValue = DrawerValue.Open),
hasSeenWidgetsIntro = false,
hasSeenShopIntro = false,
modifier = Modifier.align(Alignment.TopEnd),
)
}
}
}