-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
RefTag.swift
246 lines (236 loc) · 8.65 KB
/
RefTag.swift
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import Argo
import Runes
import KsApi
public enum RefTag {
case activity
case activitySample
case category
case categoryFeatured
case categoryWithSort(DiscoveryParams.Sort)
case city
case dashboard
case dashboardActivity
case discovery
case discoveryWithSort(DiscoveryParams.Sort)
case liveStream
case liveStreamCountdown
case liveStreamDiscovery
case liveStreamReplay
case messageThread
case profile
case profileBacked
case profileSaved
case projectPage
case push
case recommended
case recommendedWithSort(DiscoveryParams.Sort)
case recsWithSort(DiscoveryParams.Sort)
case search
case searchFeatured
case searchPopular
case searchPopularFeatured
case social
case socialWithSort(DiscoveryParams.Sort)
case starredWithSort(DiscoveryParams.Sort)
case thanks
case unrecognized(String)
case update
/**
Create a RefTag value from a code string. If a ref tag cannot be matched, an `.unrecognized` tag is
returned.
- parameter code: A code string.
- returns: A ref tag.
*/
// swiftlint:disable cyclomatic_complexity
public init(code: String) {
switch code {
case "activity": self = .activity
case "category": self = .category
case "category_featured": self = .categoryFeatured
case "discovery_activity_sample": self = .activitySample
case "category_ending_soon": self = .categoryWithSort(.endingSoon)
case "category_home": self = .categoryWithSort(.magic)
case "category_newest": self = .categoryWithSort(.newest)
case "category_popular": self = .categoryWithSort(.popular)
case "city": self = .city
case "dashboard": self = .dashboard
case "dashboard_activity": self = .dashboardActivity
case "discovery": self = .discovery
case "discovery_ending_soon": self = .discoveryWithSort(.endingSoon)
case "discovery_home": self = .discoveryWithSort(.magic)
case "discovery_newest": self = .discoveryWithSort(.newest)
case "discovery_popular": self = .discoveryWithSort(.popular)
case "live_stream": self = .liveStream
case "live_stream_countdown": self = .liveStreamCountdown
case "live_stream_discovery": self = .liveStreamDiscovery
case "live_stream_replay": self = .liveStreamReplay
case "message_thread": self = .messageThread
case "profile": self = .profile
case "profile_backed": self = .profileBacked
case "profile_saved": self = .profileSaved
case "project_page": self = .projectPage
case "push": self = .push
case "recommended": self = .recommended
case "recommended_ending_soon": self = .recommendedWithSort(.endingSoon)
case "recommended_home": self = .recommendedWithSort(.magic)
case "recommended_newest": self = .recommendedWithSort(.newest)
case "recommended_popular": self = .recommendedWithSort(.popular)
case "recs_ending_soon": self = .recsWithSort(.endingSoon)
case "recs_home": self = .recsWithSort(.magic)
case "recs_newest": self = .recsWithSort(.newest)
case "recs_popular": self = .recsWithSort(.popular)
case "search": self = .search
case "search_featured": self = .searchFeatured
case "search_popular": self = .searchPopular
case "search_popular_featured": self = .searchPopularFeatured
case "social": self = .social
case "social_ending_soon": self = .socialWithSort(.endingSoon)
case "social_home": self = .socialWithSort(.magic)
case "social_newest": self = .socialWithSort(.newest)
case "social_popular": self = .socialWithSort(.popular)
case "starred_ending_soon": self = .starredWithSort(.endingSoon)
case "starred_home": self = .starredWithSort(.magic)
case "starred_newest": self = .starredWithSort(.newest)
case "starred_popular": self = .starredWithSort(.popular)
case "thanks": self = .thanks
case "update": self = .update
default: self = .unrecognized(code)
}
}
// swiftlint:enable cyclomatic_complexity
/// A string representation of the ref tag that can be used in analytics tracking, cookies, etc...
public var stringTag: String {
switch self {
case .activity:
return "activity"
case .category:
return "category"
case .categoryFeatured:
return "category_featured"
case .activitySample:
return "discovery_activity_sample"
case let .categoryWithSort(sort):
return "category" + sortRefTagSuffix(sort)
case .city:
return "city"
case .dashboard:
return "dashboard"
case .dashboardActivity:
return "dashboard_activity"
case .discovery:
return "discovery"
case let .discoveryWithSort(sort):
return "discovery" + sortRefTagSuffix(sort)
case .liveStream:
return "live_stream"
case .liveStreamCountdown:
return "live_stream_countdown"
case .liveStreamDiscovery:
return "live_stream_discovery"
case .liveStreamReplay:
return "live_stream_replay"
case .messageThread:
return "message_thread"
case .profile:
return "profile"
case .profileBacked:
return "profile_backed"
case .profileSaved:
return "profile_saved"
case .projectPage:
return "project_page"
case .push:
return "push"
case .recommended:
return "recommended"
case let .recommendedWithSort(sort):
return "recommended" + sortRefTagSuffix(sort)
case let .recsWithSort(sort):
return "recs" + sortRefTagSuffix(sort)
case .search:
return "search"
case .searchFeatured:
return "search_featured"
case .searchPopular:
return "search_popular"
case .searchPopularFeatured:
return "search_popular_featured"
case .social:
return "social"
case let .socialWithSort(sort):
return "social" + sortRefTagSuffix(sort)
case let .starredWithSort(sort):
return "starred" + sortRefTagSuffix(sort)
case .thanks:
return "thanks"
case let .unrecognized(code):
return code
case .update:
return "update"
}
}
}
extension RefTag: Equatable {
}
public func == (lhs: RefTag, rhs: RefTag) -> Bool {
switch (lhs, rhs) {
case (.activity, .activity), (.category, .category), (.categoryFeatured, .categoryFeatured),
(.activitySample, .activitySample), (.city, .city), (.dashboard, .dashboard),
(.dashboardActivity, .dashboardActivity), (.discovery, .discovery),
(.liveStreamCountdown, .liveStreamCountdown), (.liveStreamDiscovery, .liveStreamDiscovery),
(.liveStreamReplay, .liveStreamReplay), (.messageThread, .messageThread), (.profile, .profile),
(.profileBacked, .profileBacked), (.profileSaved, .profileSaved), (.projectPage, .projectPage),
(.push, .push), (.recommended, .recommended), (.search, .search), (.searchFeatured, .searchFeatured),
(.searchPopular, .searchPopular), (.searchPopularFeatured, .searchPopularFeatured), (.social, .social),
(.thanks, .thanks), (.update, .update):
return true
case let (.categoryWithSort(lhs), .categoryWithSort(rhs)):
return lhs == rhs
case let (.discoveryWithSort(lhs), .discoveryWithSort(rhs)):
return lhs == rhs
case let (.recommendedWithSort(lhs), .recommendedWithSort(rhs)):
return lhs == rhs
case let (.recsWithSort(lhs), .recsWithSort(rhs)):
return lhs == rhs
case let (.socialWithSort(lhs), .socialWithSort(rhs)):
return lhs == rhs
case let (.starredWithSort(lhs), .starredWithSort(rhs)):
return lhs == rhs
case let (.unrecognized(lhs), .unrecognized(rhs)):
return lhs == rhs
default:
return false
}
}
extension RefTag: CustomStringConvertible {
public var description: String {
return self.stringTag
}
}
extension RefTag: Hashable {
public var hashValue: Int {
return self.stringTag.hashValue
}
}
private func sortRefTagSuffix(_ sort: DiscoveryParams.Sort) -> String {
switch sort {
case .endingSoon:
return "_ending_soon"
case .magic:
return "_home"
case .newest:
return "_newest"
case .popular:
return "_popular"
}
}
extension RefTag: Argo.Decodable {
public static func decode(_ json: JSON) -> Decoded<RefTag> {
switch json {
case let .string(code):
return .success(RefTag(code: code))
default:
return .failure(.custom("RefTag code must be a string."))
}
}
}