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

BackPressHandler plugin #32

Merged
merged 3 commits into from
Aug 11, 2022
Merged

BackPressHandler plugin #32

merged 3 commits into from
Aug 11, 2022

Conversation

CherryPerry
Copy link
Collaborator

@CherryPerry CherryPerry commented Jul 14, 2022

Description

Currently BackPressHandler is not an actual plugin and is manually supported only for a routing source. In this PR I tried to implement BackPressHandler that is available for any component in a node.

Also in this PR i tried to support predictive back gesture feature of Android 13. As you can see on the attached video it works as expected.

Concerns

AndroidX

In this PR I implement BackPressHandler as a provider of AndroidX OnBackPressedCallback. It allows more seamless integration with other AndroidX components but has the following issues:

  • We are one step further away from multi platform support and it is Android only library.
  • Client has to work with not the best of class API, but still more familiar than some custom API.
  • OnBackPressedCallback.isEnabled is not reactive/observable, so we need to manually synchronise internal state of an owner component with this property like feature.state.subscribe { callback.isEnabled = state.someCheck }.
  • In case of custom API it is possible to create more interesting API like
interface OnBackPressedCallback {
   // flow here instead of simple boolean property
   // to be able to easily map internal state into boolean state in a reactive way 
   val isEnabled: StateFlow<Boolean>
   fun dispatchBackPressed()
}
  • But in this case we are forcing clients (including us) to use StateFlow in their internal components (like interactors) instead of RxJava for example. But we are already doing it in RoutingSource.
  • We can't keep current API of fun onBackPressed() because we need to declare if we support going back or not first, so AndroidX and Android integration works fine. This change also allowed me to remove one property from RoutingSource.
  • By using AndroidX API directly we are guarding ourselves from potential changes like predictive back button in the future.

Registration order

In the current implementation and in this pull request we are relying on Compose to register OnBackPressedCallback. Invocation order of OnBackPressedCallback of different nodes in the same parent is defined by the order of Composable functions. If a node is out of a composition, it won't handle back presses. Implementation details is here.

Most likely this problem is out of scope of this pull request

Check list

  • I have updated CHANGELOG.md if required.
  • I have updated documentation if required.
predictive-support.webm

@CherryPerry CherryPerry changed the title [Draft] BackPressHandler plugin BackPressHandler plugin Aug 2, 2022
@CherryPerry CherryPerry force-pushed the androidx-back-integration branch 2 times, most recently from 86eb800 to f6621df Compare August 10, 2022 13:46
Copy link
Collaborator

@LachlanMcKee LachlanMcKee left a comment

Choose a reason for hiding this comment

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

Could you add an example in the Sandbox? I can't figure out how it should be used in a real app/feature. The Android Test does not make it clear how straightforward it is.

@LachlanMcKee
Copy link
Collaborator

Thanks for updating the example!

@CherryPerry CherryPerry linked an issue Aug 11, 2022 that may be closed by this pull request
@CherryPerry CherryPerry merged commit e2ed004 into main Aug 11, 2022
@CherryPerry CherryPerry deleted the androidx-back-integration branch August 11, 2022 09:25
@KovalevAndrey KovalevAndrey mentioned this pull request Aug 30, 2022
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.

Nested BackPressHandlers are ignored after app goes to background
3 participants