Skip to content

Instantly share code, notes, and snippets.

@jmadaminov
Last active November 1, 2022 14:38
Show Gist options
  • Save jmadaminov/8ffec6b7c16847b73b3afd1a4a0fb40a to your computer and use it in GitHub Desktop.
Save jmadaminov/8ffec6b7c16847b73b3afd1a4a0fb40a to your computer and use it in GitHub Desktop.
UI-Event through UiState
@Composable
private fun MainScreen(viewModel: MainViewModel) {
val context = LocalContext.current
val uiState = viewModel.uiState.collectAsState()
Box(modifier = Modifier.fillMaxSize()) {
Text(
modifier = Modifier
.align(Alignment.TopCenter)
.padding(top = 50.dp),
text = uiState.value.time.toString(),
fontSize = 50.sp
)
uiState.value.error?.let { errorString ->
LaunchedEffect(uiState) {
Toast.makeText(context, errorString, Toast.LENGTH_SHORT).show()
viewModel.errorToastShown()
}
}
Button(
modifier = Modifier
.align(Alignment.BottomCenter)
.padding(bottom = 50.dp),
onClick = { viewModel.makeApiCall() }) {
Text(text = "Show Toast")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment