-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hoot.swift
296 lines (238 loc) · 8.07 KB
/
Hoot.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
//
// Hoot.swift
// airhoot
//
// Created by Craig Cronin on 6/2/17.
// Copyright © 2017 130R Studios. All rights reserved.
//
import Foundation
import SwiftDate
import SwiftyJSON
import PromiseKit
public class Hoot:NSObject {
enum HootType {
case text, photo, photoText, giphy
}
private var jsonHootObject:JSON
//TODO: File task to move this logic to API
var type: HootType {
if (self.isGiphy) {
return .giphy
}
if (self.photo != nil) {
if (self.text != "") {
return .photoText
}
return .photo
}
return .text
}
//TODO: Switch to guid once new API is live
var guid: String {
return self.jsonHootObject["hoot_id"].stringValue
//return self.jsonHootObject["guid"].stringValue
}
var key_hash: String {
return self.jsonHootObject["key_hash"].stringValue
}
var key_id: String {
return self.jsonHootObject["key_id"].stringValue
}
var text: String? {
return self.jsonHootObject["text"].stringValue
}
var photo: URL? {
if let photoString = self.jsonHootObject["photo"].string {
return URL(string: photoString, relativeTo: AHApiClient.photoUrl)
} else {
return nil
}
}
var giphy: GiphyObject? {
let giphyJson = self.jsonHootObject["giphy"]
if giphyJson.exists() {
return GiphyObject(withServerJSON: giphyJson)
}
return nil
}
var num_ups:String {
return self.jsonHootObject["num_ups"].stringValue
}
var isGiphy:Bool {
return self.jsonHootObject["is_giphy"].boolValue
}
var isLiked:Bool {
return self.jsonHootObject["is_liked"].boolValue
}
var num_downs:String {
return self.jsonHootObject["num_downs"].stringValue
}
var isDisliked:Bool {
return self.jsonHootObject["is_disliked"].boolValue
}
var num_favorites:String {
return self.jsonHootObject["num_favorites"].stringValue
}
var isFavorite:Bool {
return self.jsonHootObject["is_favorite"].boolValue
}
var num_replies:String {
return self.jsonHootObject["num_replies"].stringValue
}
var isReplied:Bool {
return self.jsonHootObject["is_replied"].boolValue
}
var creation: Date {
let seconds = self.jsonHootObject["creation"].intValue
return Date(timeIntervalSince1970: TimeInterval(seconds))
}
var coordinates:HootCoordinates? {
let coordinateJson = self.jsonHootObject["coordinates"]
if coordinateJson.exists() {
return HootCoordinates(withJson: coordinateJson)
}
return nil
}
var replies:[HootReply] {
guard let replyJsonArray = self.jsonHootObject["replies"].array else { return [HootReply]() }
var array = [HootReply]()
for replyJson in replyJsonArray {
if let reply = HootReply(withJson: replyJson) {
array.append(reply)
}
}
return array
}
var redoodles:[ReDoodle] {
var array = [ReDoodle]()
for x in 0..<self.replies.count {
let reply = self.replies[x]
if reply.is_redoodle {
array.append(ReDoodle(reply: reply, index: x))
}
}
return array
}
var timeLocationLabelText:String {
guard let coordinates = self.coordinates else { return "" }
let date = DateInRegion(absoluteDate: self.creation)
let (colloquial, _) = try! date.colloquialSinceNow()
return colloquial + " in \(coordinates.locality), \(coordinates.admin)"
}
init?(withJson json:JSON) {
guard let _ = json["hoot_id"].string else {
return nil
}
self.jsonHootObject = json
super.init()
}
func update(withJson jsonObject:JSON) {
self.jsonHootObject = jsonObject
}
func fetchDetails() -> Promise<Hoot> {
return AHApiClient.shared.fetchHoot(withId: self.guid).then { apiResponse -> Hoot in
self.update(withJson: apiResponse)
return self
}
}
func like() -> Promise<Hoot> {
return AHApiClient.shared.like(hootWithId: self.guid).then { (_) -> Promise<Hoot> in
return self.fetchDetails()
}
}
func dislike() -> Promise<Hoot> {
return AHApiClient.shared.dislike(hootWithId: self.guid).then { (_) -> Promise<Hoot> in
return self.fetchDetails()
}
}
func favorite() -> Promise<Hoot> {
return AHApiClient.shared.favorite(hootWithId: self.guid).then { (_) -> Promise<Hoot> in
return self.fetchDetails()
}
}
func reply(withText text:String, location:HootLocation?) -> Promise<Hoot> {
return AHApiClient.shared.reply(toHootWithId: self.guid, text: text, location: location).then { apiResponse -> Promise<Hoot> in
return self.fetchDetails()
}
}
class func post(fromDictionary dict:[String:AnyObject]) -> Promise<Hoot> {
return AHApiClient.shared.post(withDictionary: dict).then { jsonResponse -> Hoot in
guard let hoot = Hoot(withJson: jsonResponse) else {
throw NSError(domain: "post", code: 0, userInfo: [NSLocalizedDescriptionKey:"Hoot cannot be created with provided data"])
}
return hoot
}
}
class func hootArray(fromJSON json:JSON) -> [Hoot] {
guard let hootJSONArray = json["hoots"].array else { return [Hoot]() }
var hootArray = [Hoot]()
for hootJson in hootJSONArray {
if let hoot = Hoot(withJson: hootJson) {
hootArray.append(hoot)
}
}
return hootArray
}
}
extension AHApiClient {
func like(hootWithId guid: String) -> Promise<Void> {
var params = self.fetchStandardParams()
params?["hoot"] = guid as AnyObject
return self.request(withMethod: .post, forPath: "hoot/like", params: params).then { _ -> Void in }
}
func dislike(hootWithId guid: String) -> Promise<Void> {
var params = self.fetchStandardParams()
params?["hoot"] = guid as AnyObject
return self.request(withMethod: .post, forPath: "hoot/dislike", params: params).then { response -> Void in }
}
func favorite(hootWithId guid: String) -> Promise<Void> {
var params = self.fetchStandardParams()
params?["hoot"] = guid as AnyObject
return self.request(withMethod: .post, forPath: "hoot/favorite", params: params).then { response -> Void in }
}
func reply(toHootWithId guid:String, text:String, location:HootLocation?) -> Promise<Void> {
var params = self.fetchStandardParams()
params?["hoot"] = guid as AnyObject
params?["text"] = text as AnyObject
if let location = location {
params?.update(other: location.toDict())
} else {
params?.update(other: HootLocation.unknownLocationDict())
}
return self.request(withMethod: .post, forPath: "reply/create", params: params).then { response -> Void in }
}
func fetchHoot(withId guid:String) -> Promise<JSON> {
var params = self.fetchStandardParams()
params?["hoot"] = guid as AnyObject
params?["version"] = "2.00" as AnyObject
params?["more"] = "1" as AnyObject
return self.request(withMethod: .get, forPath: "hoot/single", params: params).then { response -> JSON in
guard let hootJson = response["hoots"].arrayValue.first else {
throw NSError(domain: "hoot/single", code: 0, userInfo: [NSLocalizedDescriptionKey:"Empty response array"])
}
return hootJson
}
}
func post(withDictionary dict:[String:AnyObject]) -> Promise<JSON> {
let path = "hoot/create"
var params = self.fetchStandardParams()
for (key, value) in dict {
params?[key] = value
}
if let image = dict["image"] as? UIImage {
return self.uploadRequest(withImage: image, withPath: path, params: params).then { response -> JSON in
guard let hootJson = response["hoots"].arrayValue.first else {
throw NSError(domain: "hoot/create", code: 0, userInfo: [NSLocalizedDescriptionKey:"Empty response array"])
}
return hootJson
}
} else {
return self.request(withMethod: .post, forPath: path, params: params).then { response -> JSON in
guard let hootJson = response["hoots"].arrayValue.first else {
throw NSError(domain: "hoot/create", code: 0, userInfo: [NSLocalizedDescriptionKey:"Empty response array"])
}
return hootJson
}
}
}
}