Skip to content

Commit ca3e5c9

Browse files
committed
Handle /read and /reader URLs
1 parent 3040de4 commit ca3e5c9

File tree

3 files changed

+67
-47
lines changed

3 files changed

+67
-47
lines changed

WordPress/Classes/Utility/Universal Links/Route.swift

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,33 @@ import UIKit
88
/// Path: /me/account/:username
99
///
1010
protocol Route {
11-
var path: String { get }
11+
var path: RoutePath { get }
12+
var alternatePaths: [RoutePath] { get }
1213
var section: DeepLinkSection? { get }
1314
var source: DeepLinkSource { get }
1415
var action: NavigationAction { get }
1516
var shouldTrack: Bool { get }
1617
var jetpackPowered: Bool { get }
1718
}
1819

20+
extension Route {
21+
var alternatePaths: [RoutePath] {
22+
[]
23+
}
24+
25+
var allPaths: [RoutePath] {
26+
[path] + alternatePaths
27+
}
28+
}
29+
30+
typealias RoutePath = String
31+
32+
extension RoutePath {
33+
var components: [String] {
34+
return (self as NSString).pathComponents
35+
}
36+
}
37+
1938
extension Route {
2039
// Default routes to handling links rather than other source types
2140
var source: DeepLinkSource {
@@ -75,11 +94,6 @@ struct FailureNavigationAction: NavigationAction {
7594
// MARK: - Route helper methods
7695

7796
extension Route {
78-
/// Returns the path components of a route's path.
79-
var components: [String] {
80-
return (path as NSString).pathComponents
81-
}
82-
8397
func isEqual(to route: Route) -> Bool {
8498
return path == route.path
8599
}

WordPress/Classes/Utility/Universal Links/RouteMatcher.swift

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,33 +24,33 @@ class RouteMatcher {
2424
///
2525
func routesMatching(_ url: URL) -> [MatchedRoute] {
2626
let pathComponents = url.pathComponents
27-
28-
return routes.compactMap({ route -> MatchedRoute? in
29-
let values = valuesDictionary(forURL: url)
30-
31-
// If the paths are the same, we definitely have a match
32-
if route.path == url.path {
33-
return route.matched(with: values)
34-
}
35-
36-
let routeComponents = route.components
37-
38-
// Ensure the paths have the same number of components
39-
guard routeComponents.count == pathComponents.count else {
40-
return nil
41-
}
42-
43-
guard let placeholderValues = placeholderDictionary(
44-
forKeyComponents: routeComponents,
45-
valueComponents: pathComponents
46-
) else {
47-
return nil
48-
}
49-
50-
let allValues = values.merging(placeholderValues, uniquingKeysWith: { (current, _) in current })
51-
52-
return route.matched(with: allValues)
53-
})
27+
let values = valuesDictionary(forURL: url)
28+
29+
return routes.compactMap { route -> MatchedRoute? in
30+
route.allPaths.compactMap { candidate in
31+
// If the paths are the same, we definitely have a match
32+
if candidate == url.path {
33+
return route.matched(with: values)
34+
}
35+
36+
let candidateComponents = candidate.components
37+
38+
// Ensure the paths have the same number of components
39+
guard candidateComponents.count == pathComponents.count else {
40+
return nil
41+
}
42+
43+
guard let placeholderValues = placeholderDictionary(
44+
forKeyComponents: candidateComponents,
45+
valueComponents: pathComponents
46+
) else {
47+
return nil
48+
}
49+
50+
let allValues = values.merging(placeholderValues, uniquingKeysWith: { (current, _) in current })
51+
return route.matched(with: allValues)
52+
}.first
53+
}
5454
}
5555

5656
private func valuesDictionary(forURL url: URL) -> [String: String] {

WordPress/Classes/Utility/Universal Links/Routes+Reader.swift

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,35 +20,41 @@ enum ReaderRoute {
2020

2121
extension ReaderRoute: Route {
2222
var path: String {
23+
// Guaranteed to be safe force-unwrap if `alternatePaths` is exhaustive and returns at least one value
24+
// for every variant
25+
alternatePaths.first!
26+
}
27+
28+
var alternatePaths: [RoutePath] {
2329
switch self {
2430
case .root:
25-
return "/read"
31+
return ["/read", "/reader"]
2632
case .discover:
27-
return "/discover"
33+
return ["/discover"]
2834
case .search:
29-
return "/read/search"
35+
return ["/read/search", "/reader/search"]
3036
case .a8c:
31-
return "/read/a8c"
37+
return ["/read/a8c", "/reader/a8c"]
3238
case .p2:
33-
return "/read/p2"
39+
return ["/read/p2", "/reader/p2"]
3440
case .likes:
35-
return "/activities/likes"
41+
return ["/activities/likes"]
3642
case .manageFollowing:
37-
return "/following/manage"
43+
return ["/following/manage"]
3844
case .list:
39-
return "/read/list/:username/:list_name"
45+
return ["/read/list/:username/:list_name", "/reader/list/:username/:list_name"]
4046
case .tag:
41-
return "/tag/:tag_name"
47+
return ["/tag/:tag_name"]
4248
case .feed:
43-
return "/read/feeds/:feed_id"
49+
return ["/read/feeds/:feed_id", "/reader/feeds/:feed_id"]
4450
case .blog:
45-
return "/read/blogs/:blog_id"
51+
return ["/read/blogs/:blog_id", "/reader/blogs/:blog_id"]
4652
case .feedsPost:
47-
return "/read/feeds/:feed_id/posts/:post_id"
53+
return ["/read/feeds/:feed_id/posts/:post_id", "/reader/feeds/:feed_id/posts/:post_id"]
4854
case .blogsPost:
49-
return "/read/blogs/:blog_id/posts/:post_id"
55+
return ["/read/blogs/:blog_id/posts/:post_id", "/reader/blogs/:blog_id/posts/:post_id"]
5056
case .wpcomPost:
51-
return "/:post_year/:post_month/:post_day/:post_name"
57+
return ["/:post_year/:post_month/:post_day/:post_name"]
5258
}
5359
}
5460

0 commit comments

Comments
 (0)