Travis Griggs
03/07/2023, 1:03 AMvar remaining by remember { mutableStateOf(keyInfo.remaining) }
LaunchedEffect(remaining) {
delay(1.seconds)
remaining = keyInfo.remaining
}
SettingsLabel(text = remaining.clockPrinted())
ephemient
03/07/2023, 3:05 AMprivate fun Duration.truncatedTo(unit: Duration): Duration = unit.multipliedBy(this.dividedBy(unit))
@Composable
fun CountDownTimer(
goal: Instant = Instant.ofEpochSecond(Int.MAX_VALUE.toLong()),
tick: Duration = Duration.ofSeconds(1L),
clock: Clock = Clock.systemUTC(),
) {
val remaining by flow {
do {
val remaining = Duration.between(clock.instant(), goal)
var next = remaining.truncatedTo(tick)
if (next >= remaining) next -= tick
next = next.coerceAtLeast(Duration.ZERO)
delay((remaining - next).toMillis())
emit(next)
} while (next > Duration.ZERO)
}.collectAsState(Duration.between(clock.instant(), goal).coerceAtLeast(Duration.ZERO).truncatedTo(tick))
Text(text = remaining.toString())
}
LeoColman
03/07/2023, 11:46 PM@IgnoredOnParcel
val millisLeft = flow {
while (true) {
emit(calculateMillisLeft())
delay(3)
}
}
calculateMillisLeft
. As @ephemient pointed out, we want to avoid losing the small delays here and there.ephemient
03/08/2023, 5:08 AMDavide Giuseppe Farella
03/09/2023, 12:43 PMephemient
03/10/2023, 8:04 AM