nowinandroid
nowinandroid copied to clipboard
Add type-safe navigation
Status: Blocked by https://issuetracker.google.com/340966212
Overview Navigation 2.8.0-alpha08 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 about the API design.
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:
- Refactored
InterestsListDetailScreen
to avoid the need forInterests2PaneViewModel
since it was only providing a single property - 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
Combined test coverage report
Overall Project | 40.34% -0.49% |
:green_apple: |
---|---|---|
Files changed | 57.67% | :x: |
Module | Coverage | |
---|---|---|
foryou | 56.16% -3.2% |
:x: |
bookmarks | 51.21% -1.59% |
:x: |
interests | 49.95% -3.74% |
:x: |
topic | 47.33% -2.09% |
:x: |
app | 32% -0.09% |
:green_apple: |
Files
Module | File | Coverage | |
---|---|---|---|
foryou | ForYouNavigation.kt | 0% -66.81% |
:x: |
bookmarks | BookmarksNavigation.kt | 0% -43.18% |
:x: |
interests | InterestsViewModel.kt | 100% | :green_apple: |
InterestsNavigation.kt | 48.81% -45.83% |
:x: | |
InterestsScreen.kt | 25.7% -0.09% |
:x: | |
topic | TopicViewModel.kt | 73.38% | :green_apple: |
TopicScreen.kt | 49.85% | :green_apple: | |
TopicNavigation.kt | 33.62% -31.9% |
:x: | |
app | TopLevelDestination.kt | 100% | :green_apple: |
InterestsListDetailScreen.kt | 92.15% -0.69% |
:green_apple: | |
NiaAppState.kt | 90.61% -1.04% |
:green_apple: | |
NiaApp.kt | 83.44% | :green_apple: | |
NiaNavHost.kt | 81.03% | :green_apple: |
Combined test coverage report
Overall Project | 40.34% -0.49% |
:green_apple: |
---|---|---|
Files changed | 55.61% | :x: |
Module | Coverage | |
---|---|---|
foryou | 56.16% -3.2% |
:x: |
bookmarks | 51.21% -1.59% |
:x: |
interests | 49.95% -3.69% |
:x: |
topic | 47.33% -2.09% |
:x: |
app | 32% -0.09% |
:green_apple: |
Files
Module | File | Coverage | |
---|---|---|---|
foryou | ForYouNavigation.kt | 0% -66.81% |
:x: |
bookmarks | BookmarksNavigation.kt | 0% -43.18% |
:x: |
interests | InterestsViewModel.kt | 100% | :green_apple: |
InterestsNavigation.kt | 48.81% -45.24% |
:x: | |
InterestsScreen.kt | 25.7% -0.09% |
:x: | |
topic | TopicViewModel.kt | 73.38% | :green_apple: |
TopicScreen.kt | 49.85% | :green_apple: | |
TopicNavigation.kt | 33.62% -31.9% |
:x: | |
app | TopLevelDestination.kt | 100% | :green_apple: |
InterestsListDetailScreen.kt | 92.15% -0.69% |
:green_apple: | |
NiaAppState.kt | 90.61% -1.04% |
:green_apple: | |
NiaApp.kt | 83.44% | :green_apple: | |
NiaNavHost.kt | 81.03% | :green_apple: |
Combined test coverage report
Overall Project | 40.37% -0.49% |
:green_apple: |
---|---|---|
Files changed | 55.94% | :x: |
Module | Coverage | |
---|---|---|
foryou | 56.16% -3.2% |
:x: |
bookmarks | 51.21% -1.59% |
:x: |
interests | 49.95% -3.69% |
:x: |
topic | 47.33% -2.09% |
:x: |
app | 32.04% -0.09% |
:green_apple: |
Files
Module | File | Coverage | |
---|---|---|---|
foryou | ForYouNavigation.kt | 0% -66.81% |
:x: |
bookmarks | BookmarksNavigation.kt | 0% -43.18% |
:x: |
interests | InterestsViewModel.kt | 100% | :green_apple: |
InterestsNavigation.kt | 48.81% -45.24% |
:x: | |
InterestsScreen.kt | 25.7% -0.09% |
:x: | |
topic | TopicViewModel.kt | 73.38% | :green_apple: |
TopicScreen.kt | 49.85% | :green_apple: | |
TopicNavigation.kt | 33.62% -31.9% |
:x: | |
app | TopLevelDestination.kt | 100% | :green_apple: |
InterestsListDetailScreen.kt | 92.15% -0.69% |
:green_apple: | |
NiaAppState.kt | 90.61% -1.04% |
:green_apple: | |
NiaApp.kt | 83.44% | :green_apple: | |
NiaNavHost.kt | 81.61% | :green_apple: |
Combined test coverage report
Overall Project | 40.36% -0.49% |
:green_apple: |
---|---|---|
Files changed | 58.1% | :x: |
Module | Coverage | |
---|---|---|
foryou | 56.16% -3.2% |
:x: |
bookmarks | 51.21% -1.59% |
:x: |
interests | 49.95% -3.69% |
:x: |
topic | 47.33% -2.09% |
:x: |
app | 32.1% -0.06% |
:green_apple: |
Files
Module | File | Coverage | |
---|---|---|---|
foryou | ForYouNavigation.kt | 0% -66.81% |
:x: |
bookmarks | BookmarksNavigation.kt | 0% -43.18% |
:x: |
interests | InterestsViewModel.kt | 100% | :green_apple: |
InterestsNavigation.kt | 48.81% -45.24% |
:x: | |
InterestsScreen.kt | 25.7% -0.09% |
:x: | |
topic | TopicViewModel.kt | 73.38% | :green_apple: |
TopicScreen.kt | 49.85% | :green_apple: | |
TopicNavigation.kt | 33.62% -31.9% |
:x: | |
app | TopLevelDestination.kt | 100% | :green_apple: |
InterestsListDetailScreen.kt | 93.88% -0.28% |
:green_apple: | |
NiaAppState.kt | 90.61% -1.04% |
:green_apple: | |
NiaApp.kt | 83.44% | :green_apple: | |
NiaNavHost.kt | 81.61% | :green_apple: |
Combined test coverage report
Overall Project | 40.58% -0.5% |
:green_apple: |
---|---|---|
Files changed | 52.96% | :x: |
Module | Coverage | |
---|---|---|
foryou | 56.21% -3.25% |
:x: |
bookmarks | 52.3% -1.59% |
:x: |
interests | 48.73% -3.8% |
:x: |
topic | 47.23% -2.09% |
:x: |
app | 33.08% -0.08% |
:green_apple: |
Files
Module | File | Coverage | |
---|---|---|---|
foryou | ForYouNavigation.kt | 0% -70.18% |
:x: |
bookmarks | BookmarksNavigation.kt | 0% -43.18% |
:x: |
interests | InterestsViewModel.kt | 95.35% -3.88% |
:green_apple: |
InterestsNavigation.kt | 48.81% -45.24% |
:x: | |
topic | TopicViewModel.kt | 72.69% | :green_apple: |
TopicScreen.kt | 49.85% | :green_apple: | |
TopicNavigation.kt | 33.62% -31.9% |
:x: | |
app | TopLevelDestination.kt | 100% | :green_apple: |
Interests2PaneViewModel.kt | 91.89% -8.11% |
:green_apple: | |
NiaAppState.kt | 90.61% -1.04% |
:green_apple: | |
InterestsListDetailScreen.kt | 89.9% -0.1% |
:green_apple: | |
NiaApp.kt | 83.44% | :green_apple: | |
NiaNavHost.kt | 81.61% | :green_apple: |
Combined test coverage report
Overall Project | 41.48% -0.2% |
:green_apple: |
---|---|---|
Files changed | 70.51% | :green_apple: |
Module | Coverage | |
---|---|---|
foryou | 60.17% -1.09% |
:x: |
bookmarks | 55.06% -1.25% |
:x: |
interests | 50.52% -1.63% |
:x: |
topic | 49.92% -0.7% |
:green_apple: |
app | 30.43% -0.05% |
:green_apple: |
Files
Module | File | Coverage | |
---|---|---|---|
foryou | ForYouScreen.kt | 57.33% | :green_apple: |
ForYouNavigation.kt | 0% -58.02% |
:x: | |
bookmarks | BookmarksNavigation.kt | 0% -41.18% |
:x: |
interests | InterestsViewModel.kt | 95.28% -3.94% |
:green_apple: |
InterestsNavigation.kt | 29.27% -60.98% |
:x: | |
topic | TopicViewModel.kt | 72.49% | :green_apple: |
TopicScreen.kt | 52.93% | :green_apple: | |
TopicNavigation.kt | 14.29% -20.95% |
:x: | |
app | TopLevelDestination.kt | 100% | :green_apple: |
NiaAppState.kt | 94.92% -0.35% |
:green_apple: | |
InterestsListDetailScreen.kt | 94.14% -0.11% |
:green_apple: | |
Interests2PaneViewModel.kt | 91.43% -8.57% |
:green_apple: | |
NiaApp.kt | 89.87% | :green_apple: | |
NiaNavHost.kt | 83.53% | :green_apple: |
Combined test coverage report
Overall Project | 44.67% -0.21% |
:green_apple: |
---|---|---|
Files changed | 70.12% | :green_apple: |
Module | Coverage | |
---|---|---|
foryou | 60.31% -0.79% |
:x: |
bookmarks | 54.03% -1.25% |
:x: |
interests | 50.35% -2.15% |
:x: |
topic | 49.92% -0.7% |
:green_apple: |
app | 48.47% -0.04% |
:green_apple: |
Files
Module | File | Coverage | |
---|---|---|---|
foryou | ForYouViewModel.kt | 88.33% | :green_apple: |
ForYouScreen.kt | 57.33% | :green_apple: | |
ForYouNavigation.kt | 0% -45.33% |
:x: | |
bookmarks | BookmarksNavigation.kt | 0% -41.18% |
:x: |
interests | InterestsViewModel.kt | 94.7% -4.55% |
:green_apple: |
InterestsNavigation.kt | 24% -68% |
:x: | |
topic | TopicViewModel.kt | 72.49% | :green_apple: |
TopicScreen.kt | 52.93% | :green_apple: | |
TopicNavigation.kt | 14.29% -20.95% |
:x: | |
app | TopLevelDestination.kt | 100% | :green_apple: |
InterestsListDetailScreen.kt | 98.34% | :green_apple: | |
NiaAppState.kt | 94.79% | :green_apple: | |
Interests2PaneViewModel.kt | 91.43% -8.57% |
:green_apple: | |
NiaApp.kt | 89.98% | :green_apple: | |
NiaNavHost.kt | 83.2% | :green_apple: |
Combined test coverage report
Overall Project | 42.34% -0.21% |
:green_apple: |
---|---|---|
Files changed | 69.28% | :green_apple: |
Module | Coverage | |
---|---|---|
bookmarks | 52.45% -1.15% |
:x: |
foryou | 52.31% -0.75% |
:x: |
app | 48.16% -0.06% |
:green_apple: |
interests | 45.28% -1.93% |
:x: |
topic | 44.14% -0.73% |
:green_apple: |
Files
Module | File | Coverage | |
---|---|---|---|
bookmarks | BookmarksNavigation.kt | 0% -35.9% |
:x: |
foryou | ForYouViewModel.kt | 88.33% | :green_apple: |
ForYouScreen.kt | 48.71% | :green_apple: | |
ForYouNavigation.kt | 0% -44.71% |
:x: | |
app | TopLevelDestination.kt | 100% | :green_apple: |
InterestsListDetailScreen.kt | 93.9% | :green_apple: | |
Interests2PaneViewModel.kt | 91.43% -8.57% |
:green_apple: | |
NiaAppState.kt | 90.53% -0.36% |
:green_apple: | |
NiaApp.kt | 86.51% | :green_apple: | |
NiaNavHost.kt | 81.4% | :green_apple: | |
interests | InterestsViewModel.kt | 94.7% -4.55% |
:green_apple: |
InterestsNavigation.kt | 24% -68% |
:x: | |
topic | TopicViewModel.kt | 72.49% | :green_apple: |
TopicScreen.kt | 45.88% | :green_apple: | |
TopicNavigation.kt | 13.39% -23.21% |
:x: |
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.
Combined test coverage report
Overall Project | 42.31% -0.21% |
:green_apple: |
---|---|---|
Files changed | 69.28% | :green_apple: |
Module | Coverage | |
---|---|---|
bookmarks | 52.45% -1.15% |
:x: |
foryou | 52.31% -0.75% |
:x: |
app | 48.16% -0.06% |
:green_apple: |
interests | 45.28% -1.93% |
:x: |
topic | 44.14% -0.73% |
:green_apple: |
Files
Module | File | Coverage | |
---|---|---|---|
bookmarks | BookmarksNavigation.kt | 0% -35.9% |
:x: |
foryou | ForYouViewModel.kt | 88.33% | :green_apple: |
ForYouScreen.kt | 48.71% | :green_apple: | |
ForYouNavigation.kt | 0% -44.71% |
:x: | |
app | TopLevelDestination.kt | 100% | :green_apple: |
InterestsListDetailScreen.kt | 93.9% | :green_apple: | |
Interests2PaneViewModel.kt | 91.43% -8.57% |
:green_apple: | |
NiaAppState.kt | 90.53% -0.36% |
:green_apple: | |
NiaApp.kt | 86.51% | :green_apple: | |
NiaNavHost.kt | 81.4% | :green_apple: | |
interests | InterestsViewModel.kt | 94.7% -4.55% |
:green_apple: |
InterestsNavigation.kt | 24% -68% |
:x: | |
topic | TopicViewModel.kt | 72.49% | :green_apple: |
TopicScreen.kt | 45.88% | :green_apple: | |
TopicNavigation.kt | 13.39% -23.21% |
:x: |