From a0c455b82c32a0f1e02cb5c82d820289e89e4778 Mon Sep 17 00:00:00 2001 From: Tangem Date: Mon, 21 Oct 2024 23:12:33 +0400 Subject: [PATCH] Updated on 2026-08-14 --- .../managetokens/ValidateTokenFormUseCase.kt | 21 +++++++++++-------- .../managetokens/model/AddCustomTokenForm.kt | 4 +++- .../CustomTokenFormValidationException.kt | 2 -- .../model/CustomTokenFormModel.kt | 19 ++++++++++------- .../utils/CustomCurrencyValidator.kt | 7 +++++-- .../utils/ui/CustomCurrencyFormOperations.kt | 3 --- 6 files changed, 31 insertions(+), 25 deletions(-) diff --git a/domain/manage-tokens/src/main/kotlin/com/tangem/domain/managetokens/ValidateTokenFormUseCase.kt b/domain/manage-tokens/src/main/kotlin/com/tangem/domain/managetokens/ValidateTokenFormUseCase.kt index ffbfa795bf..79a0481bc4 100644 --- a/domain/manage-tokens/src/main/kotlin/com/tangem/domain/managetokens/ValidateTokenFormUseCase.kt +++ b/domain/manage-tokens/src/main/kotlin/com/tangem/domain/managetokens/ValidateTokenFormUseCase.kt @@ -16,12 +16,19 @@ class ValidateTokenFormUseCase( networkId: Network.ID, formValues: AddCustomTokenForm.Raw, ): EitherNel = either { - if (formValues.name.isBlank() && formValues.symbol.isBlank() && formValues.decimals.isBlank()) { - val contractAddress = withError({ nonEmptyListOf(it) }) { - ensureIsContractAddressValid(formValues.contractAddress, networkId) - } + if (formValues.name.isBlank() && + formValues.symbol.isBlank() && + formValues.decimals.isBlank() + ) { + if (formValues.contractAddress.isBlank()) { + AddCustomTokenForm.Validated.Empty + } else { + val contractAddress = withError({ nonEmptyListOf(it) }) { + ensureIsContractAddressValid(formValues.contractAddress, networkId) + } - AddCustomTokenForm.Validated.ContractAddress(contractAddress) + AddCustomTokenForm.Validated.ContractAddressOnly(contractAddress) + } } else { zipOrAccumulate( { ensureIsContractAddressValid(formValues.contractAddress, networkId) }, @@ -43,10 +50,6 @@ class ValidateTokenFormUseCase( contractAddress: String, networkId: Network.ID, ): String { - ensure(contractAddress.isNotBlank()) { - CustomTokenFormValidationException.ContractAddress.Empty - } - val isValid = catch({ repository.validateContractAddress(contractAddress, networkId) }) { raise(CustomTokenFormValidationException.DataError(it)) } diff --git a/domain/manage-tokens/src/main/kotlin/com/tangem/domain/managetokens/model/AddCustomTokenForm.kt b/domain/manage-tokens/src/main/kotlin/com/tangem/domain/managetokens/model/AddCustomTokenForm.kt index a5b5dc5bdc..c2cebdb84e 100644 --- a/domain/manage-tokens/src/main/kotlin/com/tangem/domain/managetokens/model/AddCustomTokenForm.kt +++ b/domain/manage-tokens/src/main/kotlin/com/tangem/domain/managetokens/model/AddCustomTokenForm.kt @@ -11,7 +11,9 @@ sealed class AddCustomTokenForm { sealed class Validated : AddCustomTokenForm() { - data class ContractAddress( + data object Empty : Validated() + + data class ContractAddressOnly( val contractAddress: String, ) : Validated() diff --git a/domain/manage-tokens/src/main/kotlin/com/tangem/domain/managetokens/model/exceptoin/CustomTokenFormValidationException.kt b/domain/manage-tokens/src/main/kotlin/com/tangem/domain/managetokens/model/exceptoin/CustomTokenFormValidationException.kt index a197980215..37781b95e1 100644 --- a/domain/manage-tokens/src/main/kotlin/com/tangem/domain/managetokens/model/exceptoin/CustomTokenFormValidationException.kt +++ b/domain/manage-tokens/src/main/kotlin/com/tangem/domain/managetokens/model/exceptoin/CustomTokenFormValidationException.kt @@ -4,8 +4,6 @@ sealed class CustomTokenFormValidationException { sealed class ContractAddress : CustomTokenFormValidationException() { - data object Empty : ContractAddress() - data object Invalid : ContractAddress() } diff --git a/features/manage-tokens/impl/src/main/kotlin/com/tangem/features/managetokens/model/CustomTokenFormModel.kt b/features/manage-tokens/impl/src/main/kotlin/com/tangem/features/managetokens/model/CustomTokenFormModel.kt index c1c6f9922e..a6a587dc8c 100644 --- a/features/manage-tokens/impl/src/main/kotlin/com/tangem/features/managetokens/model/CustomTokenFormModel.kt +++ b/features/manage-tokens/impl/src/main/kotlin/com/tangem/features/managetokens/model/CustomTokenFormModel.kt @@ -61,15 +61,9 @@ internal class CustomTokenFormModel @Inject constructor( if (params.network.canHandleTokens) { observeTokenFormUpdates() - } else { - modelScope.launch { - customCurrencyValidator.createCoin( - userWalletId = params.userWalletId, - networkId = params.network.id, - derivationPath = getDerivationPath(), - ) - } } + + createCoin() } private fun getInitialState(): CustomTokenFormUM { @@ -112,6 +106,7 @@ internal class CustomTokenFormModel @Inject constructor( } .distinctUntilChanged() .sample(periodMillis = 1_000) + .drop(count = 1) // Skip initial state .onEach { formValues -> customCurrencyValidator.validateForm( userWalletId = params.userWalletId, @@ -151,6 +146,14 @@ internal class CustomTokenFormModel @Inject constructor( } } + private fun createCoin() = modelScope.launch { + customCurrencyValidator.createCoin( + userWalletId = params.userWalletId, + networkId = params.network.id, + derivationPath = getDerivationPath(), + ) + } + private fun updateStateWithCurrency( currency: CryptoCurrency, fillForm: Boolean, diff --git a/features/manage-tokens/impl/src/main/kotlin/com/tangem/features/managetokens/utils/CustomCurrencyValidator.kt b/features/manage-tokens/impl/src/main/kotlin/com/tangem/features/managetokens/utils/CustomCurrencyValidator.kt index 685849ec59..d5ba7409da 100644 --- a/features/manage-tokens/impl/src/main/kotlin/com/tangem/features/managetokens/utils/CustomCurrencyValidator.kt +++ b/features/manage-tokens/impl/src/main/kotlin/com/tangem/features/managetokens/utils/CustomCurrencyValidator.kt @@ -75,9 +75,12 @@ internal class CustomCurrencyValidator @Inject constructor( is AddCustomTokenForm.Validated.All -> { findOrCreateCurrency(userWalletId, networkId, derivationPath, validatedForm) } - is AddCustomTokenForm.Validated.ContractAddress -> { + is AddCustomTokenForm.Validated.ContractAddressOnly -> { findToken(userWalletId, networkId, derivationPath, validatedForm) } + is AddCustomTokenForm.Validated.Empty -> { + createCoin(userWalletId, networkId, derivationPath) + } } }.saveInAndJoin(validateFormJobHolder) } @@ -132,7 +135,7 @@ internal class CustomCurrencyValidator @Inject constructor( userWalletId: UserWalletId, networkId: Network.ID, derivationPath: Network.DerivationPath, - validatedForm: AddCustomTokenForm.Validated.ContractAddress, + validatedForm: AddCustomTokenForm.Validated.ContractAddressOnly, ) { updateStatus(Status.SearchingToken) diff --git a/features/manage-tokens/impl/src/main/kotlin/com/tangem/features/managetokens/utils/ui/CustomCurrencyFormOperations.kt b/features/manage-tokens/impl/src/main/kotlin/com/tangem/features/managetokens/utils/ui/CustomCurrencyFormOperations.kt index d99bb9473b..03c990ab76 100644 --- a/features/manage-tokens/impl/src/main/kotlin/com/tangem/features/managetokens/utils/ui/CustomCurrencyFormOperations.kt +++ b/features/manage-tokens/impl/src/main/kotlin/com/tangem/features/managetokens/utils/ui/CustomCurrencyFormOperations.kt @@ -94,9 +94,6 @@ internal fun CustomTokenFormUM.updateWithContractAddressException( val updatedFields = fields.mutate { it[Field.CONTRACT_ADDRESS] = it.getValue(Field.CONTRACT_ADDRESS).copy( error = when (exception) { - CustomTokenFormValidationException.ContractAddress.Empty -> { - null // Should not display this error - } CustomTokenFormValidationException.ContractAddress.Invalid -> { resourceReference(R.string.custom_token_creation_error_invalid_contract_address) }