-
-
Save LouisCAD/c91f971c9771036f6af14fde3527e42a to your computer and use it in GitHub Desktop.
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
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