Skip to content

Instantly share code, notes, and snippets.

@EmmanuelGuther
Created October 25, 2019 12:01
Show Gist options
  • Select an option

  • Save EmmanuelGuther/39feefd3192d4c6e2027c3a27376bf0d to your computer and use it in GitHub Desktop.

Select an option

Save EmmanuelGuther/39feefd3192d4c6e2027c3a27376bf0d to your computer and use it in GitHub Desktop.
A collection to kotlin extension functions to manage different credit card
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 <= this@creditCardNumberFormatter.text.toString().length && (this@creditCardNumberFormatter
.text.toString().length == 4 || this@creditCardNumberFormatter
.text.toString().length == 9 || this@creditCardNumberFormatter
.text.toString().length == 14) -> {
val newNumber = """${this@creditCardNumberFormatter.text}$CARD_NUMBER_SEPARATOR"""
this@creditCardNumberFormatter.setText(newNumber)
val pos = this@creditCardNumberFormatter.text.length
this@creditCardNumberFormatter.setSelection(pos)
}
count >= this@creditCardNumberFormatter.text.toString().length && (this@creditCardNumberFormatter
.text.toString().length == 4 || this@creditCardNumberFormatter
.text.toString().length == 9 || this@creditCardNumberFormatter
.text.toString().length == 14) -> {
this@creditCardNumberFormatter.setText(this@creditCardNumberFormatter.text.toString().substring(0, this@creditCardNumberFormatter.text.toString().length - 1))
val pos = this@creditCardNumberFormatter.text.length
this@creditCardNumberFormatter.setSelection(pos)
}
}
count = this@creditCardNumberFormatter.text.toString().length
afterTextChanged.invoke(this@creditCardNumberFormatter.text.toString())
}
})
}
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 <= this@creditCardDateFormatter.text.toString().length && (this@creditCardDateFormatter.text.toString().length == 2) -> {
val newNumber = """${this@creditCardDateFormatter.text}$CARD_DATE_SEPARATOR"""
this@creditCardDateFormatter.setText(newNumber)
val pos = this@creditCardDateFormatter.text.length
this@creditCardDateFormatter.setSelection(pos)
}
count >= this@creditCardDateFormatter.text.toString().length && (this@creditCardDateFormatter.text.toString().length == 2) -> {
this@creditCardDateFormatter.setText(this@creditCardDateFormatter.text.toString().substring(0, this@creditCardDateFormatter.text.toString().length - 1))
val pos = this@creditCardDateFormatter.text.length
this@creditCardDateFormatter.setSelection(pos)
}
}
count = this@creditCardDateFormatter.text.toString().length
afterTextChanged.invoke(this@creditCardDateFormatter.text.toString())
}
})
}
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