https://kotlinlang.org logo
Title
z

Zlatko

04/03/2023, 5:18 PM
id like to decouple the dialog from activity. but i couldn't think of a way to do it, as when data is updated dialogShow variable is still false. does this mean, unlike with the older system where alert dialogs get stacked on the screen, we need to keep a queue of dialogs?
@Composable
fun MainActivityScreen() {
val viewModel: MainActivityViewModel = viewModel()
    var userData by remember { mutableStateOf(viewModel.userLiveData.value ?: UserData.EMPTY) }

    Column(....) {
        Text(...)
        Button(onClick = {
            userData = UserData.getRandomUser()
        }) {
            Text(text = "get new user")
        }
    }
    UserDataDialog(userData)
}


@Composable
fun UserDataDialog(userData: UserData) {
    var dialogShow by remember { mutableStateOf(true) }

    fun onDismiss() {
        dialogShow = false
    }

    if (dialogShow) {
        AlertDialog(...)
    }
}