Updated on 2026-08-14
This commit is contained in:
parent
64af848bd9
commit
b2d8f0bb76
3 changed files with 16 additions and 37 deletions
|
|
@ -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()
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue