-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Migrate Jetnews to Navigation 3 #1675
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bsagmoe
wants to merge
4
commits into
main
Choose a base branch
from
bsagmoe/jetnews-nav3
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,470
−684
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
60 changes: 60 additions & 0 deletions
60
JetNews/app/src/main/java/com/example/jetnews/deeplink/JetnewsDeeplinks.kt
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| /* | ||
| * Copyright 2026 The Android Open Source Project | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.example.jetnews.deeplink | ||
|
|
||
| import android.net.Uri | ||
| import androidx.core.net.toUri | ||
| import androidx.navigation3.runtime.NavKey | ||
| import com.example.jetnews.deeplink.util.DeepLinkMatcher | ||
| import com.example.jetnews.deeplink.util.DeepLinkPattern | ||
| import com.example.jetnews.deeplink.util.DeepLinkRequest | ||
| import com.example.jetnews.deeplink.util.KeyDecoder | ||
| import com.example.jetnews.ui.home.HomeKey | ||
| import com.example.jetnews.ui.interests.InterestsKey | ||
| import com.example.jetnews.ui.navigation.DeepLinkKey | ||
| import com.example.jetnews.ui.post.PostKey | ||
|
|
||
| val HomeDeepLinkPattern = DeepLinkPattern( | ||
| HomeKey.serializer(), | ||
| uriPattern = "https://developer.android.com/jetnews".toUri(), | ||
| ) | ||
|
|
||
| val PostDeepLinkPattern = DeepLinkPattern( | ||
| PostKey.serializer(), | ||
| uriPattern = "https://developer.android.com/jetnews/posts/{postId}".toUri(), | ||
| ) | ||
|
|
||
| val InterestsDeepLinkPattern = DeepLinkPattern( | ||
| InterestsKey.serializer(), | ||
| uriPattern = "https://developer.android.com/jetnews/interests".toUri(), | ||
| ) | ||
|
|
||
| val JetnewsDeepLinkPatterns = listOf(HomeDeepLinkPattern, PostDeepLinkPattern, InterestsDeepLinkPattern) | ||
|
|
||
| fun Uri.handleDeepLink(): List<NavKey>? { | ||
| val deepLinkRequest = DeepLinkRequest(this) | ||
|
|
||
| val deepLinkMatchResult = JetnewsDeepLinkPatterns.firstNotNullOfOrNull { | ||
| DeepLinkMatcher(deepLinkRequest, it).match() | ||
| } ?: return null | ||
|
|
||
| val initialKey = KeyDecoder(deepLinkMatchResult.args).decodeSerializableValue(deepLinkMatchResult.serializer) | ||
|
|
||
| return generateSequence(initialKey) { (it as? DeepLinkKey)?.parent } | ||
| .toList() | ||
| .asReversed() | ||
| } |
94 changes: 94 additions & 0 deletions
94
JetNews/app/src/main/java/com/example/jetnews/deeplink/util/DeepLinkMatcher.kt
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| /* | ||
| * Copyright 2026 The Android Open Source Project | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.example.jetnews.deeplink.util | ||
|
|
||
| import android.util.Log | ||
| import androidx.navigation3.runtime.NavKey | ||
| import kotlinx.serialization.KSerializer | ||
|
|
||
| internal class DeepLinkMatcher<T : NavKey>(val request: DeepLinkRequest, val deepLinkPattern: DeepLinkPattern<T>) { | ||
| /** | ||
| * Match a [DeepLinkRequest] to a [DeepLinkPattern]. | ||
| * | ||
| * Returns a [DeepLinkMatchResult] if this matches the pattern, returns null otherwise | ||
| */ | ||
| fun match(): DeepLinkMatchResult<T>? { | ||
| if (request.uri.scheme != deepLinkPattern.uriPattern.scheme) return null | ||
| if (!request.uri.authority.equals(deepLinkPattern.uriPattern.authority, ignoreCase = true)) return null | ||
| if (request.pathSegments.size != deepLinkPattern.pathSegments.size) return null | ||
| // exact match (url does not contain any arguments) | ||
| if (request.uri == deepLinkPattern.uriPattern) | ||
| return DeepLinkMatchResult(deepLinkPattern.serializer, mapOf()) | ||
|
|
||
| val args = mutableMapOf<String, Any>() | ||
| // match the path | ||
| request.pathSegments | ||
| .asSequence() | ||
| // zip to compare the two objects side by side, order matters here so we | ||
| // need to make sure the compared segments are at the same position within the url | ||
| .zip(deepLinkPattern.pathSegments.asSequence()) | ||
| .forEach { it -> | ||
| // retrieve the two path segments to compare | ||
| val requestedSegment = it.first | ||
| val candidateSegment = it.second | ||
| // if the potential match expects a path arg for this segment, try to parse the | ||
| // requested segment into the expected type | ||
| if (candidateSegment.isParamArg) { | ||
| val parsedValue = try { | ||
| candidateSegment.typeParser.invoke(requestedSegment) | ||
| } catch (e: IllegalArgumentException) { | ||
| Log.e(TAG_LOG_ERROR, "Failed to parse path value:[$requestedSegment].", e) | ||
| return null | ||
| } | ||
| args[candidateSegment.stringValue] = parsedValue | ||
| } else if (requestedSegment != candidateSegment.stringValue) { | ||
| // if it's path arg is not the expected type, its not a match | ||
| return null | ||
| } | ||
| } | ||
| // match queries (if any) | ||
| request.queries.forEach { query -> | ||
| val name = query.key | ||
| // If the pattern does not define this query parameter, ignore it. | ||
| // This prevents a NullPointerException. | ||
| val queryStringParser = deepLinkPattern.queryValueParsers[name] ?: return@forEach | ||
|
|
||
| val queryParsedValue = try { | ||
| queryStringParser.invoke(query.value) | ||
| } catch (e: IllegalArgumentException) { | ||
| Log.e(TAG_LOG_ERROR, "Failed to parse query name:[$name] value:[${query.value}].", e) | ||
| return null | ||
| } | ||
| args[name] = queryParsedValue | ||
| } | ||
| // provide the serializer of the matching key and map of arg names to parsed arg values | ||
| return DeepLinkMatchResult(deepLinkPattern.serializer, args) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Created when a requested deeplink matches with a supported deeplink | ||
| * | ||
| * @param [T] the backstack key associated with the deeplink that matched with the requested deeplink | ||
| * @param serializer serializer for [T] | ||
| * @param args The map of argument name to argument value. The value is expected to have already | ||
| * been parsed from the raw url string back into its proper KType as declared in [T]. | ||
| * Includes arguments for all parts of the uri - path, query, etc. | ||
| * */ | ||
| internal data class DeepLinkMatchResult<T : NavKey>(val serializer: KSerializer<T>, val args: Map<String, Any>) | ||
|
|
||
| const val TAG_LOG_ERROR = "Nav3RecipesDeepLink" |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This text also shows up in the list screen, so the test didn't actually test that the article screen was opened to the correct article.