dialogStateOf

fun <T : Any> dialogStateOf(data: T?, buttonPositiveEnabled: Boolean = true, buttonNegativeEnabled: Boolean = true, dismissAllowed: Boolean = true, swipeAllowed: Boolean = true, onStateChanged: (T?) -> Unit? = null): DialogState<T>

creates a DialogState object for usage outside of Compose (e.g. ViewModel)

Parameters

data

the initial data of this state (should nearly always be null which means that the dialog is not visible initially)

buttonPositiveEnabled

define if the positive button should be enabled initially or not

buttonNegativeEnabled

define if the negative button should be enabled initially or not

dismissAllowed

define if the dialog can be initially dismissed or not

swipeAllowed

define if the dialog can be initially swiped away or not

onStateChanged

callback that will be called whenever the dialog is shown or dismissed with the current data (null if dismissed)

NOTE:

  • no automatic state saving (no rememberSaveable)

  • interaction state will be reset whenever the dialog is shown again

EXAMPLE USAGE in a ViewModel (if state should survice process death):

class MyViewModel: ViewModel() {
private val KEY = "dialog_data"
val dialog = dialogStateOf(
data = savedStateHandle.get<String?>(KEY),
onStateChanged = { savedStateHandle[KEY] = it }
)
}

fun dialogStateOf(visible: Boolean = false, buttonPositiveEnabled: Boolean = true, buttonNegativeEnabled: Boolean = true, dismissAllowed: Boolean = true, swipeAllowed: Boolean = true, onStateChanged: (visible: Boolean) -> Unit? = null): DialogStateNoData

creates a DialogState object for usage outside of Compose (e.g. ViewModel)

Parameters

visible

the initial visibility state

buttonPositiveEnabled

define if the positive button should be enabled initially or not

buttonNegativeEnabled

define if the negative button should be enabled initially or not

dismissAllowed

define if the dialog can be initially dismissed or not

swipeAllowed

define if the dialog can be initially swiped away or not

onStateChanged

callback that will be called whenever the dialog is shown or dismissed with the current data (true if shown, false if dismissed)

NOTE:

  • no automatic state saving (like rememberDialogState) - you have to handle this by yourself if needed

  • interaction state will be reset whenever the dialog is shown again

EXAMPLE USAGE in a ViewModel (if state should survice process death):

class MyViewModel: ViewModel() {
private val KEY = "dialog_data"
val dialog = dialogStateOf(
data = savedStateHandle.get<Boolean?>(KEY),
onStateChanged = { savedStateHandle[KEY] = it }
)
}