-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathNotificationContentRouter.swift
More file actions
125 lines (114 loc) · 5.11 KB
/
Copy pathNotificationContentRouter.swift
File metadata and controls
125 lines (114 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import UIKit
import FormattableContentKit
import WordPressData
struct NotificationContentRouter {
private let coordinator: ContentCoordinator
private let notification: WordPressData.Notification
private let expirationFiveMinutes = TimeInterval(60 * 5)
init(activity: WordPressData.Notification, coordinator: ContentCoordinator) {
self.coordinator = coordinator
self.notification = activity
}
func routeTo(_ url: URL) {
guard let range = getRange(with: url) else {
return
}
do {
try displayContent(of: range, with: url)
} catch {
coordinator.displayWebViewWithURL(url, source: "notifications")
}
}
/// Route to the controller that best represents the notification source.
///
/// - Throws: throws if the notification doesn't have a resource URL
///
func routeToNotificationSource() throws {
guard let fallbackURL = notification.resourceURL else {
throw DefaultContentCoordinator.DisplayError.missingParameter
}
do {
try displayNotificationSource()
} catch {
coordinator.displayWebViewWithURL(fallbackURL, source: "notifications")
}
}
func routeTo(_ image: UIImage) {
coordinator.displayFullscreenImage(image)
}
private func displayNotificationSource() throws {
switch notification.kind {
case .follow:
try coordinator.displayStreamWithSiteID(notification.metaSiteID)
case .like:
fallthrough
case .matcher:
fallthrough
case .newPost:
fallthrough
case .post:
try coordinator.displayReaderWithPostId(notification.metaPostID, siteID: notification.metaSiteID)
case .comment:
// Focus on the primary comment, and default to the reply ID if its set
let commentID = notification.metaCommentID ?? notification.metaReplyID
try coordinator.displayCommentsWithPostId(notification.metaPostID,
siteID: notification.metaSiteID,
commentID: commentID,
source: .commentNotification)
case .commentLike:
// Focus on the primary comment, and default to the reply ID if its set
let commentID = notification.metaCommentID ?? notification.metaReplyID
try coordinator.displayCommentsWithPostId(notification.metaPostID,
siteID: notification.metaSiteID,
commentID: commentID,
source: .commentLikeNotification)
default:
throw DefaultContentCoordinator.DisplayError.unsupportedType
}
}
private func displayContent(of range: FormattableContentRange, with url: URL) throws {
guard let range = range as? NotificationContentRange else {
throw DefaultContentCoordinator.DisplayError.missingParameter
}
switch range.kind {
case .site:
try coordinator.displayStreamWithSiteID(range.siteID)
case .post:
try coordinator.displayReaderWithPostId(range.postID, siteID: range.siteID)
case .comment:
// Focus on the comment reply if it's set over the primary comment ID
let commentID = notification.metaReplyID ?? notification.metaCommentID
try coordinator.displayCommentsWithPostId(range.postID,
siteID: range.siteID,
commentID: commentID,
source: .commentNotification)
case .stats:
/// Backup notifications are configured as "stat" notifications
/// For now this is just a workaround to fix the routing
if url.absoluteString.matches(regex: "\\/backup\\/").count > 0 {
try coordinator.displayBackupWithSiteID(range.siteID)
} else {
trackStatsRoute()
try coordinator.displayStatsWithSiteID(range.siteID, url: url)
}
case .follow:
try coordinator.displayFollowersWithSiteID(range.siteID, expirationTime: expirationFiveMinutes)
case .user:
try coordinator.displayStreamWithSiteID(range.siteID)
case .scan:
try coordinator.displayScanWithSiteID(range.siteID)
default:
throw DefaultContentCoordinator.DisplayError.unsupportedType
}
}
private func getRange(with url: URL) -> FormattableContentRange? {
return notification.contentRange(with: url)
}
private func trackStatsRoute() {
var properties: [AnyHashable: Any] = [WPAppAnalyticsKeyTapSource: "notification"]
if FeatureFlag.newStats.enabled {
properties[WPAnalyticsEvent.isNewStatsKey] = "1"
}
WPAppAnalytics.track(.statsAccessed, withProperties: properties)
}
}