-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStopScheduleViewController.m
More file actions
217 lines (175 loc) · 6.95 KB
/
StopScheduleViewController.m
File metadata and controls
217 lines (175 loc) · 6.95 KB
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
//
// StopScheduleViewController.m
// BusStop
//
// Created by Chris Woodard on 4/13/13.
// Copyright (c) 2013 0xC0ffee. All rights reserved.
//
#import "StopScheduleViewController.h"
#import "ScheduleRouteChooserViewController.h"
#import "FPPopoverController.h"
#import "ScheduleCell.h"
#import "MBProgressHUD.h"
#import "BusStopREST.h"
@interface StopScheduleViewController ()
@end
@implementation StopScheduleViewController
-(void)updateCurrentTime
{
NSDate *rightNow = [NSDate dateWithTimeIntervalSinceNow:0];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *currentComponents = [calendar components:NSHourCalendarUnit|NSMinuteCalendarUnit fromDate:rightNow];
NSString *aAMPM = @"AM";
int ahr = [currentComponents hour];
if(ahr>12)
{
ahr -= 12;
aAMPM = @"PM";
}
self.currentTimeLabel.text = [NSString stringWithFormat:@"%0d:%02d %@", ahr, [currentComponents minute], aAMPM];
}
-(void)updateTime:(NSTimer *)timer
{
[self updateCurrentTime];
}
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.visibleEntries = [[NSMutableArray alloc] initWithCapacity:0];
}
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
// Custom initialization
self.visibleEntries = [[NSMutableArray alloc] initWithCapacity:0];
}
return self;
}
-(void)switchToSlotsForRoute:(NSString *)routeId
{
self.currentRouteId = routeId;
[self.visibleEntries removeAllObjects];
for( NSString *storedRouteId in [self.allEntries allKeys])
{
if([routeId isEqualToString:storedRouteId])
{
for( NSString *tripHeadSign in self.allEntries[storedRouteId])
{
for( NSDictionary *slot in self.allEntries[storedRouteId][tripHeadSign])
{
[self.visibleEntries addObject:@{@"tripHeadSign":tripHeadSign, @"arrives":slot[@"arrival"], @"departs":slot[@"departure"]}];
}
}
}
}
[self.slotsTable reloadData];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self updateCurrentTime];
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTime:) userInfo:nil repeats:YES];
self.stopNameLabel.text = self.busStop.name;
self.allEntries = [[NSMutableDictionary alloc] initWithCapacity:0];
NSCalendar *calendar = [NSCalendar currentCalendar];
BusStopREST *mgr = [[BusStopREST alloc] init];
NSDictionary *stopSched = [mgr scheduleForStop:self.busStop.id];
NSArray *stopRouteSchedules = stopSched[@"data"][@"entry"][@"stopRouteSchedules"];
for( NSDictionary *row in stopRouteSchedules )
{
// row[@"routeId"]
NSString *routeId = row[@"routeId"];
NSArray *stopRouteDirectionSchedules = row[@"stopRouteDirectionSchedules"];
NSMutableDictionary *trips = [[NSMutableDictionary alloc] initWithCapacity:0];
for( NSDictionary *row2 in stopRouteDirectionSchedules )
{// one per "trip" - tripId and tripHeadSign
NSString *tripHeadSign = row2[@"tripHeadsign"];
NSMutableArray *times = [[NSMutableArray alloc] initWithCapacity:0];
NSArray *scheduleStopTimes = row2[@"scheduleStopTimes"];
for( NSDictionary *row3 in scheduleStopTimes )
{
NSNumber *arrivalTimeNbr = row3[@"arrivalTime"];
NSNumber *departureTimeNbr = row3[@"departureTime"];
NSDate *arrivalTime = [NSDate dateWithTimeIntervalSince1970:[arrivalTimeNbr doubleValue]/1000.0];
NSDate *departureTime = [NSDate dateWithTimeIntervalSince1970:[departureTimeNbr doubleValue]/1000.0];
NSDateComponents *arrivalComponents = [calendar components:NSHourCalendarUnit|NSMinuteCalendarUnit fromDate:arrivalTime];
NSDateComponents *departureComponents = [calendar components:NSHourCalendarUnit|NSMinuteCalendarUnit fromDate:departureTime];
NSString *aAMPM = @"AM";
int ahr = [arrivalComponents hour];
if(ahr>12)
{
ahr -= 12;
aAMPM = @"PM";
}
NSString *dAMPM = @"AM";
int dhr = [departureComponents hour];
if(dhr>12)
{
dhr -= 12;
dAMPM = @"PM";
}
NSString *arrivalStr = [NSString stringWithFormat:@"arrives %0d:%02d %@", ahr, [arrivalComponents minute], aAMPM];
NSString *departureStr = [NSString stringWithFormat:@"departs %0d:%02d %@", dhr, [departureComponents minute], dAMPM];
[times addObject:@{@"arrival":arrivalStr, @"departure":departureStr}];
}
trips[tripHeadSign] = times;
}
self.allEntries[routeId] = trips;
}
if(stopRouteSchedules.count>0)
{
self.currentRouteId = stopRouteSchedules[0][@"routeId"];
[self switchToSlotsForRoute:self.currentRouteId];
}
}
-(void)didSelectRouteWithId:(NSString *)routeId
{
[self switchToSlotsForRoute:routeId];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSInteger numEntries = self.visibleEntries.count;
return numEntries;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *row = self.visibleEntries[indexPath.row];
ScheduleCell *cell = (ScheduleCell *)[tableView dequeueReusableCellWithIdentifier:@"ScheduleCell"];
if(nil == cell)
cell = (ScheduleCell *)[[ScheduleCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ScheduleCell"];
cell.headSignLabel.text = row[@"tripHeadSign"];
cell.arrivalLabel.text = row[@"arrives"];
cell.departureLabel.text = row[@"departs"];
return cell;
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"yeah");
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
ScheduleRouteChooserViewController *vc = (ScheduleRouteChooserViewController *)[segue destinationViewController];
NSMutableDictionary *d = [[NSMutableDictionary alloc] initWithCapacity:0];
for( NSString *routeId in [self.allEntries allKeys])
{
d[routeId] = @YES;
}
vc.delegate = self;
vc.currentKey = self.currentRouteId;
vc.routes = d;
}
@end