This repository has been archived by the owner on Nov 21, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewController.swift
391 lines (323 loc) · 15.5 KB
/
ViewController.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
//
// ViewController.swift
// VineTracker
//
// Created by Connor Wybranowski on 9/3/15.
// Copyright (c) 2015 Wybro. All rights reserved.
//
import UIKit
import JBChartView
class ViewController: UIViewController, JBLineChartViewDelegate, JBLineChartViewDataSource, UITextFieldDelegate {
let followerRecords = [7000, 7200, 7100, 7036, 7250, 7312, 6900, 7400, 7233, 7166]
// var dataPoints = NSMutableArray()
var dataPoints: [Int] = [Int]()
var cachedUser: [String:AnyObject] = [String:AnyObject]()
@IBOutlet var followerLineChartView: JBLineChartView!
@IBOutlet var avatarPicImageView: UIImageView!
@IBOutlet var followerCountLabel: UILabel!
@IBOutlet var newFollowersLabel: UILabel!
@IBOutlet var userSearchField: UITextField!
@IBOutlet var userSearchRightConstraint: NSLayoutConstraint!
@IBOutlet var refreshButton: UIButton!
@IBOutlet var toggleInfoButton: UIButton!
@IBOutlet var infoViewHeightConstraint: NSLayoutConstraint!
@IBOutlet var infoView: UIView!
@IBOutlet var newFollowersFromPreviousDayLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
userSearchField.delegate = self
followerLineChartView.dataSource = self
followerLineChartView.delegate = self
followerLineChartView.backgroundColor = UIColor.clearColor()
followerLineChartView.showsLineSelection = false
followerLineChartView.showsVerticalSelection = false
followerLineChartView.reloadData()
println("Launched")
self.avatarPicImageView.layer.cornerRadius = self.avatarPicImageView.frame.size.width / 2
self.avatarPicImageView.clipsToBounds = true
infoViewHeightConstraint.constant = 0
checkUserSearchSettings()
fetchNewData(self.getUserSearchSettings())
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
userSearchField.resignFirstResponder()
hideUserSearch()
}
override func viewDidLayoutSubviews() {
// println("viewDidLayout")
// println(self.cachedUser)
if let actualCachedUserFollowerData = cachedUser["newFollowersData"] as? [String: AnyObject] {
if let cachedFollowersFromPreviousDay = actualCachedUserFollowerData["newFollowersFromPreviousDate"] as? Int {
println("cached user found - updating view")
updateInfoViewLabel(cachedFollowersFromPreviousDay)
}
}
else {
println("No cached user")
}
// if let cachedFollowersFromPreviousDay = cachedUser["newFollowersData"]!["newFollowersFromPreviousDate"] as? Int {
// updateInfoViewLabel(cachedFollowersFromPreviousDay)
// }
}
func numberOfLinesInLineChartView(lineChartView: JBLineChartView!) -> UInt {
return 1
}
func lineChartView(lineChartView: JBLineChartView!, numberOfVerticalValuesAtLineIndex lineIndex: UInt) -> UInt {
return UInt(dataPoints.count)
}
func lineChartView(lineChartView: JBLineChartView!, verticalValueForHorizontalIndex horizontalIndex: UInt, atLineIndex lineIndex: UInt) -> CGFloat {
return CGFloat(dataPoints[Int(horizontalIndex)] as NSNumber)
}
// func lineChartView(lineChartView: JBLineChartView!, smoothLineAtLineIndex lineIndex: UInt) -> Bool {
// return true
// }
func lineChartView(lineChartView: JBLineChartView!, verticalSelectionColorForLineAtLineIndex lineIndex: UInt) -> UIColor! {
return UIColor.redColor()
}
func lineChartView(lineChartView: JBLineChartView!, colorForLineAtLineIndex lineIndex: UInt) -> UIColor! {
return UIColor.whiteColor()
}
func lineChartView(lineChartView: JBLineChartView!, widthForLineAtLineIndex lineIndex: UInt) -> CGFloat {
return 3
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
fetchNewData(textField.text)
return true
}
func textFieldDidBeginEditing(textField: UITextField) {
refreshButton.enabled = false
}
func textFieldDidEndEditing(textField: UITextField) {
refreshButton.enabled = true
}
@IBAction func refresh(sender: AnyObject) {
println("Refreshing")
refreshAnimation()
fetchNewData(self.getUserSearchSettings())
}
func updateUserSearchSettings(searchString: String) {
let sharedDefaults = NSUserDefaults(suiteName: "group.com.Wybro.VineTracker")
sharedDefaults?.setObject(searchString, forKey: "userSearchString")
sharedDefaults?.synchronize()
checkUserSearchSettings()
}
func getUserSearchSettings() -> String? {
let sharedDefaults = NSUserDefaults(suiteName: "group.com.Wybro.VineTracker")
var returnString = sharedDefaults?.objectForKey("userSearchString") as? String
println(returnString)
return returnString
}
func checkUserSearchSettings() {
if let searchSetting = getUserSearchSettings() as String! {
refreshButton.enabled = true
}
else {
refreshButton.enabled = false
}
}
func fetchNewData(searchString: String?) {
println("Fetching data")
// VineConnection.getUserDataForID(bcUserId, completionHandler: { (vineUser:VineUser) -> () in
if (searchString != nil) {
if !searchString!.isEmpty {
self.updateUserSearchSettings(searchString!)
VineConnection.getUserDataForName(searchString!, completionHandler: { (vineUser:VineUser) -> () in
// Update settings
// self.updateUserSearchSettings(searchString!)
println(vineUser.username)
println(vineUser.followerCount)
// println(vineUser.loopCount)
let sharedDefaults = NSUserDefaults(suiteName: "group.com.Wybro.VineTracker")
var newFollowers = 0
var newFollowersFromPreviousDate = 0
if let foundUser = sharedDefaults?.objectForKey("\(vineUser.userId)") as? [String:AnyObject] {
// println("User found!")
// println(foundUser)
var newDataPoints = foundUser["dataPoints"] as! [Int]
newDataPoints.append(vineUser.followerCount)
if newDataPoints.endIndex >= 20 {
newDataPoints.removeAtIndex(0)
}
self.updateGraph(newDataPoints)
let calendar = NSCalendar.currentCalendar()
var startingFollowers = foundUser["newFollowersData"]!["startingFollowers"] as! Int
newFollowersFromPreviousDate = foundUser["newFollowersData"]!["newFollowersFromPreviousDate"] as! Int // CHANGED
newFollowers = vineUser.followerCount - startingFollowers
if let savedDate = foundUser["newFollowersData"]!["date"] as? NSDate {
// Saved date is not in today - change startingFollowers
if !calendar.isDateInToday(savedDate) {
startingFollowers = vineUser.followerCount
newFollowersFromPreviousDate = newFollowers
}
}
// self.updateInfoViewLabel(newFollowersFromPreviousDate)
var now = NSDate()
var user = ["username": vineUser.username, "userId": vineUser.userId, "followerCount": vineUser.followerCount, "loopCount": vineUser.loopCount, "dataPoints":newDataPoints, "newFollowersData": ["date": now, "startingFollowers": startingFollowers, "newFollowersFromPreviousDate": newFollowersFromPreviousDate]]
// println(user)
sharedDefaults?.setObject(user, forKey: "\(vineUser.userId)")
sharedDefaults?.synchronize()
// Cache user to update more info label
if let userToSave = user as? [String: AnyObject] {
println("caching user")
self.cachedUser = userToSave
// println(self.cachedUser)
}
println("Saved user")
// println(user)
}
else {
println("User not found -- creating new record")
var now = NSDate()
var user = ["username": vineUser.username, "userId": vineUser.userId, "followerCount": vineUser.followerCount, "loopCount": vineUser.loopCount, "dataPoints":[vineUser.followerCount], "newFollowersData": ["date": now, "startingFollowers": vineUser.followerCount, "newFollowersFromPreviousDate": newFollowers]]
sharedDefaults?.setObject(user, forKey: "\(vineUser.userId)")
sharedDefaults?.synchronize()
// Cache user to update more info label
if let userToSave = user as? [String: AnyObject] {
println("caching user")
self.cachedUser = userToSave
// println(self.cachedUser)
}
println("New user")
// println(user)
}
// Use separate UI update function here
// self.updateInfoViewLabel(newFollowersFromPreviousDate)
self.updateLabels(vineUser.followerCount, newFollowers: newFollowers)
self.updateAvatarPic(vineUser.avatarPic)
})
}
}
}
func updateLabels(followers: Int, newFollowers: Int) {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
var followersFormatted = NSNumberFormatter.localizedStringFromNumber(followers, numberStyle: NSNumberFormatterStyle.DecimalStyle)
var newFollowersFormatted = NSNumberFormatter.localizedStringFromNumber(newFollowers, numberStyle: NSNumberFormatterStyle.DecimalStyle)
self.followerCountLabel.text = followersFormatted
if newFollowers >= 0 {
self.newFollowersLabel.text = "+\(newFollowersFormatted)"
}
else if newFollowers < 0 {
self.newFollowersLabel.text = "\(newFollowersFormatted)"
}
})
}
func updateInfoViewLabel(newFollowers: Int) {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
var newFollowersFormatted = NSNumberFormatter.localizedStringFromNumber(newFollowers, numberStyle: NSNumberFormatterStyle.DecimalStyle)
if newFollowers >= 0 {
self.newFollowersFromPreviousDayLabel.text = "+\(newFollowersFormatted)"
}
else if newFollowers < 0 {
self.newFollowersFromPreviousDayLabel.text = "\(newFollowersFormatted)"
}
})
}
func updateAvatarPic(image: UIImage) {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.avatarPicImageView.image = image
})
}
func updateGraph(dataPointsArr: [Int]) {
dataPoints.removeAll(keepCapacity: false)
println("Updating graph")
println(dataPointsArr)
for entry in dataPointsArr {
dataPoints.append(Int(entry as NSNumber))
}
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.followerLineChartView.reloadData()
})
}
@IBAction func settingsAction(sender: UIButton) {
if sender.tag == 0 {
showUserSearch()
userSearchField.becomeFirstResponder()
sender.tag = 1
}
else {
hideUserSearch()
sender.tag = 0
}
}
func showUserSearch() {
userSearchField.enabled = true
userSearchField.alpha = 0
userSearchField.hidden = false
userSearchRightConstraint.constant = 439
self.view.layoutIfNeeded()
userSearchRightConstraint.constant = 8
UIView.animateWithDuration(0.4, delay: 0, options: .CurveEaseOut, animations: { () -> Void in
self.view.layoutIfNeeded()
self.userSearchField.alpha = 1
}) { (completed) -> Void in
//
}
}
func hideUserSearch() {
userSearchField.enabled = false
userSearchRightConstraint.constant = 439
UIView.animateWithDuration(0.4, delay: 0, options: .CurveEaseOut, animations: { () -> Void in
self.view.layoutIfNeeded()
self.userSearchField.alpha = 0
}) { (completed) -> Void in
self.userSearchField.hidden = true
}
}
func refreshAnimation() {
UIView.animateWithDuration(0.3, animations: { () -> Void in
let transform = CGAffineTransformMakeRotation(CGFloat(180.0 * M_PI/180.0))
self.refreshButton.transform = transform
}) { (completed) -> Void in
self.closingRefreshAnimation()
}
}
func closingRefreshAnimation() {
UIView.animateWithDuration(0.3, animations: { () -> Void in
let transform = CGAffineTransformMakeRotation(0)
self.refreshButton.transform = transform
}) { (completed) -> Void in
//
}
}
@IBAction func toggleInfo(sender: UIButton) {
// Hidden - show view
if sender.tag == 0 {
showMoreInfo()
sender.tag = 1
}
// Visible - hide view
else {
hideMoreInfo()
sender.tag = 0
}
}
func showMoreInfo() {
infoView.alpha = 0
infoView.hidden = false
infoViewHeightConstraint.constant = 80
UIView.animateWithDuration(0.3, delay: 0, options: .CurveEaseOut, animations: { () -> Void in
self.view.layoutIfNeeded()
self.infoView.alpha = 1
let transform = CGAffineTransformMakeRotation(CGFloat(180.0 * M_PI/180.0))
self.toggleInfoButton.transform = transform
}) { (completed) -> Void in
//
}
}
func hideMoreInfo() {
infoViewHeightConstraint.constant = 60
UIView.animateWithDuration(0.3, delay: 0, options: .CurveEaseOut, animations: { () -> Void in
self.view.layoutIfNeeded()
self.infoView.alpha = 0
let transform = CGAffineTransformMakeRotation(0)
self.toggleInfoButton.transform = transform
}) { (completed) -> Void in
self.infoView.hidden = true
}
}
}