-
Notifications
You must be signed in to change notification settings - Fork 0
/
FiltersViewController.m
318 lines (265 loc) · 13.4 KB
/
FiltersViewController.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
//
// FiltersViewController.m
// yelpSearch
//
// Created by Eugenia Leong on 3/22/14.
// Copyright (c) 2014 Eugenia Leong. All rights reserved.
//
#import "FiltersViewController.h"
#import "FilterOptionCell.h"
#import <objc/runtime.h>
@interface FiltersViewController ()
@property (nonatomic, strong) NSArray *filterCategories;
@property (weak, nonatomic) IBOutlet UITableView *filtersTableView;
@end
NSString * const SWITCHES = @"switches";
NSString * const EXPAND = @"expand";
NSString * const TOGGLE_SWITCH = @"ToggleSwitchCell";
NSString * const FILTER_CELL = @"FilterOptionCell";
NSMutableDictionary *selectedCategories;
NSMutableDictionary *expandedCategories;
UIBarButtonItem *cancelButton;
UIBarButtonItem *searchButton;
//NSUserDefaults *defaults;
static NSString *tag = @"CellTag";
@implementation FiltersViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.title = @"Filters";
self.filterCategories = [NSArray arrayWithObjects:
@{
@"name":@"Most Popular",
@"type":@"switches",
@"options":@[@"Open Now",@"Hot & New",@"Offering a Deal",@"Delivery"]
},
@{
@"name":@"Distance",
@"type":@"expand",
@"options":@[@"Auto",@"2 blocks",@"6 blocks",@"1 mile",@"5 miles"]
},
@{
@"name":@"Sort By",
@"type":@"expand",
@"options":@[@"Best Match",@"Distance",@"Rating"]
},
@{
@"name":@"General Features",
@"type":@"switches",
@"options":@[@"Take-out",@"Accepts Credit Cards",@"Wheelchair Accessible",@"Full Bar",@"Beer & Wine Only",@"Happy Hour",@"Free Wi-Fi"]
},
@{
@"name":@"Categories",
@"type":@"expand",
@"options":@[@"American (New)", @"American (Traditional)", @"Asian Fusion", @"Barbeque", @"Breakfast & Brunch", @"Buffets", @"Burgers", @"Cafes", @"Chinese", @"Delis", @"Fast Food", @"French", @"German", @"Gluten-Free", @"Greek", @"Halal", @"Iberian", @"Indian", @"Italian", @"Japanese", @"Korean", @"Latin American", @"Malaysian", @"Mediterranean", @"Mexican", @"Middle Eastern", @"Pakistani", @"Pizza", @"Portuguese", @"Salad", @"Sandwiches", @"Seafood", @"Soul Food", @"Soup", @"Sushi Bars", @"Thai", @"Vegan", @"Vegetarian", @"Vietnamese"]
},
nil
];
//defaults = [NSUserDefaults standardUserDefaults];
selectedCategories = [NSMutableDictionary
dictionaryWithDictionary:
@{
@"Open Now" : @NO,//[defaults objectForKey:@"Open Now"],
@"Offering a Deal" : @NO,//[defaults objectForKey:@"Offering a Deal"],
@"Hot & New" : @NO,//[defaults objectForKey:@"Hot & New"],
@"Delivery" : @NO,
@"Take-out" : @NO,
@"Accepts Credit Cards" : @NO,
@"Wheelchair Accessible" : @NO,
@"Full Bar" : @NO,
@"Beer & Wine Only" : @NO,
@"Happy Hour" : @NO,
@"Free Wi-Fi" : @NO
}];
//NSLog(@"%@", selectedCategories);
expandedCategories = [NSMutableDictionary dictionaryWithDictionary:
@{
@"Distance" : [NSMutableDictionary dictionaryWithDictionary:@{@"expanded": @NO, @"selected":@0}],
@"Sort By" : [NSMutableDictionary dictionaryWithDictionary:@{@"expanded":@NO, @"selected":@0}],
@"Categories" : [NSMutableDictionary dictionaryWithDictionary:@{@"expanded":@NO, @"selected":@35}]
}];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.filtersTableView.dataSource = self;
self.filtersTableView.delegate = self;
// table view cells
UINib *toggleSwitchNib = [UINib nibWithNibName:TOGGLE_SWITCH bundle:nil];
[self.filtersTableView registerNib:toggleSwitchNib forCellReuseIdentifier:TOGGLE_SWITCH];
UINib *filterOptionNib = [UINib nibWithNibName:FILTER_CELL bundle:nil];
[self.filtersTableView registerNib:filterOptionNib forCellReuseIdentifier:FILTER_CELL];
// add cancel button
cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel:)];
[cancelButton setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateNormal];
self.navigationItem.leftBarButtonItem = cancelButton;
searchButton = [[UIBarButtonItem alloc] initWithTitle:@"Search" style:UIBarButtonItemStyleDone target:self action:@selector(search:)];
[searchButton setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateNormal];
self.navigationItem.rightBarButtonItem = searchButton;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)cancel:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
- (void)search:(id)sender
{
[self saveValues];
[self.navigationController popViewControllerAnimated:YES];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger sectionIndex = indexPath.section;
NSString *labelName = self.filterCategories[indexPath.section][@"options"][indexPath.row];
if (self.filterCategories[sectionIndex][@"type"] == SWITCHES)
{
ToggleSwitchCell *cell = [tableView dequeueReusableCellWithIdentifier:TOGGLE_SWITCH forIndexPath:indexPath];
cell.switchName.text = labelName;
BOOL toggleValue = [[selectedCategories objectForKey:labelName] boolValue];//[defaults boolForKey:labelName];
//NSLog(@"%@: %d", labelName, toggleValue);
[cell.toggleSwitch setOn:toggleValue animated:NO];
//[cell.toggleSwitch addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
objc_setAssociatedObject(cell.toggleSwitch, &tag, cell, OBJC_ASSOCIATION_RETAIN);
cell.delegate = self;
return cell;
}
else
{
FilterOptionCell *cell = [tableView dequeueReusableCellWithIdentifier:FILTER_CELL forIndexPath:indexPath];
NSString *sectionName = self.filterCategories[indexPath.section][@"name"];
//NSLog(@"cellForRowAtIndexPath %@ selected index %d", sectionName, [expandedCategories[sectionName][@"selected"] integerValue]);
if (![expandedCategories[sectionName][@"expanded"] boolValue])
{
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
//labelName = [defaults objectForKey:self.filterCategories[sectionIndex][@"name"]];
int selectedIndex = [expandedCategories[self.filterCategories[sectionIndex][@"name"]][@"selected"] integerValue];
labelName = self.filterCategories[sectionIndex][@"options"][selectedIndex];
}
// expanded
else
{
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
cell.filterOptionName.text = labelName;
return cell;
}
}
- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSString * filterCategory = self.filterCategories[section][@"type"];
if ( filterCategory == SWITCHES)
{
return [self.filterCategories[section][@"options"] count];
}
else if (filterCategory == EXPAND)
{
NSString * sectionName = self.filterCategories[section][@"name"];
if ([expandedCategories[sectionName][@"expanded"] boolValue])
{
return [self.filterCategories[section][@"options"] count];
}
else
{
return 1;
}
}
return 0;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.filterCategories.count;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return self.filterCategories[section][@"name"];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.filtersTableView deselectRowAtIndexPath:indexPath animated:YES];
NSInteger sectionIndex = indexPath.section;
if (self.filterCategories[sectionIndex][@"type"] == EXPAND)
{
NSString *categoryName = self.filterCategories[sectionIndex][@"name"];
BOOL isExpanded = ![expandedCategories[categoryName][@"expanded"] boolValue];
[expandedCategories[categoryName] setValue:[NSNumber numberWithBool:isExpanded] forKey:@"expanded"];
//NSLog(@"didSelectRowAtIndexPath %@ selected index %d", categoryName, [expandedCategories[categoryName][@"selected"] integerValue]);
if (isExpanded == YES)
{
NSMutableArray* rows = [NSMutableArray array];
int prevSelectedIndex = [expandedCategories[categoryName][@"selected"] integerValue];
NSString *selectedStr = self.filterCategories[indexPath.section][@"options"][prevSelectedIndex];
for (int i = 0; i < [self.filterCategories[sectionIndex][@"options"] count]; i++) {
if (self.filterCategories[indexPath.section][@"options"][i] == selectedStr)
{
continue;
}
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:indexPath.row + i inSection:indexPath.section];
[rows addObject:newIndexPath];
}
[self.filtersTableView insertRowsAtIndexPaths:rows withRowAnimation:UITableViewRowAnimationFade];
}
else
{
NSMutableArray* rows = [NSMutableArray array];
[expandedCategories[categoryName] setValue:[NSNumber numberWithInt:indexPath.row] forKey:@"selected"];
FilterOptionCell *cell = (FilterOptionCell*)[self.filtersTableView cellForRowAtIndexPath:indexPath];
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
for (int i = 0; i < [self.filterCategories[sectionIndex][@"options"] count]; i++) {
if (indexPath.row == i)
{
continue;
}
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:i inSection:indexPath.section];
[rows addObject:newIndexPath];
}
[self.filtersTableView deleteRowsAtIndexPaths:rows withRowAnimation:UITableViewRowAnimationFade];
}
}
}
-(void) switchChanged:(id)sender {
UISwitch* switcher = (UISwitch*)sender;
ToggleSwitchCell *cell = objc_getAssociatedObject(switcher, &tag);
[selectedCategories setObject:@(switcher.on) forKey:cell.switchName.text];
[self.filtersTableView reloadData];
}
-(void) saveValues
{
// save values
/*
for (NSString* toggleKey in selectedCategories.allKeys)
{
[defaults setBool:[selectedCategories[toggleKey] boolValue] forKey:toggleKey];
}
*/
[self.delegate addItemViewController:self setSwitches:selectedCategories];
int index = [expandedCategories[@"Distance"][@"selected"] intValue];
NSString *distanceStr = self.filterCategories[1][@"options"][index];
//[defaults setObject:selectedStr forKey:@"Distance"];
[self.delegate addItemViewController:self setDistance:distanceStr];
index = [expandedCategories[@"Sort By"][@"selected"] intValue];
NSInteger sortBy = index;//[self.filterCategories[2][@"options"][index] integerValue];
[self.delegate addItemViewController:self setSortBy:sortBy];
//[defaults setObject:selectedStr forKey:@"Sort By"];
index = [expandedCategories[@"Categories"][@"selected"] intValue];
NSString *categoryStr = self.filterCategories[4][@"options"][index];
//[defaults setObject:selectedStr forKey:@"Categories"];
[self.delegate addItemViewController:self setCategory:categoryStr];
//[defaults synchronize];
}
#pragma mark - ToggleSwitchCellDelegate methods
-(void)sender:(ToggleSwitchCell *)sender didChangeValue:(BOOL)value
{
NSLog(@"cell %@ switched to : %d", sender.switchName.text, value);
NSIndexPath *indexPath = [self.filtersTableView indexPathForCell:sender];
[selectedCategories setObject:@(value) forKey:sender.switchName.text];
[self.filtersTableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationNone];
}
@end