Skip to content

Instantly share code, notes, and snippets.

@KatieBarnett
Created December 21, 2023 04:57
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save KatieBarnett/ba23f3824777b50b24233143b11eb138 to your computer and use it in GitHub Desktop.
Confirm close ModalBottomSheet end state
val coroutineScope = rememberCoroutineScope()
var items by rememberSaveable { mutableStateOf(listOf<String>()) }
var showAddSheet by rememberSaveable { mutableStateOf(false) }
var showConfirmChanges by rememberSaveable { mutableStateOf(false) }
val addSheetState = rememberModalBottomSheetState(
confirmValueChange = {
if (it == SheetValue.Hidden) {
showConfirmChanges = true
false
} else {
// We're expanding the sheet so we always return true
true
}
}
)
Scaffold(...) { innerPadding ->
ShoppingList(
items = items,
onAddItemClick = { showAddSheet = true },
modifier = Modifier.fillMaxSize().padding(innerPadding)
)
if (showAddSheet) {
ModalBottomSheet(
onDismissRequest = { showConfirmChanges = true },
sheetState = addSheetState,
modifier = Modifier
) {
BottomSheetContent(
onSaveClick = {
items = items.plus(it)
coroutineScope.launch {
addSheetState.hide()
}.invokeOnCompletion {
if (!addSheetState.isVisible) {
showAddSheet = false
}
}
}
)
}
}
if (showConfirmChanges) {
AlertDialog(
onDismissRequest = { showConfirmChanges = false },
title = { Text(stringResource(R.string.confirm_changes_title)) },
text = { Text(stringResource(R.string.confirm_changes_text)) },
confirmButton = {
TextButton(onClick = {
coroutineScope.launch {
addSheetState.hide()
}.invokeOnCompletion {
if (!addSheetState.isVisible) {
showAddSheet = false
}
}
showConfirmChanges = false
}) {
Text(stringResource(R.string.confirm_changes_confirm))
}
},
dismissButton = {
TextButton(onClick = {
showConfirmChanges = false
}) {
Text(stringResource(R.string.confirm_changes_dismiss))
}
},
properties = DialogProperties(
dismissOnBackPress = false,
dismissOnClickOutside = false
)
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment