Skip to content

Instantly share code, notes, and snippets.

@li2
Created September 23, 2019 03:20
Show Gist options
  • Save li2/68b8523f8711ad3610fff78c6c9a07cf to your computer and use it in GitHub Desktop.
Save li2/68b8523f8711ad3610fff78c6c9a07cf to your computer and use it in GitHub Desktop.
/**
* Return query text changes observable.
*
* - filter: to filter undesired text like blank text to avoid unnecessary API call.
* - debounce: to ignore the previous items in the given time and only emit the last one, to avoid too much API calls.
* - distinctUntilChanged: to avoid the duplicate API call.
*/
fun EditText.queryTextChanges(): Observable<String> {
return textChanges()
.map { it.toString() }
.filter { it.isNotBlank() }
.debounce(300, TimeUnit.MILLISECONDS)
.distinctUntilChanged()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment