-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
DiscoveryParams.swift
163 lines (146 loc) · 5.59 KB
/
DiscoveryParams.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
import Prelude
public struct DiscoveryParams {
public var backed: Bool?
public var category: Category?
public var collaborated: Bool?
public var created: Bool?
public var hasVideo: Bool?
public var includePOTD: Bool?
public var page: Int?
public var perPage: Int?
public var query: String?
public var recommended: Bool?
public var seed: Int?
public var similarTo: Project?
public var social: Bool?
public var sort: Sort?
public var staffPicks: Bool?
public var starred: Bool?
public var state: State?
public enum State: String, Decodable {
case all
case live
case successful
}
public enum Sort: String, Decodable {
case endingSoon = "end_date"
case magic
case newest
case popular = "popularity"
public var trackingString: String {
switch self {
case .endingSoon: return "ending_soon"
case .magic: return "magic"
case .newest: return "newest"
case .popular: return "popular"
}
}
}
public static let defaults = DiscoveryParams(
backed: nil, category: nil, collaborated: nil,
created: nil, hasVideo: nil, includePOTD: nil,
page: nil, perPage: nil, query: nil, recommended: nil,
seed: nil, similarTo: nil, social: nil, sort: nil,
staffPicks: nil, starred: nil, state: nil
)
public static let recommendedDefaults = DiscoveryParams.defaults
|> DiscoveryParams.lens.includePOTD .~ true
|> DiscoveryParams.lens.backed .~ false
|> DiscoveryParams.lens.recommended .~ true
public var queryParams: [String: String] {
var params: [String: String] = [:]
params["backed"] = self.backed == true ? "1" : self.backed == false ? "-1" : nil
params["category_id"] = self.category?.intID?.description
params["collaborated"] = self.collaborated?.description
params["created"] = self.created?.description
params["has_video"] = self.hasVideo?.description
params["page"] = self.page?.description
params["per_page"] = self.perPage?.description
params["recommended"] = self.recommended?.description
params["seed"] = self.seed?.description
params["similar_to"] = self.similarTo?.id.description
params["social"] = self.social == true ? "1" : self.social == false ? "-1" : nil
params["sort"] = self.sort?.rawValue
params["staff_picks"] = self.staffPicks?.description
params["starred"] = self.starred == true ? "1" : self.starred == false ? "-1" : nil
params["state"] = self.state?.rawValue
params["term"] = self.query
return params
}
}
extension DiscoveryParams: Equatable {}
public func == (a: DiscoveryParams, b: DiscoveryParams) -> Bool {
return a.queryParams == b.queryParams
}
extension DiscoveryParams: Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(self.description)
}
}
extension DiscoveryParams: CustomStringConvertible, CustomDebugStringConvertible {
public var description: String {
return self.queryParams.description
}
public var debugDescription: String {
return self.queryParams.debugDescription
}
}
extension DiscoveryParams: Decodable {
public init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
self.backed = try stringIntToBool(values.decodeIfPresent(String.self, forKey: .backed))
self.category = try values.decodeIfPresent(Category.self, forKey: .category)
self.collaborated = try stringToBool(values.decodeIfPresent(String.self, forKey: .collaborated))
self.created = try stringToBool(values.decodeIfPresent(String.self, forKey: .created))
self.hasVideo = try stringToBool(values.decodeIfPresent(String.self, forKey: .hasVideo))
self.includePOTD = try stringToBool(values.decodeIfPresent(String.self, forKey: .includePOTD))
self.page = try values.decodeIfPresent(String.self, forKey: .page).flatMap { Int($0) }
self.perPage = try values.decodeIfPresent(String.self, forKey: .perPage).flatMap { Int($0) }
self.query = try values.decodeIfPresent(String.self, forKey: .query)
self.recommended = try stringToBool(values.decodeIfPresent(String.self, forKey: .recommended))
self.seed = try values.decodeIfPresent(String.self, forKey: .seed).flatMap { Int($0) }
self.similarTo = try values.decodeIfPresent(Project.self, forKey: .similarTo)
self.social = try stringIntToBool(values.decodeIfPresent(String.self, forKey: .social))
self.sort = try values.decodeIfPresent(Sort.self, forKey: .sort)
self.staffPicks = try stringToBool(values.decodeIfPresent(String.self, forKey: .staffPicks))
self.starred = try stringIntToBool(values.decodeIfPresent(String.self, forKey: .starred))
self.state = try values.decodeIfPresent(State.self, forKey: .state)
}
enum CodingKeys: String, CodingKey {
case backed
case category
case collaborated
case created
case hasVideo = "has_video"
case includePOTD = "include_potd"
case page
case perPage = "per_page"
case query = "term"
case recommended
case seed
case similarTo = "similar_to"
case social
case sort
case staffPicks = "staff_picks"
case starred
case state
}
}
private func stringToBool(_ string: String?) -> Bool? {
guard let string = string else { return nil }
switch string {
// taken from server's `value_to_boolean` function
case "true", "1", "t", "T", "TRUE", "on", "ON":
return true
case "false", "0", "f", "F", "FALSE", "off", "OFF":
return false
default:
return nil
}
}
private func stringIntToBool(_ string: String?) -> Bool? {
guard let string = string else { return nil }
return Int(string)
.filter { $0 <= 1 && $0 >= -1 }
.flatMap { ($0 == 0) ? nil : ($0 == 1) }
}