Skip to content

Instantly share code, notes, and snippets.

@AndreVero
Last active April 15, 2024 07:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndreVero/e64c1095eafc22951bacd4313b453bee to your computer and use it in GitHub Desktop.
Save AndreVero/e64c1095eafc22951bacd4313b453bee to your computer and use it in GitHub Desktop.
Time picker state class
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