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

implement result API based on NavBackStackEntry #56

Merged
merged 5 commits into from Mar 14, 2022
Merged

Conversation

gabrielittner
Copy link
Member

@gabrielittner gabrielittner commented Mar 10, 2022

This removes the fragment navigation result APIs and replaces them with new result APIs that work for both fragment and compose navigation.

How the API works

Inside your navigator you register for the results like before

val imageResultRequest = registerForNavigationResult<FooRoute, ImageResult>()

The new part here is that you need to specify FooRoute which is the route to the destination of the navigator.

The returned NavigationResultRequest is a ResultOwner like before so the results can be collected through imagePickerResultRequest.results.

What's new is that the request also holds a key. This key should be passed to the destination from which we want to retrieve a result:

@Parcelize
data class ImagePickerDirections(
    val key: NavigationResultRequest.Key<ImageResult>
)

The image picker in this example can then call deliverNavigationResult with this key to return a result. To keep the API simple there is no automatic back navigation anymore like we had in navigateBackWithResult:

navigator.deliverNavigationResult(imagePickerDirections.key, imageResult)
navigator.navigateBack()

Advantages

  • works for compose as well
  • no more extra FragmentNavEventNavigator class
  • the string key for the result doesn't need to be a shared constant anymore it's only used in registerForNavigationResult

Disadvantages

  • internally going through the SavedStateHandle of a NavBackStackEntry isn't as nice as the very direct Fragment API

@gabrielittner
Copy link
Member Author

Maybe we can also remove the string key from registerForNavigationResult<FooRoute, ImageResult>("my-request-key") we could create it internally using the classes of FooRoute and ImageResult. The only limitation that this would bring is that you'd only be able to have one result request per result type. So you couldn't have to separate requests for ImageResult in the same screen, but I'm also not sure if there is a use case for that.

@gabrielittner
Copy link
Member Author

If we later on find a use case for multiple requests for the same result from the same screen we could internally have an AtomicInteger that we increment and append to the key in registerForNavigationResult. We'd need to require ordering for calls to it but that's the same already for the activity and permission APIs were the underlying AndroidX APIs require ordering. So I'd say we can remove the string key

@gabrielittner
Copy link
Member Author

Removed the string key in c8df194

Copy link
Contributor

@kboyarshinov kboyarshinov left a comment

Choose a reason for hiding this comment

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

LGTM 👍

…avEventNavigator.kt

Co-authored-by: Kirill Boyarshinov <1917615+kboyarshinov@users.noreply.github.com>
@gabrielittner gabrielittner merged commit bc567ca into main Mar 14, 2022
@gabrielittner gabrielittner deleted the result-api branch March 14, 2022 09:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants