Skip to content

Add type-safe navigation#1413

Merged
dturner merged 42 commits intomainfrom
dt/nav-safe-args
Sep 5, 2024
Merged

Add type-safe navigation#1413
dturner merged 42 commits intomainfrom
dt/nav-safe-args

Conversation

@dturner
Copy link
Collaborator

@dturner dturner commented May 3, 2024

Overview
Navigation 2.8.0 introduces type-safe APIs for the Navigation DSL. Instead of using strings for routes and argument names, you can define types which are verified at compile-time. This blog post explains more.

What have I done and why
Migrated the navigation code from strings to types. For example, here's the old navigation code for the topic feature:

const val TOPIC_ID_ARG = "topicId"
const val TOPIC_ROUTE = "topic_route"

fun NavController.navigateToTopic(topicId: String, navOptions: NavOptionsBuilder.() -> Unit = {}) {
    val encodedId = URLEncoder.encode(topicId, URL_CHARACTER_ENCODING)
    val newRoute = "$TOPIC_ROUTE/$encodedId"
    navigate(newRoute) {
        navOptions()
    }
}

fun NavGraphBuilder.topicScreen(
    showBackButton: Boolean,
    onBackClick: () -> Unit,
    onTopicClick: (String) -> Unit,
) {
    composable(
        route = "topic_route/{$TOPIC_ID_ARG}",
        arguments = listOf(
            navArgument(TOPIC_ID_ARG) { type = NavType.StringType },
        ),
    ) {
        TopicScreen(...)
    }
}

And here's the new type-safe code:

@Serializable data class Topic(val id: String)

fun NavController.navigateToTopic(topicId: String, navOptions: NavOptionsBuilder.() -> Unit = {}) {
    navigate(route = Topic(topicId)) {
        navOptions()
    }
}

fun NavGraphBuilder.topicScreen(
    showBackButton: Boolean,
    onBackClick: () -> Unit,
    onTopicClick: (String) -> Unit,
) {
    composable<Topic> {
        TopicScreen(...)
    }
}

Note that there's no need to URL encode the arguments because there's no danger of them being interpreted as placeholders (e.g. "{topicId}") in the route string (because there is no route string).

I have also updated the app manifest to allow deeplinks to be tested from the terminal. Example: adb shell am start -a android.intent.action.VIEW -d "https://www.nowinandroid.apps.samples.google.com/foryou/2" com.google.samples.apps.nowinandroid.demo.debug

dturner and others added 7 commits May 1, 2024 13:46
Change-Id: Idf4386f10c780d3edc1f8aa11b428cb146e982c3
Change-Id: I4f3c310be693ecbcbc8b99c4e573d7fc6e9a2f74
Change-Id: I72f617c594b5e0ae272cf94d2d7288446153420a
Change-Id: I02a8efb46695b3a90701966bfea4ed76aeec131b
…e string value

Change-Id: I4767578028b55c2bc7b1763bdeef87345b9fbf06
Change-Id: Ia112f87c7f1bb7fa9ebe08b82d26e00b4ad17d05
@github-actions
Copy link

github-actions bot commented May 3, 2024

Combined test coverage report

Overall Project 40.34% -0.49% 🍏
Files changed 57.67%

Module Coverage
foryou 56.16% -3.2%
bookmarks 51.21% -1.59%
interests 49.95% -3.74%
topic 47.33% -2.09%
app 32% -0.09% 🍏
Files
Module File Coverage
foryou ForYouNavigation.kt 0% -66.81%
bookmarks BookmarksNavigation.kt 0% -43.18%
interests InterestsViewModel.kt 100% 🍏
InterestsNavigation.kt 48.81% -45.83%
InterestsScreen.kt 25.7% -0.09%
topic TopicViewModel.kt 73.38% 🍏
TopicScreen.kt 49.85% 🍏
TopicNavigation.kt 33.62% -31.9%
app TopLevelDestination.kt 100% 🍏
InterestsListDetailScreen.kt 92.15% -0.69% 🍏
NiaAppState.kt 90.61% -1.04% 🍏
NiaApp.kt 83.44% 🍏
NiaNavHost.kt 81.03% 🍏

dturner and others added 3 commits May 3, 2024 17:06
@github-actions
Copy link

github-actions bot commented May 3, 2024

Combined test coverage report

Overall Project 40.34% -0.49% 🍏
Files changed 55.61%

Module Coverage
foryou 56.16% -3.2%
bookmarks 51.21% -1.59%
interests 49.95% -3.69%
topic 47.33% -2.09%
app 32% -0.09% 🍏
Files
Module File Coverage
foryou ForYouNavigation.kt 0% -66.81%
bookmarks BookmarksNavigation.kt 0% -43.18%
interests InterestsViewModel.kt 100% 🍏
InterestsNavigation.kt 48.81% -45.24%
InterestsScreen.kt 25.7% -0.09%
topic TopicViewModel.kt 73.38% 🍏
TopicScreen.kt 49.85% 🍏
TopicNavigation.kt 33.62% -31.9%
app TopLevelDestination.kt 100% 🍏
InterestsListDetailScreen.kt 92.15% -0.69% 🍏
NiaAppState.kt 90.61% -1.04% 🍏
NiaApp.kt 83.44% 🍏
NiaNavHost.kt 81.03% 🍏

Change-Id: Ia5bb0abd366653aff5cf5a772ac11a837e96b9b1
@github-actions
Copy link

github-actions bot commented May 3, 2024

Combined test coverage report

Overall Project 40.37% -0.49% 🍏
Files changed 55.94%

Module Coverage
foryou 56.16% -3.2%
bookmarks 51.21% -1.59%
interests 49.95% -3.69%
topic 47.33% -2.09%
app 32.04% -0.09% 🍏
Files
Module File Coverage
foryou ForYouNavigation.kt 0% -66.81%
bookmarks BookmarksNavigation.kt 0% -43.18%
interests InterestsViewModel.kt 100% 🍏
InterestsNavigation.kt 48.81% -45.24%
InterestsScreen.kt 25.7% -0.09%
topic TopicViewModel.kt 73.38% 🍏
TopicScreen.kt 49.85% 🍏
TopicNavigation.kt 33.62% -31.9%
app TopLevelDestination.kt 100% 🍏
InterestsListDetailScreen.kt 92.15% -0.69% 🍏
NiaAppState.kt 90.61% -1.04% 🍏
NiaApp.kt 83.44% 🍏
NiaNavHost.kt 81.61% 🍏

…lScreen

Change-Id: I659f729191bce00683b1621c360c8f36f00595f9
@github-actions
Copy link

github-actions bot commented May 3, 2024

Combined test coverage report

Overall Project 40.36% -0.49% 🍏
Files changed 58.1%

Module Coverage
foryou 56.16% -3.2%
bookmarks 51.21% -1.59%
interests 49.95% -3.69%
topic 47.33% -2.09%
app 32.1% -0.06% 🍏
Files
Module File Coverage
foryou ForYouNavigation.kt 0% -66.81%
bookmarks BookmarksNavigation.kt 0% -43.18%
interests InterestsViewModel.kt 100% 🍏
InterestsNavigation.kt 48.81% -45.24%
InterestsScreen.kt 25.7% -0.09%
topic TopicViewModel.kt 73.38% 🍏
TopicScreen.kt 49.85% 🍏
TopicNavigation.kt 33.62% -31.9%
app TopLevelDestination.kt 100% 🍏
InterestsListDetailScreen.kt 93.88% -0.28% 🍏
NiaAppState.kt 90.61% -1.04% 🍏
NiaApp.kt 83.44% 🍏
NiaNavHost.kt 81.61% 🍏

dturner added 6 commits May 7, 2024 23:10
…inor tidy ups.

Change-Id: Icfa79eac6f7327c365f79fd7d15dfa1f8c77184d
Change-Id: Ia5ab36f2d75d8d061e63c0cac5262503bc93a638
Change-Id: Ia197b2403e8250720387123d8c5d5d9ad809a036
Change-Id: Ia7460a618b2ddf8a25debce92308573bc18368a8
Change-Id: I535ca7fcec48c32b727c1c6c465a719d60dcb0f9
Change-Id: I63c0abb16cbf540ef72dfb945518a48113bdbf75
@dturner dturner force-pushed the dt/nav-safe-args branch from 028e43a to 4a84cf2 Compare May 8, 2024 18:32
dturner and others added 6 commits May 30, 2024 11:20
* main:
  Remove the printModulePaths task and read modules direct from settings
  Update pull_request_template.md
  Made pull request instructions clearer
  Update gradle wrapper
  Upgrade AGP to 8.4.0, Android Build Tools to 31.4.0, and Gradle to 8.7.
  Regenerate SVG graphs and cleanup/reformat files
  Update build.gradle.kts
  Remove duplicated AndroidX Work testing dependency
  Bump gradle/wrapper-validation-action from 2 to 3
  🤖 Updates baselines for Dependency Guard
  Bump com.dropbox.dependency-guard from 0.4.3 to 0.5.0
  Bump protobuf from 3.25.2 to 4.26.0
  Bump app.cash.turbine:turbine from 1.0.0 to 1.1.0
  Bump androidxMacroBenchmark from 1.2.2 to 1.2.3
  🤖 Updates baselines for Dependency Guard
  Unnecessary safe call on a non-null receiver
  Bump androidx.activity:activity-compose from 1.8.0 to 1.8.2
  Apply suggestions from code review
  Move Android instrumented test `ThemeTest` to unit test

Change-Id: Id3283279ca5a9d90c2bbc4740d6c583ba8dded41
Change-Id: Ie9120031fc3738d0640c922624f15db0e15417d7
* main:
  Kotlin 2.0.0 (#1036)
  Update app/src/main/kotlin/com/google/samples/apps/nowinandroid/ui/NiaApp.kt
  🤖 Updates screenshots
  🤖 Updates baselines for Dependency Guard
  Update material3-adaptive to beta01
  🤖 Updates baselines for Dependency Guard
  Bump androidxComposeAlpha from 1.7.0-alpha08 to 1.7.0-beta01
  🤖 Updates baselines for Dependency Guard
  Bump hilt from 2.51 to 2.51.1
  Add NavigationSuiteScaffold
  Fix state loss with workaround

Change-Id: I14d7294fe93f68b9aca6df9bf7bf87ff95583581
Change-Id: I47456f19f2b41cafe44ba7636cad6d1fea782a33
Change-Id: I782fab167103edbdaa1d95839b4fbc6e659fef4e
@github-actions
Copy link

Combined test coverage report

Overall Project 41.48% -0.2% 🍏
Files changed 70.51% 🍏

Module Coverage
foryou 60.17% -1.09%
bookmarks 55.06% -1.25%
interests 50.52% -1.63%
topic 49.92% -0.7% 🍏
app 30.43% -0.05% 🍏
Files
Module File Coverage
foryou ForYouScreen.kt 57.33% 🍏
ForYouNavigation.kt 0% -58.02%
bookmarks BookmarksNavigation.kt 0% -41.18%
interests InterestsViewModel.kt 95.28% -3.94% 🍏
InterestsNavigation.kt 29.27% -60.98%
topic TopicViewModel.kt 72.49% 🍏
TopicScreen.kt 52.93% 🍏
TopicNavigation.kt 14.29% -20.95%
app TopLevelDestination.kt 100% 🍏
NiaAppState.kt 94.92% -0.35% 🍏
InterestsListDetailScreen.kt 94.14% -0.11% 🍏
Interests2PaneViewModel.kt 91.43% -8.57% 🍏
NiaApp.kt 89.87% 🍏
NiaNavHost.kt 83.53% 🍏

dturner and others added 9 commits July 19, 2024 09:00
Change-Id: I7c880cc73374e053ea0d41df00a4bda95ed4b551
Change-Id: I385b6f49ba37801e40bb90254abe96e2350d48e8
* main: (58 commits)
  Instrumented tests for InterestsListDetailScreen (#1518)
  Remove Firebase AD_SERVICE_CONFIG property
  updateProdReleaseBadging
  Merge Android & JVM plugins into a single `HiltConventionPlugin`
  Update dependency guard
  🤖 Updates baselines for Dependency Guard
  Update to Lifecycle 2.8.3
  Add android.adservices.AD_SERVICES_CONFIG to badges
  Apply suggestions from code review
  Update app/src/main/AndroidManifest.xml
  generateModuleGraphs
  dependencyGuardBaseline
  updateProdReleaseBadging
  Fix spotless.
  Change set to `=` operator
  Cleanup unused reference in `libs.versions.toml`
  Regenerate SVG graphs
  Update comment wording
  Use stdin to avoid repeating the output file name
  Replace bash commands with proper svgo cli
  ...

Change-Id: I1d804dcb6ac0ca857cb303b8c8bff5704b5d27a5
Change-Id: I9d589cfe5c20a6b55708b99f5d79b10271af8ab4
Change-Id: I945632c419e11671eb73690999f9a234e577d16b
Change-Id: Ib093c206809a470b7f6135933bd2a72046865684
Change-Id: Ib6e47d9a9cced4c967756f95a5c98587628bd2cb
@github-actions
Copy link

github-actions bot commented Sep 4, 2024

Combined test coverage report

Overall Project 44.67% -0.21% 🍏
Files changed 70.12% 🍏

Module Coverage
foryou 60.31% -0.79%
bookmarks 54.03% -1.25%
interests 50.35% -2.15%
topic 49.92% -0.7% 🍏
app 48.47% -0.04% 🍏
Files
Module File Coverage
foryou ForYouViewModel.kt 88.33% 🍏
ForYouScreen.kt 57.33% 🍏
ForYouNavigation.kt 0% -45.33%
bookmarks BookmarksNavigation.kt 0% -41.18%
interests InterestsViewModel.kt 94.7% -4.55% 🍏
InterestsNavigation.kt 24% -68%
topic TopicViewModel.kt 72.49% 🍏
TopicScreen.kt 52.93% 🍏
TopicNavigation.kt 14.29% -20.95%
app TopLevelDestination.kt 100% 🍏
InterestsListDetailScreen.kt 98.34% 🍏
NiaAppState.kt 94.79% 🍏
Interests2PaneViewModel.kt 91.43% -8.57% 🍏
NiaApp.kt 89.98% 🍏
NiaNavHost.kt 83.2% 🍏

Copy link
Contributor

@alexvanyo alexvanyo left a comment

Choose a reason for hiding this comment

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

Looks great!

…nandroid/interests/InterestsViewModelTest.kt

Co-authored-by: Alex Vanyo <vanyo@google.com>
@github-actions
Copy link

github-actions bot commented Sep 4, 2024

Combined test coverage report

Overall Project 42.34% -0.21% 🍏
Files changed 69.28% 🍏

Module Coverage
bookmarks 52.45% -1.15%
foryou 52.31% -0.75%
app 48.16% -0.06% 🍏
interests 45.28% -1.93%
topic 44.14% -0.73% 🍏
Files
Module File Coverage
bookmarks BookmarksNavigation.kt 0% -35.9%
foryou ForYouViewModel.kt 88.33% 🍏
ForYouScreen.kt 48.71% 🍏
ForYouNavigation.kt 0% -44.71%
app TopLevelDestination.kt 100% 🍏
InterestsListDetailScreen.kt 93.9% 🍏
Interests2PaneViewModel.kt 91.43% -8.57% 🍏
NiaAppState.kt 90.53% -0.36% 🍏
NiaApp.kt 86.51% 🍏
NiaNavHost.kt 81.4% 🍏
interests InterestsViewModel.kt 94.7% -4.55% 🍏
InterestsNavigation.kt 24% -68%
topic TopicViewModel.kt 72.49% 🍏
TopicScreen.kt 45.88% 🍏
TopicNavigation.kt 13.39% -23.21%

@Jaehwa-Noh
Copy link
Contributor

Could you check this PR release mode onto the device or emulator? May the @Serializable Route be obfuscated, and makes runtime error. In this, you need to update a proguard rules.

dturner and others added 3 commits September 5, 2024 08:39
Change-Id: I695d6e4117dd35bd34aefdaa8544f2bae2af7b3b
Change-Id: I4f1754950719e0a5fb0cc49d01a10ae1a388fd8f
@github-actions
Copy link

github-actions bot commented Sep 5, 2024

Combined test coverage report

Overall Project 42.31% -0.21% 🍏
Files changed 69.28% 🍏

Module Coverage
bookmarks 52.45% -1.15%
foryou 52.31% -0.75%
app 48.16% -0.06% 🍏
interests 45.28% -1.93%
topic 44.14% -0.73% 🍏
Files
Module File Coverage
bookmarks BookmarksNavigation.kt 0% -35.9%
foryou ForYouViewModel.kt 88.33% 🍏
ForYouScreen.kt 48.71% 🍏
ForYouNavigation.kt 0% -44.71%
app TopLevelDestination.kt 100% 🍏
InterestsListDetailScreen.kt 93.9% 🍏
Interests2PaneViewModel.kt 91.43% -8.57% 🍏
NiaAppState.kt 90.53% -0.36% 🍏
NiaApp.kt 86.51% 🍏
NiaNavHost.kt 81.4% 🍏
interests InterestsViewModel.kt 94.7% -4.55% 🍏
InterestsNavigation.kt 24% -68%
topic TopicViewModel.kt 72.49% 🍏
TopicScreen.kt 45.88% 🍏
TopicNavigation.kt 13.39% -23.21%

@dturner dturner merged commit db65a6f into main Sep 5, 2024
@dturner dturner deleted the dt/nav-safe-args branch September 5, 2024 17:48
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.

7 participants