BIT-1017: Add "Account already added" dialog to Landing Screen (#344)

This commit is contained in:
Brian Yencho
2023-12-07 09:14:03 -06:00
committed by Álison Fernandes
parent 5ffe9c914d
commit a91de90fcd
4 changed files with 226 additions and 1 deletions

View File

@@ -60,6 +60,7 @@ import com.x8bit.bitwarden.ui.platform.components.BitwardenSwitch
import com.x8bit.bitwarden.ui.platform.components.BitwardenTextButton
import com.x8bit.bitwarden.ui.platform.components.BitwardenTextField
import com.x8bit.bitwarden.ui.platform.components.BitwardenTopAppBar
import com.x8bit.bitwarden.ui.platform.components.BitwardenTwoButtonDialog
import kotlinx.collections.immutable.toImmutableList
/**
@@ -87,6 +88,32 @@ fun LandingScreen(
}
when (val dialog = state.dialog) {
is LandingState.DialogState.AccountAlreadyAdded -> {
BitwardenTwoButtonDialog(
title = stringResource(id = R.string.account_already_added),
message = stringResource(
id = R.string.switch_to_already_added_account_confirmation,
),
confirmButtonText = stringResource(id = R.string.yes),
dismissButtonText = stringResource(id = R.string.cancel),
onConfirmClick = remember(viewModel) {
{
viewModel.trySendAction(
LandingAction.ConfirmSwitchToMatchingAccountClick(
account = dialog.accountSummary,
),
)
}
},
onDismissClick = remember(viewModel) {
{ viewModel.trySendAction(LandingAction.DialogDismiss) }
},
onDismissRequest = remember(viewModel) {
{ viewModel.trySendAction(LandingAction.DialogDismiss) }
},
)
}
is LandingState.DialogState.Error -> {
BitwardenBasicDialog(
visibilityState = BasicDialogState.Shown(

View File

@@ -42,6 +42,17 @@ class LandingViewModel @Inject constructor(
),
) {
/**
* Returns the [AccountSummary] from the current state that matches the current email input,
* of `null` if there is no match.
*/
private val matchingAccountSummary: AccountSummary?
get() {
val currentEmail = state.emailInput
val accountSummaries = state.accountSummaries
return accountSummaries.find { it.email == currentEmail }
}
init {
// As state updates:
// - write to saved state handle
@@ -65,6 +76,10 @@ class LandingViewModel @Inject constructor(
override fun handleAction(action: LandingAction) {
when (action) {
is LandingAction.SwitchAccountClick -> handleSwitchAccountClicked(action)
is LandingAction.ConfirmSwitchToMatchingAccountClick -> {
handleConfirmSwitchToMatchingAccountClicked(action)
}
is LandingAction.ContinueButtonClick -> handleContinueButtonClicked()
LandingAction.CreateAccountClick -> handleCreateAccountClicked()
is LandingAction.DialogDismiss -> handleDialogDismiss()
@@ -81,6 +96,12 @@ class LandingViewModel @Inject constructor(
authRepository.switchAccount(userId = action.account.userId)
}
private fun handleConfirmSwitchToMatchingAccountClicked(
action: LandingAction.ConfirmSwitchToMatchingAccountClick,
) {
authRepository.switchAccount(userId = action.account.userId)
}
private fun handleEmailInputUpdated(action: LandingAction.EmailInputChanged) {
val email = action.input
mutableStateFlow.update {
@@ -103,6 +124,17 @@ class LandingViewModel @Inject constructor(
return
}
matchingAccountSummary?.let { accountSummary ->
mutableStateFlow.update {
it.copy(
dialog = LandingState.DialogState.AccountAlreadyAdded(
accountSummary = accountSummary,
),
)
}
return
}
val email = mutableStateFlow.value.emailInput
val isRememberMeEnabled = mutableStateFlow.value.isRememberMeEnabled
@@ -170,6 +202,15 @@ data class LandingState(
*/
sealed class DialogState : Parcelable {
/**
* Represents a dialog indicating that the current email matches the existing
* [accountSummary].
*/
@Parcelize
data class AccountAlreadyAdded(
val accountSummary: AccountSummary,
) : DialogState()
/**
* Represents an error dialog with the given [message].
*/
@@ -213,6 +254,13 @@ sealed class LandingAction {
val account: AccountSummary,
) : LandingAction()
/**
* Indicates the user has confirmed they would like to switch to the existing [account].
*/
data class ConfirmSwitchToMatchingAccountClick(
val account: AccountSummary,
) : LandingAction()
/**
* Indicates that the continue button has been clicked and the app should navigate to Login.
*/