From 5603059bb89cf16f44ca5a4aae09c67b516b70d3 Mon Sep 17 00:00:00 2001 From: Tangem Date: Mon, 27 May 2024 18:53:26 +0500 Subject: [PATCH] Updated on 2026-08-14 --- .../presentation/state/amount/AmountStateFactory.kt | 9 ++++++++- .../state/amount/SendAmountReduceByConverter.kt | 13 +++++++++---- .../state/confirm/SendNotificationFactory.kt | 3 ++- .../impl/presentation/state/fee/FeeCalculation.kt | 6 +++--- .../state/previewdata/SendClickIntentsStub.kt | 1 + .../impl/presentation/viewmodel/SendClickIntents.kt | 1 + .../impl/presentation/viewmodel/SendViewModel.kt | 6 +++++- 7 files changed, 29 insertions(+), 10 deletions(-) diff --git a/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/state/amount/AmountStateFactory.kt b/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/state/amount/AmountStateFactory.kt index 715c696b71..d933d3276f 100644 --- a/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/state/amount/AmountStateFactory.kt +++ b/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/state/amount/AmountStateFactory.kt @@ -62,7 +62,14 @@ internal class AmountStateFactory( fun getOnAmountValueChange(value: String) = amountFieldChangeConverter.convert(value) - fun getOnAmountReduceByState(reduceAmountBy: BigDecimal) = amountReduceByConverter.convert(reduceAmountBy) + fun getOnAmountReduceByState(reduceAmountBy: BigDecimal, reduceAmountByDiff: BigDecimal) = + amountReduceByConverter.convert( + SendAmountReduceByConverter.ReduceByData( + reduceAmountBy = reduceAmountBy, + reduceAmountByDiff = reduceAmountByDiff, + ), + ) + fun getOnAmountReduceToState(reduceAmountTo: BigDecimal) = amountReduceToConverter.convert(reduceAmountTo) fun getOnMaxAmountClick(): SendUiState { diff --git a/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/state/amount/SendAmountReduceByConverter.kt b/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/state/amount/SendAmountReduceByConverter.kt index 59dadf2844..e937600d51 100644 --- a/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/state/amount/SendAmountReduceByConverter.kt +++ b/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/state/amount/SendAmountReduceByConverter.kt @@ -16,8 +16,8 @@ internal class SendAmountReduceByConverter( private val stateRouterProvider: Provider, private val currentStateProvider: Provider, private val cryptoCurrencyStatusProvider: Provider, -) : Converter { - override fun convert(value: BigDecimal): SendUiState { +) : Converter { + override fun convert(value: ReduceByData): SendUiState { val state = currentStateProvider() val cryptoCurrencyStatus = cryptoCurrencyStatusProvider() val isEditState = stateRouterProvider().isEditState @@ -27,7 +27,7 @@ internal class SendAmountReduceByConverter( val fiatDecimals = amountTextField.fiatAmount.decimals val amountValue = amountState.amountTextField.cryptoAmount.value ?: return state - val decimalCryptoValue = amountValue.minus(value) + val decimalCryptoValue = amountValue.minus(value.reduceAmountByDiff) val cryptoValue = decimalCryptoValue.parseBigDecimal(cryptoDecimals) val (fiatValue, decimalFiatValue) = cryptoValue.getFiatValue( fiatRate = cryptoCurrencyStatus.value.fiatRate, @@ -41,7 +41,7 @@ internal class SendAmountReduceByConverter( return state.copyWrapped( isEditState = isEditState, sendState = state.sendState?.copy( - reduceAmountBy = value, + reduceAmountBy = value.reduceAmountBy, ), amountState = amountState.copy( isPrimaryButtonEnabled = !isExceedBalance && !isZero, @@ -59,4 +59,9 @@ internal class SendAmountReduceByConverter( ), ) } + + internal data class ReduceByData( + val reduceAmountBy: BigDecimal, + val reduceAmountByDiff: BigDecimal, + ) } \ No newline at end of file diff --git a/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/state/confirm/SendNotificationFactory.kt b/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/state/confirm/SendNotificationFactory.kt index f6a100ff4e..a79ede8df6 100644 --- a/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/state/confirm/SendNotificationFactory.kt +++ b/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/state/confirm/SendNotificationFactory.kt @@ -213,7 +213,8 @@ internal class SendNotificationFactory( ), onConfirmClick = { clickIntents.onAmountReduceClick( - reduceAmountBy = currencyDeposit.minus(diff), + reduceAmountByDiff = currencyDeposit.minus(diff), + reduceAmountBy = currencyDeposit, clazz = SendNotification.Error.ExistentialDeposit::class.java, ) }, diff --git a/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/state/fee/FeeCalculation.kt b/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/state/fee/FeeCalculation.kt index b500af1923..2171c34904 100644 --- a/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/state/fee/FeeCalculation.kt +++ b/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/state/fee/FeeCalculation.kt @@ -29,7 +29,7 @@ internal fun checkAndCalculateSubtractedAmount( return if (isFeeCoverage) { balance.minus(reduceAmountBy).minus(feeValue) } else { - amountValue.minus(reduceAmountBy) + amountValue } } @@ -44,8 +44,8 @@ internal fun checkFeeCoverage( reduceAmountBy: BigDecimal?, ): Boolean { if (!isSubtractAvailable) return false - val amountWithReduced = (reduceAmountBy ?: BigDecimal.ZERO) + amountValue - return balance < amountWithReduced + feeValue && balance > feeValue && balance >= amountWithReduced + val reducedBy = balance - (reduceAmountBy ?: BigDecimal.ZERO) + return reducedBy < amountValue + feeValue && reducedBy > feeValue && reducedBy >= amountValue } /** diff --git a/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/state/previewdata/SendClickIntentsStub.kt b/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/state/previewdata/SendClickIntentsStub.kt index 0fedb031f2..8c08c468e2 100644 --- a/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/state/previewdata/SendClickIntentsStub.kt +++ b/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/state/previewdata/SendClickIntentsStub.kt @@ -62,6 +62,7 @@ internal object SendClickIntentsStub : SendClickIntents { override fun onAmountReduceClick( reduceAmountBy: BigDecimal?, + reduceAmountByDiff: BigDecimal?, reduceAmountTo: BigDecimal?, clazz: Class, ) {} diff --git a/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/viewmodel/SendClickIntents.kt b/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/viewmodel/SendClickIntents.kt index 32ba81ce73..8eb2a331d5 100644 --- a/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/viewmodel/SendClickIntents.kt +++ b/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/viewmodel/SendClickIntents.kt @@ -69,6 +69,7 @@ internal interface SendClickIntents { fun onAmountReduceClick( reduceAmountBy: BigDecimal? = null, + reduceAmountByDiff: BigDecimal? = reduceAmountBy, reduceAmountTo: BigDecimal? = null, clazz: Class, ) diff --git a/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/viewmodel/SendViewModel.kt b/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/viewmodel/SendViewModel.kt index ed22f1435a..1d5b01c162 100644 --- a/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/viewmodel/SendViewModel.kt +++ b/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/viewmodel/SendViewModel.kt @@ -798,11 +798,15 @@ internal class SendViewModel @Inject constructor( override fun onAmountReduceClick( reduceAmountBy: BigDecimal?, + reduceAmountByDiff: BigDecimal?, reduceAmountTo: BigDecimal?, clazz: Class, ) { uiState = when { - reduceAmountBy != null -> amountStateFactory.getOnAmountReduceByState(reduceAmountBy) + reduceAmountBy != null && reduceAmountByDiff != null -> amountStateFactory.getOnAmountReduceByState( + reduceAmountBy = reduceAmountBy, + reduceAmountByDiff = reduceAmountByDiff, + ) reduceAmountTo != null -> amountStateFactory.getOnAmountReduceToState(reduceAmountTo) else -> return }