Skip to content

Instantly share code, notes, and snippets.

@LouisCAD
Last active August 21, 2018 12:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LouisCAD/c91f971c9771036f6af14fde3527e42a to your computer and use it in GitHub Desktop.
Save LouisCAD/c91f971c9771036f6af14fde3527e42a to your computer and use it in GitHub Desktop.
import android.view.View
import kotlinx.coroutines.experimental.channels.Channel
import kotlinx.coroutines.experimental.channels.ReceiveChannel
import kotlinx.coroutines.experimental.suspendCancellableCoroutine
suspend fun View.awaitOneClick() = suspendCancellableCoroutine<Unit> { continuation ->
continuation.invokeOnCancellation {
setOnClickListener(null)
}
setOnClickListener {
setOnClickListener(null)
continuation.resume(Unit)
}
}
suspend fun View.clicks(
capacity: Int = Channel.UNLIMITED
): ReceiveChannel<Unit> = Channel<Unit>(capacity).apply { setOnClickListener { offer(Unit) } }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment