diff --git a/presentation/src/main/java/org/cryptomator/presentation/presenter/UnlockVaultPresenter.kt b/presentation/src/main/java/org/cryptomator/presentation/presenter/UnlockVaultPresenter.kt index f66b096f..e874cfb7 100644 --- a/presentation/src/main/java/org/cryptomator/presentation/presenter/UnlockVaultPresenter.kt +++ b/presentation/src/main/java/org/cryptomator/presentation/presenter/UnlockVaultPresenter.kt @@ -77,30 +77,58 @@ class UnlockVaultPresenter @Inject constructor( return } + val vault = intent.vaultModel().toVault() getUnverifiedVaultConfigUseCase - .withVault(intent.vaultModel().toVault()) + .withVault(vault) .run(object : DefaultResultHandler>() { override fun onSuccess(unverifiedVaultConfig: Optional) { - if (unverifiedVaultConfig.isAbsent || unverifiedVaultConfig.get().keyId.scheme == CryptoConstants.MASTERKEY_SCHEME) { - when (intent.vaultAction()) { - UnlockVaultIntent.VaultAction.UNLOCK, UnlockVaultIntent.VaultAction.UNLOCK_FOR_BIOMETRIC_AUTH -> { - startedUsingPrepareUnlock = sharedPreferencesHandler.backgroundUnlockPreparation() - pendingUnlockFor(intent.vaultModel().toVault())?.unverifiedVaultConfig = unverifiedVaultConfig.orElse(null) - unlockVault(intent.vaultModel()) - } - UnlockVaultIntent.VaultAction.CHANGE_PASSWORD -> view?.showChangePasswordDialog(intent.vaultModel(), unverifiedVaultConfig.orElse(null)) - else -> TODO("Not yet implemented") - } + onUnverifiedVaultConfigRetrieved(unverifiedVaultConfig) + } + override fun onError(e: Throwable) { + if (!authenticationExceptionHandler.handleAuthenticationException(this@UnlockVaultPresenter, e, ActivityResultCallbacks.authenticatedAfterGettingVaultConfig(vault))) { + super.onError(e) + finishWithResult(null) } } - - override fun onError(e: Throwable) { - super.onError(e) - finishWithResult(null) - } }) } + @Callback(dispatchResultOkOnly = false) + fun authenticatedAfterGettingVaultConfig(result: ActivityResult, vault: Vault) { + if (result.isResultOk) { + val cloud = result.getSingleResult(CloudModel::class.java).toCloud() + getUnverifiedVaultConfigUseCase + .withVault(Vault.aCopyOf(vault).withCloud(cloud).build()) + .run(object : DefaultResultHandler>() { + override fun onSuccess(unverifiedVaultConfig: Optional) { + onUnverifiedVaultConfigRetrieved(unverifiedVaultConfig) + } + override fun onError(e: Throwable) { + super.onError(e) + finishWithResult(null) + } + }) + } else { + view?.closeDialog() + val error = result.getSingleResult(Throwable::class.java) + error?.let { showError(it) } + } + } + + private fun onUnverifiedVaultConfigRetrieved(unverifiedVaultConfig: Optional) { + if (unverifiedVaultConfig.isAbsent || unverifiedVaultConfig.get().keyId.scheme == CryptoConstants.MASTERKEY_SCHEME) { + when (intent.vaultAction()) { + UnlockVaultIntent.VaultAction.UNLOCK, UnlockVaultIntent.VaultAction.UNLOCK_FOR_BIOMETRIC_AUTH -> { + startedUsingPrepareUnlock = sharedPreferencesHandler.backgroundUnlockPreparation() + pendingUnlockFor(intent.vaultModel().toVault())?.unverifiedVaultConfig = unverifiedVaultConfig.orElse(null) + unlockVault(intent.vaultModel()) + } + UnlockVaultIntent.VaultAction.CHANGE_PASSWORD -> view?.showChangePasswordDialog(intent.vaultModel(), unverifiedVaultConfig.orElse(null)) + else -> TODO("Not yet implemented") + } + } + } + private fun unlockVault(vaultModel: VaultModel) { if (canUseBiometricOn(vaultModel)) { if (startedUsingPrepareUnlock) {