-
Notifications
You must be signed in to change notification settings - Fork 20
/
BRCEventsTableViewController.m
339 lines (288 loc) · 14 KB
/
BRCEventsTableViewController.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
//
// BRCEventsTableViewController.m
// iBurn
//
// Created by Christopher Ballinger on 7/28/14.
// Copyright (c) 2014 Burning Man Earth. All rights reserved.
//
#import "BRCEventsTableViewController.h"
#import "BRCDatabaseManager.h"
#import "BRCEventObjectTableViewCell.h"
@import YapDatabase;
#import "BRCDataObject.h"
#import "BRCFilteredTableViewController_Private.h"
#import "BRCEventObject.h"
#import "NSDate+iBurn.h"
#import "NSDateFormatter+iBurn.h"
#import "BRCEventsFilterTableViewController.h"
#import "NSUserDefaults+iBurn.h"
#import "ASDayPicker.h"
#import <KVOController/NSObject+FBKVOController.h>
#import "PureLayout.h"
static const CGFloat kDayPickerHeight = 65.0f;
@interface BRCEventsTableViewController () <BRCEventsFilterTableViewControllerDelegate, UIPopoverPresentationControllerDelegate>
@property (nonatomic, strong, readonly) NSDate *selectedDay;
@property (nonatomic, strong) NSArray *dayPickerRowTitles;
@property (nonatomic, strong) NSArray *festivalDates;
@property (nonatomic, strong) UISegmentedControl *dayPickerSegmentedControl;
@property (nonatomic) BOOL isRefreshingEventTimeSort;
@property (nonatomic, strong) ASDayPicker *dayPicker;
@property (nonatomic, strong) NSLayoutConstraint *dayPickerHeight;
@property (nonatomic, strong) NSLayoutConstraint *tableViewHeaderHeight;
@property (nonatomic, strong) UIView *tableHeaderView;
@end
@implementation BRCEventsTableViewController
@synthesize selectedDay = _selectedDay;
- (void) filterButtonPressed:(id)sender {
BRCEventsFilterTableViewController *filterViewController = [[BRCEventsFilterTableViewController alloc] initWithDelegate:self];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:filterViewController];
[self presentViewController:navigationController animated:YES completion:nil];
}
- (NSInteger) indexForDay:(NSDate*)date {
NSInteger dayCount = [NSDate brc_daysBetweenDate:[self.festivalDates firstObject] andDate:date];
NSParameterAssert(dayCount >= 0);
return dayCount;
}
- (NSDate*) dateForIndex:(NSInteger)index {
NSParameterAssert(index < self.festivalDates.count);
return [self.festivalDates objectAtIndex:index];
}
- (void) viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *filterButton = [[UIBarButtonItem alloc] initWithTitle:@"Filter" style:UIBarButtonItemStylePlain target:self action:@selector(filterButtonPressed:)];
UIBarButtonItem *loadingButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.loadingIndicatorView];
self.navigationItem.rightBarButtonItems = @[filterButton, loadingButtonItem];
self.selectedDay = [NSDate date];
self.searchController.hidesNavigationBarDuringPresentation = YES;
[self layoutTableHeaderViewWithWidth:self.view.bounds.size.width];
}
// http://stackoverflow.com/questions/27512738/uisearchbar-subview-of-uitableviewheader
- (void) layoutTableHeaderViewWithWidth:(CGFloat)width {
NSParameterAssert(self.tableView != nil);
// Configure header view
UIView *tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, 0)];
tableHeaderView.translatesAutoresizingMaskIntoConstraints = NO;
// Create container view for search bar
UIView *searchBarContainerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, 44)];
searchBarContainerView.translatesAutoresizingMaskIntoConstraints = NO;
NSParameterAssert(self.searchController.searchBar != nil);
self.searchController.searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[searchBarContainerView addSubview:self.searchController.searchBar];
[self.searchController.searchBar sizeToFit];
CGRect searchBarFrame = self.searchController.searchBar.frame;
searchBarFrame.origin = CGPointMake(0, 0);
self.searchController.searchBar.frame = searchBarFrame;
[self setupDayPicker];
[tableHeaderView addSubview:self.dayPicker];
[tableHeaderView addSubview:searchBarContainerView];
// setup table header view constraints
[searchBarContainerView autoSetDimension:ALDimensionHeight toSize:44.0];
[searchBarContainerView autoSetDimension:ALDimensionWidth toSize:width];
[searchBarContainerView autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:tableHeaderView];
_dayPickerHeight = [self.dayPicker autoSetDimension:ALDimensionHeight toSize:kDayPickerHeight];
[self.dayPicker autoSetDimension:ALDimensionWidth toSize:width];
[self.dayPicker autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:tableHeaderView withOffset:8];
[self.dayPicker autoPinEdge:ALEdgeBottom toEdge:ALEdgeTop ofView:searchBarContainerView];
NSLayoutConstraint *headerWidthConstraint = [NSLayoutConstraint
constraintWithItem:tableHeaderView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual
toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:width
];
[tableHeaderView addConstraint:headerWidthConstraint];
CGFloat height = [tableHeaderView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
[tableHeaderView removeConstraint:headerWidthConstraint];
tableHeaderView.frame = CGRectMake(0, 0, width, height);
[tableHeaderView autoSetDimension:ALDimensionWidth toSize:width];
self.tableViewHeaderHeight = [tableHeaderView autoSetDimension:ALDimensionHeight toSize:height];
//tableHeaderView.translatesAutoresizingMaskIntoConstraints = YES;
// Then add header to table view
self.tableView.tableHeaderView = tableHeaderView;
}
- (void) updateViewConstraints {
if (!self.hasAddedConstraints) {
NSParameterAssert(self.tableView != nil);
NSParameterAssert(self.dayPicker != nil);
[self.tableView autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero];
self.hasAddedConstraints = YES;
}
[super updateViewConstraints];
}
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self layoutTableHeaderViewWithWidth:self.view.bounds.size.width];
}
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
[self layoutTableHeaderViewWithWidth:self.view.bounds.size.width];
}
- (void) setupDayPicker {
self.dayPicker = [[ASDayPicker alloc] initForAutoLayout];
self.dayPicker.daysScrollView.scrollEnabled = NO;
[self.dayPicker setStartDate:[BRCEventObject festivalStartDate] endDate:[BRCEventObject festivalEndDate]];
[self.dayPicker setWeekdayTitles:[ASDayPicker weekdayTitlesWithLocaleIdentifier:nil length:3 uppercase:YES]];
self.dayPicker.selectedDate = self.selectedDay;
[self.dayPicker setSelectedDateBackgroundImage:[UIImage imageNamed:@"BRCDateSelection"]];
[self.KVOController observe:self.dayPicker keyPath:NSStringFromSelector(@selector(selectedDate)) options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew block:^(id observer, ASDayPicker *dayPicker, NSDictionary *change) {
NSDate *newDate = dayPicker.selectedDate;
self.selectedDay = newDate;
}];
}
- (NSDate*) selectedDayInFestivalRange:(NSDate*)dayCandidate {
NSDate *validDate = [dayCandidate brc_dateWithinStartDate:[BRCEventObject festivalStartDate] endDate:[BRCEventObject festivalEndDate]];
return validDate;
}
- (void) setSelectedDay:(NSDate *)selectedDay {
if (!selectedDay) {
selectedDay = [NSDate date];
}
NSDate *selectedDayInFestivalRange = [self selectedDayInFestivalRange:selectedDay];
_selectedDay = selectedDayInFestivalRange;
NSString *dayString = [[NSDateFormatter brc_dayOfWeekDateFormatter] stringFromDate:_selectedDay];
self.navigationItem.leftBarButtonItem.title = dayString;
[self updateFilteredViews];
[self replaceTimeBasedEventMappings];
[self updateMappingsWithCompletionBlock:^{
[self.tableView reloadData];
}];
}
- (NSDate*) selectedDay {
if (!_selectedDay) {
NSDate *selectedDayInFestivalRange = [self selectedDayInFestivalRange:[NSDate date]];
_selectedDay = selectedDayInFestivalRange;
}
return _selectedDay;
}
- (void)updateFilteredViews
{
self.isUpdatingFilters = YES;
[[BRCDatabaseManager sharedInstance] refreshEventFilteredViewsWithSelectedDay:self.selectedDay completionBlock:^{
self.isUpdatingFilters = NO;
[self.tableView reloadData];
}];
}
- (void) setupMappings {
// we need to override search mappings for events
self.searchMappings = [[YapDatabaseViewMappings alloc] initWithGroupFilterBlock:^BOOL(NSString *group, YapDatabaseReadTransaction *transaction) {
return YES;
} sortBlock:^NSComparisonResult(NSString *group1, NSString *group2, YapDatabaseReadTransaction *transaction) {
return [group1 compare:group2];
} view:self.searchViewName];
[self replaceTimeBasedEventMappings];
}
- (void) replaceTimeBasedEventMappings {
NSString *selectedDay = [[NSDateFormatter brc_eventGroupDateFormatter] stringFromDate:self.selectedDay];
self.mappings = [[YapDatabaseViewMappings alloc] initWithGroupFilterBlock:^BOOL(NSString *group, YapDatabaseReadTransaction *transaction) {
if (group && selectedDay) {
return [group containsString:selectedDay];
}
return NO;
} sortBlock:^NSComparisonResult(NSString *group1, NSString *group2, YapDatabaseReadTransaction *transaction) {
return [group1 compare:group2];
} view:self.viewName];
}
- (void) refreshEventTimeSort {
if (self.isRefreshingEventTimeSort) {
return;
}
self.isRefreshingEventTimeSort = YES;
[[BRCDatabaseManager sharedInstance] refreshEventsSortingWithCompletionBlock:^{
[self updateMappingsWithCompletionBlock:^{
[self.tableView reloadData];
self.isRefreshingEventTimeSort = NO;
}];
}];
}
#pragma mark UITableViewDataSource
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
if (![self isSearchResultsControllerTableView:tableView])
{
NSArray *rawGroups = [self.mappings allGroups];
NSMutableArray *groups = [NSMutableArray arrayWithCapacity:rawGroups.count];
[rawGroups enumerateObjectsUsingBlock:^(NSString *groupName, NSUInteger idx, BOOL *stop) {
NSString *hour = [[groupName componentsSeparatedByString:@" "] lastObject];
NSInteger hourNumber = hour.integerValue;
hourNumber = hourNumber % 12;
if (hourNumber == 0) {
hourNumber = 12;
}
hour = [NSString stringWithFormat:@"%d", (int)hourNumber];
[groups addObject:hour];
}];
[groups insertObject:UITableViewIndexSearch atIndex:0];
return groups;
}
return nil;
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
if (![self isSearchResultsControllerTableView:tableView])
{
// https://github.com/kharrison/CodeExamples/blob/master/WorldFacts/WorldFacts/UYLCountryTableViewController.m
if (index > 0)
{
// The index is offset by one to allow for the extra search icon inserted at the front
// of the index
return index - 1;
}
else
{
// if magnifying glass http://stackoverflow.com/questions/19093168/uitableview-section-index-not-able-to-scroll-to-search-bar-index
// The first entry in the index is for the search icon so we return section not found
// and force the table to scroll to the top.
[tableView setContentOffset:CGPointMake(0.0, -tableView.contentInset.top)];
return NSNotFound;
}
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
if ([cell isKindOfClass:[BRCEventObjectTableViewCell class]]) {
BRCEventObjectTableViewCell *eventCell = (BRCEventObjectTableViewCell*)cell;
eventCell.locationLabel.hidden = NO;
}
return cell;
}
#pragma - mark BRCEventsFilterTableViewControllerDelegate Methods
- (void)didSetNewFilterSettingsInFilterTableViewController:(BRCEventsFilterTableViewController *)viewController
{
[self updateFilteredViews];
}
- (void)didSetNewSortSettingsInFilterTableViewController:(BRCEventsFilterTableViewController *)viewController
{
[self refreshEventTimeSort];
}
#pragma mark UIAdaptivePresentationControllerDelegate
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
// This makes our date picker appear in a popover
return UIModalPresentationNone;
}
#pragma mark UISearchControllerDelegate
- (void)willPresentSearchController:(UISearchController *)searchController {
self.dayPicker.userInteractionEnabled = NO;
[self.view layoutIfNeeded];
[UIView animateWithDuration:0.5 animations:^{
self.dayPicker.alpha = 0.0;
[self.dayPicker removeConstraint:self.dayPickerHeight];
self.tableViewHeaderHeight.constant = self.tableViewHeaderHeight.constant - kDayPickerHeight;
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
}];
}
- (void)willDismissSearchController:(UISearchController *)searchController {
self.dayPicker.userInteractionEnabled = YES;
[self.view layoutIfNeeded];
[UIView animateWithDuration:0.5 animations:^{
self.dayPicker.alpha = 1.0;
//self.dayPickerHeight.constant = kDayPickerHeight;
//self.tableViewHeaderHeight.constant = self.tableViewHeaderHeight.constant + kDayPickerHeight;
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
[self layoutTableHeaderViewWithWidth:self.view.bounds.size.width];
}];
}
- (void) didDismissSearchController:(UISearchController *)searchController {
}
@end