From 90486a54fce52ca1e3f18245f5a0c7c3e6635ed5 Mon Sep 17 00:00:00 2001 From: Tangem Date: Mon, 8 Jul 2024 13:59:55 +0400 Subject: [PATCH] Updated on 2026-08-14 --- .../impl/domain/CustomTokenInteractor.kt | 2 +- .../domain/DefaultCustomTokenInteractor.kt | 23 +++++++++-------- .../viewmodels/AddCustomTokenViewModel.kt | 25 +++++++++++++------ .../tokens/AddCryptoCurrenciesUseCase.kt | 21 ++++++++-------- .../domain/tokens/error/AddCurrencyError.kt | 6 ----- 5 files changed, 41 insertions(+), 36 deletions(-) delete mode 100644 domain/tokens/src/main/kotlin/com/tangem/domain/tokens/error/AddCurrencyError.kt diff --git a/app/src/main/java/com/tangem/tap/features/customtoken/impl/domain/CustomTokenInteractor.kt b/app/src/main/java/com/tangem/tap/features/customtoken/impl/domain/CustomTokenInteractor.kt index 16a0cb98a5..22bea5bf11 100644 --- a/app/src/main/java/com/tangem/tap/features/customtoken/impl/domain/CustomTokenInteractor.kt +++ b/app/src/main/java/com/tangem/tap/features/customtoken/impl/domain/CustomTokenInteractor.kt @@ -17,5 +17,5 @@ interface CustomTokenInteractor { /** Save token [customCurrency] */ @Throws(TangemError::class) - suspend fun saveToken(customCurrency: CustomCurrency) + suspend fun saveToken(customCurrency: CustomCurrency): Result } \ No newline at end of file diff --git a/app/src/main/java/com/tangem/tap/features/customtoken/impl/domain/DefaultCustomTokenInteractor.kt b/app/src/main/java/com/tangem/tap/features/customtoken/impl/domain/DefaultCustomTokenInteractor.kt index de73abac25..ad210d7625 100644 --- a/app/src/main/java/com/tangem/tap/features/customtoken/impl/domain/DefaultCustomTokenInteractor.kt +++ b/app/src/main/java/com/tangem/tap/features/customtoken/impl/domain/DefaultCustomTokenInteractor.kt @@ -1,5 +1,7 @@ package com.tangem.tap.features.customtoken.impl.domain +import arrow.core.getOrElse +import arrow.core.raise.result import com.tangem.blockchain.common.Blockchain import com.tangem.blockchainsdk.utils.toNetworkId import com.tangem.data.tokens.utils.CryptoCurrencyFactory @@ -15,7 +17,6 @@ import com.tangem.tap.domain.model.Currency import com.tangem.tap.features.customtoken.impl.domain.models.FoundToken import com.tangem.tap.proxy.redux.DaggerGraphState import com.tangem.tap.store -import timber.log.Timber /** * Default implementation of custom token interactor @@ -45,16 +46,18 @@ class DefaultCustomTokenInteractor( ) } - override suspend fun saveToken(customCurrency: CustomCurrency) { - val userWallet = getSelectedWalletSyncUseCase().fold(ifLeft = { return }, ifRight = { it }) - val currency = Currency.fromCustomCurrency(customCurrency) - - val currencies = listOfNotNull(element = currency.toCryptoCurrency(userWallet.scanResponse)) - derivePublicKeysUseCase(userWalletId = userWallet.walletId, currencies = currencies) - .onRight { - addCryptoCurrenciesUseCase(userWalletId = userWallet.walletId, currencies = currencies) + override suspend fun saveToken(customCurrency: CustomCurrency): Result { + return result { + val userWallet = getSelectedWalletSyncUseCase().getOrElse { + error("Failed to get selected wallet: $it") } - .onLeft { Timber.e("Failed to derive public keys: $it") } + + val currency = Currency.fromCustomCurrency(customCurrency) + val currencies = listOfNotNull(element = currency.toCryptoCurrency(userWallet.scanResponse)) + + derivePublicKeysUseCase(userWalletId = userWallet.walletId, currencies = currencies).bind() + addCryptoCurrenciesUseCase(userWalletId = userWallet.walletId, currencies = currencies).bind() + } } private fun Currency.toCryptoCurrency(scanResponse: ScanResponse): CryptoCurrency? { diff --git a/app/src/main/java/com/tangem/tap/features/customtoken/impl/presentation/viewmodels/AddCustomTokenViewModel.kt b/app/src/main/java/com/tangem/tap/features/customtoken/impl/presentation/viewmodels/AddCustomTokenViewModel.kt index d725069217..5f523da11c 100644 --- a/app/src/main/java/com/tangem/tap/features/customtoken/impl/presentation/viewmodels/AddCustomTokenViewModel.kt +++ b/app/src/main/java/com/tangem/tap/features/customtoken/impl/presentation/viewmodels/AddCustomTokenViewModel.kt @@ -886,17 +886,26 @@ internal class AddCustomTokenViewModel @Inject constructor( analyticsSender.sendWhenAddTokenButtonClicked(currency) - viewModelScope.launch(dispatchers.io) { - val oldButtonState = uiState.floatingButton + viewModelScope.launch { uiState = uiState.copySealed( - floatingButton = uiState.floatingButton.copy(isEnabled = false, showProgress = true), + floatingButton = uiState.floatingButton.copy( + isEnabled = false, + showProgress = true, + ), ) - runCatching { featureInteractor.saveToken(currency) } + + val result = featureInteractor.saveToken(currency) + + uiState = uiState.copySealed( + floatingButton = uiState.floatingButton.copy( + isEnabled = true, + showProgress = false, + ), + ) + + result .onSuccess { featureRouter.openWalletScreen() } - .onFailure { - uiState = uiState.copySealed(floatingButton = oldButtonState) - Timber.e(it) - } + .onFailure { Timber.e(it, "Unable to save custom token") } } } } diff --git a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/AddCryptoCurrenciesUseCase.kt b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/AddCryptoCurrenciesUseCase.kt index 9febf735fe..e8c6f0b7a8 100644 --- a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/AddCryptoCurrenciesUseCase.kt +++ b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/AddCryptoCurrenciesUseCase.kt @@ -5,7 +5,6 @@ import arrow.core.raise.Raise import arrow.core.raise.catch import arrow.core.raise.either import arrow.core.toNonEmptyListOrNull -import com.tangem.domain.tokens.error.AddCurrencyError import com.tangem.domain.tokens.model.CryptoCurrency import com.tangem.domain.tokens.model.Network import com.tangem.domain.tokens.repository.CurrenciesRepository @@ -32,9 +31,9 @@ class AddCryptoCurrenciesUseCase( * * @param userWalletId The ID of the user's wallet. * @param currency Cryptocurrency to add. - * @return Either an [AddCurrencyError] or [Unit] indicating the success of the operation. + * @return Either an [Throwable] or [Unit] indicating the success of the operation. */ - suspend operator fun invoke(userWalletId: UserWalletId, currency: CryptoCurrency): Either { + suspend operator fun invoke(userWalletId: UserWalletId, currency: CryptoCurrency): Either { return invoke(userWalletId, listOf(currency)) } @@ -47,13 +46,13 @@ class AddCryptoCurrenciesUseCase( * @param userWalletId The ID of the user's wallet. * @param cryptoCurrency Token to add. * @param network Network where we add - * @return Either an [AddCurrencyError] or [Unit] indicating the success of the operation. + * @return Either an [Throwable] or [Unit] indicating the success of the operation. */ suspend operator fun invoke( userWalletId: UserWalletId, cryptoCurrency: CryptoCurrency.Token, network: Network, - ): Either = either { + ): Either = either { val tokenToAdd = currenciesRepository.createTokenCurrency(cryptoCurrency = cryptoCurrency, network = network) invoke(userWalletId = userWalletId, currencies = listOf(tokenToAdd)) } @@ -66,14 +65,14 @@ class AddCryptoCurrenciesUseCase( * * @param userWalletId The ID of the user's wallet. * @param currencies The list of cryptocurrencies to add. - * @return Either an [AddCurrencyError] or [Unit] indicating the success of the operation. + * @return Either an [Throwable] or [Unit] indicating the success of the operation. */ suspend operator fun invoke( userWalletId: UserWalletId, currencies: List, - ): Either = either { + ): Either = either { val existingCurrencies = catch({ currenciesRepository.getMultiCurrencyWalletCurrenciesSync(userWalletId) }) { - raise(AddCurrencyError.DataError(it)) + raise(it) } val currenciesToAdd = currencies .filterNot(existingCurrencies::contains) @@ -81,7 +80,7 @@ class AddCryptoCurrenciesUseCase( ?: return@either catch({ currenciesRepository.addCurrencies(userWalletId, currenciesToAdd) }) { - raise(AddCurrencyError.DataError(it)) + raise(it) } refreshUpdatedNetworks(userWalletId, currenciesToAdd, existingCurrencies) @@ -91,7 +90,7 @@ class AddCryptoCurrenciesUseCase( * Refreshes the network statuses for tokens that have corresponding coins in the * [existingCurrencies] list. */ - private suspend fun Raise.refreshUpdatedNetworks( + private suspend fun Raise.refreshUpdatedNetworks( userWalletId: UserWalletId, currenciesToAdd: List, existingCurrencies: List, @@ -114,7 +113,7 @@ class AddCryptoCurrenciesUseCase( ) }, ) { - raise(AddCurrencyError.DataError(it)) + raise(it) } } diff --git a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/error/AddCurrencyError.kt b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/error/AddCurrencyError.kt deleted file mode 100644 index 87255e52f7..0000000000 --- a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/error/AddCurrencyError.kt +++ /dev/null @@ -1,6 +0,0 @@ -package com.tangem.domain.tokens.error - -sealed class AddCurrencyError { - - data class DataError(val cause: Throwable) : AddCurrencyError() -} \ No newline at end of file