Decouple unlocking and syncing and add syncIfNecessary (#733)

This commit is contained in:
Brian Yencho
2024-01-23 14:51:15 -06:00
committed by Álison Fernandes
parent 91df6b5e25
commit e1a078e511
9 changed files with 77 additions and 70 deletions

View File

@@ -227,7 +227,7 @@ class AuthRepositoryImpl(
settingsRepository.setDefaultsIfNecessary(
userId = userStateJson.activeUserId,
)
vaultRepository.sync()
vaultRepository.syncIfNecessary()
hasPendingAccountAddition = false
LoginResult.Success
}

View File

@@ -95,10 +95,19 @@ interface VaultRepository : VaultLockManager {
fun deleteVaultData(userId: String)
/**
* Attempt to sync the vault data.
* Sync the vault data for the current user.
*
* Unlike [syncIfNecessary], this will always perform the requested sync and should only be
* utilized in cases where the user specifically requested the action.
*/
fun sync()
/**
* Checks if conditions have been met to perform a sync request and, if so, syncs the vault
* data for the current user.
*/
fun syncIfNecessary()
/**
* Flow that represents the data for a specific vault item as found by ID. This may emit `null`
* if the item cannot be found.
@@ -129,18 +138,17 @@ interface VaultRepository : VaultLockManager {
fun emitTotpCodeResult(totpCodeResult: TotpCodeResult)
/**
* Attempt to unlock the vault with the given [masterPassword] and syncs the vault data for the
* currently active user.
* Attempt to unlock the vault with the given [masterPassword] and for the currently active
* user.
*/
suspend fun unlockVaultWithMasterPasswordAndSync(
suspend fun unlockVaultWithMasterPassword(
masterPassword: String,
): VaultUnlockResult
/**
* Attempt to unlock the vault with the given [pin] and syncs the vault data for the currently
* active user.
* Attempt to unlock the vault with the given [pin] for the currently active user.
*/
suspend fun unlockVaultWithPinAndSync(
suspend fun unlockVaultWithPin(
pin: String,
): VaultUnlockResult

View File

@@ -257,6 +257,11 @@ class VaultRepositoryImpl(
}
}
override fun syncIfNecessary() {
// TODO: Add conditions for when a sync may be performed and test accordingly (BIT-1454)
sync()
}
override fun getVaultItemStateFlow(itemId: String): StateFlow<DataState<CipherView?>> =
vaultDataStateFlow
.map { dataState ->
@@ -346,7 +351,7 @@ class VaultRepositoryImpl(
}
@Suppress("ReturnCount")
override suspend fun unlockVaultWithMasterPasswordAndSync(
override suspend fun unlockVaultWithMasterPassword(
masterPassword: String,
): VaultUnlockResult {
val userId = activeUserId ?: return VaultUnlockResult.InvalidStateError
@@ -361,14 +366,13 @@ class VaultRepositoryImpl(
)
.also {
if (it is VaultUnlockResult.Success) {
sync()
deriveTemporaryPinProtectedUserKeyIfNecessary(userId = userId)
}
}
}
@Suppress("ReturnCount")
override suspend fun unlockVaultWithPinAndSync(
override suspend fun unlockVaultWithPin(
pin: String,
): VaultUnlockResult {
val userId = activeUserId ?: return VaultUnlockResult.InvalidStateError
@@ -381,11 +385,6 @@ class VaultRepositoryImpl(
pinProtectedUserKey = pinProtectedUserKey,
),
)
.also {
if (it is VaultUnlockResult.Success) {
sync()
}
}
}
override suspend fun unlockVault(

View File

@@ -133,13 +133,13 @@ class VaultUnlockViewModel @Inject constructor(
viewModelScope.launch {
val vaultUnlockResult = when (state.vaultUnlockType) {
VaultUnlockType.MASTER_PASSWORD -> {
vaultRepo.unlockVaultWithMasterPasswordAndSync(
vaultRepo.unlockVaultWithMasterPassword(
mutableStateFlow.value.input,
)
}
VaultUnlockType.PIN -> {
vaultRepo.unlockVaultWithPinAndSync(
vaultRepo.unlockVaultWithPin(
mutableStateFlow.value.input,
)
}

View File

@@ -74,6 +74,9 @@ class VaultViewModel @Inject constructor(
get() = state.vaultFilterData?.selectedVaultFilterType ?: VaultFilterType.AllVaults
init {
// Attempt a sync each time we are on a fresh Vault Screen.
vaultRepository.syncIfNecessary()
// Reset the current vault filter type for the current user
vaultRepository.vaultFilterType = vaultFilterTypeOrDefault
settingsRepository