mirror of
https://github.com/bitwarden/android.git
synced 2026-06-02 02:36:58 -05:00
Add dialog support for TrustedDeviceScreen (#1239)
This commit is contained in:
committed by
Álison Fernandes
parent
a6a4c40693
commit
79f8703d9b
@@ -37,6 +37,10 @@ import com.x8bit.bitwarden.ui.platform.components.appbar.BitwardenTopAppBar
|
||||
import com.x8bit.bitwarden.ui.platform.components.appbar.NavigationIcon
|
||||
import com.x8bit.bitwarden.ui.platform.components.button.BitwardenFilledButton
|
||||
import com.x8bit.bitwarden.ui.platform.components.button.BitwardenOutlinedButton
|
||||
import com.x8bit.bitwarden.ui.platform.components.dialog.BasicDialogState
|
||||
import com.x8bit.bitwarden.ui.platform.components.dialog.BitwardenBasicDialog
|
||||
import com.x8bit.bitwarden.ui.platform.components.dialog.BitwardenLoadingDialog
|
||||
import com.x8bit.bitwarden.ui.platform.components.dialog.LoadingDialogState
|
||||
import com.x8bit.bitwarden.ui.platform.components.scaffold.BitwardenScaffold
|
||||
import com.x8bit.bitwarden.ui.platform.components.text.BitwardenClickableText
|
||||
import com.x8bit.bitwarden.ui.platform.components.toggle.BitwardenSwitch
|
||||
@@ -72,6 +76,11 @@ fun TrustedDeviceScreen(
|
||||
}
|
||||
}
|
||||
|
||||
TrustedDeviceDialogs(
|
||||
dialogState = state.dialogState,
|
||||
handlers = handlers,
|
||||
)
|
||||
|
||||
TrustedDeviceScaffold(
|
||||
state = state,
|
||||
handlers = handlers,
|
||||
@@ -192,11 +201,34 @@ private fun TrustedDeviceScaffold(
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun TrustedDeviceDialogs(
|
||||
dialogState: TrustedDeviceState.DialogState?,
|
||||
handlers: TrustedDeviceHandlers,
|
||||
) {
|
||||
when (dialogState) {
|
||||
is TrustedDeviceState.DialogState.Error -> BitwardenBasicDialog(
|
||||
visibilityState = BasicDialogState.Shown(
|
||||
title = dialogState.title,
|
||||
message = dialogState.message,
|
||||
),
|
||||
onDismissRequest = handlers.onDismissDialog,
|
||||
)
|
||||
|
||||
is TrustedDeviceState.DialogState.Loading -> BitwardenLoadingDialog(
|
||||
visibilityState = LoadingDialogState.Shown(dialogState.message),
|
||||
)
|
||||
|
||||
null -> Unit
|
||||
}
|
||||
}
|
||||
|
||||
@Preview
|
||||
@Composable
|
||||
private fun TrustedDeviceScaffold_preview() {
|
||||
TrustedDeviceScaffold(
|
||||
state = TrustedDeviceState(
|
||||
dialogState = null,
|
||||
isRemembered = false,
|
||||
emailAddress = "email@bitwarden.com",
|
||||
environmentLabel = "vault.bitwarden.pw",
|
||||
@@ -207,6 +239,7 @@ private fun TrustedDeviceScaffold_preview() {
|
||||
),
|
||||
handlers = TrustedDeviceHandlers(
|
||||
onBackClick = {},
|
||||
onDismissDialog = {},
|
||||
onRememberToggle = {},
|
||||
onContinueClick = {},
|
||||
onApproveWithAdminClick = {},
|
||||
|
||||
@@ -29,6 +29,7 @@ class TrustedDeviceViewModel @Inject constructor(
|
||||
val trustedDevice = account?.trustedDevice
|
||||
if (trustedDevice == null) authRepository.logout()
|
||||
TrustedDeviceState(
|
||||
dialogState = null,
|
||||
emailAddress = account?.email.orEmpty(),
|
||||
environmentLabel = environmentRepository.environment.label,
|
||||
isRemembered = true,
|
||||
@@ -44,6 +45,7 @@ class TrustedDeviceViewModel @Inject constructor(
|
||||
override fun handleAction(action: TrustedDeviceAction) {
|
||||
when (action) {
|
||||
TrustedDeviceAction.BackClick -> handleBackClick()
|
||||
TrustedDeviceAction.DismissDialog -> handleDismissDialog()
|
||||
is TrustedDeviceAction.RememberToggle -> handleRememberToggle(action)
|
||||
TrustedDeviceAction.ContinueClick -> handleContinueClick()
|
||||
TrustedDeviceAction.ApproveWithAdminClick -> handleApproveWithAdminClick()
|
||||
@@ -57,6 +59,10 @@ class TrustedDeviceViewModel @Inject constructor(
|
||||
authRepository.logout()
|
||||
}
|
||||
|
||||
private fun handleDismissDialog() {
|
||||
mutableStateFlow.update { it.copy(dialogState = null) }
|
||||
}
|
||||
|
||||
private fun handleRememberToggle(action: TrustedDeviceAction.RememberToggle) {
|
||||
mutableStateFlow.update { it.copy(isRemembered = action.isRemembered) }
|
||||
}
|
||||
@@ -89,6 +95,7 @@ class TrustedDeviceViewModel @Inject constructor(
|
||||
*/
|
||||
@Parcelize
|
||||
data class TrustedDeviceState(
|
||||
val dialogState: DialogState?,
|
||||
val emailAddress: String,
|
||||
val environmentLabel: String,
|
||||
val isRemembered: Boolean,
|
||||
@@ -96,7 +103,29 @@ data class TrustedDeviceState(
|
||||
val showOtherDeviceButton: Boolean,
|
||||
val showRequestAdminButton: Boolean,
|
||||
val showMasterPasswordButton: Boolean,
|
||||
) : Parcelable
|
||||
) : Parcelable {
|
||||
/**
|
||||
* Represents the current state of any dialogs on the screen.
|
||||
*/
|
||||
sealed class DialogState : Parcelable {
|
||||
/**
|
||||
* Represents a dismissible dialog with the given error [message].
|
||||
*/
|
||||
@Parcelize
|
||||
data class Error(
|
||||
val title: Text?,
|
||||
val message: Text,
|
||||
) : DialogState()
|
||||
|
||||
/**
|
||||
* Represents a loading dialog with the given [message].
|
||||
*/
|
||||
@Parcelize
|
||||
data class Loading(
|
||||
val message: Text,
|
||||
) : DialogState()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Models events for the Trusted Device screen.
|
||||
@@ -131,6 +160,11 @@ sealed class TrustedDeviceAction {
|
||||
*/
|
||||
data object BackClick : TrustedDeviceAction()
|
||||
|
||||
/**
|
||||
* User clicked to dismiss the dialog.
|
||||
*/
|
||||
data object DismissDialog : TrustedDeviceAction()
|
||||
|
||||
/**
|
||||
* User toggled the remember device switch.
|
||||
*/
|
||||
|
||||
@@ -9,6 +9,7 @@ import com.x8bit.bitwarden.ui.auth.feature.trusteddevice.TrustedDeviceViewModel
|
||||
*/
|
||||
data class TrustedDeviceHandlers(
|
||||
val onBackClick: () -> Unit,
|
||||
val onDismissDialog: () -> Unit,
|
||||
val onRememberToggle: (Boolean) -> Unit,
|
||||
val onContinueClick: () -> Unit,
|
||||
val onApproveWithDeviceClick: () -> Unit,
|
||||
@@ -24,6 +25,7 @@ data class TrustedDeviceHandlers(
|
||||
fun create(viewModel: TrustedDeviceViewModel): TrustedDeviceHandlers =
|
||||
TrustedDeviceHandlers(
|
||||
onBackClick = { viewModel.trySendAction(TrustedDeviceAction.BackClick) },
|
||||
onDismissDialog = { viewModel.trySendAction(TrustedDeviceAction.DismissDialog) },
|
||||
onRememberToggle = {
|
||||
viewModel.trySendAction(TrustedDeviceAction.RememberToggle(it))
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user