Updated on 2026-08-14

This commit is contained in:
Tangem 2024-07-17 17:53:37 +04:00
parent da1d75b51d
commit bccd93464f
8 changed files with 67 additions and 57 deletions

View file

@ -2,7 +2,7 @@ package com.tangem.domain.wallets.models
sealed interface UpdateWalletError {
data object DataError : UpdateWalletError
data object NameAlreadyExists : UpdateWalletError
data class DataError(val cause: Throwable) : UpdateWalletError
}

View file

@ -1,11 +1,9 @@
package com.tangem.domain.wallets.usecase
import arrow.core.Either
import arrow.core.left
import arrow.core.raise.either
import arrow.core.right
import com.tangem.common.doOnFailure
import com.tangem.common.doOnSuccess
import arrow.core.raise.ensure
import com.tangem.common.CompletionResult
import com.tangem.domain.wallets.legacy.UserWalletsListManager
import com.tangem.domain.wallets.models.UpdateWalletError
import com.tangem.domain.wallets.models.UserWallet
@ -18,19 +16,17 @@ import com.tangem.domain.wallets.models.UserWalletId
*/
class RenameWalletUseCase(private val userWalletsListManager: UserWalletsListManager) {
suspend operator fun invoke(userWalletId: UserWalletId, name: String): Either<UpdateWalletError, UserWallet> {
val existingNames = userWalletsListManager.userWalletsSync
suspend operator fun invoke(userWalletId: UserWalletId, name: String): Either<UpdateWalletError, UserWallet> =
either {
val existingNames = userWalletsListManager.userWalletsSync
if (existingNames.any { it.name == name && it.walletId != userWalletId }) {
return UpdateWalletError.NameAlreadyExists.left()
ensure(existingNames.none { it.name == name && it.walletId != userWalletId }) {
UpdateWalletError.NameAlreadyExists
}
when (val result = userWalletsListManager.update(userWalletId) { it.copy(name = name) }) {
is CompletionResult.Failure -> raise(UpdateWalletError.DataError(result.error))
is CompletionResult.Success -> result.data
}
}
return either {
userWalletsListManager.update(userWalletId) { it.copy(name = name) }
.doOnSuccess { return it.right() }
.doOnFailure { return UpdateWalletError.DataError.left() }
return UpdateWalletError.DataError.left()
}
}
}

View file

@ -1,11 +1,8 @@
package com.tangem.domain.wallets.usecase
import arrow.core.Either
import arrow.core.left
import arrow.core.raise.either
import arrow.core.right
import com.tangem.common.doOnFailure
import com.tangem.common.doOnSuccess
import com.tangem.common.CompletionResult
import com.tangem.domain.wallets.legacy.UserWalletsListManager
import com.tangem.domain.wallets.models.UpdateWalletError
import com.tangem.domain.wallets.models.UserWallet
@ -23,13 +20,10 @@ class UpdateWalletUseCase(private val userWalletsListManager: UserWalletsListMan
suspend operator fun invoke(
userWalletId: UserWalletId,
update: suspend (UserWallet) -> UserWallet,
): Either<UpdateWalletError, UserWallet> {
return either {
userWalletsListManager.update(userWalletId, update)
.doOnSuccess { return it.right() }
.doOnFailure { return UpdateWalletError.DataError.left() }
return UpdateWalletError.DataError.left()
): Either<UpdateWalletError, UserWallet> = either {
when (val result = userWalletsListManager.update(userWalletId, update)) {
is CompletionResult.Failure -> raise(UpdateWalletError.DataError(result.error))
is CompletionResult.Success -> result.data
}
}
}