Updated on 2026-08-14

This commit is contained in:
Tangem 2024-05-03 12:56:05 +05:00
parent 0bbe6b8a4a
commit 7362f95be4
9 changed files with 101 additions and 11 deletions

View file

@ -142,6 +142,7 @@ internal sealed class SendStates {
val txUrl: String,
val ignoreAmountReduce: Boolean,
val reduceAmountBy: BigDecimal?,
val reduceAmountTo: BigDecimal?,
val isFromConfirmation: Boolean,
val showTapHelp: Boolean,
val notifications: ImmutableList<SendNotification>,

View file

@ -45,8 +45,15 @@ internal class AmountStateFactory(
currentStateProvider = currentStateProvider,
)
}
private val amountReducedConverter by lazy {
SendAmountReducedConverter(
private val amountReduceByConverter by lazy {
SendAmountReduceByConverter(
stateRouterProvider = stateRouterProvider,
currentStateProvider = currentStateProvider,
cryptoCurrencyStatusProvider = cryptoCurrencyStatusProvider,
)
}
private val amountReduceToConverter by lazy {
SendAmountReduceToConverter(
stateRouterProvider = stateRouterProvider,
currentStateProvider = currentStateProvider,
cryptoCurrencyStatusProvider = cryptoCurrencyStatusProvider,
@ -55,7 +62,8 @@ internal class AmountStateFactory(
fun getOnAmountValueChange(value: String) = amountFieldChangeConverter.convert(value)
fun getOnAmountReducedState(reduceAmountBy: BigDecimal) = amountReducedConverter.convert(reduceAmountBy)
fun getOnAmountReduceByState(reduceAmountBy: BigDecimal) = amountReduceByConverter.convert(reduceAmountBy)
fun getOnAmountReduceToState(reduceAmountTo: BigDecimal) = amountReduceToConverter.convert(reduceAmountTo)
fun getOnMaxAmountClick(): SendUiState {
return amountFieldMaxAmountConverter.convert(Unit)

View file

@ -12,7 +12,7 @@ import com.tangem.utils.converter.Converter
import com.tangem.utils.isNullOrZero
import java.math.BigDecimal
internal class SendAmountReducedConverter(
internal class SendAmountReduceByConverter(
private val stateRouterProvider: Provider<StateRouter>,
private val currentStateProvider: Provider<SendUiState>,
private val cryptoCurrencyStatusProvider: Provider<CryptoCurrencyStatus>,

View file

@ -0,0 +1,60 @@
package com.tangem.features.send.impl.presentation.state.amount
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.ui.text.input.KeyboardType
import com.tangem.common.extensions.isZero
import com.tangem.core.ui.utils.parseBigDecimal
import com.tangem.domain.tokens.model.CryptoCurrencyStatus
import com.tangem.features.send.impl.presentation.state.SendUiState
import com.tangem.features.send.impl.presentation.state.StateRouter
import com.tangem.utils.Provider
import com.tangem.utils.converter.Converter
import com.tangem.utils.isNullOrZero
import java.math.BigDecimal
internal class SendAmountReduceToConverter(
private val stateRouterProvider: Provider<StateRouter>,
private val currentStateProvider: Provider<SendUiState>,
private val cryptoCurrencyStatusProvider: Provider<CryptoCurrencyStatus>,
) : Converter<BigDecimal, SendUiState> {
override fun convert(value: BigDecimal): SendUiState {
val state = currentStateProvider()
val cryptoCurrencyStatus = cryptoCurrencyStatusProvider()
val isEditState = stateRouterProvider().isEditState
val amountState = state.getAmountState(isEditState) ?: return state
val amountTextField = amountState.amountTextField
val cryptoDecimals = amountTextField.cryptoAmount.decimals
val fiatDecimals = amountTextField.fiatAmount.decimals
val cryptoValue = value.parseBigDecimal(cryptoDecimals)
val (fiatValue, decimalFiatValue) = cryptoValue.getFiatValue(
fiatRate = cryptoCurrencyStatus.value.fiatRate,
isFiatValue = false,
decimals = fiatDecimals,
)
val checkValue = if (amountTextField.isFiatValue) fiatValue else cryptoValue
val isExceedBalance = checkValue.checkExceedBalance(cryptoCurrencyStatus, amountTextField)
val isZero = if (amountTextField.isFiatValue) decimalFiatValue.isNullOrZero() else value.isZero()
return state.copyWrapped(
isEditState = isEditState,
sendState = state.sendState?.copy(
reduceAmountBy = value,
),
amountState = amountState.copy(
isPrimaryButtonEnabled = !isExceedBalance && !isZero,
amountTextField = amountTextField.copy(
value = cryptoValue,
fiatValue = fiatValue,
isError = isExceedBalance,
cryptoAmount = amountTextField.cryptoAmount.copy(value = value),
fiatAmount = amountTextField.fiatAmount.copy(value = decimalFiatValue),
keyboardOptions = KeyboardOptions(
imeAction = getKeyboardAction(isExceedBalance, value),
keyboardType = KeyboardType.Number,
),
),
),
)
}
}

View file

@ -17,6 +17,7 @@ internal class SendConfirmStateConverter(
txUrl = "",
ignoreAmountReduce = false,
reduceAmountBy = null,
reduceAmountTo = null,
isFromConfirmation = true,
showTapHelp = isTapHelpPreviewEnabledProvider(),
notifications = persistentListOf(),

View file

@ -166,8 +166,8 @@ internal class SendNotificationFactory(
),
onConfirmClick = {
clickIntents.onAmountReduceClick(
utxoLimit.maxAmount,
SendNotification.Error.TransactionLimitError::class.java,
reduceAmountTo = utxoLimit.maxAmount,
clazz = SendNotification.Error.TransactionLimitError::class.java,
)
},
),
@ -226,7 +226,10 @@ internal class SendNotificationFactory(
SendNotification.Warning.HighFeeError(
amount = threshold.toPlainString(),
onConfirmClick = {
clickIntents.onAmountReduceClick(threshold, SendNotification.Warning.HighFeeError::class.java)
clickIntents.onAmountReduceClick(
reduceAmountBy = threshold,
clazz = SendNotification.Warning.HighFeeError::class.java,
)
},
onCloseClick = {
clickIntents.onNotificationCancel(SendNotification.Warning.HighFeeError::class.java)

View file

@ -60,7 +60,11 @@ internal object SendClickIntentsStub : SendClickIntents {
override fun onShareClick() {}
override fun onAmountReduceClick(reduceAmountBy: BigDecimal, clazz: Class<out SendNotification>) {}
override fun onAmountReduceClick(
reduceAmountBy: BigDecimal?,
reduceAmountTo: BigDecimal?,
clazz: Class<out SendNotification>,
) {}
override fun onNotificationCancel(clazz: Class<out SendNotification>) {}
}

View file

@ -67,7 +67,11 @@ internal interface SendClickIntents {
fun onShareClick()
fun onAmountReduceClick(reduceAmountBy: BigDecimal, clazz: Class<out SendNotification>)
fun onAmountReduceClick(
reduceAmountBy: BigDecimal? = null,
reduceAmountTo: BigDecimal? = null,
clazz: Class<out SendNotification>,
)
fun onNotificationCancel(clazz: Class<out SendNotification>)
// endregion

View file

@ -797,8 +797,17 @@ internal class SendViewModel @Inject constructor(
analyticsEventHandler.send(SendAnalyticEvents.ShareButtonClicked)
}
override fun onAmountReduceClick(reduceAmountBy: BigDecimal, clazz: Class<out SendNotification>) {
uiState = amountStateFactory.getOnAmountReducedState(reduceAmountBy)
override fun onAmountReduceClick(
reduceAmountBy: BigDecimal?,
reduceAmountTo: BigDecimal?,
clazz: Class<out SendNotification>,
) {
uiState = when {
reduceAmountBy != null -> amountStateFactory.getOnAmountReduceByState(reduceAmountBy)
reduceAmountTo != null -> amountStateFactory.getOnAmountReduceToState(reduceAmountTo)
else -> return
}
uiState = sendNotificationFactory.dismissNotificationState(clazz)
feeReload()
}