Skip to content

Commit a6e5ed1

Browse files
authored
fix: improve nullability checks and fix mem leak in SequenceStepDot (#77)
1 parent d998c44 commit a6e5ed1

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

library/src/main/java/com/transferwise/sequencelayout/SequenceStepDot.kt

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -72,34 +72,38 @@ internal class SequenceStepDot @JvmOverloads constructor(
7272
}
7373

7474
private fun setupAnimator() {
75-
pulseAnimator = AnimatorInflater.loadAnimator(context, R.animator.fading_pulse) as AnimatorSet
76-
pulseAnimator!!.setTarget(pulseView)
77-
pulseAnimator!!.addListener(object : AnimatorListenerAdapter() {
78-
override fun onAnimationEnd(animator: Animator) {
79-
if (isActivated) {
80-
animator.start()
81-
}
75+
pulseAnimator =
76+
(AnimatorInflater.loadAnimator(context, R.animator.fading_pulse) as AnimatorSet).apply {
77+
setTarget(pulseView)
78+
addListener(object : AnimatorListenerAdapter() {
79+
override fun onAnimationEnd(animator: Animator) {
80+
if (isActivated) {
81+
animator.start()
82+
}
83+
}
84+
})
85+
start()
8286
}
83-
})
8487
}
8588

8689
private fun startAnimation() {
87-
if (pulseAnimator == null) {
88-
setupAnimator()
89-
}
90-
if (pulseAnimator!!.isStarted) {
91-
return
90+
pulseAnimator.let {
91+
if (it == null) {
92+
setupAnimator()
93+
} else if (it.isStarted) {
94+
return
95+
}
96+
pulseView.visibility = VISIBLE
9297
}
93-
94-
pulseView.visibility = VISIBLE
95-
pulseAnimator!!.start()
9698
}
9799

98100
private fun stopAnimation() {
99-
if (pulseAnimator == null || !pulseAnimator!!.isStarted) {
100-
return
101+
pulseAnimator.let {
102+
if (it == null || !it.isStarted) {
103+
return
104+
}
105+
it.end()
101106
}
102-
pulseAnimator!!.end()
103107
pulseView.visibility = GONE
104108
}
105109

@@ -122,7 +126,11 @@ internal class SequenceStepDot @JvmOverloads constructor(
122126
}
123127

124128
override fun onDetachedFromWindow() {
125-
pulseAnimator?.cancel()
129+
pulseAnimator?.apply {
130+
removeAllListeners()
131+
cancel()
132+
}
133+
pulseAnimator = null
126134
super.onDetachedFromWindow()
127135
}
128136
}

0 commit comments

Comments
 (0)