From 58fdd5dd9835dda737ba7218a9a716ef00675ff5 Mon Sep 17 00:00:00 2001 From: Kirill Nayduik Date: Mon, 13 Sep 2021 21:53:49 +0300 Subject: [PATCH] Add SimpleTextWatcher for listening text changes --- .../components/utils/SimpleTextWatcher.kt | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 utils/src/main/java/ru/touchin/roboswag/components/utils/SimpleTextWatcher.kt 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 + } +}