From b2d8f0bb768757d8112d6b3ae0062a375dccfa20 Mon Sep 17 00:00:00 2001 From: Tangem Date: Mon, 21 Sep 2020 14:27:44 +0300 Subject: [PATCH] Updated on 2026-08-14 --- .../features/send/redux/SendScreenAction.kt | 2 +- .../redux/middlewares/AmountMiddleware.kt | 23 +++++---------- .../send/redux/reducers/AmountReducer.kt | 28 ++++++------------- 3 files changed, 16 insertions(+), 37 deletions(-) diff --git a/app/src/main/java/com/tangem/tap/features/send/redux/SendScreenAction.kt b/app/src/main/java/com/tangem/tap/features/send/redux/SendScreenAction.kt index bd93f890d5..a97d84ef97 100644 --- a/app/src/main/java/com/tangem/tap/features/send/redux/SendScreenAction.kt +++ b/app/src/main/java/com/tangem/tap/features/send/redux/SendScreenAction.kt @@ -66,7 +66,7 @@ sealed class AmountActionUi : SendScreenActionUi { } sealed class AmountAction : SendScreenAction { - data class SetAmount(val amount: BigDecimal, val isUserInput: Boolean) : AmountAction() + data class SetAmount(val amountCrypto: BigDecimal, val isUserInput: Boolean) : AmountAction() data class SetAmountError(val error: TapError?) : AmountAction() } diff --git a/app/src/main/java/com/tangem/tap/features/send/redux/middlewares/AmountMiddleware.kt b/app/src/main/java/com/tangem/tap/features/send/redux/middlewares/AmountMiddleware.kt index 3c001122bd..e80abad6a3 100644 --- a/app/src/main/java/com/tangem/tap/features/send/redux/middlewares/AmountMiddleware.kt +++ b/app/src/main/java/com/tangem/tap/features/send/redux/middlewares/AmountMiddleware.kt @@ -3,7 +3,6 @@ package com.tangem.tap.features.send.redux.middlewares import com.tangem.blockchain.common.Amount import com.tangem.blockchain.common.TransactionError import com.tangem.common.extensions.isZero -import com.tangem.tap.common.extensions.isNegative import com.tangem.tap.common.redux.AppState import com.tangem.tap.domain.TapError import com.tangem.tap.features.send.redux.AmountAction @@ -31,16 +30,19 @@ class AmountMiddleware { private fun handleUserInput(data: String, appState: AppState?, dispatch: (Action) -> Unit) { val sendState = appState?.sendState ?: return + val amountState = sendState.amountState val data = if (data == ".") "0.0" else data val inputValue = when { data.isEmpty() || data == "0" -> BigDecimal.ZERO else -> BigDecimal(data) } + if (inputValue.isZero() && amountState.amountToSendCrypto.isZero()) return - if (inputValue.isZero() && sendState.amountState.amountToSendCrypto.isZero()) return + val inputValueCrypto = if (amountState.mainCurrency.type == MainCurrencyType.CRYPTO) inputValue + else sendState.convertToCrypto(inputValue) - dispatch(AmountAction.SetAmount(inputValue, true)) + dispatch(AmountAction.SetAmount(inputValueCrypto, true)) dispatch(AmountActionUi.CheckAmountToSend) } @@ -84,20 +86,9 @@ class AmountMiddleware { } private fun setMaxAmount(appState: AppState?, dispatch: (Action) -> Unit) { - val sendState = appState?.sendState ?: return - val amountState = sendState.amountState + val amountState = appState?.sendState?.amountState ?: return - val maxAmount = if (sendState.feeState.feeIsIncluded) amountState.balanceCrypto - else amountState.balanceCrypto.minus(sendState.feeState.getCurrentFee()) - - if (maxAmount.isNegative()) { - dispatch(AmountAction.SetAmountError(TapError.FeeExceedsBalance)) - } else { - val currentCurrencyValue = if (amountState.mainCurrency.type == MainCurrencyType.CRYPTO) maxAmount - else sendState.convertToFiat(maxAmount) - - dispatch(AmountAction.SetAmount(currentCurrencyValue, false)) - } + dispatch(AmountAction.SetAmount(amountState.balanceCrypto, false)) } private fun toggleMainCurrency(appState: AppState?, dispatch: (Action) -> Unit) { diff --git a/app/src/main/java/com/tangem/tap/features/send/redux/reducers/AmountReducer.kt b/app/src/main/java/com/tangem/tap/features/send/redux/reducers/AmountReducer.kt index db8fe61e47..ab2505b1ca 100644 --- a/app/src/main/java/com/tangem/tap/features/send/redux/reducers/AmountReducer.kt +++ b/app/src/main/java/com/tangem/tap/features/send/redux/reducers/AmountReducer.kt @@ -1,7 +1,6 @@ package com.tangem.tap.features.send.redux.reducers import com.tangem.common.extensions.isZero -import com.tangem.tap.common.extensions.scaleToFiat import com.tangem.tap.common.extensions.stripZeroPlainString import com.tangem.tap.features.send.redux.AmountAction import com.tangem.tap.features.send.redux.AmountActionUi @@ -59,25 +58,14 @@ class AmountReducer : SendInternalReducer { private fun handleAction(action: AmountAction, sendState: SendState, state: AmountState): SendState { val result = when (action) { is AmountAction.SetAmount -> { - when (state.mainCurrency.type) { - MainCurrencyType.FIAT -> { - val amountCrypto = sendState.convertToCrypto(action.amount) - state.copy( - viewAmountValue = InputViewValue(action.amount.scaleToFiat(false).stripZeroPlainString(), action.isUserInput), - amountToSendCrypto = amountCrypto, - cursorAtTheSamePosition = true, - error = null - ) - } - MainCurrencyType.CRYPTO -> { - state.copy( - viewAmountValue = InputViewValue(action.amount.stripZeroPlainString(), action.isUserInput), - amountToSendCrypto = action.amount, - cursorAtTheSamePosition = true, - error = null - ) - } - } + val amount = if (state.mainCurrency.type == MainCurrencyType.CRYPTO) action.amountCrypto + else sendState.convertToFiat(action.amountCrypto, true) + state.copy( + viewAmountValue = InputViewValue(amount.stripZeroPlainString(), action.isUserInput), + amountToSendCrypto = action.amountCrypto, + cursorAtTheSamePosition = true, + error = null + ) } is AmountAction.SetAmountError -> { state.copy(error = action.error)