Listen to UserState changes on the Vault Unlock screen (#290)

This commit is contained in:
Brian Yencho
2023-11-29 08:12:43 -06:00
committed by Álison Fernandes
parent f8f89a5903
commit d4d64c2a6d
2 changed files with 95 additions and 3 deletions

View File

@@ -6,6 +6,7 @@ import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.viewModelScope
import com.x8bit.bitwarden.R
import com.x8bit.bitwarden.data.auth.repository.AuthRepository
import com.x8bit.bitwarden.data.auth.repository.model.UserState
import com.x8bit.bitwarden.data.platform.repository.EnvironmentRepository
import com.x8bit.bitwarden.data.vault.repository.VaultRepository
import com.x8bit.bitwarden.data.vault.repository.model.VaultUnlockResult
@@ -65,6 +66,12 @@ class VaultUnlockViewModel @Inject constructor(
}
}
.launchIn(viewModelScope)
authRepository
.userStateFlow
.onEach {
sendAction(VaultUnlockAction.Internal.UserStateUpdateReceive(userState = it))
}
.launchIn(viewModelScope)
}
override fun handleAction(action: VaultUnlockAction) {
@@ -78,6 +85,10 @@ class VaultUnlockViewModel @Inject constructor(
is VaultUnlockAction.Internal.ReceiveVaultUnlockResult -> {
handleReceiveVaultUnlockResult(action)
}
is VaultUnlockAction.Internal.UserStateUpdateReceive -> {
handleUserStateUpdateReceive(action)
}
}
}
@@ -146,6 +157,24 @@ class VaultUnlockViewModel @Inject constructor(
}
}
}
private fun handleUserStateUpdateReceive(
action: VaultUnlockAction.Internal.UserStateUpdateReceive,
) {
// Leave the current data alone if there is no UserState; we are in the process of logging
// out.
val userState = action.userState ?: return
mutableStateFlow.update {
val accountSummaries = userState.toAccountSummaries()
val activeAccountSummary = userState.toActiveAccountSummary()
it.copy(
initials = activeAccountSummary.initials,
avatarColorString = activeAccountSummary.avatarColorHex,
accountSummaries = accountSummaries,
)
}
}
}
/**
@@ -252,5 +281,12 @@ sealed class VaultUnlockAction {
data class ReceiveVaultUnlockResult(
val vaultUnlockResult: VaultUnlockResult,
) : Internal()
/**
* Indicates a change in user state has been received.
*/
data class UserStateUpdateReceive(
val userState: UserState?,
) : Internal()
}
}