Coverage Summary for Class: AnimatableExtensionsKt (com.vsevolodganin.clicktrack.utils.compose)
Class |
Class, %
|
Method, %
|
Branch, %
|
Line, %
|
Instruction, %
|
AnimatableExtensionsKt |
0%
(0/1)
|
0%
(0/3)
|
0%
(0/6)
|
0%
(0/19)
|
0%
(0/79)
|
package com.vsevolodganin.clicktrack.utils.compose
import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.AnimationResult
import androidx.compose.animation.core.AnimationVector1D
import androidx.compose.animation.core.DecayAnimationSpec
import androidx.compose.animation.core.calculateTargetValue
import kotlin.math.abs
suspend fun Animatable<Float, AnimationVector1D>.fling(
initialVelocity: Float,
animationSpec: DecayAnimationSpec<Float>,
adjustTarget: ((Float) -> Float)?,
block: (Animatable<Float, AnimationVector1D>.() -> Unit)? = null,
): AnimationResult<Float, AnimationVector1D> {
val targetValue = animationSpec.calculateTargetValue(value, initialVelocity)
val adjustedTarget = adjustTarget?.invoke(targetValue)
return if (adjustedTarget != null) {
animateTo(
targetValue = adjustedTarget,
initialVelocity = initialVelocity,
block = block,
)
} else {
animateDecay(
initialVelocity = initialVelocity,
animationSpec = animationSpec,
block = block,
)
}
}
suspend fun Animatable<Float, AnimationVector1D>.fling(
initialVelocity: Float,
animationSpec: DecayAnimationSpec<Float>,
anchors: List<Float>,
block: (Animatable<Float, AnimationVector1D>.() -> Unit)? = null,
): AnimationResult<Float, AnimationVector1D> {
return fling(
initialVelocity = initialVelocity,
animationSpec = animationSpec,
adjustTarget = { target ->
val point = anchors.minByOrNull { abs(it - target) }
point ?: target
},
block = block,
)
}