hotwired / turbo-android

Android framework for making Turbo native apps
MIT License
423 stars 51 forks source link

How to dismiss the native BottomSheet with code? #195

Closed shkim closed 2 years ago

shkim commented 2 years ago

If the presenting dialog is an instance of TurboFragment, I can close the dialog by calling navigateBack() and underlying page works fine.

But if the dialog is an instance of TurboBottomSheetDialogFragment, navigateBack() call will make the underlying page locked. I tried navigateUp(), dismiss() but the result was same. ( If I click the back button or touch the outer dark side of sheet, then everything is working as expected. )

What is the proper way to close the sheet programmatically?

https://user-images.githubusercontent.com/1094548/136361753-cb798877-ddbd-4bf9-a318-7dada971cd72.mov

I prepared the sample code: https://github.com/shkim/turbo-android/commit/00130f2d316995c20abca194675262e4cd2537e8

and here are steps to modify the "demo":

  1. change the configuration.json to popup native sheet when '/numbers' url is requested:

    {
      "patterns": [
        "/numbers$"
      ],
      "properties": {
        "uri": "turbo://fragment/numbers/sheet",
        "title": "Numbers"
      }
    },
  2. setup click handler for NumberBottomSheetFragment.kt:

    view.findViewById<MaterialTextView>(R.id.number).apply {
            text = "CLICK TO CLOSE" //Uri.parse(location).lastPathSegment
            setOnClickListener {
                //dismiss()
                //navigateUp()
                navigateBack()
            }
        }
jayohms commented 2 years ago

Does cancel() work? If so, I'll update the internal logic so that navigateBack() and navigateUp() work properly, since those should be the standard way to go back to the previous screen.

shkim commented 2 years ago

I got the Unresolved reference error with just "cancel()" there.

jayohms commented 2 years ago

Sorry, use requireDialog().cancel()

shkim commented 2 years ago

Thanks. it works.

jayohms commented 2 years ago

Thanks, I've added a new issue to track fixing the default behavior: https://github.com/hotwired/turbo-android/issues/197