diff --git a/app/src/main/java/com/tangem/tap/domain/userWalletList/implementation/BiometricUserWalletsListManager.kt b/app/src/main/java/com/tangem/tap/domain/userWalletList/implementation/BiometricUserWalletsListManager.kt index 59819e3d7d..f4c32feb54 100644 --- a/app/src/main/java/com/tangem/tap/domain/userWalletList/implementation/BiometricUserWalletsListManager.kt +++ b/app/src/main/java/com/tangem/tap/domain/userWalletList/implementation/BiometricUserWalletsListManager.kt @@ -57,21 +57,11 @@ internal class BiometricUserWalletsListManager( get() = state.value.userWallets.size override suspend fun unlock(): CompletionResult { - return unlockWithBiometryInternal() - .mapFailure { error -> - if (error is UserWalletsListError) { - error - } else { - UserWalletsListError.UnableToUnlockUserWallets(cause = error) - } - } - .map { - selectedUserWalletSync.guard { - throw UserWalletsListError.UnableToUnlockUserWallets( - cause = IllegalStateException("No user wallet selected"), - ) - } - } + return unlockWithBiometryInternal().mapUnlockResult() + } + + override suspend fun unlockAndSelect(selectedWalletId: UserWalletId): CompletionResult { + return unlockWithBiometryInternal(selectedWalletId = selectedWalletId).mapUnlockResult() } override fun lock() { @@ -198,7 +188,7 @@ internal class BiometricUserWalletsListManager( } } - private suspend fun unlockWithBiometryInternal(): CompletionResult { + private suspend fun unlockWithBiometryInternal(selectedWalletId: UserWalletId? = null): CompletionResult { return keysRepository.getAll() .map { keys -> state.update { prevState -> @@ -207,7 +197,7 @@ internal class BiometricUserWalletsListManager( ) } } - .flatMap { loadModels() } + .flatMap { loadModels(selectedWalletId = selectedWalletId) } .map { state.update { prevState -> val hasLockedUserWallets = prevState.userWallets.any { it.isLocked } @@ -216,6 +206,24 @@ internal class BiometricUserWalletsListManager( } } + private fun CompletionResult.mapUnlockResult(): CompletionResult { + return this + .mapFailure { error -> + if (error is UserWalletsListError) { + error + } else { + UserWalletsListError.UnableToUnlockUserWallets(cause = error) + } + } + .map { + selectedUserWalletSync.guard { + throw UserWalletsListError.UnableToUnlockUserWallets( + cause = IllegalStateException("No user wallet selected"), + ) + } + } + } + private suspend fun saveEncryptionKeyIfNotNull(userWallet: UserWallet): CompletionResult { val encryptionKey = userWallet.scanResponse.card.encryptionKey ?.let { UserWalletEncryptionKey(userWallet.walletId, it) } @@ -237,7 +245,7 @@ internal class BiometricUserWalletsListManager( } } - private suspend fun loadModels(): CompletionResult { + private suspend fun loadModels(selectedWalletId: UserWalletId? = null): CompletionResult { return getSavedUserWallets() .map { userWallets -> if (userWallets.isNotEmpty()) { @@ -247,7 +255,7 @@ internal class BiometricUserWalletsListManager( prevState.copy( userWallets = wallets, selectedUserWalletId = findOrSetSelectedUserWalletId( - prevSelectedWalletId = prevState.selectedUserWalletId, + prevSelectedWalletId = selectedWalletId ?: prevState.selectedUserWalletId, userWallets = wallets, ), ) diff --git a/domain/wallets/src/main/java/com/tangem/domain/wallets/legacy/UserWalletsListManager.kt b/domain/wallets/src/main/java/com/tangem/domain/wallets/legacy/UserWalletsListManager.kt index ff4e805ad1..db6a353f7b 100644 --- a/domain/wallets/src/main/java/com/tangem/domain/wallets/legacy/UserWalletsListManager.kt +++ b/domain/wallets/src/main/java/com/tangem/domain/wallets/legacy/UserWalletsListManager.kt @@ -110,6 +110,13 @@ interface UserWalletsListManager { */ suspend fun unlock(): CompletionResult + /** + * Unlock all [UserWallet]s and select passed [UserWalletId] + * + * @param selectedWalletId [UserWalletId] of [UserWallet] which must be selected + */ + suspend fun unlockAndSelect(selectedWalletId: UserWalletId): CompletionResult + /** Remove [UserWallet]s from [userWallets] and set [isLocked] as true */ fun lock() } diff --git a/domain/wallets/src/main/java/com/tangem/domain/wallets/usecase/UnlockWalletsUseCase.kt b/domain/wallets/src/main/java/com/tangem/domain/wallets/usecase/UnlockWalletsUseCase.kt index 3459902720..2a713a0ac8 100644 --- a/domain/wallets/src/main/java/com/tangem/domain/wallets/usecase/UnlockWalletsUseCase.kt +++ b/domain/wallets/src/main/java/com/tangem/domain/wallets/usecase/UnlockWalletsUseCase.kt @@ -8,6 +8,7 @@ import com.tangem.common.doOnSuccess import com.tangem.domain.wallets.legacy.WalletsStateHolder import com.tangem.domain.wallets.legacy.asLockable import com.tangem.domain.wallets.models.UnlockWalletError +import com.tangem.domain.wallets.models.UserWalletId /** * Unlock wallets use case @@ -18,11 +19,11 @@ import com.tangem.domain.wallets.models.UnlockWalletError */ class UnlockWalletsUseCase(private val walletsStateHolder: WalletsStateHolder) { - suspend operator fun invoke(): Either { + suspend operator fun invoke(selectedWalletId: UserWalletId): Either { val userWalletsListManager = walletsStateHolder.userWalletsListManager?.asLockable() ?: return UnlockWalletError.CommonError.left() - userWalletsListManager.unlock() + userWalletsListManager.unlockAndSelect(selectedWalletId = selectedWalletId) .doOnSuccess { return Unit.right() } .doOnFailure { return UnlockWalletError.CommonError.left() } diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/viewmodels/WalletViewModel.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/viewmodels/WalletViewModel.kt index a182b01ae7..34ffd5f578 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/viewmodels/WalletViewModel.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/viewmodels/WalletViewModel.kt @@ -763,10 +763,14 @@ internal class WalletViewModel @Inject constructor( } override fun onUnlockWalletClick() { + val state = uiState as? WalletState.ContentState ?: return + analyticsEventsHandler.send(WalletScreenAnalyticsEvent.NoticeWalletLocked) - viewModelScope.launch(dispatchers.io) { - unlockWalletsUseCase() + viewModelScope.launch(dispatchers.main) { + unlockWalletsUseCase( + selectedWalletId = state.walletsListConfig.wallets[state.walletsListConfig.selectedWalletIndex].id, + ) } }