Updated on 2026-08-14

This commit is contained in:
Tangem 2026-03-03 19:15:58 +05:00
parent 6a04ccb285
commit cf94c1f826
3 changed files with 42 additions and 22 deletions

View file

@ -84,7 +84,7 @@ class ManageCryptoCurrenciesUseCase(
withContext(NonCancellable) {
val accountStatus = getAccountStatus(accountId = accountId)
val modifiedCurrencyList = accountStatus.tokenList.flattenCurrencies()
var modifiedCurrencyList = accountStatus.tokenList.flattenCurrencies()
.modify(add = add, remove = remove)
if (!modifiedCurrencyList.hasChanges) {
@ -92,12 +92,21 @@ class ManageCryptoCurrenciesUseCase(
return@withContext
}
val derivingResult = derivePublicKeys(userWalletId = userWalletId, currencies = modifiedCurrencyList.added)
if (!skipDerivationErrors && derivingResult.isLeft()) {
modifiedCurrencyList = accountStatus.tokenList.flattenCurrencies()
.modify(add = emptyList(), remove = remove)
if (!modifiedCurrencyList.hasChanges) {
derivingResult.bind()
}
}
saveAccount(
account = accountStatus.account.copy(cryptoCurrencies = modifiedCurrencyList.total),
)
val result = derivePublicKeys(userWalletId = userWalletId, currencies = modifiedCurrencyList.added)
parallelUpdatingScope.launch {
syncTokens(userWalletId, modifiedCurrencyList)
@ -107,7 +116,7 @@ class ManageCryptoCurrenciesUseCase(
}
if (!skipDerivationErrors) {
result.bind()
derivingResult.bind()
}
}
}

View file

@ -1,6 +1,7 @@
package com.tangem.features.managetokens.model
import arrow.core.getOrElse
import com.tangem.common.core.TangemSdkError
import com.tangem.core.analytics.api.AnalyticsEventHandler
import com.tangem.core.decompose.di.ModelScoped
import com.tangem.core.decompose.model.Model
@ -8,6 +9,7 @@ import com.tangem.core.decompose.model.ParamsContainer
import com.tangem.core.decompose.ui.UiMessageSender
import com.tangem.core.ui.extensions.resourceReference
import com.tangem.core.ui.extensions.stringReference
import com.tangem.core.ui.extensions.wrappedList
import com.tangem.core.ui.message.DialogMessage
import com.tangem.domain.managetokens.CreateCryptoCurrencyUseCase
import com.tangem.domain.managetokens.FindTokenUseCase
@ -146,7 +148,7 @@ internal class CustomTokenFormModel @Inject constructor(
is CustomCurrencyValidator.Status.Validating,
-> Unit
is CustomCurrencyValidator.Status.SearchingToken -> updateStateWithProgress()
is CustomCurrencyValidator.Status.UnexpectedException -> showErrorDialog()
is CustomCurrencyValidator.Status.UnexpectedException -> showErrorDialog(validatorState.cause)
is CustomCurrencyValidator.Status.FormValidationException -> updateStateWithExceptions(
exceptions = validatorState.exceptions,
)
@ -236,9 +238,17 @@ internal class CustomTokenFormModel @Inject constructor(
}
}
private fun showErrorDialog() {
private fun showErrorDialog(throwable: Throwable) {
Timber.e(throwable)
val message = when (throwable) {
is TangemSdkError -> resourceReference(
R.string.generic_error_code,
wrappedList(throwable.code.toString()),
)
else -> resourceReference(R.string.common_unknown_error)
}
val dialog = DialogMessage(
message = resourceReference(R.string.common_unknown_error),
message = message,
)
messageSender.send(dialog)
@ -341,8 +351,17 @@ internal class CustomTokenFormModel @Inject constructor(
) {
val currency = createdCurrency
if (currency == null) {
Timber.e("Trying to add currency without validation")
showErrorDialog()
showErrorDialog(IllegalStateException("Trying to add currency without validation"))
return@resource
}
useCasesFacade.derivePublicKeysUseCase(listOf(currency)).getOrElse {
showErrorDialog(IllegalStateException("Failed to derive public keys"))
return@resource
}
useCasesFacade.addCryptoCurrenciesUseCase(currency).getOrElse { throwable ->
showErrorDialog(throwable)
return@resource
}
@ -353,18 +372,6 @@ internal class CustomTokenFormModel @Inject constructor(
)
analyticsEventHandler.send(event)
useCasesFacade.derivePublicKeysUseCase(listOf(currency)).getOrElse {
Timber.e(it, "Failed to derive public keys")
showErrorDialog()
return@resource
}
useCasesFacade.addCryptoCurrenciesUseCase(currency).getOrElse {
Timber.e(it, "Failed to add currency")
showErrorDialog()
return@resource
}
params.onCurrencyAdded(currency)
}

View file

@ -44,7 +44,11 @@ internal class CustomTokenFormUseCasesFacade @AssistedInject constructor(
either {
val accountId = getAccountId(currency)
manageCryptoCurrenciesUseCase(accountId = accountId, add = currency).bind()
manageCryptoCurrenciesUseCase(
accountId = accountId,
add = currency,
skipDerivationErrors = false,
).bind()
}
} else {
addCryptoCurrenciesUseCase.invoke(userWalletId = userWalletId, currency = currency)