Last active
November 1, 2022 14:38
-
-
Save jmadaminov/8ffec6b7c16847b73b3afd1a4a0fb40a to your computer and use it in GitHub Desktop.
UI-Event through UiState
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 | |
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