From 3dcc3311012097082e2fe595ecf6660b6fe5ab5c Mon Sep 17 00:00:00 2001 From: Kukadia Jayesh Date: Wed, 24 Jan 2024 15:17:38 +0530 Subject: [PATCH] Null check for activeDot --- .../sequencelayout/SequenceLayout.kt | 84 ++++++++++--------- 1 file changed, 43 insertions(+), 41 deletions(-) diff --git a/library/src/main/java/com/transferwise/sequencelayout/SequenceLayout.kt b/library/src/main/java/com/transferwise/sequencelayout/SequenceLayout.kt index ebc94b2..f69c66b 100644 --- a/library/src/main/java/com/transferwise/sequencelayout/SequenceLayout.kt +++ b/library/src/main/java/com/transferwise/sequencelayout/SequenceLayout.kt @@ -288,52 +288,54 @@ public class SequenceLayout(context: Context, attrs: AttributeSet?, defStyleAttr if (activeStepIndex != -1) { val activeDot = dotsWrapper.getChildAt(activeStepIndex) - val activeDotTopMargin = (activeDot.layoutParams as LayoutParams).topMargin - val progressBarForegroundTopMargin = - (progressBarForeground.layoutParams as LayoutParams).topMargin - val scaleEnd = - (activeDotTopMargin + progressStepOffSet + (activeDot.measuredHeight / 2) - - progressBarForegroundTopMargin) / progressBarBackground.measuredHeight.toFloat() - - ViewCompat.animate(progressBarForeground) - .setStartDelay(resources.getInteger(R.integer.sequence_step_duration).toLong()) - .scaleY(scaleEnd).setInterpolator(LinearInterpolator()).setDuration( - activeStepIndex * resources.getInteger(R.integer.sequence_step_duration) - .toLong() - ).setUpdateListener { - val animatedOffset = - progressBarForeground.scaleY * progressBarBackground.measuredHeight - dotsWrapper.children().forEachIndexed { i, view -> - if (i > activeStepIndex) { - return@forEachIndexed - } - val dot = view as SequenceStepDot - val dotTopMargin = - (dot.layoutParams as LayoutParams).topMargin - progressBarForegroundTopMargin - (dot.measuredHeight / 2) - if (animatedOffset >= dotTopMargin) { - if (i < activeStepIndex && !dot.isEnabled) { - dot.isEnabled = true - } else if (i == activeStepIndex && !dot.isActivated) { - dot.isActivated = true + if (activeDot != null) { + val activeDotTopMargin = (activeDot.layoutParams as LayoutParams).topMargin + val progressBarForegroundTopMargin = + (progressBarForeground.layoutParams as LayoutParams).topMargin + val scaleEnd = + (activeDotTopMargin + progressStepOffSet + (activeDot.measuredHeight / 2) - + progressBarForegroundTopMargin) / progressBarBackground.measuredHeight.toFloat() + + ViewCompat.animate(progressBarForeground) + .setStartDelay(resources.getInteger(R.integer.sequence_step_duration).toLong()) + .scaleY(scaleEnd).setInterpolator(LinearInterpolator()).setDuration( + activeStepIndex * resources.getInteger(R.integer.sequence_step_duration) + .toLong() + ).setUpdateListener { + val animatedOffset = + progressBarForeground.scaleY * progressBarBackground.measuredHeight + dotsWrapper.children().forEachIndexed { i, view -> + if (i > activeStepIndex) { + return@forEachIndexed + } + val dot = view as SequenceStepDot + val dotTopMargin = + (dot.layoutParams as LayoutParams).topMargin - progressBarForegroundTopMargin - (dot.measuredHeight / 2) + if (animatedOffset >= dotTopMargin) { + if (i < activeStepIndex && !dot.isEnabled) { + dot.isEnabled = true + } else if (i == activeStepIndex && !dot.isActivated) { + dot.isActivated = true + } } } - } - }.withEndAction { - - postDelayed({ - // Animate Every activated dot - stepsWrapper.children.forEachIndexed { index, view -> - if (view is SequenceStep && view.isActive()) { - dotsWrapper.children().getOrNull(index)?.apply { - if (this is SequenceStepDot) { - this.isEnabled = true - this.isActivated = true + }.withEndAction { + + postDelayed({ + // Animate Every activated dot + stepsWrapper.children.forEachIndexed { index, view -> + if (view is SequenceStep && view.isActive()) { + dotsWrapper.children().getOrNull(index)?.apply { + if (this is SequenceStepDot) { + this.isEnabled = true + this.isActivated = true + } } } } - } - }, 500) - }.start() + }, 500) + }.start() + } } }