Skip to content

Instantly share code, notes, and snippets.

@droid-it
Last active August 15, 2021 07:02
Show Gist options
  • Save droid-it/15ddcbe8b62b5fdbc40da9af48533ab4 to your computer and use it in GitHub Desktop.
Save droid-it/15ddcbe8b62b5fdbc40da9af48533ab4 to your computer and use it in GitHub Desktop.
@Composable
fun TimerScreen1() {
Column(
modifier = Modifier
.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally
) {
var timerDuration by remember {
mutableStateOf(1000L) // default value = 1 sec
}
Button({
timerDuration -= 1000
}) {
Text("-1 second")
}
Text(timerDuration.toString())
Button({
timerDuration += 1000
}) {
Text("+1 second")
}
Timer(timerDuration = timerDuration)
}
}
@Composable
fun Timer(timerDuration: Long) {
LaunchedEffect(key1 = timerDuration, block = {
try {
startTimer(timerDuration) {
println("Timer ended")
}
} catch (ex: Exception) {
println("timer cancelled")
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment