Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Extensions for Android Animation package.
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