Created
October 25, 2019 12:01
-
-
Save EmmanuelGuther/39feefd3192d4c6e2027c3a27376bf0d to your computer and use it in GitHub Desktop.
A collection to kotlin extension functions to manage different credit card
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
| const val CARD_NUMBER_SEPARATOR = " " | |
| const val CARD_DATE_SEPARATOR = "/" | |
| fun EditText.creditCardNumberFormatter(afterTextChanged: (String) -> Unit) { | |
| var count = 0 | |
| this.addTextChangedListener(object : TextWatcher { | |
| override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) { | |
| } | |
| override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) { | |
| } | |
| override fun afterTextChanged(editable: Editable?) { | |
| when { | |
| count <= [email protected]().length && (this@creditCardNumberFormatter | |
| .text.toString().length == 4 || this@creditCardNumberFormatter | |
| .text.toString().length == 9 || this@creditCardNumberFormatter | |
| .text.toString().length == 14) -> { | |
| val newNumber = """${[email protected]}$CARD_NUMBER_SEPARATOR""" | |
| [email protected](newNumber) | |
| val pos = [email protected] | |
| [email protected](pos) | |
| } | |
| count >= [email protected]().length && (this@creditCardNumberFormatter | |
| .text.toString().length == 4 || this@creditCardNumberFormatter | |
| .text.toString().length == 9 || this@creditCardNumberFormatter | |
| .text.toString().length == 14) -> { | |
| [email protected]([email protected]().substring(0, [email protected]().length - 1)) | |
| val pos = [email protected] | |
| [email protected](pos) | |
| } | |
| } | |
| count = [email protected]().length | |
| afterTextChanged.invoke([email protected]()) | |
| } | |
| }) | |
| } | |
| fun EditText.getCreditCardNumberNotFormatted():String{ | |
| return this.text.toString().replace(CARD_NUMBER_SEPARATOR,"") | |
| } | |
| fun EditText.creditCardDateFormatter(afterTextChanged: (String) -> Unit) { | |
| var count = 0 | |
| this.addTextChangedListener(object : TextWatcher { | |
| override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) { | |
| } | |
| override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) { | |
| } | |
| override fun afterTextChanged(editable: Editable?) { | |
| when { | |
| count <= [email protected]().length && ([email protected]().length == 2) -> { | |
| val newNumber = """${[email protected]}$CARD_DATE_SEPARATOR""" | |
| [email protected](newNumber) | |
| val pos = [email protected] | |
| [email protected](pos) | |
| } | |
| count >= [email protected]().length && ([email protected]().length == 2) -> { | |
| [email protected]([email protected]().substring(0, [email protected]().length - 1)) | |
| val pos = [email protected] | |
| [email protected](pos) | |
| } | |
| } | |
| count = [email protected]().length | |
| afterTextChanged.invoke([email protected]()) | |
| } | |
| }) | |
| } | |
| fun EditText.getCreditCarddateNotFormatted():String{ | |
| return this.text.toString().replace(CARD_DATE_SEPARATOR,"") | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment