Skip to content

Migrate type-safe Navigation and Compose navigation snippets - #1219

Open
edward-williams-stellarelements wants to merge 1 commit into
android:mainfrom
StellarElements:entcwilliams/navigation-type-safe-compose
Open

edward-williams-stellarelements wants to merge 1 commit into
android:mainfrom
StellarElements:entcwilliams/navigation-type-safe-compose

Conversation

@edward-williams-stellarelements

Copy link
Copy Markdown

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 under com.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:

  • Type-safe-destinations and type-safety pages: each navigate(...) call sits inside a private function that takes the NavController.
  • Encapsulate page: the page defines Contacts, ContactDetails, and contactsDestination() in code blocks this PR doesn't migrate, and it doesn't print ContactsUiState. 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 the Contacts and ContactDetails destinations with the screens from the first region, so the graph in MyApp() has its start destination. The ContactScreens.kt region sits inside a private object, because its internal functions take those private types. MyApp() gets its navController from rememberNavController() inside an exclude block, which renders as the page's ... line.
  • Circular page: the region sits inside a private composable that declares navController and a NavHost with destinations a and b, so the graph has the a destination that the snippet pops up to. DestinationC is 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. spotlessApply leaves it unchanged, and spotlessCheck passes.

List of modifications (visible diffs vs. the guide pages)

  • Type-safe-destinations page, android_compose_navigation_type_safe_destinations_routes: the page's block starts with import 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.
  • Type-safe-destinations page, android_compose_navigation_type_safe_destinations_navigate: copied verbatim.
  • Type-safety page, 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.
  • Type-safety page, android_compose_navigation_type_safety_navigate: navController.navigate(Profile(id = 123)) becomes navController.navigate(Profile(id = "123")). The page's own route is data class Profile(val id: String), and the page text says the route class defines val id: String, so an Int argument doesn't compile.
  • Encapsulate page, android_compose_navigation_encapsulate_screens:
    • The two placeholder function bodies, { ... }, become { /* ... */ } on the same line, so they compile as empty bodies and keep the page's line layout.
    • Full stops added to the two comments: // Displays a list of contacts. and // Displays the details for an individual contact.
    • The file-name comment // ContactScreens.kt is copied as is.
  • Encapsulate page, android_compose_navigation_encapsulate_navigation_events: copied verbatim.
  • Encapsulate page, android_compose_navigation_encapsulate_my_app: copied verbatim. The page's ... line is the exclude block.
  • Circular page, android_compose_navigation_circular_pop_up_to: copied verbatim.

Snippets not migrated

  • Type-safe-destinations page: the three "Before" blocks stay on the guide page: the const val ROUTE_HOME / ROUTE_PROFILE strings, the string-route NavHost, and navController.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.
  • The other code blocks on these pages are left as they are on the guide page for now:
    • Type-safe-destinations page: the libs.versions.toml block, the type-safe NavHost, ProfileViewModel, the custom NavType, and its typeMap registration.
    • Type-safety page: the NavHost and ProfileViewModel blocks.
    • Encapsulate page: the two blocks under "Extract destinations" and the two ContactsNavigation.kt blocks under "Create destinations".
    • Circular page: the Views navigation XML.

Build: ./gradlew :compose:snippets:spotlessApply, then ./gradlew :compose:snippets:spotlessCheck :compose:snippets:compileDebugKotlin passes, with no warnings in the new files.

@snippet-bot

snippet-bot Bot commented Oct 23, 2026

Copy link
Copy Markdown

Here is the summary of changes.

You are about to add 8 region tags.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

This branch has not been deployed

No deployments
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.

1 participant