-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTableTableViewController.swift
653 lines (549 loc) · 26.7 KB
/
TableTableViewController.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
//
// TableTableViewController.swift
// RutgersBusApp
//
// Created by Kalu on 1/1/16.
// Copyright © 2016 Niral. All rights reserved.
//
import UIKit
import CoreLocation
//[{ var direction: "To Busch Student Center", var title: "A",var predictions: [ '7', '20', '31', '43', '55' ] }]
var dataResults = [[String: AnyObject]]();
var timeResults = [[String: AnyObject]]();
var nearbyResults = NSDictionary ();
//var stopSearchResults:Array<BusStopInformation>?
class TableTableViewController: UITableViewController,CLLocationManagerDelegate, UISearchResultsUpdating {
// main data structures
//busStopRoutes is simply a list of Active Bus Stops
var busStopRoutes = [String] ()
// nearbyBusStops is simply a list of Nearby Bus Stops (to a certain accuracy)
var nearbyBusStops = [String] ()
//busStopInfo simply contains all relevant info (predictions, routes) corresponding to the busStop
var busStopInfo = [BusStopInformation]()
//data structures required for search bar (which will filter the tableview)
var filteredBusStopInfo = [String] ()
var resultSearchController = UISearchController! ()
//used to check if there is no available data
var noData = false;
var noNearbyStops = false;
// used to check current location:
let locationManager = CLLocationManager();
var lat = Double();
var long = Double();
override func viewDidLoad(){
super.viewDidLoad()
//navigationController!.navigationBar.barTintColor = UIColor(red: 206/255, green: 17/255, blue: 38/255, alpha: 1)
//init pull to refresh controller
self.refreshControl?.addTarget(self, action: "refresh:", forControlEvents: UIControlEvents.ValueChanged)
//call to retrieve data:
jsonBusRoutes()
//Location Services:
//init location manager
self.locationManager.requestAlwaysAuthorization()
// For use in foreground
self.locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
} else{
print("Location Services Not Enabled");
}
// Stuff for Search Bar:
self.resultSearchController = UISearchController(searchResultsController: nil)
self.resultSearchController.searchResultsUpdater = self
self.resultSearchController.dimsBackgroundDuringPresentation = false
self.resultSearchController.searchBar.sizeToFit()
self.resultSearchController.searchBar.placeholder = "Search Stops"
self.tableView.tableHeaderView = self.resultSearchController.searchBar
self.resultSearchController.searchBar.tintColor = UIColor(red: 206/255, green: 17/255, blue: 38/255, alpha: 1)
self.tableView.reloadData()
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){
// method to get current location if available
var locValue:CLLocationCoordinate2D = manager.location!.coordinate
print("locations = Lat:\(locValue.latitude) Long:\(locValue.longitude)")
self.lat = locValue.latitude
self.long = locValue.longitude
//once current lat and long are available, find nearest available bus stops
nearbyBusStops.removeAll()
jsonNearestStop()
}
func parseBusRoutes(let stops: [[String: AnyObject]]){
print("entered parseBusRoutes")
for stop in stops{
if let name = stop["title"] as? String {
//print(name);
self.busStopRoutes.append(name);
jsonStopPredictions(name);
}
}
//print(busStopRoutes.count);
}
func parseBusPredictions(let infos: [[String: AnyObject]], let stopTag:String) {
var routeInfo = [BusInfo]()
print("starting to add to array predictions");
//print(infos)
// print("Bus Stop Info: \(stopTag)")
//print("***************");
for data in infos{
// print("Info Data:")
// print(data)
//print("")
if var name = data["title"] as? String where !name.isEmpty {
//print("title:\(name)");
// print("")
var predictionTimes = [String] ()
if let dir = data["direction"] as? String where !dir.isEmpty {
name += " " + "(" + dir + ")";
// print("new name \(name)");
}
if !(data["predictions"] is NSNull) {
//print("predictions not null")
let preds = data["predictions"];
do {
let predictions = try preds as? [[String:AnyObject]]
//print(predictions)
for time in predictions! {
var mins = time["minutes"] as! String
if mins.isEqual("0"){
mins="<1";
}
// print("mins:\(mins)")
let secs = time["seconds"] as! String
// print("seconds:\(secs)")
predictionTimes.append("\(mins) (mins)")
}
//print("")
// print(bPredictions!.count);
/*var aObject:AnyObject = preds
let anyMirror = Mirror(reflecting: aObject)
print(anyMirror.subjectType) */
}
catch let error as JSONError {
print(error.rawValue)
} catch {
print(error)
}
}
else {
// "No Predictions Available"
// print("No Predictions Available");
//print("")
predictionTimes.append("No Predictions Available")
}
var newBusInfo = BusInfo(busRoute:name, prediction: predictionTimes)
routeInfo.append(newBusInfo);
}
} // outer for loop
var newBusStopInfo = BusStopInformation(busStop:stopTag, busStopInfo:routeInfo)
self.busStopInfo.append(newBusStopInfo);
}// method
enum JSONError: String, ErrorType {
case NoData = "ERROR: no data"
case ConversionFailed = "ERROR: conversion from JSON failed"
}
func jsonBusRoutes() {
print("entered jsonBusRoutes");
let urlPath = "http://runextbus.heroku.com/active"
guard let endpoint = NSURL(string: urlPath) else { print("Error creating endpoint"); return }
let request = NSMutableURLRequest(URL:endpoint)
NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) -> Void in
do {
guard let dat = data else { throw JSONError.NoData; self.noData = true;}
guard let json = try NSJSONSerialization.JSONObjectWithData(dat, options: []) as? NSDictionary else { throw JSONError.ConversionFailed }
// print(json)
if let stops = json["stops"] as? [[String: AnyObject]] {
dispatch_async(dispatch_get_main_queue()) {
//print("entered main thread")
dataResults=stops
// print(stops);
self.parseBusRoutes(dataResults);
self.tableView.reloadData();
}
}
} catch let error as JSONError {
print(error.rawValue)
} catch {
print(error)
}
}.resume()
}
func jsonBusRoutesRefresh() {
print("entered jsonBusRoutesRefresh");
let urlPath = "http://runextbus.heroku.com/active"
guard let endpoint = NSURL(string: urlPath) else { print("Error creating endpoint"); return }
let request = NSMutableURLRequest(URL:endpoint)
print(request)
dispatch_async(dispatch_get_main_queue()) {
NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) -> Void in
do {
guard let dat = data else { throw JSONError.NoData; self.noData = true;}
guard let json = try NSJSONSerialization.JSONObjectWithData(dat, options: []) as? NSDictionary else { throw JSONError.ConversionFailed }
print(json)
if let stops = json["stops"] as? [[String: AnyObject]] {
dispatch_async(dispatch_get_main_queue()) {
print("entered main thread")
dataResults=stops
// print(stops);
self.parseBusRoutes(dataResults);
self.tableView.reloadData();
}
}
} catch let error as JSONError {
print(error.rawValue)
} catch {
print(error)
}
}.resume()
}
}
func jsonStopPredictions(let stopTag:String) {
print("jsonStopPredictions")
let stopTagNoSpaces = stopTag.stringByReplacingOccurrencesOfString(" ", withString: "%20", options: NSStringCompareOptions.LiteralSearch, range: nil)
let urlPath = "http://runextbus.heroku.com/stop/\(stopTagNoSpaces)"
guard let endpoint = NSURL(string: urlPath) else { print("Error creating endpoint"); return }
let request = NSMutableURLRequest(URL:endpoint)
NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) -> Void in
do {
guard let dat = data else { throw JSONError.NoData }
guard let json = try NSJSONSerialization.JSONObjectWithData(dat, options: []) as? NSArray else { throw JSONError.ConversionFailed }
dispatch_async(dispatch_get_main_queue()) {
timeResults=json as! [[String : AnyObject]]
// print(json);
self.parseBusPredictions(timeResults,stopTag: stopTag);
self.tableView.reloadData();
}
} catch let error as JSONError {
print(error.rawValue)
} catch {
print(error)
}
}.resume()
}
func jsonNearestStop() {
//let stopTagNoSpaces = stopTag.stringByReplacingOccurrencesOfString(" ", withString: "%20", options: NSStringCompareOptions.LiteralSearch, range: //nil)
let urlPath = "http://runextbus.heroku.com/nearby/\(self.lat)/\(self.long)"
guard let endpoint = NSURL(string: urlPath) else { print("Error creating endpoint"); return }
let request = NSMutableURLRequest(URL:endpoint)
NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) -> Void in
//print("Inside")
do {
guard let dat = data else { throw JSONError.NoData }
guard let json = try NSJSONSerialization.JSONObjectWithData(dat, options: []) as? NSDictionary else { throw JSONError.ConversionFailed }
dispatch_async(dispatch_get_main_queue()) {
nearbyResults = json
// print(json);
self.parseNearbyStops()
self.tableView.reloadData();
}
} catch let error as JSONError {
print(error.rawValue)
} catch {
print(error)
}
}.resume()
}
func parseNearbyStops(){
//stop.value is the accuracy measurement (based upon number of geocache characters that match)
//stop.key is the corresponding bus stop
for stop in nearbyResults {
let accuracy = stop.value as? Int
//based on some experiementation, it seems accuracy when >5 provides best results
if (accuracy! > 4) {
print(stop.key)
nearbyBusStops.append(stop.key as! String);
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 2;
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
// print(busStopRoutes.count);
if section == 0 {
if nearbyBusStops.count > 0 {
return nearbyBusStops.count;
}
else {
self.noNearbyStops = true;
return 1;
}
}
else {
if self.resultSearchController.active {
return self.filteredBusStopInfo.count
}
else{
self.title = "Stops"
if(dataResults.count == 0){
// print("here");
return 1;
}
// print("\(dataResults.count)")
return dataResults.count
}
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("stopCell", forIndexPath: indexPath)
// print("called")
if (dataResults.count == 0){
// print("reached in no active routes");
cell.textLabel?.text = "No Active Stops";
cell.selectionStyle = UITableViewCellSelectionStyle.None
cell.textLabel?.textColor = UIColor.grayColor();
cell.userInteractionEnabled = false;
cell.detailTextLabel?.text=""
return cell;
} else{
cell.selectionStyle = UITableViewCellSelectionStyle.Default
cell.textLabel?.textColor = UIColor.blackColor();
cell.userInteractionEnabled = true;
}
if indexPath.section == 1 {
if self.resultSearchController.active {
let busStop = filteredBusStopInfo[indexPath.row]
cell.textLabel?.text = busStop
var prediction = ""
// search for data associated with corresponding bus stop
for stop in self.busStopInfo {
//execute logic once bus stop is found: (break out of loop once found)
if stop.busStop.isEqual(busStop) {
// initialize prediction
prediction = stop.busStopInfo[0].prediction[0];
//if first prediction is "No Predictions Available" then check:
if prediction.isEqual("No Predictions Available"){
//print("here for \(busStop)");
for routes in stop.busStopInfo{
for pred in routes.prediction{
if !(pred.isEqual("No Predictions Available")) {
prediction = pred;
break;
}
}
if !(prediction.isEqual("No Predictions Available")){
break;
}
}
}
print("Prediction=\(prediction)")
cell.detailTextLabel?.text = prediction;
break;
}
}
//prevents selection on cells for which there are no bus time predictions
//(put into an if else structure due to refreshing, as the prediction data could change
if prediction.isEqual("No Predictions Available"){
// print("here for \(busStop)");
cell.selectionStyle = UITableViewCellSelectionStyle.None
cell.textLabel?.textColor = UIColor.grayColor();
cell.userInteractionEnabled = false;
return cell;
} else{
// print("here for \(busStop)");
cell.selectionStyle = UITableViewCellSelectionStyle.Default
cell.textLabel?.textColor = UIColor.blackColor();
cell.userInteractionEnabled = true;
}
}
// print("reached in active routes");
// Configure the cell...
else{
//print("Index:\(indexPath.row)")
if busStopRoutes.count != 0 {
let busStop = busStopRoutes[indexPath.row]
print("Bus Stop: \(busStop)")
// print("Index:\(indexPath.row)")
cell.textLabel?.text = busStop
var prediction = ""
// search for data associated with corresponding bus stop
for stop in self.busStopInfo {
//execute logic once bus stop is found: (break out of loop once found)
if stop.busStop.isEqual(busStop) {
// initialize prediction
prediction = stop.busStopInfo[0].prediction[0];
//if first prediction is "No Predictions Available" then check:
if prediction.isEqual("No Predictions Available"){
//print("here for \(busStop)");
for routes in stop.busStopInfo{
for pred in routes.prediction{
if !(pred.isEqual("No Predictions Available")) {
prediction = pred;
break;
}
}
if !(prediction.isEqual("No Predictions Available")){
break;
}
}
}
cell.detailTextLabel?.text = prediction;
break;
}
}
//prevents selection on cells for which there are no bus time predictions
//(put into an if else structure due to refreshing, as the prediction data could change
if prediction.isEqual("No Predictions Available"){
// print("here for \(busStop)");
cell.selectionStyle = UITableViewCellSelectionStyle.None
cell.textLabel?.textColor = UIColor.grayColor();
cell.userInteractionEnabled = false;
return cell;
} else{
// print("here for \(busStop)");
cell.selectionStyle = UITableViewCellSelectionStyle.Default
cell.textLabel?.textColor = UIColor.blackColor();
cell.userInteractionEnabled = true;
}
}
} // end section 1
}
else { // section 0
if (nearbyBusStops.count == 0 ){
cell.textLabel!.text = "No Nearby Stops"
cell.detailTextLabel!.text = "No Available Predictions"
cell.selectionStyle = UITableViewCellSelectionStyle.None
cell.textLabel?.textColor = UIColor.grayColor();
cell.userInteractionEnabled = false;
return cell;
}
else{
let busStop = nearbyBusStops[indexPath.row];
cell.textLabel!.text = busStop;
var prediction = ""
// search for data associated with corresponding bus stop
for stop in self.busStopInfo {
print("cell detail label")
//execute logic once bus stop is found: (break out of loop once found)
if stop.busStop.isEqual(busStop) {
// initialize prediction
prediction = stop.busStopInfo[0].prediction[0];
//if first prediction is "No Predictions Available" then check:
if prediction.isEqual("No Predictions Available"){
//print("here for \(busStop)");
for routes in stop.busStopInfo{
for pred in routes.prediction{
if !(pred.isEqual("No Predictions Available")) {
prediction = pred;
break;
}
}
if !(prediction.isEqual("No Predictions Available")){
break;
}
}
}
cell.detailTextLabel?.text = prediction;
break;
}
}
//prevents selection on cells for which there are no bus time predictions
//(put into an if else structure due to refreshing, as the prediction data could change
if prediction.isEqual("No Predictions Available"){
// print("here for \(busStop)");
cell.selectionStyle = UITableViewCellSelectionStyle.None
cell.textLabel?.textColor = UIColor.grayColor();
cell.userInteractionEnabled = false;
return cell;
} else{
// print("here for \(busStop)");
cell.selectionStyle = UITableViewCellSelectionStyle.Default
cell.textLabel?.textColor = UIColor.blackColor();
cell.userInteractionEnabled = true;
}
}
cell.selectionStyle = UITableViewCellSelectionStyle.Default
cell.textLabel?.textColor = UIColor.blackColor();
cell.userInteractionEnabled = true;
}
//cell.detailTextLabel?.text="Arriving in \(busStopRoutesTimes[indexPath.row][0])"
return cell
}
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if section == 0 {
return "Nearby Bus Stops:"
} else {
return "Active Bus Stops:"
}
}
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
var secondScene = segue.destinationViewController as! BusRoutesTableViewController
// print("here prepare4Segue!");
if self.tableView.indexPathForSelectedRow!.section == 1 {
if self.resultSearchController.active{
// print("Search Controller Active")
if let indexPath = self.tableView.indexPathForSelectedRow {
let selectedStop = filteredBusStopInfo[indexPath.row]
// print("Selected Stop:\(selectedStop)");
secondScene.currentStop = selectedStop
//pass on BusStopInformation to the next scene
for stop in busStopInfo{
if selectedStop.isEqual(stop.busStop){
secondScene.currentInfo = stop
self.resultSearchController.active = false;
}
}
}
}
else {
// print("Search Controller Inactive")
// Pass the selected object to the new view controller.
if let indexPath = self.tableView.indexPathForSelectedRow {
let selectedStop = busStopRoutes[indexPath.row]
secondScene.currentStop = selectedStop
//pass on BusStopInformation to the next scene
for stop in busStopInfo{
if selectedStop.isEqual(stop.busStop){
secondScene.currentInfo = stop
}
}
}
}
} // end section 1
else{ // section 0
if let indexPath = self.tableView.indexPathForSelectedRow {
let selectedStop = nearbyBusStops[indexPath.row]
secondScene.currentStop = selectedStop
//pass on BusStopInformation to the next scene
for stop in busStopInfo{
if selectedStop.isEqual(stop.busStop){
secondScene.currentInfo = stop
}
}
}
}
}
/*func searchDisplayController(controller: UISearchDisplayController, shouldReloadTableForSearchString searchString: String?) -> Bool {
self.filterContentForSearchText(searchString!)
return true;
} */
func updateSearchResultsForSearchController(searchController: UISearchController) {
self.filteredBusStopInfo.removeAll(keepCapacity:false);
let searchPredicate = NSPredicate(format:"SELF CONTAINS[c] %@", searchController.searchBar.text!);
let array = (self.busStopRoutes as NSArray).filteredArrayUsingPredicate(searchPredicate)
self.filteredBusStopInfo = array as! [String]
self.tableView.reloadData()
}
@IBAction func refresh(sender: UIRefreshControl) {
print("Refresh")
dispatch_async(dispatch_get_main_queue()) {
//self.busStopRoutes.removeAll();
//self.busStopInfo.removeAll();
// self.nearbyBusStops.removeAll()
self.jsonBusRoutes()
self.tableView.reloadData()
}
sender.endRefreshing()
}
}