Merge pull request #220 from TouchInstinct/simple_text_watcher

Add SimpleTextWatcher for listening text changes
This commit is contained in:
Kirill Nayduik 2021-09-13 22:27:27 +03:00 committed by GitHub
commit d9a9957cd4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 24 additions and 0 deletions

View File

@ -0,0 +1,24 @@
package ru.touchin.roboswag.components.utils
import android.text.Editable
import android.text.TextWatcher
class SimpleTextWatcher(private val onTextChanged: (Editable) -> Unit) : TextWatcher {
private var ignore = false
override fun afterTextChanged(s: Editable) {
doPreventListenerLoop { onTextChanged(s) }
}
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) = Unit
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) = Unit
private fun doPreventListenerLoop(callback: () -> Unit) {
if (ignore) return
ignore = true
callback()
ignore = false
}
}