diff --git a/app/src/main/java/com/tangem/tap/common/text/DecimalDigitsInputFilter.kt b/app/src/main/java/com/tangem/tap/common/text/DecimalDigitsInputFilter.kt new file mode 100644 index 0000000000..bb19069a99 --- /dev/null +++ b/app/src/main/java/com/tangem/tap/common/text/DecimalDigitsInputFilter.kt @@ -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 "" + } +} \ No newline at end of file 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 c4019ff028..efeae4552c 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 @@ -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 ) } diff --git a/app/src/main/java/com/tangem/tap/features/send/redux/states/SendState.kt b/app/src/main/java/com/tangem/tap/features/send/redux/states/SendState.kt index 8a23874e94..d9362c89af 100644 --- a/app/src/main/java/com/tangem/tap/features/send/redux/states/SendState.kt +++ b/app/src/main/java/com/tangem/tap/features/send/redux/states/SendState.kt @@ -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() diff --git a/app/src/main/java/com/tangem/tap/features/send/ui/stateSubscribers/SendStateSubscriber.kt b/app/src/main/java/com/tangem/tap/features/send/ui/stateSubscribers/SendStateSubscriber.kt index c7da55a950..b1ec1b925b 100644 --- a/app/src/main/java/com/tangem/tap/features/send/ui/stateSubscribers/SendStateSubscriber.kt +++ b/app/src/main/java/com/tangem/tap/features/send/ui/stateSubscribers/SendStateSubscriber.kt @@ -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) {