-
-
Save tuannha95/784718a9335be40d388801bc4b5538e3 to your computer and use it in GitHub Desktop.
Kotlin coroutine debounce for EditText
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
fun <T> debounce( | |
waitMs: Long = 300L, | |
scope: CoroutineScope, | |
destinationFunction: (T) -> Unit | |
): (T) -> Unit { | |
var debounceJob: Job? = null | |
return { param: T -> | |
debounceJob?.cancel() | |
debounceJob = scope.launch { | |
delay(waitMs) | |
destinationFunction(param) | |
} | |
} | |
} | |
val debounceTextChange = debounce(300L, viewModel.viewModelScope, viewModel::search) | |
editText.onChange(debounceTextChange) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
what is onChange method ?
how to implement this in edit text ?