Skip to content

Instantly share code, notes, and snippets.

@filpgame
Created June 21, 2017 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save filpgame/f4762217a0d2fc83cd11a7731bd16608 to your computer and use it in GitHub Desktop.
Save filpgame/f4762217a0d2fc83cd11a7731bd16608 to your computer and use it in GitHub Desktop.
MoneyEditText Kotlin
class MoneyEditText : EditText {
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
private var moneyChangeListener: OnMoneyChangeListener? = null
private val moneyTextWatcher: KTextWatcher.() -> Unit = {
var beforeValue = 0L
beforeTextChanged { charSequence, _, _, _ ->
beforeValue = safeRemoveSymbols(charSequence)
}
onTextChanged { charSequence, _, _, _ ->
removeTextChangedListener(this)
val amount = safeRemoveSymbols(charSequence)
val overTheMaximum: Boolean
if (maxAmount != 0L && amount > maxAmount) {
setText(beforeValue.parseCentsToCurrency())
overTheMaximum = true
} else {
setText(amount.parseCentsToCurrency())
overTheMaximum = false
}
setSelection(text.length)
addTextChangedListener(this)
moneyChangeListener?.onMoneyChanged?.invoke(beforeValue, this@MoneyEditText.amount, overTheMaximum)
}
}
var maxAmount = 0L
var amount: Long = 0
get() = safeRemoveSymbols(text)
set(value) {
field = value
setText(value.toString())
}
init {
textWatcher(moneyTextWatcher)
}
private fun safeRemoveSymbols(amount: CharSequence?): Long {
try {
return amount.toString().replace("[R$,.]".toRegex(), "").toLong()
} catch (e: NumberFormatException) {
e("Erro ao Remover símbolos da String: $amount", e)
return 0L
}
}
fun moneyChangeListener(listener: (OnMoneyChangeListener.() -> Unit)) {
moneyChangeListener = OnMoneyChangeListener().apply(listener)
}
inner class OnMoneyChangeListener {
var onMoneyChanged: ((beforeAmount: Long, currentAmount: Long, overTheMax: Boolean) -> Unit)? = null
fun onMoneyChanged(event: (beforeAmount: Long, currentAmount: Long, overTheMax: Boolean) -> Unit) {
onMoneyChanged = event
}
}
}
fun Double.asLong() = (this * 100).toLong()
fun Double.parseCentsToCurrency(): String {
return NumberFormat.getCurrencyInstance(Locale("pt", "BR")).format((this / 100))
}
fun Long.parseCentsToCurrency(): String {
return NumberFormat.getCurrencyInstance(Locale("pt", "BR")).format((this.toDouble() / 100))
}
fun TextView.textWatcher(init: KTextWatcher.() -> Unit) = addTextChangedListener(KTextWatcher().apply(init))
class KTextWatcher : TextWatcher {
private var beforeTextChanged: ((CharSequence?, Int, Int, Int) -> Unit)? = null
private var onTextChanged: ((CharSequence?, Int, Int, Int) -> Unit)? = null
private var afterTextChanged: ((Editable?) -> Unit)? = null
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
beforeTextChanged?.invoke(s, start, count, after)
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
onTextChanged?.invoke(s, start, before, count)
}
override fun afterTextChanged(s: Editable?) {
afterTextChanged?.invoke(s)
}
fun beforeTextChanged(listener: (CharSequence?, Int, Int, Int) -> Unit) {
beforeTextChanged = listener
}
fun onTextChanged(listener: (CharSequence?, Int, Int, Int) -> Unit) {
onTextChanged = listener
}
fun afterTextChanged(listener: (Editable?) -> Unit) {
afterTextChanged = listener
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment