google / accompanist

A collection of extension libraries for Jetpack Compose
https://google.github.io/accompanist
Apache License 2.0
7.31k stars 592 forks source link

Implemented DismissDispatcher to be able to intercept BottomSheet navigation dismissal #1769

Closed PavloZoria closed 2 months ago

PavloZoria commented 2 months ago

Implemented DismissDispatcher: Intercepting BottomSheet Dismissal

I've implemented DismissDispatcher to provide more control over user-initiated dismissal of BottomSheets. This functionality mirrors the behavior of BackHandler for handling back button presses.

What can you intercept?

How to use DismissDispatcher:

  1. Call DismissHandler in your sheet's content.
  2. Override the callback function. The usage is similar to BackHandler.

Code Example:

@Composable
private fun BottomSheet(
  navigateUp: () -> Unit,
) {
  var showConfirmationDialog by remember {
    mutableStateOf(false)
  }

  if (showConfirmationDialog) {
    ConfirmationDialog(
      onCancel = { showConfirmationDialog = false },
      onApprove = { navigateUp() }
    )
  }

  DismissHandler {
    showConfirmationDialog = true
  }

  BottomSheetContent()
}

Important Note:

This approach does not intercept dismissal initiated by the NavController. This allows you to easily close the BottomSheet programmatically using the NavController if needed.

Attachments

https://github.com/google/accompanist/assets/38030130/2f7bd0d6-39e7-44d7-8778-b77da91c2613

bentrengrove commented 2 months ago

Thank you for the contribution but unfortunately Navigation Material has been deprecated in Accompanist as the functionality is now available in the main Compose libraries.

Because of this I will have to close out this PR as we won't be making any further changes here.

PavloZoria commented 2 months ago

@bentrengrove I'd like your thoughts on two options: