Created
April 5, 2021 13:33
-
-
Save KwabenBerko/7f87e0dbac8ae618731dfc83cec1fc44 to your computer and use it in GitHub Desktop.
Debounced SearchView
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 SearchView.doOnQueryTextChanged(): Flow<CharSequence?> { | |
return callbackFlow<CharSequence?> { | |
val listener = object : SearchView.OnQueryTextListener { | |
override fun onQueryTextSubmit(query: String?) = false | |
override fun onQueryTextChange(newText: String?): Boolean { | |
offer(newText) | |
return false | |
} | |
} | |
setOnQueryTextListener(listener) | |
awaitClose { setOnQueryTextListener(null) } | |
}.onStart { emit(query) } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: