Last active
September 8, 2020 12:11
-
-
Save NikolaDespotoski/f654ad62de9435ae1141ba227ae1e8d0 to your computer and use it in GitHub Desktop.
Extensions for Android Animation package.
This file contains hidden or 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
data class AnimationListenerChain(val animation: Animation, val listener: Animation.AnimationListener = object : Animation.AnimationListener() { | |
override fun onAnimationStart(animation: Animation?) {} | |
override fun onAnimationEnd(animation: Animation?) {} | |
override fun onAnimationRepeat(animation: Animation?) {} | |
}) | |
inline fun Animation.doOnStart(crossinline onStart: () -> Unit): AnimationListenerChain { | |
val chain = AnimationListenerChain(this) | |
return chain.doOnStart(onStart) | |
} | |
inline fun Animation.doOnEnd(crossinline onEnd: () -> Unit): AnimationListenerChain { | |
val chain = AnimationListenerChain(this) | |
return chain.doOnEnd(onEnd) | |
} | |
inline fun AnimationListenerChain.doOnEnd(crossinline onEnd: () -> Unit): AnimationListenerChain { | |
val nextListener: Animation.AnimationListener = object : AnimationListenerAdapter() { | |
override fun onAnimationEnd(animation: Animation?) { | |
onEnd() | |
} | |
override fun onAnimationRepeat(animation: Animation?) { | |
listener.onAnimationRepeat(animation) | |
} | |
override fun onAnimationStart(animation: Animation?) { | |
listener.onAnimationStart(animation) | |
} | |
} | |
animation.setAnimationListener(nextListener) | |
return copy(listener = nextListener) | |
} | |
inline fun AnimationListenerChain.doOnStart(crossinline onStart: () -> Unit): AnimationListenerChain { | |
val nextListener: Animation.AnimationListener = object : Animation.AnimationListener { | |
override fun onAnimationEnd(animation: Animation?) { | |
listener.onAnimationEnd(animation) | |
} | |
override fun onAnimationRepeat(animation: Animation?) { | |
listener.onAnimationRepeat(animation) | |
} | |
override fun onAnimationStart(animation: Animation?) { | |
onStart() | |
} | |
} | |
animation.setAnimationListener(nextListener) | |
return copy(listener = nextListener) | |
} | |
inline fun AnimationListenerChain.doOnRepeat(crossinline onRepeat: () -> Unit): AnimationListenerChain { | |
val nextListener: Animation.AnimationListener = object : Animation.AnimationListener { | |
override fun onAnimationEnd(animation: Animation?) { | |
listener.onAnimationEnd(animation) | |
} | |
override fun onAnimationRepeat(animation: Animation?) { | |
onRepeat() | |
} | |
override fun onAnimationStart(animation: Animation?) { | |
listener.onAnimationStart(animation) | |
} | |
} | |
animation.setAnimationListener(nextListener) | |
return copy(listener = nextListener) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment