Repository navigation
Migrate NavOptions, pass data, conditional navigation, animate transitions, and programmatic navigation snippets - #1227
Open
edward-williams-stellarelements wants to merge 1 commit into
Conversation
…tions, and programmatic navigation snippets
|
Here is the summary of changes. You are about to add 10 region tags.
This comment is generated by snippet-bot.
|
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Code snippets are for:
Companion CL: cl/1007653640
This PR adds 10 region tags to
:views, one for each code block it migrates from these pages: 7 navigation graph XML blocks and 3 Kotlin blocks. Each navigation graph document that a guide prints gets its own file inviews/src/main/res/navigation/. The two excerpts that the Pass-data page prints from one graph share a file.android_views_navigation_navoptions_nav_graphviews/src/main/res/navigation/nav_graph_navoptions.xmlandroid_views_navigation_navoptions_navigate_actionviews/.../navigation/navoptions/NavigateWithOptions.ktandroid_views_navigation_navoptions_programmaticviews/.../navigation/navoptions/NavigateWithOptions.ktandroid_views_navigation_navoptions_deep_linkviews/.../navigation/navoptions/NavigateWithOptions.ktandroid_views_navigation_pass_data_destination_argumentviews/src/main/res/navigation/nav_graph_pass_data.xmlandroid_views_navigation_pass_data_action_argumentviews/src/main/res/navigation/nav_graph_pass_data.xmlandroid_views_navigation_pass_data_global_actionviews/src/main/res/navigation/nav_graph_pass_data_global_action.xmlandroid_views_navigation_conditional_nav_graphviews/src/main/res/navigation/nav_graph_conditional.xmlandroid_views_navigation_animate_transitions_action_animationsviews/src/main/res/navigation/nav_graph_animate_transitions.xmlandroid_views_navigation_programmatic_nav_graphviews/src/main/res/navigation/nav_graph_programmatic.xmlviews/...isviews/src/main/java/com/example/example/snippet/views.Dependencies added to
:viewsandroidx.navigation:navigation-fragment-ktx, as a newandroidx-navigation-fragment-ktxcatalog entry on the existingandroidx-navigationversion. The catalog and Gradle lines are identical to the ones in Migrate activity and dialog destination, action, and NavController snippets #1220 and Migrate navigation graph design, Kotlin DSL, nested graph, and Navigation Editor snippets #1221, so whichever of these PRs merges first, the others drop the duplicate lines when they rebase. Directly or through the Navigation runtime library it brings in, it provides:Fragment.findNavController()andNavController.navigate()in the three Navoptions Kotlin snippets, and thenavOptions {}builder with itsanim {}block.<navigation>,<fragment>,<action>, and<argument>elements and theirapp:attributes (startDestination,destination,enterAnim,exitAnim,popEnterAnim,popExitAnim,popUpTo,popUpToInclusive,argType) in the six graph files.androidx.navigation:navigation-ui, as a newandroidx-navigation-uicatalog entry on the same version. The Navoptions page's graph uses the library's default transition animations,@anim/nav_default_enter_anim,@anim/nav_default_exit_anim,@anim/nav_default_pop_enter_anim, and@anim/nav_default_pop_exit_anim. They come from this artifact, and resource linking fails without it. It's the artifact the Navigation dependency instructions list.No other dependency or plugin is added.
Supporting code and resources
Everything in this section sits outside the region tags, so none of it shows on the guide pages.
NavigateWithOptions.kt: each region sits in a small private function.navControlleranddeepLinkUri(aUri) are parameters. The two regions that callfindNavController()without a receiver are privateFragmentextension functions, so the call resolves to the Fragment extension.nav_graph_pass_data.xml: the Pass-data page prints only a<fragment>and an<action>from one graph. The file adds the<navigation>root (the namespaces andapp:startDestination="@id/myFragment") around both regions. The<action>is a global action whose destination is the region'smyFragment.nav_graph_pass_data_global_action.xml: the page's...line is a plain exclude that hides amainFragmentdestination, whichapp:startDestination="@id/mainFragment"needs. It renders as..., like the page.nav_graph_animate_transitions.xml: the Animate-transitions page prints only the<fragment>element. The file adds the<navigation>root (with theandroid,app, andtoolsnamespaces) around the region, and aconfirmationFragmentdestination after it, so that the action'sapp:destination="@id/confirmationFragment"resolves.tools:layoutreferences only affect the Navigation Editor preview, so no layout files are added for them. The class names inandroid:nameare kept as printed.Compile-driven resources (required for the snippets to compile, no region tags):
views/src/main/res/values/navoptions.xml: the idaction_fragmentOne_to_fragmentTwo, forR.id.action_fragmentOne_to_fragmentTwoin the Navoptions page's programmatic Kotlin snippet. The page's graph doesn't contain that action, so the id is declared on its own. The name comes from the page.views/src/main/res/anim/slide_in_right.xml,slide_out_left.xml,slide_in_left.xml, andslide_out_right.xml: the Animate-transitions page's action references@anim/slide_in_right,@anim/slide_out_left,@anim/slide_in_left, and@anim/slide_out_right, and resource linking fails while they're undefined. Nothing in this repository or in:views' dependencies provides them. The names come from the page. Each file is a single<translate>animation written for this PR: the view slides in from, or out to, the side its name says, over@android:integer/config_mediumAnimTime.List of modifications (visible diffs vs. the page)
<?xml version="1.0" encoding="utf-8"?>. That line has to be the first line of the file, ahead of the license header, so it can't be inside a region. These 3 regions start at<navigation.android_views_navigation_navoptions_programmatic:spotlessApplyindents the call's three arguments by 4 spaces and lines up the closing brace of thenavOptions {}block with them. On the page, the arguments started at the same column asfindNavController(), and the closing brace was indented 2 spaces. The line count is unchanged.android_views_navigation_navoptions_programmaticandandroid_views_navigation_navoptions_deep_link: the commentUse the Kotlin DSL for building NavOptionsnow ends with a full stop.android:id="@+id/main_nav"; the fourapp:*Animattributes; andapp:startDestinationplus the home destination'sandroid:idandandroid:name. The companion CL passes their line numbers to the snippet include.navigate()line and the Pass-data<fragment>excerpt render without the extra leading spaces that the page printed before them.Snippets not migrated
onClick()block that callsSpecifyAmountFragmentDirections, and thenavArgs()block that readsConfirmationFragmentArgs. The Safe Args Gradle plugin generates these classes, and this repository doesn't apply that plugin.bundleOf()withnavigate(), and reading the argument into aTextView).@KeepParcelableArgblock: will be migrated in a follow-up pull request. Its{ ... }class body is a placeholder that needs a ruling on how to show it.proguard-rules.problock: ProGuard/R8 keep rules, which are tool configuration and stay on the page.onClick()examples (the "Safe Args example" and the one under "Ensure type safety by using Safe Args") stay on the page. They call classes that the Safe Args Gradle plugin generates, and this repository doesn't apply that plugin.ProfileFragmentandLoginFragmentKotlin blocks are Fragment-based code.FragmentNavigatorExtras).ActivityOptions) and thefinish()override, which are Activity code.NavHostFragment.create(), the twoonViewCreated()blocks (returning a result, and dialog destinations), the twonavGraphViewModels()blocks, and theNavHostFragmentblock under "Modifying inflated navigation graphs".LiveDataobserver on aNavBackStackEntry, andpreviousBackStackEntry?.savedStateHandle?.set().The follow-up pull request will add those blocks to
:views, next to this PR's files.Heads-up, unrelated to this PR and left untouched:
spotlessApplylogs "Skipping 'build.gradle.kts' because it does not converge" forviews/build.gradle.kts, which has a nonstandard license header onmain.Build:
./gradlew :views:spotlessApply(one formatting change, listed above; a second run changes nothing), then./gradlew :views:buildpasses. The added files have no compiler warnings and no lint findings.