This repository has been archived by the owner on Feb 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
BHListViewController.m
386 lines (304 loc) · 13.3 KB
/
BHListViewController.m
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
//
// BHListViewController.m
// BeeHive
//
// Created by Zhenyi ZHANG on 2/18/2014.
// Copyright (c) 2014 Zhenyi Zhang. All rights reserved.
//
#import "BHListViewController.h"
#import "BHBuilding.h"
#import "BHLocation.h"
#import "BHLocationStat.h"
#import "BHDataController.h"
#import "BHLocationTableViewCell.h"
#import "BHLocationDetailViewController.h"
#import "BHUtils.h"
//RefreshControl Library
#import "ODRefreshControl.h"
//SDWebImage Library
#import <SDWebImage/UIImageView+WebCache.h>
@implementation BHListViewController
@synthesize tableView = _tableView;
@synthesize refreshControl = _refreshControl;
@synthesize locationSearchBar = _locationSearchBar, filteredLocations = _filteredLocations, filteredBuildings = _filteredBuildings;
-(ODRefreshControl *)refreshControl
{
if (!_refreshControl) {
_refreshControl = [[ODRefreshControl alloc] initInScrollView:self.tableView];
}
return _refreshControl;
}
-(NSMutableArray *)filteredLocations
{
if (!_filteredLocations) {
_filteredLocations = [NSMutableArray arrayWithCapacity:0];
}
return _filteredLocations;
}
-(NSMutableArray *)filteredBuildings
{
if (!_filteredBuildings) {
_filteredBuildings = [NSMutableArray arrayWithCapacity:0];
}
return _filteredBuildings;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.locationSearchBar setShowsScopeBar:NO];
[self.locationSearchBar sizeToFit];
// Hide the search bar until user scrolls up
CGRect newBounds = self.tableView.bounds;
newBounds.origin.y = newBounds.origin.y + self.locationSearchBar.bounds.size.height;
self.tableView.bounds = newBounds;
// IMPORTANT!
// Added this line so that refresh control can properly be showed
if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) {
self.edgesForExtendedLayout = UIRectEdgeNone;
}
// self.edgesForExtendedLayout=UIRectEdgeNone;
// self.extendedLayoutIncludesOpaqueBars=NO;
self.automaticallyAdjustsScrollViewInsets=YES;
// Add a footer so that the tabbar do not cover the tableView bottom if is not iphone5
int footerHeight = 0;
if (IS_IPHONE_5) {
// footerHeight = 120;
footerHeight = 0;
} else if ( IS_IPHONE) {
// footerHeight = 212;
footerHeight = 0;
} else if (IS_IPAD){
footerHeight = 0;
}
UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, footerHeight)];
footer.backgroundColor = [UIColor clearColor];
self.tableView.tableFooterView = footer;
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
//Refresh Control
[self.refreshControl addTarget:self action:@selector(dropViewDidBeginRefreshing:) forControlEvents:UIControlEventValueChanged];
}
- (void)viewWillAppear:(BOOL)animated
{
BHDataController *dataController = [BHDataController sharedDataController];
[dataController fetchLocationStatForViewController:self];
}
- (void)viewDidAppear
{
// [self.tableView reloadData];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [self.filteredBuildings count];
} else {
BHDataController *sharedDataController = [BHDataController sharedDataController];
return [sharedDataController.buildingList count];
}
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (tableView == self.searchDisplayController.searchResultsTableView) {
BHBuilding *bd = [self.filteredBuildings objectAtIndex:section];
return bd.name;
} else {
BHDataController *dataController = [BHDataController sharedDataController];
BHBuilding *bd = [dataController.buildingList objectAtIndex:section];
return bd.name;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if (tableView == self.searchDisplayController.searchResultsTableView) {
BHBuilding *bd = [self.filteredBuildings objectAtIndex:section];
return [bd.locations count];
} else {
BHDataController *dataController = [BHDataController sharedDataController];
BHBuilding *bd = [dataController.buildingList objectAtIndex:section];
return [bd.locations count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"LocationCell";
BHLocationTableViewCell *cell;
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
} else {
cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
}
// Configure the cell here
BHDataController *dataController = [BHDataController sharedDataController];
BHLocation *loc;
// Check to see whether the normal table or search results table is being displayed and set the Candy object from the appropriate array
BHBuilding *bd;
if (tableView == self.searchDisplayController.searchResultsTableView) {
bd = [self.filteredBuildings objectAtIndex:indexPath.section];
} else {
bd = [dataController.buildingList objectAtIndex:indexPath.section];
}
loc = [bd.locations objectAtIndex:indexPath.row];
BHLocationStat *locStat = [dataController.locationStats objectForKey:loc.locId];
cell.textLabel.text = loc.name;
// Determine label color
cell.textLabel.textColor = [BHUtils titleColorForLocationStat:locStat];
cell.detailTextLabel.text = [NSString stringWithFormat:@"Oc: %@%% Line: %@ Go: %@", locStat.occupancyPercent, locStat.queue, locStat.bestTime];
cell.detailTextLabel.font = [UIFont systemFontOfSize:11];
// Using SDWebImage to load image
[cell.imageView setImageWithURL:[NSURL URLWithString:loc.photoUrl]
placeholderImage:[UIImage imageNamed:@"Beehive.png"]];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Perform segue to candy detail
[self performSegueWithIdentifier:@"showLocationDetailFromListView" sender:tableView];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"showLocationDetailFromListView"]) {
BHLocationDetailViewController *detailViewController = [segue destinationViewController];
BHDataController *dataController = [BHDataController sharedDataController];
NSIndexPath *indexPath;
BHBuilding *bd;
if (sender == self.searchDisplayController.searchResultsTableView) {
indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
bd = [self.filteredBuildings objectAtIndex:indexPath.section];
} else {
indexPath = [self.tableView indexPathForSelectedRow];
bd = [dataController.buildingList objectAtIndex:indexPath.section];
}
detailViewController.location = [bd.locations objectAtIndex:indexPath.row];
NSArray *weeklyStat = [dataController.locationHourlyStats objectForKey:detailViewController.location.locId];
detailViewController.weeklyStat = weeklyStat;
if (!weeklyStat) {
[dataController fetchStatForLocation:detailViewController];
}
}
}
// Refresh when dropping down
- (void)dropViewDidBeginRefreshing:(ODRefreshControl *)refreshControl
{
BHDataController *dataController = [BHDataController sharedDataController];
if (dataController.connectionLost) {
//Fetch building List for mapView
[dataController fetchBuildingsForViewController:self];
}
//Fetch locations statistic for mapView
[dataController fetchLocationStatForViewController:self];
// mimic the behavior of fectching
// double delayInSeconds = 1.5;
// dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
// dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
// NSLog(@"Refreshed");
// [refreshControl endRefreshing];
// });
}
- (IBAction)refreshList:(id)sender {
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[spinner startAnimating];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:spinner];
BHDataController *dataController = [BHDataController sharedDataController];
if (dataController.connectionLost) {
//Fetch building List for mapView
[dataController fetchBuildingsForViewController:self];
}
//Fetch locations statistic for mapView
[dataController fetchLocationStatForViewController:self];
}
// search delegate method
#pragma mark Content Filtering
#pragma mark Content Filtering
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
// Update the filtered array based on the search text and scope.
// Remove all objects from the filtered search array
[self.filteredLocations removeAllObjects];
[self.filteredBuildings removeAllObjects];
BHDataController *dataController = [BHDataController sharedDataController];
NSArray *buildingList = dataController.buildingList;
for (BHBuilding *bud in buildingList) {
BOOL addBuilding = NO;
// copy the old building object
BHBuilding *newBud = [[BHBuilding alloc] init];
newBud.name = bud.name;
newBud.description = bud.description;
if ([bud.name rangeOfString:searchText options:NSCaseInsensitiveSearch].location != NSNotFound) {
newBud.locations = bud.locations;
[self.filteredBuildings addObject:newBud];
} else {
NSMutableArray *newLocList = [[NSMutableArray alloc] init];
for (BHLocation *loc in bud.locations) {
if ([loc.name rangeOfString:searchText options:NSCaseInsensitiveSearch].location != NSNotFound) {
[newLocList addObject:loc];
addBuilding = YES;
}
}
if (addBuilding) {
newBud.locations = newLocList;
[self.filteredBuildings addObject:newBud];
}
}
}
}
#pragma mark - UISearchDisplayController Delegate Methods
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
// Tells the table data source to reload when text changes
[self filterContentForSearchText:searchString scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {
// Tells the table data source to reload when scope bar selection changes
[self filterContentForSearchText:self.searchDisplayController.searchBar.text scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
- (IBAction)searchLocation:(id)sender {
[self.locationSearchBar becomeFirstResponder];
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
@end