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
2021-12-26 20:22:00 +01:00
import android.view.LayoutInflater
2020-11-16 19:23:06 +01:00
import android.view.View
import android.widget.FrameLayout
import org.mosad.teapod.R
2021-12-26 20:22:00 +01:00
import org.mosad.teapod.databinding.ButtonFastForwardBinding
2020-11-16 19:23:06 +01:00
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
2021-12-26 20:22:00 +01:00
private val binding = ButtonFastForwardBinding . inflate ( LayoutInflater . from ( context ) )
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 {
2021-12-26 20:22:00 +01:00
addView ( binding . root )
2020-11-16 19:23:06 +01:00
2021-12-26 20:22:00 +01:00
buttonAnimation = ObjectAnimator . ofFloat ( binding . 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 ? ) {
2021-12-26 20:22:00 +01:00
binding . imageButton . isEnabled = false // disable button
binding . imageButton . setBackgroundResource ( R . drawable . ic _baseline _forward _24 )
2020-11-16 19:23:06 +01:00
}
} )
}
2021-12-26 20:22:00 +01:00
labelAnimation = ObjectAnimator . ofFloat ( binding . 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 ? ) {
2021-12-26 20:22:00 +01:00
binding . imageButton . isEnabled = true // enable button
binding . imageButton . setBackgroundResource ( R . drawable . ic _baseline _forward _10 _24 )
2020-11-16 19:23:06 +01:00
2021-12-26 20:22:00 +01:00
binding . textView . visibility = View . GONE
binding . 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 ) {
2021-12-26 20:22:00 +01:00
binding . imageButton . setOnClickListener {
2020-11-20 11:20:11 +01:00
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
2021-12-26 20:22:00 +01:00
binding . textView . visibility = View . VISIBLE
2020-11-20 11:20:11 +01:00
labelAnimation . start ( )
2020-11-16 19:23:06 +01:00
}
}