Repository navigation
Migrate type-safe Navigation and Compose navigation snippets - #1219
Open
edward-williams-stellarelements wants to merge 1 commit into
Open
edward-williams-stellarelements wants to merge 1 commit into
edward-williams-stellarelements wants to merge 1 commit into
Conversation
edward-williams-stellarelements
requested a review
from a team
as a code owner
October 23, 2026 20:52
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:
This PR adds 8 region tags in 4 new files in
:compose:snippets. Each guide page gets its own sub-package undercom.example.compose.snippets.navigation, so route names that more than one page uses (Home,Profile) don't collide:navigation/typesafedestinations/TypeSafeDestinations.kt:android_compose_navigation_type_safe_destinations_routes,android_compose_navigation_type_safe_destinations_navigate.navigation/typesafety/TypeSafety.kt:android_compose_navigation_type_safety_routes,android_compose_navigation_type_safety_navigate.navigation/encapsulate/Encapsulate.kt:android_compose_navigation_encapsulate_screens,android_compose_navigation_encapsulate_navigation_events,android_compose_navigation_encapsulate_my_app.navigation/circular/CircularNavigation.kt:android_compose_navigation_circular_pop_up_to.No new dependencies:
navigation-compose,kotlinx-serialization, and the Kotlin serialization plugin are already in:compose:snippets.Everything the snippets need to compile that the guide pages don't print sits outside the regions and is
private:navigate(...)call sits inside a private function that takes theNavController.Contacts,ContactDetails, andcontactsDestination()in code blocks this PR doesn't migrate, and it doesn't printContactsUiState. The file declares private versions of these below the regions. A helper with the name the snippet calls,contactDetailsDestination(), is defined outside the snippet too. The two helpers register theContactsandContactDetailsdestinations with the screens from the first region, so the graph inMyApp()has its start destination. TheContactScreens.ktregion sits inside a private object, because itsinternalfunctions take those private types.MyApp()gets itsnavControllerfromrememberNavController()inside an exclude block, which renders as the page's...line.navControllerand aNavHostwith destinationsaandb, so the graph has theadestination that the snippet pops up to.DestinationCis a private composable with one button.The regions keep the guide pages' own indentation, including the 2-space indentation of the encapsulate and circular blocks.
spotlessApplyleaves it unchanged, andspotlessCheckpasses.List of modifications (visible diffs vs. the guide pages)
android_compose_navigation_type_safe_destinations_routes: the page's block starts withimport kotlinx.serialization.Serializable. The snippet shows that line as a comment,// import kotlinx.serialization.Serializable, in the same place, and the real import is at the top of the file.android_compose_navigation_type_safe_destinations_navigate: copied verbatim.android_compose_navigation_type_safety_routes: full stops added to the two comments:// Define a home route that doesn't take any arguments.and// Define a profile route that takes an ID.android_compose_navigation_type_safety_navigate:navController.navigate(Profile(id = 123))becomesnavController.navigate(Profile(id = "123")). The page's own route isdata class Profile(val id: String), and the page text says the route class definesval id: String, so anIntargument doesn't compile.android_compose_navigation_encapsulate_screens:{ ... }, become{ /* ... */ }on the same line, so they compile as empty bodies and keep the page's line layout.// Displays a list of contacts.and// Displays the details for an individual contact.// ContactScreens.ktis copied as is.android_compose_navigation_encapsulate_navigation_events: copied verbatim.android_compose_navigation_encapsulate_my_app: copied verbatim. The page's...line is the exclude block.android_compose_navigation_circular_pop_up_to: copied verbatim.Snippets not migrated
const val ROUTE_HOME/ROUTE_PROFILEstrings, the string-routeNavHost, andnavController.navigate("profile/user123"). They show the string-based routes that the guide migrates away from, so they stay as the old half of each before-and-after pair.libs.versions.tomlblock, the type-safeNavHost,ProfileViewModel, the customNavType, and itstypeMapregistration.NavHostandProfileViewModelblocks.ContactsNavigation.ktblocks under "Create destinations".Build:
./gradlew :compose:snippets:spotlessApply, then./gradlew :compose:snippets:spotlessCheck :compose:snippets:compileDebugKotlinpasses, with no warnings in the new files.