Created
April 8, 2020 13:16
-
-
Save diousk/b3d41d4887ca3cd4f27bf8bcabe2cf1d 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
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