Created
April 5, 2021 13:16
-
-
Save KwabenBerko/f9a1506018fd9a030869ceedf081689e to your computer and use it in GitHub Desktop.
Debounced Text Watcher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ExperimentalCoroutinesApi | |
fun EditText.doAfterTextChanged(): Flow<CharSequence?> { | |
return callbackFlow<CharSequence?> { | |
val listener = object : TextWatcher { | |
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) = | |
Unit | |
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) = Unit | |
override fun afterTextChanged(s: Editable?) { | |
offer(s) | |
} | |
} | |
addTextChangedListener(listener) | |
awaitClose { removeTextChangedListener(listener) } | |
}.onStart { emit(text) } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: