2020-11-16 19:23:06 +01:00
package org.mosad.teapod.ui.components
import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ObjectAnimator
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.FrameLayout
import kotlinx.android.synthetic.main.button_fast_forward.view.*
import org.mosad.teapod.R
2020-11-20 11:20:11 +01:00
class FastForwardButton ( context : Context , attrs : AttributeSet ? ) : FrameLayout ( context , attrs ) {
2020-11-16 19:23:06 +01:00
private val animationDuration : Long = 800
2020-11-20 11:20:11 +01:00
private val buttonAnimation : ObjectAnimator
private val labelAnimation : ObjectAnimator
var onAnimationEndCallback : ( ( ) -> Unit ) ? = null
2020-11-16 19:23:06 +01:00
init {
inflate ( context , R . layout . button _fast _forward , this )
2020-11-20 11:20:11 +01:00
buttonAnimation = ObjectAnimator . ofFloat ( imageButton , View . ROTATION , 0f , 50f ) . apply {
2020-11-16 19:23:06 +01:00
duration = animationDuration / 4
repeatCount = 1
repeatMode = ObjectAnimator . REVERSE
addListener ( object : AnimatorListenerAdapter ( ) {
override fun onAnimationStart ( animation : Animator ? ) {
imageButton . isEnabled = false // disable button
2020-11-16 20:42:25 +01:00
imageButton . setBackgroundResource ( R . drawable . ic _baseline _forward _24 )
2020-11-16 19:23:06 +01:00
}
} )
}
2020-11-20 11:20:11 +01:00
labelAnimation = ObjectAnimator . ofFloat ( textView , View . TRANSLATION _X , 35f ) . apply {
2020-11-16 19:23:06 +01:00
duration = animationDuration
addListener ( object : AnimatorListenerAdapter ( ) {
2020-11-20 11:20:11 +01:00
// the label animation takes longer then the button animation, reset stuff in here
2020-11-16 19:23:06 +01:00
override fun onAnimationEnd ( animation : Animator ? ) {
imageButton . isEnabled = true // enable button
2020-11-16 20:42:25 +01:00
imageButton . setBackgroundResource ( R . drawable . ic _baseline _forward _10 _24 )
2020-11-16 19:23:06 +01:00
textView . visibility = View . GONE
textView . animate ( ) . translationX ( 0f )
2020-11-20 11:20:11 +01:00
onAnimationEndCallback ?. invoke ( )
2020-11-16 19:23:06 +01:00
}
} )
}
2020-11-20 11:20:11 +01:00
}
fun setOnButtonClickListener ( func : FastForwardButton . ( ) -> Unit ) {
imageButton . setOnClickListener {
func ( )
}
}
2020-11-16 19:23:06 +01:00
2020-11-20 11:20:11 +01:00
fun runOnClickAnimation ( ) {
// run button animation
buttonAnimation . start ( )
// run lbl animation
textView . visibility = View . VISIBLE
labelAnimation . start ( )
2020-11-16 19:23:06 +01:00
}
}