Last active
January 18, 2024 10:12
-
-
Save tdcolvin/0df764f394dd9b3be5ceeb1e1308e999 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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") | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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