Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FeatureRequest: Refresh dialog #122

Open
risalfajar opened this issue Dec 7, 2021 · 2 comments
Open

FeatureRequest: Refresh dialog #122

risalfajar opened this issue Dec 7, 2021 · 2 comments

Comments

@risalfajar
Copy link

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
        }
    }
@PranavMaganti
Copy link
Owner

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?

@risalfajar
Copy link
Author

risalfajar commented Apr 18, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants