Skip to content

Commit 630558e

Browse files
Use nested functions.
1 parent 81c4b82 commit 630558e

1 file changed

Lines changed: 14 additions & 35 deletions

File tree

  • app/src/main/java/org/schabi/newpipe/ktx

app/src/main/java/org/schabi/newpipe/ktx/View.kt

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -90,64 +90,43 @@ fun View.animate(
9090
*/
9191
fun View.animateBackgroundColor(duration: Long, @ColorInt colorStart: Int, @ColorInt colorEnd: Int) {
9292
if (MainActivity.DEBUG) {
93-
Log.d(
94-
TAG,
95-
"animateBackgroundColor() called with: " +
96-
"view = [" + this + "], duration = [" + duration + "], " +
97-
"colorStart = [" + colorStart + "], colorEnd = [" + colorEnd + "]"
93+
Log.d(TAG, "animateBackgroundColor() called with: view = [$this], duration = [$duration], " +
94+
"colorStart = [$colorStart], colorEnd = [$colorEnd]"
9895
)
9996
}
100-
val empty = arrayOf(IntArray(0))
10197
val viewPropertyAnimator = ValueAnimator.ofObject(ArgbEvaluator(), colorStart, colorEnd)
10298
viewPropertyAnimator.interpolator = FastOutSlowInInterpolator()
10399
viewPropertyAnimator.duration = duration
104-
viewPropertyAnimator.addUpdateListener { animation: ValueAnimator ->
105-
ViewCompat.setBackgroundTintList(this, ColorStateList(empty, intArrayOf(animation.animatedValue as Int)))
100+
101+
fun listenerAction(color: Int) {
102+
ViewCompat.setBackgroundTintList(this, ColorStateList.valueOf(color))
106103
}
107-
viewPropertyAnimator.addListener(
108-
onCancel = { ViewCompat.setBackgroundTintList(this, ColorStateList(empty, intArrayOf(colorEnd))) },
109-
onEnd = { ViewCompat.setBackgroundTintList(this, ColorStateList(empty, intArrayOf(colorEnd))) }
110-
)
104+
viewPropertyAnimator.addUpdateListener { listenerAction(it.animatedValue as Int) }
105+
viewPropertyAnimator.addListener(onCancel = { listenerAction(colorEnd) }, onEnd = { listenerAction(colorEnd) })
111106
viewPropertyAnimator.start()
112107
}
113108

114109
fun View.animateHeight(duration: Long, targetHeight: Int): ValueAnimator {
115110
if (MainActivity.DEBUG) {
116-
Log.d(
117-
TAG,
118-
"animateHeight: duration = [" + duration + "], " +
119-
"from " + height + " to → " + targetHeight + " in: " + this
120-
)
111+
Log.d(TAG, "animateHeight: duration = [$duration], from $height to → $targetHeight in: $this")
121112
}
122113
val animator = ValueAnimator.ofFloat(height.toFloat(), targetHeight.toFloat())
123114
animator.interpolator = FastOutSlowInInterpolator()
124115
animator.duration = duration
125-
animator.addUpdateListener { animation: ValueAnimator ->
126-
val value = animation.animatedValue as Float
127-
layoutParams.height = value.toInt()
116+
117+
fun listenerAction(value: Int) {
118+
layoutParams.height = value
128119
requestLayout()
129120
}
130-
animator.addListener(
131-
onCancel = {
132-
layoutParams.height = targetHeight
133-
requestLayout()
134-
},
135-
onEnd = {
136-
layoutParams.height = targetHeight
137-
requestLayout()
138-
}
139-
)
121+
animator.addUpdateListener { listenerAction((it.animatedValue as Float).toInt()) }
122+
animator.addListener(onCancel = { listenerAction(targetHeight) }, onEnd = { listenerAction(targetHeight) })
140123
animator.start()
141124
return animator
142125
}
143126

144127
fun View.animateRotation(duration: Long, targetRotation: Int) {
145128
if (MainActivity.DEBUG) {
146-
Log.d(
147-
TAG,
148-
"animateRotation: duration = [" + duration + "], " +
149-
"from " + rotation + " to → " + targetRotation + " in: " + this
150-
)
129+
Log.d(TAG, "animateRotation: duration = [$duration], from $rotation to → $targetRotation in: $this")
151130
}
152131
animate().setListener(null).cancel()
153132
animate()

0 commit comments

Comments
 (0)