Is there any fancy way to implement debounce logic with Kotlin Android?
I\'m not using Rx in project.
There is a way in Java, but it is too big
@masterwork's answer worked perfectly fine. Here it is for ImageButton with compiler warnings removed:
@ExperimentalCoroutinesApi // This is still experimental API
fun ImageButton.onClicked() = callbackFlow {
setOnClickListener { offer(Unit) }
awaitClose { setOnClickListener(null) }
}
// Listener for button
val someButton = someView.findViewById(R.id.some_button)
someButton
.onClicked()
.debounce(500) // 500ms debounce time
.onEach {
clickAction()
}
.launchIn(lifecycleScope)