From 817f41da3ed25a1ec15ddeb75f2f61a741bdd89f Mon Sep 17 00:00:00 2001 From: Vladimir Date: Sat, 22 Aug 2020 22:07:41 +0500 Subject: [PATCH] added global catch --- .../widget/AmountWithDecimalEditText.kt | 163 +++++++++--------- 1 file changed, 79 insertions(+), 84 deletions(-) diff --git a/views/src/main/java/ru/touchin/widget/AmountWithDecimalEditText.kt b/views/src/main/java/ru/touchin/widget/AmountWithDecimalEditText.kt index 0b94d90..8dedf59 100644 --- a/views/src/main/java/ru/touchin/widget/AmountWithDecimalEditText.kt +++ b/views/src/main/java/ru/touchin/widget/AmountWithDecimalEditText.kt @@ -47,82 +47,81 @@ class AmountWithDecimalEditText @JvmOverloads constructor( if (isTextWasArtificiallyChanged) { isTextWasArtificiallyChanged = false val cursorPos = selectionStart - var text = text.toString() - possibleDecimalSeparators.forEach { - text = text.replace(it, decimalSeparator) - } - - if (text == decimalSeparator || text.count { it == decimalSeparator[0] } > 1) { - if (abs(textBefore.length - text.length) > 1) { - setTextWhichWasPasted(text) - } else { - setText(textBefore) - setSelection(max(cursorPos - 1, 0)) + try { + var text = text.toString() + possibleDecimalSeparators.forEach { + text = text.replace(it, decimalSeparator) } - return@doOnTextChanged - } - if (text.take(2) == "00") { - if (abs(textBefore.length - text.length) > 1) { - setTextWhichWasPasted(text) - } else { - setText(textBefore) - setSelection(max(cursorPos - 1, 0)) - } - return@doOnTextChanged - } - - if (text.length >= 2 && text[0] == '0' && text[1] != decimalSeparator[0]) { - if (abs(textBefore.length - text.length) > 1) { - setTextWhichWasPasted(text) - } else { - setTextWhichWasPasted(text) - setSelection(max(cursorPos - 1, 0)) - } - return@doOnTextChanged - } - - val decimalPartLength_ = text.split(decimalSeparator).getOrNull(1)?.length - if (!isSeparatorCutInvalidDecimalLength && decimalPartLength_ != null && decimalPartLength_ > decimalPartLength) { - if (abs(textBefore.length - text.length) > 1) { - setTextWhichWasPasted(text) - } else { - setText(textBefore) - setSelection(max(cursorPos - 1, 0)) - } - return@doOnTextChanged - } - - val textAfter = if (text.isNotEmpty()) { - text.withoutFormatting().formatMoney(decimalPartLength_) - } else "" - - if (textAfter.isEmpty()) { - setText("") - return@doOnTextChanged - } - - if (!isTextErased(textBefore, textAfter)) { - val diff = textAfter.length - textBefore.length - 1 - setText(textAfter) - setSelection(min(cursorPos + diff, textAfter.length)) - } else { - if (!textBefore.contains(decimalSeparator) - && textAfter.contains(decimalSeparator) - ) { - setText(textAfter) - setSelection(min(textAfter.length, textAfter.indexOf(decimalSeparator) + 1)) + if (text == decimalSeparator || text.count { it == decimalSeparator[0] } > 1) { + if (abs(textBefore.length - text.length) > 1) { + setTextWhichWasPasted(text) + } else { + setText(textBefore) + setSelection(max(cursorPos - 1, 0)) + } return@doOnTextChanged } - val diff = textBefore.length - textAfter.length - if (diff == 0) { - setText(textAfter) - setSelection(min(cursorPos, textAfter.length)) - } else { - setText(textAfter) - setSelection(max(cursorPos - diff + 1, 0)) + + if (text.take(2) == "00") { + if (abs(textBefore.length - text.length) > 1) { + setTextWhichWasPasted(text) + } else { + setText(textBefore) + setSelection(max(cursorPos - 1, 0)) + } + return@doOnTextChanged } + if (text.length >= 2 && text[0] == '0' && text[1] != decimalSeparator[0]) { + if (abs(textBefore.length - text.length) > 1) { + setTextWhichWasPasted(text) + } else { + setTextWhichWasPasted(text) + setSelection(max(cursorPos - 1, 0)) + } + return@doOnTextChanged + } + + val decimalPartLength_ = text.split(decimalSeparator).getOrNull(1)?.length + if (!isSeparatorCutInvalidDecimalLength && decimalPartLength_ != null && decimalPartLength_ > decimalPartLength) { + if (abs(textBefore.length - text.length) > 1) { + setTextWhichWasPasted(text) + } else { + setText(textBefore) + setSelection(max(cursorPos - 1, 0)) + } + return@doOnTextChanged + } + + val textAfter = if (text.isNotEmpty()) { + text.withoutFormatting().formatMoney(decimalPartLength_) + } else "" + + if (!isTextErased(textBefore, textAfter)) { + val diff = textAfter.length - textBefore.length - 1 + setText(textAfter) + setSelection(min(cursorPos + diff, textAfter.length)) + } else { + if (!textBefore.contains(decimalSeparator) + && textAfter.contains(decimalSeparator) + ) { + setText(textAfter) + setSelection(min(textAfter.length, textAfter.indexOf(decimalSeparator) + 1)) + return@doOnTextChanged + } + val diff = textBefore.length - textAfter.length + if (diff == 0) { + setText(textAfter) + setSelection(min(cursorPos, textAfter.length)) + } else { + setText(textAfter) + setSelection(max(cursorPos - diff + 1, 0)) + } + + } + } catch (e: Throwable) { + setText("") } } else { textBefore = text.toString() @@ -174,21 +173,17 @@ class AmountWithDecimalEditText @JvmOverloads constructor( textAfter.length <= textBefore.length private fun String.formatMoney(decimalPartLength_: Int?): String { - try { - var mask = COMMON_MONEY_MASK - if (decimalPartLength_ != null && decimalPartLength != 0) { - mask += "." + "0".repeat(min(decimalPartLength_, decimalPartLength)) - } - - val formatter = DecimalFormat(mask) - formatter.decimalFormatSymbols = DecimalFormatSymbols().also { - it.decimalSeparator = decimalSeparator[0] - it.groupingSeparator = GROUPING_SEPARATOR - } - return formatter.format(this.prepareForDoubleCast().toDouble().floor()) - } catch (e: Throwable) { - return "" + var mask = COMMON_MONEY_MASK + if (decimalPartLength_ != null && decimalPartLength != 0) { + mask += "." + "0".repeat(min(decimalPartLength_, decimalPartLength)) } + + val formatter = DecimalFormat(mask) + formatter.decimalFormatSymbols = DecimalFormatSymbols().also { + it.decimalSeparator = decimalSeparator[0] + it.groupingSeparator = GROUPING_SEPARATOR + } + return formatter.format(this.prepareForDoubleCast().toDouble().floor()) } private fun Double.floor() =