aoriani
02/25/2022, 3:13 AMdata class DialogState(val isShown: Boolean = false, val message: String = "")
@Composable
fun Screen(navController: NavController, viewModel: MyViewModel) {
val dialogState by rememberUpdateState(DialogState)
val scope = rememberCoroutineScope()
Button(onClick = { scope.launch {
when (val result = viewModel.doRequest()) {
is Success -> navController.popBackStack()
is NotFound -> dialogState = DialogState(true, "Not Found")
else -> dialogState = DialogState(true, "error")
}})
if (dialogState.isShown) {
AlertDialog(.......)
}
}
Would this be correct ?Eric Chee
02/25/2022, 3:34 AMclass ViewModel {
val resultFlow: StateFLow<Result>
fun doRequest() { viewmodelScope.launch{
val result = somerequest()
resultFlow.value = result
}
}
@Composable
fun ...() {
val result viewModel.resultFLowObserveAsState()
when (result) {
....
}
}
Zach Klippenstein (he/him) [MOD]
03/03/2022, 4:05 AMrememberUpdatedState
looks a bit suspicious - are you passing it a companion object? It's definitely not a parameter, which is basically all that function is intended foraoriani
03/05/2022, 2:21 AMval dialogState by rememberUpdateState(DialogState())
The initial state is a new DialogState
object created with default values for the params