Skip to content

Instantly share code, notes, and snippets.

@tdcolvin
Last active January 18, 2024 10:12
Show Gist options
  • Save tdcolvin/0df764f394dd9b3be5ceeb1e1308e999 to your computer and use it in GitHub Desktop.
Save tdcolvin/0df764f394dd9b3be5ceeb1e1308e999 to your computer and use it in GitHub Desktop.
@Composable
fun PlanetsScreen(...) {
val revealPIN by viewModel.isShowingPin.collectAsStateWithLifecycle()
val scope = rememberCoroutineScope()
Column {
Button(
onClick = {
scope.launch {
// Here we call a function which takes at least 10 seconds to run,
// directly from the main thread. Safe because the thread isn't blocked.
viewModel.revealPinBriefly()
}
}
) {
Text("Reveal PIN")
}
if (revealPIN) {
Text(text = "Your PIN is 1234")
}
}
}
val isShowingPin = MutableStateFlow(false)
// This function suspends the coroutine for a long time, but
// doesn't block the calling thread. So it can be called from
// the main/UI thread safely.
suspend fun revealPinBriefly() {
isShowingPin.value = true
delay(10_000)
isShowingPin.value = false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment