How is this general approach for injecting a selec...
# compose
m
How is this general approach for injecting a selector dialog into a viewmodel’s workflow?
Copy code
// in a Composable
var showItemSelectorDialog: ((Item?) -> Unit)? by remember {
    mutableStateOf(null)
}
if (showItemSelectorDialog != null) {
    ItemSelectorDialog(
        onItemSelect = { item ->
            showItemSelectorDialog?.invoke(item)
            showItemSelectorDialog = null
        },
    )
}

// in some click handler
scope.launch {
    viewModel.performSuspendingAction(
        // depending on certain conditions, at some point the viewmodel may need to request an item choice from the user before continuing with the workflow
        selectItem = {
            suspendCancellableCoroutine { continuation ->
                showItemSelectorDialog = { item ->
                    if (item == null) {
                        continuation.cancel()
                    } else {
                        continuation.resume(item)
                    }
                }
            }
        },
    )
}