@@ -11,6 +11,7 @@ import Glyptodon
11
11
import FlatUIKit
12
12
import Alamofire
13
13
import SwiftyJSON
14
+ import ReachabilitySwift
14
15
15
16
class HFFeedViewController : UIViewController , UITableViewDelegate , UITableViewDataSource {
16
17
@@ -32,8 +33,11 @@ class HFFeedViewController: UIViewController, UITableViewDelegate, UITableViewDa
32
33
var refreshControl : UIRefreshControl !
33
34
var alamoCalled = false ;
34
35
var didFinishFetching = false ;
36
+ var wasntAbleToConnectFirst = false
35
37
38
+ var reachability : Reachability ?
36
39
40
+ var thursdayFeedArray : [ HFScheduleItem ] = [ HFScheduleItem] ( )
37
41
var fridayFeedArray : [ HFScheduleItem ] = [ HFScheduleItem] ( )
38
42
var saturdayFeedArray : [ HFScheduleItem ] = [ HFScheduleItem] ( )
39
43
var sundayFeedArray : [ HFScheduleItem ] = [ HFScheduleItem] ( )
@@ -78,38 +82,40 @@ class HFFeedViewController: UIViewController, UITableViewDelegate, UITableViewDa
78
82
79
83
callAlamo ( apiURL)
80
84
callAlamo ( scheduleapiURL) ;
81
-
82
-
83
-
84
-
85
85
}
86
86
87
-
88
87
func checkForContent( ) {
89
88
if didFinishFetching == false {
90
89
feedTableView. alpha = 0.0
91
90
tableViewContainerView. glyptodon. show ( " Loading Feed. \n Please Wait. " )
92
- UIApplication . sharedApplication ( ) . networkActivityIndicatorVisible = true
93
91
} else {
94
92
tableViewContainerView. glyptodon. hide ( )
95
- UIApplication . sharedApplication ( ) . networkActivityIndicatorVisible = false
96
93
feedTableView. alpha = 1.0
97
94
}
98
95
}
99
96
100
97
func refresh( sender: AnyObject ) {
101
98
// Code to refresh table view
102
- UIApplication . sharedApplication ( ) . networkActivityIndicatorVisible = true
103
99
self . titles. removeAll ( )
104
100
self . contents. removeAll ( )
105
101
self . updatesDates. removeAll ( )
102
+
103
+ self . thursdayFeedArray. removeAll ( )
104
+ self . fridayFeedArray. removeAll ( )
105
+ self . saturdayFeedArray. removeAll ( )
106
+ self . sundayFeedArray. removeAll ( )
107
+ self . scheduleNames. removeAll ( )
108
+
109
+ self . feedTableView. reloadData ( )
110
+
106
111
callAlamo ( apiURL)
112
+ callAlamo ( scheduleapiURL)
113
+
107
114
if ( alamoCalled == true ) {
108
115
let delay = 0.75 * Double( NSEC_PER_SEC)
109
116
let time = dispatch_time ( DISPATCH_TIME_NOW, Int64 ( delay) )
110
117
dispatch_after ( time, dispatch_get_main_queue ( ) ) {
111
118
self . refreshControl. endRefreshing ( )
112
- UIApplication . sharedApplication ( ) . networkActivityIndicatorVisible = false
113
119
self . feedTableView. reloadData ( )
114
120
}
115
121
self . alamoCalled = false
@@ -124,16 +130,53 @@ class HFFeedViewController: UIViewController, UITableViewDelegate, UITableViewDa
124
130
override func viewDidAppear( animated: Bool ) {
125
131
//getUpdatesFromParse()
126
132
//callAlamo(apiURL)
127
- UIApplication . sharedApplication ( ) . applicationIconBadgeNumber = 0
133
+ do {
134
+ reachability = try Reachability . reachabilityForInternetConnection ( )
135
+ } catch {
136
+ print ( " Unable to create Reachability " )
137
+ return
138
+ }
139
+
140
+ NSNotificationCenter . defaultCenter ( ) . addObserver ( self , selector: " reachabilityChanged: " , name: ReachabilityChangedNotification, object: reachability)
141
+ do {
142
+ try reachability? . startNotifier ( )
143
+ } catch {
144
+ print ( " could not start reachability notifier " )
145
+ }
146
+
147
+ }
148
+
149
+ func reachabilityChanged( note: NSNotification ) {
150
+
151
+ let reachability = note. object as! Reachability
152
+
153
+ if reachability. isReachable ( ) && wasntAbleToConnectFirst == true {
154
+ checkForContent ( )
155
+ refresh ( self )
156
+ } else {
157
+ print ( " Network not reachable " )
158
+ }
128
159
}
129
160
130
161
func callAlamo( url : NSURL ) {
131
162
Alamofire . request ( . GET, url, encoding: . JSON) . validate ( ) . responseJSON ( completionHandler: {
132
-
133
163
response in
134
- self . parseResults ( JSON ( response. result. value!) , url: url)
135
- self . alamoCalled = true
136
-
164
+
165
+ switch response. result {
166
+ case . Success( let json) :
167
+ self . parseResults ( JSON ( response. result. value!) , url: url)
168
+ self . alamoCalled = true
169
+ case . Failure( let error) :
170
+ if let err = error as? NSURLError where err == . NotConnectedToInternet {
171
+ // no internet connection
172
+ self . feedTableView. alpha = 0.0
173
+ self . tableViewContainerView. glyptodon. show ( " No Internet Connection " )
174
+ } else {
175
+ // other failures
176
+ print ( " fuck " )
177
+ }
178
+ self . wasntAbleToConnectFirst = true
179
+ }
137
180
} )
138
181
}
139
182
@@ -163,7 +206,16 @@ class HFFeedViewController: UIViewController, UITableViewDelegate, UITableViewDa
163
206
164
207
let thaTime = theTIMEBIH ( startTime, formatIn: formatInput, formatOut: " yyyy-MM-dd " ) ;
165
208
166
- if thaTime. rangeOfString ( " 2017-02-17 " ) != nil {
209
+ if thaTime. rangeOfString ( " 2017-02-16 " ) != nil {
210
+ let finalStartTime = theTIMEBIH ( startTime, formatIn: formatInput, formatOut: " h:mm a " ) ;
211
+
212
+ let newScheduleItem = HFScheduleItem ( title: name,
213
+ subtitle: description,
214
+ start: finalStartTime)
215
+ self . thursdayFeedArray. append ( newScheduleItem)
216
+ }
217
+
218
+ else if thaTime. rangeOfString ( " 2017-02-17 " ) != nil {
167
219
let finalStartTime = theTIMEBIH ( startTime, formatIn: formatInput, formatOut: " h:mm a " ) ;
168
220
169
221
let newScheduleItem = HFScheduleItem ( title: name,
@@ -186,12 +238,12 @@ class HFFeedViewController: UIViewController, UITableViewDelegate, UITableViewDa
186
238
let newScheduleItem = HFScheduleItem ( title: name,
187
239
subtitle: description,
188
240
start: finalStartTime)
241
+ self . didFinishFetching = true
189
242
self . sundayFeedArray. append ( newScheduleItem)
190
243
}
191
244
}
192
245
193
246
}
194
- self . didFinishFetching = true
195
247
checkForContent ( )
196
248
self . feedTableView. reloadData ( )
197
249
}
@@ -206,7 +258,7 @@ class HFFeedViewController: UIViewController, UITableViewDelegate, UITableViewDa
206
258
case update:
207
259
return titles. count
208
260
case scheudle:
209
- return 3
261
+ return 4
210
262
default : return 0
211
263
}
212
264
}
@@ -217,12 +269,14 @@ class HFFeedViewController: UIViewController, UITableViewDelegate, UITableViewDa
217
269
return 1
218
270
} else {
219
271
if section == 0 {
220
- return fridayFeedArray . count
272
+ return thursdayFeedArray . count
221
273
} else if section == 1 {
222
- return saturdayFeedArray . count
274
+ return fridayFeedArray . count
223
275
} else if section == 2 {
276
+ return saturdayFeedArray. count
277
+ } else if section == 3 {
224
278
return sundayFeedArray. count
225
- } else {
279
+ } else {
226
280
return 0
227
281
}
228
282
}
@@ -260,13 +314,16 @@ class HFFeedViewController: UIViewController, UITableViewDelegate, UITableViewDa
260
314
let scheduleItem : HFScheduleItem !
261
315
262
316
if indexPath. section == 0 {
263
- scheduleItem = fridayFeedArray [ indexPath. row]
317
+ scheduleItem = thursdayFeedArray [ indexPath. row]
264
318
} else if indexPath. section == 1 {
265
- scheduleItem = saturdayFeedArray [ indexPath. row]
319
+ scheduleItem = fridayFeedArray [ indexPath. row]
266
320
} else if indexPath. section == 2 {
321
+ scheduleItem = saturdayFeedArray [ indexPath. row]
322
+ } else if indexPath. section == 3 {
267
323
scheduleItem = sundayFeedArray [ indexPath. row]
268
- } else {
269
- scheduleItem = fridayFeedArray [ indexPath. row]
324
+ }
325
+ else {
326
+ scheduleItem = thursdayFeedArray [ indexPath. row]
270
327
// SWITCH TO A DEFAULT ITEM
271
328
}
272
329
@@ -277,19 +334,25 @@ class HFFeedViewController: UIViewController, UITableViewDelegate, UITableViewDa
277
334
if indexPath. row == 0 {
278
335
cell. configureFlatCellWithColor ( tempCellColor, selectedColor: tempCellColor, roundingCorners: [ . TopLeft, . TopRight] )
279
336
cell. cornerRadius = 3.5
280
- } else if indexPath. section == 0 && indexPath. row == fridayFeedArray. count - 1 {
337
+ } else if indexPath. section == 0 && indexPath. row == thursdayFeedArray. count - 1 {
338
+ cell. configureFlatCellWithColor ( tempCellColor, selectedColor: tempCellColor, roundingCorners: [ . BottomLeft,
339
+ . BottomRight] )
340
+ cell. cornerRadius = 3.5
341
+ } else if indexPath. section == 1 && indexPath. row == fridayFeedArray. count - 1 {
281
342
cell. configureFlatCellWithColor ( tempCellColor, selectedColor: tempCellColor, roundingCorners: [ . BottomLeft,
282
343
. BottomRight] )
283
344
cell. cornerRadius = 3.5
284
- } else if indexPath. section == 1 && indexPath. row == saturdayFeedArray. count - 1 {
345
+ } else if indexPath. section == 2 && indexPath. row == saturdayFeedArray. count - 1 {
285
346
cell. configureFlatCellWithColor ( tempCellColor, selectedColor: tempCellColor, roundingCorners: [ . BottomLeft,
286
347
. BottomRight] )
287
348
cell. cornerRadius = 3.5
288
- } else if indexPath. section == 2 && indexPath. row == sundayFeedArray. count - 1 {
349
+ } else if indexPath. section == 3 && indexPath. row == sundayFeedArray. count - 1 {
289
350
cell. configureFlatCellWithColor ( tempCellColor, selectedColor: tempCellColor, roundingCorners: [ . BottomLeft,
290
351
. BottomRight] )
291
352
cell. cornerRadius = 3.5
292
- } else {
353
+ }
354
+
355
+ else {
293
356
cell. configureFlatCellWithColor ( tempCellColor, selectedColor: tempCellColor, roundingCorners: . AllCorners)
294
357
cell. cornerRadius = 0
295
358
}
@@ -320,10 +383,12 @@ class HFFeedViewController: UIViewController, UITableViewDelegate, UITableViewDa
320
383
cell. backgroundColor = UIColor . colorFromHex ( 0xEDECF3 ) // CLEAR
321
384
322
385
if section == 0 {
323
- cell. headerLabel. text = " Friday "
386
+ cell. headerLabel. text = " Thursday "
324
387
} else if section == 1 {
325
- cell. headerLabel. text = " Saturday "
388
+ cell. headerLabel. text = " Friday "
326
389
} else if section == 2 {
390
+ cell. headerLabel. text = " Saturday "
391
+ } else if section == 3 {
327
392
cell. headerLabel. text = " Sunday "
328
393
}
329
394
return cell
0 commit comments