Updated on 2026-08-14

This commit is contained in:
Tangem 2020-09-14 17:02:46 +03:00
parent 00400a72dd
commit d9f6b0576e
4 changed files with 36 additions and 0 deletions

View file

@ -0,0 +1,26 @@
package com.tangem.tap.common.text
import android.text.InputFilter
import android.text.Spanned
import java.util.regex.Pattern
/**
[REDACTED_AUTHOR]
*/
class DecimalDigitsInputFilter(digitsBeforeDecimal: Int, digitsAfterDecimal: Int) : InputFilter {
private val pattern: Pattern = Pattern.compile("(([1-9]{1}[0-9]{0," + (digitsBeforeDecimal - 1) + "})?||[0]{1})((\\.[0-9]{0," + digitsAfterDecimal + "})?)||(\\.)?")
override fun filter(source: CharSequence, sourceStart: Int, sourceEnd: Int, destination: Spanned, destinationStart: Int, destinationEnd: Int): CharSequence? {
val destString = destination.toString()
val prefix = destString.substring(0, destinationStart)
val suffix = destString.substring(destinationEnd, destString.length)
val newDestination = prefix + suffix
val resultPrefix = newDestination.substring(0, destinationStart)
val resultSuffix = newDestination.substring(destinationStart, newDestination.length)
val result = resultPrefix + source.toString() + resultSuffix
val hasMatches = pattern.matcher(result).matches()
return if (hasMatches) null else ""
}
}

View file

@ -43,6 +43,7 @@ class AmountReducer : SendInternalReducer {
viewAmountValue = fiatToSend.stripZeroPlainString(),
viewBalanceValue = converter.toFiat(state.balanceCrypto).stripZeroPlainString(),
mainCurrency = Value(MainCurrencyType.FIAT, TapCurrency.main),
maxLengthOfAmount = sendState.getDecimals(action.mainCurrency),
cursorAtTheSamePosition = false
)
}
@ -52,6 +53,7 @@ class AmountReducer : SendInternalReducer {
viewAmountValue = state.amountToSendCrypto.stripZeroPlainString(),
viewBalanceValue = state.balanceCrypto.stripZeroPlainString(),
mainCurrency = mainCurrency,
maxLengthOfAmount = sendState.getDecimals(action.mainCurrency),
cursorAtTheSamePosition = false
)
}

View file

@ -32,6 +32,11 @@ data class SendState(
}
fun addressPayIdIsReady(): Boolean = store.state.sendState.addressPayIdState.isReady()
fun getDecimals(type: MainCurrencyType): Int = when (type) {
MainCurrencyType.FIAT -> 2
MainCurrencyType.CRYPTO -> amount?.decimals ?: 0
}
}
class NoneState : StateType
@ -44,6 +49,7 @@ data class AmountState(
val amountToSendCrypto: BigDecimal = BigDecimal.ZERO,
val balanceCrypto: BigDecimal = BigDecimal.ZERO,
val cursorAtTheSamePosition: Boolean = true,
val maxLengthOfAmount: Int = 2,
val error: AmountAction.Error? = null
) : StateType {
fun isReady(): Boolean = error == null && !amountToSendCrypto.isZero()

View file

@ -9,6 +9,7 @@ import com.tangem.tap.common.extensions.beginDelayedTransition
import com.tangem.tap.common.extensions.enableError
import com.tangem.tap.common.extensions.show
import com.tangem.tap.common.extensions.update
import com.tangem.tap.common.text.DecimalDigitsInputFilter
import com.tangem.tap.features.send.BaseStoreFragment
import com.tangem.tap.features.send.redux.AddressPayIdVerifyAction.Error
import com.tangem.tap.features.send.redux.AmountAction
@ -91,6 +92,7 @@ class SendStateSubscriber(fragment: BaseStoreFragment) : FragmentStateSubscriber
}
}
fg.etAmountToSend.filters = arrayOf(DecimalDigitsInputFilter(12, state.maxLengthOfAmount))
val amountToSend = state.viewAmountValue
fg.tvAmountToSendShadow.text = amountToSend
if (amountToSend.length > 10) {