diff --git a/utils/src/main/java/ru/touchin/roboswag/components/utils/SimpleTextWatcher.kt b/utils/src/main/java/ru/touchin/roboswag/components/utils/SimpleTextWatcher.kt new file mode 100644 index 0000000..e935e93 --- /dev/null +++ b/utils/src/main/java/ru/touchin/roboswag/components/utils/SimpleTextWatcher.kt @@ -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 + } +}