Skip to content

Instantly share code, notes, and snippets.

@devrath
Created July 27, 2022 03:40
Show Gist options
  • Select an option

  • Save devrath/115e276d42b0fc3254bd8070760d99f7 to your computer and use it in GitHub Desktop.

Select an option

Save devrath/115e276d42b0fc3254bd8070760d99f7 to your computer and use it in GitHub Desktop.
CallbackFlow builder
// https://medium.com/swlh/callback-flows-in-android-d2c6ed5bc488
fun TextView.textWatcherFlow(): Flow<CharSequence?> = callbackFlow<CharSequence?> {
val textWatcher = object: TextWatcher {
override fun afterTextChanged(s: Editable?) {
sendBlocking(s)
}
// Other callback mehthods
}
addTextChangedListener(textWatcher)
awaitClose { removeTextChangedListener(textWatcher) }
}.buffer(Channel.CONFLATED)
.debounce(300L)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment