Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2/4 Better support for View.findViewTreeOnBackPressedDispatcherOwner. #1027

Merged
merged 1 commit into from
Jun 26, 2023

Conversation

rjrjr
Copy link
Contributor

@rjrjr rjrjr commented Jun 14, 2023

fun View.findViewTreeOnBackPressedDispatcherOwner() is AndroidX's preferred entry point to the exciting new world of OnBackPressedDispatcherOwner. With this PR we support it explicitly, in particular taking care to ensure that it can be called by newly constructed views before they have been attached to a parent. That is, we take care to make eager calls View.setViewTreeSavedStateRegistryOwner on every view we build.

We accomplish this mainly by riding the rails previously laid down via WorkflowLifecycleOwner.installOn, which now requires an OnBackPressedDispatcherOwner parameter. (This is the method used by WorkflowViewStub et al to ensure that we're managing the JetPack Lifecycle correctly, and OnBackPressedDispatcherOwner is just another piece of that puzzle.)

In aid of that, we introduce a new ViewEnvironmentKey, OnBackPressedDispatcherOwnerKey. It is initialized by WorkflowLayout, our new ComponentDialog.setContent extension, and @Composable fun WorkflowRendering(). This key is not intended for use by feature code, it's more of an implementation detail that has to stay public to allow custom containers to be built. It ensures that WorkflowViewStub and friends will have access to the correct OnBackPressedDispatcherOwner before they have access to a parent view.

  • TODO: add tests of @Composable fun BackHandler(), in both activity and dialog windows.

@rjrjr rjrjr changed the title Better support for View.findViewTreeOnBackPressedDispatcherOwner. 2/3 Better support for View.findViewTreeOnBackPressedDispatcherOwner. Jun 14, 2023
@rjrjr rjrjr force-pushed the ray/1-component-set-dialog branch from 8ff77ce to 7154b36 Compare June 14, 2023 21:03
@rjrjr rjrjr force-pushed the ray/2-install-on-back-dispatcher branch from d29aea2 to f62b52f Compare June 14, 2023 21:04
@rjrjr rjrjr force-pushed the ray/1-component-set-dialog branch from 7154b36 to 7f258e5 Compare June 14, 2023 22:37
@rjrjr rjrjr force-pushed the ray/2-install-on-back-dispatcher branch 3 times, most recently from dfe3296 to faec646 Compare June 15, 2023 16:48
@rjrjr rjrjr force-pushed the ray/1-component-set-dialog branch from 7f258e5 to e4f4fe1 Compare June 15, 2023 16:48
@rjrjr rjrjr marked this pull request as ready for review June 15, 2023 16:48
@rjrjr rjrjr requested review from a team and zach-klippenstein as code owners June 15, 2023 16:48
@rjrjr rjrjr force-pushed the ray/1-component-set-dialog branch from e4f4fe1 to 8d1228d Compare June 15, 2023 17:16
@rjrjr rjrjr force-pushed the ray/2-install-on-back-dispatcher branch from faec646 to 78620bb Compare June 15, 2023 17:16
@rjrjr rjrjr force-pushed the ray/1-component-set-dialog branch from 8d1228d to 5973c5b Compare June 15, 2023 23:24
@rjrjr rjrjr force-pushed the ray/2-install-on-back-dispatcher branch from 78620bb to 08e8dd3 Compare June 15, 2023 23:24
@rjrjr rjrjr changed the title 2/3 Better support for View.findViewTreeOnBackPressedDispatcherOwner. 2/4 Better support for View.findViewTreeOnBackPressedDispatcherOwner. Jun 15, 2023
contentHolder.show(newOverlay.content, newEnvironment)
contentHolder.show(
newOverlay.content,
newEnvironment + (OnBackPressedDispatcherOwnerKey to this@setContent)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
newEnvironment + (OnBackPressedDispatcherOwnerKey to this@setContent)
// Note we need to add back the OnBackPressedDispatcherOwner each time we receive a new ViewEnvironment.
newEnvironment + (OnBackPressedDispatcherOwnerKey to this@setContent)

Comment on lines +167 to +171
override fun getLifecycle(): Lifecycle =
error("To support back press handling extend ComponentDialog: $dialog")

override fun getOnBackPressedDispatcher(): OnBackPressedDispatcher =
error("To support back press handling extend ComponentDialog: $dialog")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@rjrjr rjrjr force-pushed the ray/2-install-on-back-dispatcher branch from 08e8dd3 to d417e33 Compare June 21, 2023 20:30
@rjrjr rjrjr force-pushed the ray/1-component-set-dialog branch from 5973c5b to a9e7dda Compare June 21, 2023 20:30
`fun View.findViewTreeOnBackPressedDispatcherOwner()` is AndroidX's preferred entry point to the exciting new world of `OnBackPressedDispatcherOwner`. With this PR we support it explicitly, in particular taking care to ensure that it can be called by newly constructed views before they have been attached to a parent. That is, we take care to make eager calls `View.setViewTreeSavedStateRegistryOwner` on every view we build.

We accomplish this mainly by riding the rails previously laid down via `WorkflowLifecycleOwner.installOn`, which now requires an `OnBackPressedDispatcherOwner` parameter. (This is the method used by `WorkflowViewStub` _et al_ to ensure that we're managing the JetPack Lifecycle correctly, and `OnBackPressedDispatcherOwner` is just another piece of that puzzle.)

In aid of that, we introduce a new `ViewEnvironmentKey`, `OnBackPressedDispatcherOwnerKey`. It is initialized by `WorkflowLayout`, our new `ComponentDialog.setContent` extension, and `@Composable fun WorkflowRendering()`. This key is not intended for use by feature code, it's more of an implementation detail that has to stay public to allow custom containers to be built. It ensures that `WorkflowViewStub` and friends will have access to the correct `OnBackPressedDispatcherOwner` before they have access to a parent view.

TODO: add tests of `@Composable fun BackHandler()`, in both activity and dialog windows.
@rjrjr rjrjr force-pushed the ray/1-component-set-dialog branch from a9e7dda to 6f4712f Compare June 24, 2023 00:51
@rjrjr rjrjr force-pushed the ray/2-install-on-back-dispatcher branch from d417e33 to 0335e65 Compare June 24, 2023 00:51
@rjrjr rjrjr merged commit e1daa09 into ray/1-component-set-dialog Jun 26, 2023
@rjrjr rjrjr deleted the ray/2-install-on-back-dispatcher branch June 26, 2023 21:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants