Last active
April 15, 2024 07:23
-
-
Save AndreVero/e64c1095eafc22951bacd4313b453bee to your computer and use it in GitHub Desktop.
Time picker state class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SelectedItem( | |
// Distance to the closest/selected item | |
val angle: Float = 361f, | |
// Index of the selected item | |
val index: Int = 0, | |
) | |
class TimePickerState { | |
// Angle changes | |
private val _angle = Animatable(0f) | |
val angle: Float | |
get() = _angle.value | |
// Angle state after the end of the animation | |
var oldAngle: Float = 0f | |
// Currently selected item | |
var selectedItem = SelectedItem() | |
// Animation that we will use for spins | |
private val decayAnimationSpec = FloatSpringSpec( | |
dampingRatio = Spring.DampingRatioNoBouncy, | |
stiffness = Spring.StiffnessVeryLow, | |
) | |
suspend fun stop() { | |
_angle.stop() | |
} | |
suspend fun snapTo(angle: Float) { | |
_angle.snapTo(angle) | |
} | |
suspend fun animateTo(angle: Float) { | |
// Save the new old angle as this is the last step of the animation | |
oldAngle = angle | |
_angle.animateTo(angle, decayAnimationSpec) | |
} | |
suspend fun decayTo(angle: Float, velocity: Float) { | |
_angle.animateTo( | |
targetValue = angle, | |
initialVelocity = velocity, | |
animationSpec = decayAnimationSpec, | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment