generated from bitwarden/template
-
Notifications
You must be signed in to change notification settings - Fork 9
/
AuthenticatorNavBarScreen.kt
337 lines (318 loc) · 12.9 KB
/
AuthenticatorNavBarScreen.kt
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package com.bitwarden.authenticator.ui.authenticator.feature.navbar
import android.os.Parcelable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.consumeWindowInsets
import androidx.compose.foundation.layout.exclude
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.ime
import androidx.compose.foundation.layout.navigationBars
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.statusBars
import androidx.compose.material3.BottomAppBar
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.NavigationBarItemDefaults
import androidx.compose.material3.ScaffoldDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.semantics.testTag
import androidx.compose.ui.text.style.TextOverflow
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.NavHostController
import androidx.navigation.NavOptions
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navOptions
import com.bitwarden.authenticator.R
import com.bitwarden.authenticator.ui.authenticator.feature.itemlisting.ITEM_LISTING_GRAPH_ROUTE
import com.bitwarden.authenticator.ui.authenticator.feature.itemlisting.ITEM_LIST_ROUTE
import com.bitwarden.authenticator.ui.authenticator.feature.itemlisting.itemListingGraph
import com.bitwarden.authenticator.ui.authenticator.feature.itemlisting.navigateToItemListGraph
import com.bitwarden.authenticator.ui.platform.base.util.EventsEffect
import com.bitwarden.authenticator.ui.platform.base.util.max
import com.bitwarden.authenticator.ui.platform.base.util.toDp
import com.bitwarden.authenticator.ui.platform.components.scaffold.BitwardenScaffold
import com.bitwarden.authenticator.ui.platform.components.scrim.BitwardenAnimatedScrim
import com.bitwarden.authenticator.ui.platform.feature.settings.SETTINGS_GRAPH_ROUTE
import com.bitwarden.authenticator.ui.platform.feature.settings.navigateToSettingsGraph
import com.bitwarden.authenticator.ui.platform.theme.RootTransitionProviders
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.parcelize.Parcelize
/**
* Top level composable for the authenticator screens.
*/
@Composable
fun AuthenticatorNavBarScreen(
viewModel: AuthenticatorNavBarViewModel = hiltViewModel(),
navController: NavHostController = rememberNavController(),
onNavigateBack: () -> Unit,
onNavigateToSearch: () -> Unit,
onNavigateToQrCodeScanner: () -> Unit,
onNavigateToManualKeyEntry: () -> Unit,
onNavigateToEditItem: (itemId: String) -> Unit,
onNavigateToExport: () -> Unit,
onNavigateToTutorial: () -> Unit,
) {
EventsEffect(viewModel = viewModel) { event ->
navController.apply {
val navOptions = navController.authenticatorNavBarScreenNavOptions()
when (event) {
AuthenticatorNavBarEvent.NavigateToSettings -> {
navigateToSettingsGraph(navOptions)
}
AuthenticatorNavBarEvent.NavigateToVerificationCodes -> {
navigateToItemListGraph(navOptions)
}
}
}
}
LaunchedEffect(Unit) {
navController
.currentBackStackEntryFlow
.onEach {
viewModel.trySendAction(AuthenticatorNavBarAction.BackStackUpdate)
}
.launchIn(this)
}
AuthenticatorNavBarScaffold(
navController = navController,
verificationTabClickedAction = {
viewModel.trySendAction(AuthenticatorNavBarAction.VerificationCodesTabClick)
},
settingsTabClickedAction = {
viewModel.trySendAction(AuthenticatorNavBarAction.SettingsTabClick)
},
navigateBack = onNavigateBack,
navigateToSearch = onNavigateToSearch,
navigateToQrCodeScanner = onNavigateToQrCodeScanner,
navigateToManualKeyEntry = onNavigateToManualKeyEntry,
navigateToEditItem = onNavigateToEditItem,
navigateToExport = onNavigateToExport,
navigateToTutorial = onNavigateToTutorial,
)
}
@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun AuthenticatorNavBarScaffold(
navController: NavHostController,
verificationTabClickedAction: () -> Unit,
settingsTabClickedAction: () -> Unit,
navigateBack: () -> Unit,
navigateToSearch: () -> Unit,
navigateToQrCodeScanner: () -> Unit,
navigateToManualKeyEntry: () -> Unit,
navigateToEditItem: (itemId: String) -> Unit,
navigateToExport: () -> Unit,
navigateToTutorial: () -> Unit,
) {
BitwardenScaffold(
contentWindowInsets = ScaffoldDefaults.contentWindowInsets.exclude(WindowInsets.statusBars),
bottomBar = {
Box {
var appBarHeightPx by remember { mutableIntStateOf(0) }
AuthenticatorBottomAppBar(
modifier = Modifier
.onGloballyPositioned {
appBarHeightPx = it.size.height
},
navController = navController,
verificationCodesTabClickedAction = verificationTabClickedAction,
settingsTabClickedAction = settingsTabClickedAction,
)
BitwardenAnimatedScrim(
isVisible = false,
onClick = {
// Do nothing
},
modifier = Modifier
.fillMaxWidth()
.height(appBarHeightPx.toDp())
)
}
},
) { innerPadding ->
NavHost(
navController = navController,
startDestination = ITEM_LISTING_GRAPH_ROUTE,
modifier = Modifier
.consumeWindowInsets(WindowInsets.navigationBars)
.consumeWindowInsets(WindowInsets.ime)
.padding(innerPadding.max(WindowInsets.ime)),
enterTransition = RootTransitionProviders.Enter.fadeIn,
exitTransition = RootTransitionProviders.Exit.fadeOut,
popEnterTransition = RootTransitionProviders.Enter.fadeIn,
popExitTransition = RootTransitionProviders.Exit.fadeOut,
) {
itemListingGraph(
navController = navController,
navigateBack = navigateBack,
navigateToSearch = navigateToSearch,
navigateToQrCodeScanner = navigateToQrCodeScanner,
navigateToManualKeyEntry = navigateToManualKeyEntry,
navigateToEditItem = navigateToEditItem,
navigateToExport = navigateToExport,
navigateToTutorial = navigateToTutorial,
)
}
}
}
@Composable
private fun AuthenticatorBottomAppBar(
navController: NavController,
verificationCodesTabClickedAction: () -> Unit,
settingsTabClickedAction: () -> Unit,
modifier: Modifier = Modifier,
) {
BottomAppBar(
containerColor = MaterialTheme.colorScheme.surfaceContainer,
modifier = modifier,
) {
val destinations = listOf(
AuthenticatorNavBarTab.VerificationCodes,
AuthenticatorNavBarTab.Settings,
)
val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentDestination = navBackStackEntry?.destination
destinations.forEach { destination ->
val isSelected = currentDestination?.hierarchy?.any {
it.route == destination.route
} == true
NavigationBarItem(
icon = {
Icon(
painter = painterResource(
id = if (isSelected) {
destination.iconResSelected
} else {
destination.iconRes
},
),
contentDescription = stringResource(
id = destination.contentDescriptionRes,
),
)
},
label = {
Text(
text = stringResource(id = destination.labelRes),
maxLines = 1,
overflow = TextOverflow.Ellipsis,
)
},
selected = isSelected,
onClick = {
when (destination) {
AuthenticatorNavBarTab.VerificationCodes -> {
verificationCodesTabClickedAction()
}
AuthenticatorNavBarTab.Settings -> {
settingsTabClickedAction()
}
}
},
colors = NavigationBarItemDefaults.colors(
indicatorColor = MaterialTheme.colorScheme.secondaryContainer,
selectedIconColor = MaterialTheme.colorScheme.onSecondaryContainer,
unselectedIconColor = MaterialTheme.colorScheme.onSurface,
selectedTextColor = MaterialTheme.colorScheme.onSecondaryContainer,
unselectedTextColor = MaterialTheme.colorScheme.onSurface,
),
modifier = Modifier.semantics { testTag = destination.testTag },
)
}
}
}
/**
* Represents the different tabs available in the navigation bar
* for the authenticator screens.
*
* Each tab is modeled with properties that provide information on:
* - Regular icon resource
* - Icon resource when selected
* and other essential UI and navigational data.
*
* @property iconRes The resource ID for the regular (unselected) icon representing the tab.
* @property iconResSelected The resource ID for the icon representing the tab when it's selected.
*/
@Parcelize
private sealed class AuthenticatorNavBarTab : Parcelable {
/**
* The resource ID for the icon representing the tab when it is selected.
*/
abstract val iconResSelected: Int
/**
* Resource id for the icon representing the tab.
*/
abstract val iconRes: Int
/**
* Resource id for the label describing the tab.
*/
abstract val labelRes: Int
/**
* Resource id for the content description describing the tab.
*/
abstract val contentDescriptionRes: Int
/**
* Route of the tab.
*/
abstract val route: String
/**
* The test tag of the tab.
*/
abstract val testTag: String
/**
* Show the Verification Codes screen.
*/
@Parcelize
data object VerificationCodes : AuthenticatorNavBarTab() {
override val iconResSelected get() = R.drawable.ic_verification_codes_filled
override val iconRes get() = R.drawable.ic_verification_codes
override val labelRes get() = R.string.verification_codes
override val contentDescriptionRes get() = R.string.verification_codes
override val route get() = ITEM_LIST_ROUTE
override val testTag get() = "VerificationCodesTab"
}
/**
* Show the Settings screen.
*/
@Parcelize
data object Settings : AuthenticatorNavBarTab() {
override val iconResSelected get() = R.drawable.ic_settings_filled
override val iconRes get() = R.drawable.ic_settings
override val labelRes get() = R.string.settings
override val contentDescriptionRes get() = R.string.settings
// TODO: Replace with constant when settings screen is complete.
override val route get() = SETTINGS_GRAPH_ROUTE
override val testTag get() = "SettingsTab"
}
}
/**
* Helper function to generate [NavOptions] for [AuthenticatorNavBarScreen].
*/
private fun NavController.authenticatorNavBarScreenNavOptions(): NavOptions =
navOptions {
popUpTo(graph.findStartDestination().id) {
saveState = true
}
launchSingleTop = true
restoreState = true
}