Last active
March 30, 2022 23:38
-
-
Save behdad222/ad2e5b9a1927a398d7c8f203fa6524a6 to your computer and use it in GitHub Desktop.
Don't use this
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Don't use this | |
class PriceInputTextWatcher( | |
private val editText: EditText | |
) : TextWatcher { | |
private var ignoreOnPhoneChange = false | |
private var characterAction = CHARACTER_ACTION_UNKNOWN | |
private var actionPosition = 0 | |
override fun beforeTextChanged( | |
charSequence: CharSequence?, | |
start: Int, | |
count: Int, | |
after: Int | |
) { | |
if (count == 0 && after == 1) { | |
characterAction = CHARACTER_ACTION_A | |
} else if (count == 1 && after == 0) { | |
if (charSequence?.get(start) == SEPARATED_CHAR && start > 0) { | |
characterAction = CHARACTER_ACTION_C | |
actionPosition = start - 1 | |
} else { | |
characterAction = CHARACTER_ACTION_B | |
} | |
} else { | |
characterAction = CHARACTER_ACTION_UNKNOWN | |
} | |
} | |
override fun onTextChanged(charSequence: CharSequence?, start: Int, before: Int, count: Int) { | |
//ignore | |
} | |
override fun afterTextChanged(charSequence: Editable?) { | |
if (ignoreOnPhoneChange) { | |
return | |
} | |
var start: Int = editText.selectionStart | |
val phoneChars = "0123456789" | |
var str: String = editText.text.toString() | |
if (characterAction == CHARACTER_ACTION_C) { | |
str = str.substring(0, actionPosition) + str.substring(actionPosition + 1) | |
start-- | |
} | |
val builder = StringBuilder(str.length) | |
for (a in str.indices) { | |
val ch = str.substring(a, a + 1) | |
if (phoneChars.contains(ch)) { | |
builder.append(ch) | |
} | |
} | |
ignoreOnPhoneChange = true | |
start = makeFormattedText(builder, start) | |
charSequence?.replace(0, charSequence.length, builder) | |
if (start >= 0) { | |
editText.setSelection(min(start, editText.length())) | |
} | |
editText.invalidate() | |
ignoreOnPhoneChange = false | |
} | |
private fun makeFormattedText(builder: StringBuilder, start: Int): Int { | |
var index = 0 | |
var newStart = start | |
while (index < builder.length) { | |
if (index < PATTERN.length) { | |
if (PATTERN[index] == SEPARATED_CHAR) { | |
builder.insert(index, SEPARATED_CHAR) | |
index++ | |
if (start == index | |
&& characterAction != CHARACTER_ACTION_B | |
&& characterAction != CHARACTER_ACTION_C | |
) { | |
newStart++ | |
} | |
} | |
} else { | |
builder.insert(index, SEPARATED_CHAR) | |
if (start == index + 1 | |
&& characterAction != CHARACTER_ACTION_B | |
&& characterAction != CHARACTER_ACTION_C | |
) { | |
newStart++ | |
} | |
break | |
} | |
index++ | |
} | |
return newStart | |
} | |
companion object { | |
const val CHARACTER_ACTION_UNKNOWN = -1 | |
const val CHARACTER_ACTION_A = 1 | |
const val CHARACTER_ACTION_B = 2 | |
const val CHARACTER_ACTION_C = 3 | |
const val SEPARATED_CHAR = '٬' | |
const val PATTERN: String = "000٬000٬000٬000٬000٬000٬000٬000" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment