Skip to content

Instantly share code, notes, and snippets.

@hiking93
Created April 22, 2022 07:40
Show Gist options
  • Save hiking93/72f415600ff3c6f459d6dc8fcd99f541 to your computer and use it in GitHub Desktop.
Save hiking93/72f415600ff3c6f459d6dc8fcd99f541 to your computer and use it in GitHub Desktop.
fun View.doOnWindowInsetsChanged(
listenToAnimation: Boolean = false,
@DispatchMode dispatchMode: Int = DISPATCH_MODE_STOP,
callback: (v: View, insets: WindowInsetsCompat) -> WindowInsetsCompat,
) {
var isAnimationRunning = false
ViewCompat.setOnApplyWindowInsetsListener(this) { v, insets ->
if (isAnimationRunning) insets else callback(v, insets)
}
if (listenToAnimation) {
ViewCompat.setWindowInsetsAnimationCallback(
this,
object : WindowInsetsAnimationCompat.Callback(dispatchMode) {
override fun onPrepare(animation: WindowInsetsAnimationCompat) {
super.onPrepare(animation)
isAnimationRunning = true
}
override fun onProgress(
insets: WindowInsetsCompat,
runningAnimations: MutableList<WindowInsetsAnimationCompat>
) = callback(this@doOnWindowInsetsChanged, insets)
override fun onEnd(animation: WindowInsetsAnimationCompat) {
super.onEnd(animation)
isAnimationRunning = false
}
}
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment