Skip to content

Instantly share code, notes, and snippets.

@diousk
Created April 8, 2020 13:16
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 diousk/b3d41d4887ca3cd4f27bf8bcabe2cf1d to your computer and use it in GitHub Desktop.
Save diousk/b3d41d4887ca3cd4f27bf8bcabe2cf1d to your computer and use it in GitHub Desktop.
suspend fun Fragment.awaitKeyboardHidden() = suspendCancellableCoroutine<Unit> { cont ->
if (!KeyboardVisibilityEvent.isKeyboardVisible(activity)) {
cont.resume(Unit)
return@suspendCancellableCoroutine
}
var unRegistrar: Unregistrar? = null
unRegistrar = KeyboardVisibilityEvent.registerEventListener(activity) { open ->
if (!open) {
// keyboard dismissed, time to resume coroutine
unRegistrar?.unregister()
unRegistrar = null
cont.resume(Unit)
}
}
// remove listener when the job is cancelled
cont.invokeOnCancellation { unRegistrar?.unregister() }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment