-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilterViewController.m
More file actions
executable file
·347 lines (292 loc) · 12.6 KB
/
FilterViewController.m
File metadata and controls
executable file
·347 lines (292 loc) · 12.6 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
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
//
// FilterViewController.m
// Junction
//
// Created by Bobby Ren on 3/5/13.
//
//
#import "FilterViewController.h"
#import "AppDelegate.h"
@interface FilterViewController ()
@end
static AppDelegate * appDelegate;
@implementation FilterViewController
@synthesize labelTitle, buttonFilter, tableView;
@synthesize viewsForCell;
@synthesize companyFilter, industryFilter, friendsFilter;
@synthesize companyField, industryField, friendsSwitch;
@synthesize delegate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.viewsForCell = [[NSMutableArray alloc] init];
for (int i=0; i<15; i++) {
[self.viewsForCell addObject:[NSNull null]];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)didClickFilter:(id)sender {
NSLog(@"Did click filter");
[self.companyField resignFirstResponder];
[self.positionField resignFirstResponder];
int results = [self.delegate doFilter];
if (results > 0)
[self.delegate closeFilter];
}
-(IBAction)didClickClearAll:(id)sender {
[self.companyField resignFirstResponder];
self.industryFilter = nil;
self.companyFilter = nil;
self.positionFilter = nil;
self.friendsFilter = NO;
self.industryField.text = @"";
self.companyField.text = @"";
self.positionField.text = @"";
[self.friendsSwitch setOn:NO];
[buttonClearPosition setHidden:YES];
[buttonClearCompany setHidden:YES];
[self.delegate doFilter];
}
-(IBAction)didClickClear:(id)sender {
if ((UIButton*)sender == buttonClearCompany) {
self.companyField.text = @"";
self.companyFilter = nil;
[self.delegate doFilter];
[buttonClearCompany setHidden:YES];
}
else if ((UIButton*)sender == buttonClearPosition) {
self.positionField.text = @"";
self.positionFilter = nil;
[self.delegate doFilter];
[buttonClearPosition setHidden:YES];
}
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0)
return 3;
if (section == 1)
return 1;
return 0;
}
-(UIView*)viewForItemAtIndexPath:(NSIndexPath *)indexPath {
int section = indexPath.section;
int row = indexPath.row;
int index;
if (section == 0) {
if (row == INPUT_FILTER_INDUSTRY && [self.viewsForCell objectAtIndex:INPUT_FILTER_INDUSTRY] != [NSNull null]) {
return [self.viewsForCell objectAtIndex:INPUT_FILTER_INDUSTRY];
}
if (row == INPUT_FILTER_COMPANY && [self.viewsForCell objectAtIndex:INPUT_FILTER_COMPANY] != [NSNull null]) {
return [self.viewsForCell objectAtIndex:INPUT_FILTER_COMPANY];
}
if (row == INPUT_FILTER_POSITION && [self.viewsForCell objectAtIndex:INPUT_FILTER_POSITION] != [NSNull null]) {
return [self.viewsForCell objectAtIndex:INPUT_FILTER_POSITION];
}
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(20, 0, 300, FILTER_ROW_HEIGHT)];
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 120, FILTER_ROW_HEIGHT)];
[label setFont:[UIFont fontWithName:@"SansusWebissimo" size:14]];
[label setTextColor:[UIColor colorWithRed:105.0/255 green:200.0/255 blue:255.0/255 alpha:1]];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextAlignment:NSTextAlignmentLeft];
if (row == INPUT_FILTER_INDUSTRY) {
UIImageView * forward = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"btn-field-arrow"]];
[forward setFrame:CGRectMake(265, 5, 17, 25)];
[view addSubview:forward];
UITextField * inputField = [[UITextField alloc] initWithFrame:CGRectMake(125, 0, 200, FILTER_ROW_HEIGHT)];
[inputField setTextAlignment:NSTextAlignmentLeft];
[inputField setFont:[UIFont systemFontOfSize:15]];
inputField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[inputField setKeyboardType:UIKeyboardTypeAlphabet];
[inputField setUserInteractionEnabled:NO];
[inputField setClearButtonMode:UITextFieldViewModeAlways];
if ([self.industryFilter length])
inputField.text = self.industryFilter;
[view addSubview:inputField];
self.industryField = inputField;
[label setText:@"INDUSTRY"];
index = INPUT_FILTER_INDUSTRY;
}
else if (row == INPUT_FILTER_COMPANY) {
UITextField * inputField = [[UITextField alloc] initWithFrame:CGRectMake(125, 0, 200, FILTER_ROW_HEIGHT)];
[inputField setTextAlignment:NSTextAlignmentLeft];
[inputField setFont:[UIFont systemFontOfSize:15]];
inputField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[inputField setKeyboardType:UIKeyboardTypeAlphabet];
[inputField setPlaceholder:@"Type company name"];
[inputField setDelegate:self];
[inputField setClearButtonMode:UITextFieldViewModeAlways];
if ([self.companyFilter length])
inputField.text = self.companyFilter;
[view addSubview:inputField];
[buttonClearCompany setFrame:CGRectMake(265, 8, 20, 20)];
[buttonClearCompany setHidden:YES];
[view addSubview:buttonClearCompany];
self.companyField = inputField;
[label setText:@"COMPANY"];
index = INPUT_FILTER_COMPANY;
}
else if (row == INPUT_FILTER_POSITION) {
UITextField * inputField = [[UITextField alloc] initWithFrame:CGRectMake(125, 0, 200, FILTER_ROW_HEIGHT)];
[inputField setTextAlignment:NSTextAlignmentLeft];
[inputField setFont:[UIFont systemFontOfSize:15]];
inputField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[inputField setKeyboardType:UIKeyboardTypeAlphabet];
[inputField setPlaceholder:@"e.g. Product Manager"];
[inputField setDelegate:self];
[inputField setClearButtonMode:UITextFieldViewModeAlways];
if ([self.positionFilter length])
inputField.text = self.positionFilter;
[view addSubview:inputField];
self.positionField = inputField;
[buttonClearPosition setFrame:CGRectMake(265, 8, 20, 20)];
[buttonClearPosition setHidden:YES];
[view addSubview:buttonClearPosition];
[label setText:@"POSITION"];
index = INPUT_FILTER_POSITION;
}
[view addSubview:label];
[self.viewsForCell replaceObjectAtIndex:index withObject:view];
return [self.viewsForCell objectAtIndex:index];
}
else if (section == 1) {
if (row == 0 && [self.viewsForCell objectAtIndex:INPUT_FILTER_FRIENDS] != [NSNull null]) {
return [self.viewsForCell objectAtIndex:INPUT_FILTER_FRIENDS];
}
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, FILTER_ROW_HEIGHT)];
[label setFont:[UIFont fontWithName:@"SansusWebissimo" size:14]];
[label setTextColor:[UIColor colorWithRed:105.0/255 green:200.0/255 blue:255.0/255 alpha:1]];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextAlignment:NSTextAlignmentLeft];
[label setText:@"FRIENDS IN COMMON"];
UISwitch * toggle = [[UISwitch alloc] init];
[toggle setFrame:CGRectMake(200, 5, 120, FILTER_ROW_HEIGHT-10)];
[toggle addTarget:self action:@selector(didToggleFriendsFilter:) forControlEvents:UIControlEventValueChanged];
if (self.friendsFilter)
[toggle setOn:YES];
self.friendsSwitch = toggle;
index = INPUT_FILTER_FRIENDS;
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(20, 0, 300, FILTER_ROW_HEIGHT)];
[view addSubview:label];
[view addSubview:toggle];
[self.viewsForCell replaceObjectAtIndex:index withObject:view];
return [self.viewsForCell objectAtIndex:index];
}
return nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell * cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.textLabel.numberOfLines = 1;
[cell setBackgroundColor:[UIColor whiteColor]];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
// Configure the cell...
// for (UIView * subview in cell.subviews)
// [subview removeFromSuperview];
[cell addSubview:[self viewForItemAtIndexPath:indexPath]];
/*
if (indexPath.row == INPUT_FILTER_COMPANY) {
// [buttonClearCompany setFrame:CGRectMake(265, 5, 20, 20)];
[buttonClearCompany setHidden:YES];
cell.accessoryView = buttonClearCompany;
}
else if (indexPath.row == INPUT_FILTER_POSITION) {
// [buttonClearPosition setFrame:CGRectMake(265, 5, 20, 20)];
[buttonClearPosition setHidden:YES];
cell.accessoryView = buttonClearPosition;
}
*/
return cell;
}
-(void)didToggleFriendsFilter:(id)sender {
[self.companyField resignFirstResponder];
self.friendsFilter = !self.friendsFilter;
// [[NSNotificationCenter defaultCenter] postNotificationName:kFilterChanged object:self userInfo:nil];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
int section = indexPath.section;
int row = indexPath.row;
if (section == 0) {
if (row == INPUT_FILTER_INDUSTRY) {
// INDUSTRY filter
IndustryFilterTableViewController * industryFilterTable = [[IndustryFilterTableViewController alloc] init];
[industryFilterTable setDelegate:self];
//[self presentModalViewController:industryFilterTable animated:YES];
[appDelegate.window.rootViewController presentModalViewController:industryFilterTable animated:YES];
}
}
}
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == INPUT_FILTER_COMPANY) {
NSLog(@"Company!");
}
else if (indexPath.row == INPUT_FILTER_POSITION) {
NSLog(@"position!");
}
}
-(void)didSelectIndustryFilter:(NSString *)industry {
//[self dismissModalViewControllerAnimated:YES];
[appDelegate.window.rootViewController dismissModalViewControllerAnimated:YES];
NSLog(@"Filter selected: %@", industry);
self.industryFilter = industry;
[self.industryField setText:self.industryFilter];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return FILTER_ROW_HEIGHT;
}
#pragma mark UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
// text field must also have delegate set as file's owner
[textField resignFirstResponder];
if (textField == self.positionField)
[self.companyField becomeFirstResponder];
return YES;
}
-(void)textFieldDidEndEditing:(UITextField *)textField {
self.companyFilter = self.companyField.text;
self.positionFilter = self.positionField.text;
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ([string length] > 0) {
if (textField == self.companyField)
[buttonClearCompany setHidden:NO];
else if (textField == self.positionField)
[buttonClearPosition setHidden:NO];
}
return YES;
}
#pragma mark no reslts found
-(void)showEmptyResultsMessage {
[self.view addSubview:noResultsView];
}
-(IBAction)didClickEmptyResultsButton:(id)sender {
[self didClickClearAll:nil];
[noResultsView removeFromSuperview];
}
@end