Skip to content

Commit d9230c0

Browse files
authored
Merge pull request #8708 from Isira-Seneviratne/Reduce_View.kt_size
Reduce View.kt size.
2 parents 1a8f396 + 8024b43 commit d9230c0

1 file changed

Lines changed: 49 additions & 93 deletions

File tree

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

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

Lines changed: 49 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -92,62 +92,43 @@ fun View.animateBackgroundColor(duration: Long, @ColorInt colorStart: Int, @Colo
9292
if (MainActivity.DEBUG) {
9393
Log.d(
9494
TAG,
95-
"animateBackgroundColor() called with: " +
96-
"view = [" + this + "], duration = [" + duration + "], " +
97-
"colorStart = [" + colorStart + "], colorEnd = [" + colorEnd + "]"
95+
"animateBackgroundColor() called with: view = [$this], duration = [$duration], " +
96+
"colorStart = [$colorStart], colorEnd = [$colorEnd]"
9897
)
9998
}
100-
val empty = arrayOf(IntArray(0))
10199
val viewPropertyAnimator = ValueAnimator.ofObject(ArgbEvaluator(), colorStart, colorEnd)
102100
viewPropertyAnimator.interpolator = FastOutSlowInInterpolator()
103101
viewPropertyAnimator.duration = duration
104-
viewPropertyAnimator.addUpdateListener { animation: ValueAnimator ->
105-
ViewCompat.setBackgroundTintList(this, ColorStateList(empty, intArrayOf(animation.animatedValue as Int)))
102+
103+
fun listenerAction(color: Int) {
104+
ViewCompat.setBackgroundTintList(this, ColorStateList.valueOf(color))
106105
}
107-
viewPropertyAnimator.addListener(
108-
onCancel = { ViewCompat.setBackgroundTintList(this, ColorStateList(empty, intArrayOf(colorEnd))) },
109-
onEnd = { ViewCompat.setBackgroundTintList(this, ColorStateList(empty, intArrayOf(colorEnd))) }
110-
)
106+
viewPropertyAnimator.addUpdateListener { listenerAction(it.animatedValue as Int) }
107+
viewPropertyAnimator.addListener(onCancel = { listenerAction(colorEnd) }, onEnd = { listenerAction(colorEnd) })
111108
viewPropertyAnimator.start()
112109
}
113110

114111
fun View.animateHeight(duration: Long, targetHeight: Int): ValueAnimator {
115112
if (MainActivity.DEBUG) {
116-
Log.d(
117-
TAG,
118-
"animateHeight: duration = [" + duration + "], " +
119-
"from " + height + " to → " + targetHeight + " in: " + this
120-
)
113+
Log.d(TAG, "animateHeight: duration = [$duration], from $height to → $targetHeight in: $this")
121114
}
122115
val animator = ValueAnimator.ofFloat(height.toFloat(), targetHeight.toFloat())
123116
animator.interpolator = FastOutSlowInInterpolator()
124117
animator.duration = duration
125-
animator.addUpdateListener { animation: ValueAnimator ->
126-
val value = animation.animatedValue as Float
127-
layoutParams.height = value.toInt()
118+
119+
fun listenerAction(value: Int) {
120+
layoutParams.height = value
128121
requestLayout()
129122
}
130-
animator.addListener(
131-
onCancel = {
132-
layoutParams.height = targetHeight
133-
requestLayout()
134-
},
135-
onEnd = {
136-
layoutParams.height = targetHeight
137-
requestLayout()
138-
}
139-
)
123+
animator.addUpdateListener { listenerAction((it.animatedValue as Float).toInt()) }
124+
animator.addListener(onCancel = { listenerAction(targetHeight) }, onEnd = { listenerAction(targetHeight) })
140125
animator.start()
141126
return animator
142127
}
143128

144129
fun View.animateRotation(duration: Long, targetRotation: Int) {
145130
if (MainActivity.DEBUG) {
146-
Log.d(
147-
TAG,
148-
"animateRotation: duration = [" + duration + "], " +
149-
"from " + rotation + " to → " + targetRotation + " in: " + this
150-
)
131+
Log.d(TAG, "animateRotation: duration = [$duration], from $rotation to → $targetRotation in: $this")
151132
}
152133
animate().setListener(null).cancel()
153134
animate()
@@ -168,20 +149,13 @@ private fun View.animateAlpha(enterOrExit: Boolean, duration: Long, delay: Long,
168149
if (enterOrExit) {
169150
animate().setInterpolator(FastOutSlowInInterpolator()).alpha(1f)
170151
.setDuration(duration).setStartDelay(delay)
171-
.setListener(object : AnimatorListenerAdapter() {
172-
override fun onAnimationEnd(animation: Animator) {
173-
execOnEnd?.run()
174-
}
175-
}).start()
152+
.setListener(ExecOnEndListener(execOnEnd))
153+
.start()
176154
} else {
177155
animate().setInterpolator(FastOutSlowInInterpolator()).alpha(0f)
178156
.setDuration(duration).setStartDelay(delay)
179-
.setListener(object : AnimatorListenerAdapter() {
180-
override fun onAnimationEnd(animation: Animator) {
181-
isGone = true
182-
execOnEnd?.run()
183-
}
184-
}).start()
157+
.setListener(HideAndExecOnEndListener(this, execOnEnd))
158+
.start()
185159
}
186160
}
187161

@@ -193,24 +167,17 @@ private fun View.animateScaleAndAlpha(enterOrExit: Boolean, duration: Long, dela
193167
.setInterpolator(FastOutSlowInInterpolator())
194168
.alpha(1f).scaleX(1f).scaleY(1f)
195169
.setDuration(duration).setStartDelay(delay)
196-
.setListener(object : AnimatorListenerAdapter() {
197-
override fun onAnimationEnd(animation: Animator) {
198-
execOnEnd?.run()
199-
}
200-
}).start()
170+
.setListener(ExecOnEndListener(execOnEnd))
171+
.start()
201172
} else {
202173
scaleX = 1f
203174
scaleY = 1f
204175
animate()
205176
.setInterpolator(FastOutSlowInInterpolator())
206177
.alpha(0f).scaleX(.8f).scaleY(.8f)
207178
.setDuration(duration).setStartDelay(delay)
208-
.setListener(object : AnimatorListenerAdapter() {
209-
override fun onAnimationEnd(animation: Animator) {
210-
isGone = true
211-
execOnEnd?.run()
212-
}
213-
}).start()
179+
.setListener(HideAndExecOnEndListener(this, execOnEnd))
180+
.start()
214181
}
215182
}
216183

@@ -223,11 +190,8 @@ private fun View.animateLightScaleAndAlpha(enterOrExit: Boolean, duration: Long,
223190
.setInterpolator(FastOutSlowInInterpolator())
224191
.alpha(1f).scaleX(1f).scaleY(1f)
225192
.setDuration(duration).setStartDelay(delay)
226-
.setListener(object : AnimatorListenerAdapter() {
227-
override fun onAnimationEnd(animation: Animator) {
228-
execOnEnd?.run()
229-
}
230-
}).start()
193+
.setListener(ExecOnEndListener(execOnEnd))
194+
.start()
231195
} else {
232196
alpha = 1f
233197
scaleX = 1f
@@ -236,12 +200,8 @@ private fun View.animateLightScaleAndAlpha(enterOrExit: Boolean, duration: Long,
236200
.setInterpolator(FastOutSlowInInterpolator())
237201
.alpha(0f).scaleX(.95f).scaleY(.95f)
238202
.setDuration(duration).setStartDelay(delay)
239-
.setListener(object : AnimatorListenerAdapter() {
240-
override fun onAnimationEnd(animation: Animator) {
241-
isGone = true
242-
execOnEnd?.run()
243-
}
244-
}).start()
203+
.setListener(HideAndExecOnEndListener(this, execOnEnd))
204+
.start()
245205
}
246206
}
247207

@@ -252,22 +212,15 @@ private fun View.animateSlideAndAlpha(enterOrExit: Boolean, duration: Long, dela
252212
animate()
253213
.setInterpolator(FastOutSlowInInterpolator()).alpha(1f).translationY(0f)
254214
.setDuration(duration).setStartDelay(delay)
255-
.setListener(object : AnimatorListenerAdapter() {
256-
override fun onAnimationEnd(animation: Animator) {
257-
execOnEnd?.run()
258-
}
259-
}).start()
215+
.setListener(ExecOnEndListener(execOnEnd))
216+
.start()
260217
} else {
261218
animate()
262219
.setInterpolator(FastOutSlowInInterpolator())
263220
.alpha(0f).translationY(-height.toFloat())
264221
.setDuration(duration).setStartDelay(delay)
265-
.setListener(object : AnimatorListenerAdapter() {
266-
override fun onAnimationEnd(animation: Animator) {
267-
isGone = true
268-
execOnEnd?.run()
269-
}
270-
}).start()
222+
.setListener(HideAndExecOnEndListener(this, execOnEnd))
223+
.start()
271224
}
272225
}
273226

@@ -278,21 +231,14 @@ private fun View.animateLightSlideAndAlpha(enterOrExit: Boolean, duration: Long,
278231
animate()
279232
.setInterpolator(FastOutSlowInInterpolator()).alpha(1f).translationY(0f)
280233
.setDuration(duration).setStartDelay(delay)
281-
.setListener(object : AnimatorListenerAdapter() {
282-
override fun onAnimationEnd(animation: Animator) {
283-
execOnEnd?.run()
284-
}
285-
}).start()
234+
.setListener(ExecOnEndListener(execOnEnd))
235+
.start()
286236
} else {
287237
animate().setInterpolator(FastOutSlowInInterpolator())
288238
.alpha(0f).translationY(-height / 2.0f)
289239
.setDuration(duration).setStartDelay(delay)
290-
.setListener(object : AnimatorListenerAdapter() {
291-
override fun onAnimationEnd(animation: Animator) {
292-
isGone = true
293-
execOnEnd?.run()
294-
}
295-
}).start()
240+
.setListener(HideAndExecOnEndListener(this, execOnEnd))
241+
.start()
296242
}
297243
}
298244

@@ -314,11 +260,7 @@ fun View.slideUp(
314260
.setStartDelay(delay)
315261
.setDuration(duration)
316262
.setInterpolator(FastOutSlowInInterpolator())
317-
.setListener(object : AnimatorListenerAdapter() {
318-
override fun onAnimationEnd(animation: Animator) {
319-
execOnEnd?.run()
320-
}
321-
})
263+
.setListener(ExecOnEndListener(execOnEnd))
322264
.start()
323265
}
324266

@@ -332,6 +274,20 @@ fun View.animateHideRecyclerViewAllowingScrolling() {
332274
animate().alpha(0.0f).setDuration(200).start()
333275
}
334276

277+
private open class ExecOnEndListener(private val execOnEnd: Runnable?) : AnimatorListenerAdapter() {
278+
override fun onAnimationEnd(animation: Animator) {
279+
execOnEnd?.run()
280+
}
281+
}
282+
283+
private class HideAndExecOnEndListener(private val view: View, execOnEnd: Runnable?) :
284+
ExecOnEndListener(execOnEnd) {
285+
override fun onAnimationEnd(animation: Animator) {
286+
view.isGone = true
287+
super.onAnimationEnd(animation)
288+
}
289+
}
290+
335291
enum class AnimationType {
336292
ALPHA, SCALE_AND_ALPHA, LIGHT_SCALE_AND_ALPHA, SLIDE_AND_ALPHA, LIGHT_SLIDE_AND_ALPHA
337293
}

0 commit comments

Comments
 (0)