Skip to content

Commit

Permalink
Unit test fix
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownJoe796 committed Dec 30, 2024
1 parent 3b1ba57 commit 445c481
Showing 1 changed file with 15 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,11 @@ import android.graphics.drawable.GradientDrawable
import android.os.Build
import android.os.Build.VERSION_CODES
import android.view.animation.AccelerateDecelerateInterpolator
import android.view.animation.Interpolator
import androidx.core.animation.addListener
import androidx.core.animation.doOnEnd
import com.lightningkite.kiteui.afterTimeout
import com.lightningkite.kiteui.clockMillis
import com.lightningkite.kiteui.models.*
import com.lightningkite.kiteui.views.direct.colorInt
import kotlin.math.PI
import kotlin.math.roundToInt
import kotlin.math.sin
import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.seconds
Expand All @@ -32,15 +27,20 @@ private class MyGradientDrawable(): GradientDrawable() {
var colorsOverTime: Array<Pair<IntArray, FloatArray>>? = null
private var animator: ValueAnimator? = null
private var setInstance = 0
private var lastSetColors: IntArray? = null
fun set(goal: IntArray, ratios: FloatArray) {
if(Build.VERSION.SDK_INT >= VERSION_CODES.Q) {
setColors(goal, ratios)
} else {
colors = goal
}
lastSetColors = goal
}
fun animateColorsTo(goal: IntArray, ratios: FloatArray, duration: Duration) {
val myInstance = ++setInstance
animator?.cancel()
if(!animationsEnabled || colors?.size != goal.size) {
if(Build.VERSION.SDK_INT >= VERSION_CODES.Q) {
setColors(goal, ratios)
} else {
colors = goal
}
if(!animationsEnabled || lastSetColors?.size != goal.size) {
set(goal, ratios)
afterTimeout(100) {
if(setInstance > myInstance) return@afterTimeout
colorsOverTime?.let {
Expand All @@ -49,29 +49,25 @@ private class MyGradientDrawable(): GradientDrawable() {
}
return
}
val animationStartColors = colors ?: intArrayOf(0, 0)
val animationStartColors = lastSetColors ?: intArrayOf(0, 0)
val animationGoalColors = goal
animator = ValueAnimator.ofFloat(0f, 1f).also {
it.duration = duration.inWholeMilliseconds
it.interpolator = AccelerateDecelerateInterpolator()
it.addUpdateListener { it ->
if(setInstance > myInstance) return@addUpdateListener
val f = it.animatedFraction
colors = IntArray(animationStartColors.size) { index ->
set(IntArray(animationStartColors.size) { index ->
Color.interpolate(
Color.fromInt(animationStartColors[index]),
Color.fromInt(animationGoalColors[index]),
f
).toInt()
}
}, ratios)
}
it.doOnEnd {
if(setInstance > myInstance) return@doOnEnd
if(Build.VERSION.SDK_INT >= VERSION_CODES.Q) {
setColors(goal, ratios)
} else {
colors = goal
}
set(goal, ratios)
colorsOverTime?.let {
val diffToFirst = Color.fromInt(goal[0]).channelDifferenceSum(Color.fromInt(it[0].first[0]))
val diffToSecond = Color.fromInt(goal[0]).channelDifferenceSum(Color.fromInt(it[1].first[0]))
Expand Down

0 comments on commit 445c481

Please sign in to comment.