Updated on 2026-08-14

This commit is contained in:
Tangem 2024-03-18 16:20:25 +03:00
parent 7bcadd9f4c
commit e1480e005e
10 changed files with 154 additions and 116 deletions

View file

@ -105,16 +105,42 @@ interface UserWalletsListManager {
/**
* Receive saved [UserWallet]s, populate [userWallets] flow with it and set [isLocked] as false.
*
* @param throwIfNotAllWalletsUnlocked Indicates that the function must throw
* [UserWalletsListError.NotAllUserWalletsUnlocked] if not all user wallets are unlocked.
* @param type Defines the behavior of the operation.
*
* @return [CompletionResult] of operation, with selected [UserWallet]
* or null if there is no selected [UserWallet]
*/
suspend fun unlock(throwIfNotAllWalletsUnlocked: Boolean = false): CompletionResult<UserWallet>
suspend fun unlock(type: UnlockType): CompletionResult<UserWallet>
/** Remove [UserWallet]s from [userWallets] and set [isLocked] as true */
fun lock()
/**
* Defines the behavior of the [unlock] operation.
* */
enum class UnlockType {
/**
* Ensures that all stored [UserWallet]s are unlocked,
* or throws [UserWalletsListError.NotAllUserWalletsUnlocked].
*
* In this type [selectedUserWallet] is either a previously selected [UserWallet] or the first stored
* [UserWallet].
* */
ALL,
/**
* Ensures that at least one stored [UserWallet] is unlocked,
* or throws [UserWalletsListError.NoUserWalletSelected].
*
* In this type [selectedUserWallet] is the first stored and unlocked [UserWallet].
* */
ANY,
/**
* Same as [ALL] type, but this type can not change [selectedUserWallet] while unlocking.
* */
ALL_WITHOUT_SELECT,
}
}
// For provider

View file

@ -1,6 +1,7 @@
package com.tangem.domain.wallets.legacy
import com.tangem.common.CompletionResult
import com.tangem.domain.wallets.legacy.UserWalletsListManager.Lockable.UnlockType
import com.tangem.domain.wallets.models.UserWallet
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOf
@ -43,8 +44,8 @@ val UserWalletsListManager.isLockedSync: Boolean
*
* @see UserWalletsListManager.Lockable.unlock
* */
suspend fun UserWalletsListManager.unlockIfLockable(): CompletionResult<UserWallet> {
return asLockable()?.unlock() ?: CompletionResult.Failure(UserWalletsListError.UnableToUnlockUserWallets())
suspend fun UserWalletsListManager.unlockIfLockable(type: UnlockType = UnlockType.ANY): CompletionResult<UserWallet> {
return asLockable()?.unlock(type) ?: CompletionResult.Failure(UserWalletsListError.UnableToUnlockUserWallets())
}
/**

View file

@ -5,6 +5,7 @@ import arrow.core.raise.either
import arrow.core.raise.ensureNotNull
import com.tangem.common.doOnFailure
import com.tangem.domain.wallets.legacy.UserWalletsListError
import com.tangem.domain.wallets.legacy.UserWalletsListManager.Lockable.UnlockType
import com.tangem.domain.wallets.legacy.WalletsStateHolder
import com.tangem.domain.wallets.legacy.asLockable
import com.tangem.domain.wallets.models.UnlockWalletsError
@ -18,27 +19,26 @@ import com.tangem.domain.wallets.models.UnlockWalletsError
*/
class UnlockWalletsUseCase(private val walletsStateHolder: WalletsStateHolder) {
suspend operator fun invoke(throwIfNotAllWalletsUnlocked: Boolean = false): Either<UnlockWalletsError, Unit> =
either {
val userWalletsListManager = ensureNotNull(
value = walletsStateHolder.userWalletsListManager?.asLockable(),
raise = {
UnlockWalletsError.DataError(
cause = IllegalStateException("The lockable user wallets list manager could not be found"),
)
},
)
suspend operator fun invoke(type: UnlockType = UnlockType.ANY): Either<UnlockWalletsError, Unit> = either {
val userWalletsListManager = ensureNotNull(
value = walletsStateHolder.userWalletsListManager?.asLockable(),
raise = {
UnlockWalletsError.DataError(
cause = IllegalStateException("The lockable user wallets list manager could not be found"),
)
},
)
userWalletsListManager.unlock(throwIfNotAllWalletsUnlocked)
.doOnFailure { error ->
val e = when (error) {
is UserWalletsListError.NoUserWalletSelected -> UnlockWalletsError.NoUserWalletSelected
is UserWalletsListError.NotAllUserWalletsUnlocked ->
UnlockWalletsError.NotAllUserWalletsUnlocked
else -> UnlockWalletsError.UnableToUnlockWallets
}
raise(e)
userWalletsListManager.unlock(type)
.doOnFailure { error ->
val e = when (error) {
is UserWalletsListError.NoUserWalletSelected -> UnlockWalletsError.NoUserWalletSelected
is UserWalletsListError.NotAllUserWalletsUnlocked ->
UnlockWalletsError.NotAllUserWalletsUnlocked
else -> UnlockWalletsError.UnableToUnlockWallets
}
}
raise(e)
}
}
}