diff --git a/library/src/main/java/com/transferwise/sequencelayout/SequenceStepDot.kt b/library/src/main/java/com/transferwise/sequencelayout/SequenceStepDot.kt index 26756fb..8bb8ac7 100644 --- a/library/src/main/java/com/transferwise/sequencelayout/SequenceStepDot.kt +++ b/library/src/main/java/com/transferwise/sequencelayout/SequenceStepDot.kt @@ -72,34 +72,38 @@ internal class SequenceStepDot @JvmOverloads constructor( } private fun setupAnimator() { - pulseAnimator = AnimatorInflater.loadAnimator(context, R.animator.fading_pulse) as AnimatorSet - pulseAnimator!!.setTarget(pulseView) - pulseAnimator!!.addListener(object : AnimatorListenerAdapter() { - override fun onAnimationEnd(animator: Animator) { - if (isActivated) { - animator.start() - } + pulseAnimator = + (AnimatorInflater.loadAnimator(context, R.animator.fading_pulse) as AnimatorSet).apply { + setTarget(pulseView) + addListener(object : AnimatorListenerAdapter() { + override fun onAnimationEnd(animator: Animator) { + if (isActivated) { + animator.start() + } + } + }) + start() } - }) } private fun startAnimation() { - if (pulseAnimator == null) { - setupAnimator() - } - if (pulseAnimator!!.isStarted) { - return + pulseAnimator.let { + if (it == null) { + setupAnimator() + } else if (it.isStarted) { + return + } + pulseView.visibility = VISIBLE } - - pulseView.visibility = VISIBLE - pulseAnimator!!.start() } private fun stopAnimation() { - if (pulseAnimator == null || !pulseAnimator!!.isStarted) { - return + pulseAnimator.let { + if (it == null || !it.isStarted) { + return + } + it.end() } - pulseAnimator!!.end() pulseView.visibility = GONE } @@ -122,7 +126,11 @@ internal class SequenceStepDot @JvmOverloads constructor( } override fun onDetachedFromWindow() { - pulseAnimator?.cancel() + pulseAnimator?.apply { + removeAllListeners() + cancel() + } + pulseAnimator = null super.onDetachedFromWindow() } } \ No newline at end of file