Fix dynamic clock icon desyncing

This commit is contained in:
MM20 2022-06-19 20:21:51 +02:00
parent a303703d9e
commit 2bce703ea4
No known key found for this signature in database
GPG Key ID: 0B61A8F2DEAFA389

View File

@ -270,47 +270,43 @@ private fun ClockLayer(
scale: Float, scale: Float,
tintColor: Color?, tintColor: Color?,
) { ) {
val time = remember { val transition = rememberInfiniteTransition()
val time = remember(LocalTime.current) {
Instant.ofEpochMilli(System.currentTimeMillis()).atZone(ZoneId.systemDefault()) Instant.ofEpochMilli(System.currentTimeMillis()).atZone(ZoneId.systemDefault())
} }
val transition = rememberInfiniteTransition()
val minute by transition.animateFloat( val second = remember {
initialValue = 0f, Animatable(time.second.toFloat())
targetValue = 60f, }
animationSpec = InfiniteRepeatableSpec(
animation = tween(durationMillis = 60 * 60 * 1000, easing = LinearEasing),
initialStartOffset = StartOffset(
offsetMillis = time.minute * 60 * 1000 + time.second * 1000,
offsetType = StartOffsetType.FastForward
)
)
)
val hour by transition.animateFloat( val minute = remember {
initialValue = 0f, Animatable(time.minute.toFloat() + time.second.toFloat() / 60f)
targetValue = 12f, }
animationSpec = InfiniteRepeatableSpec(
animation = tween(durationMillis = 12 * 60 * 60 * 1000, easing = LinearEasing),
initialStartOffset = StartOffset(
offsetMillis = (time.hour % 12) * 60 * 60 * 1000 + time.minute * 60 * 1000 + time.second * 1000,
offsetType = StartOffsetType.FastForward
)
)
)
val hour = remember {
Animatable(time.hour.toFloat() + time.minute.toFloat() / 60f)
}
val second by transition.animateFloat( LaunchedEffect(time) {
initialValue = 0f, val h = time.hour.toFloat() + time.minute.toFloat() / 60f
targetValue = 60f, hour.snapTo(h)
animationSpec = InfiniteRepeatableSpec( hour.animateTo(
animation = tween(durationMillis = 60000, easing = LinearEasing), h + 1f / 60f,
initialStartOffset = StartOffset( tween(60000, easing = LinearEasing)
offsetMillis = time.second * 1000,
offsetType = StartOffsetType.FastForward
)
)
) )
}
LaunchedEffect(time) {
val m = time.minute.toFloat() + time.second.toFloat() / 60f
minute.snapTo(m)
minute.animateTo(m + 1f, tween(60000, easing = LinearEasing))
}
LaunchedEffect(time) {
val s = time.second.toFloat()
second.snapTo(s)
second.animateTo(s + 60f, tween(60000, easing = LinearEasing))
}
Canvas(modifier = Modifier.fillMaxSize()) { Canvas(modifier = Modifier.fillMaxSize()) {
val colorFilter = tintColor?.let { val colorFilter = tintColor?.let {
@ -323,13 +319,13 @@ private fun ClockLayer(
withTransform({ withTransform({
when (sublayer.role) { when (sublayer.role) {
ClockSublayerRole.Hour -> { ClockSublayerRole.Hour -> {
rotate(hour / 12f * 360f) rotate(hour.value / 12f * 360f)
} }
ClockSublayerRole.Minute -> { ClockSublayerRole.Minute -> {
rotate(minute / 60f * 360f) rotate(minute.value / 60f * 360f)
} }
ClockSublayerRole.Second -> { ClockSublayerRole.Second -> {
rotate(second / 60f * 360f) rotate(second.value / 60f * 360f)
} }
ClockSublayerRole.Static -> {} ClockSublayerRole.Static -> {}
} }