PranavMaganti/compose-material-dialogs

FeatureRequest: Refresh dialog

Opened this issue · 2 comments

I have a case: when I'm displaying a ListDialog with dynamic data, the dialog does not recompose when the data changes.

It will be great to have a function to refresh the dialog, or much better to have the dialog recompose when data is changed.

current workaround:

val listDialog = rememberMaterialDialogState()
val scope = rememberCoroutineScope()

LaunchedEffect(list) {
        // If the list changes when the dialog is open, we should refresh the dialog manually
        // by closing and opening it again
        if (listDialog.showing) {
            listDialog.hide()
            scope.launch {
                // this won't work without adding delay
                delay(50)
                listDialog.show()
            }
        }
    }

MaterialDialog(dialogState = listDialog) {
        listItems(list = list) { index, value ->
            // do something
        }
    }

I don't seem to be seeing this behaviour as when I add to the list it recomposes immediately. Do you mind providing a code example with the changing state in it?

Please try this code

@Composable
fun Main() {
    var list by remember { mutableStateOf(emptyList<String>()) }
    val dialog = rememberMaterialDialogState()
    val scope = rememberCoroutineScope()

    MaterialDialog(dialogState = dialog) {
        listItems(list = list) { index, value ->
            Log.d("MainActivity", value)
        }
    }

    Button(
        onClick = {
            dialog.show()
            if (list.isNotEmpty())
                return@Button
            scope.launch {
                list = listOf("1")
                delay(500)
                list = listOf("1", "2")
                delay(500)
                list = listOf("1", "2", "3")
            }
        }
    ) {
        Text(text = "Show Dialog")
    }
}

This is the result:
vanpra

As you can see, we need to close and open the dialog again for it to display properly