This repository has been archived by the owner on Mar 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WMClubFormViewController.m
411 lines (345 loc) · 14.8 KB
/
WMClubFormViewController.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//
// WMClubFormViewController.m
// WhiteMochaUP
//
// Created by John Liedtke on 8/15/13.
//
//
#import "WMClubFormViewController.h"
#import "WMConstants.h"
@interface WMClubFormViewController ()
@end
@implementation WMClubFormViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Hide the keyboard when pressed outside text field
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
[self.tableView addGestureRecognizer:gestureRecognizer];
gestureRecognizer.cancelsTouchesInView = NO;
// Title
[self setTitle:@"Add Event"];
// Find out the path of clubs.plist
NSString *path = [[NSBundle mainBundle] pathForResource:@"clubs" ofType:@"plist"];
clubs = [[NSDictionary alloc] initWithContentsOfFile:path];
clubType = [[NSArray alloc] initWithObjects:@"Meeting", @"Event", nil];
// Init keyboard crap
NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"WMPrevNext" owner:self options:nil];
prevNext = [subviewArray objectAtIndex:0];
[prevNext setPrevNextDelegate:self];
NSMutableArray *fields = [[NSMutableArray alloc] initWithObjects:clubField, dateField, locationField, typeField, nil];
[prevNext setFields:fields];
for (UITextField *tf in fields) [tf setInputAccessoryView:prevNext];
// Club picker
clubPicker = [[UIPickerView alloc] init];
[clubPicker setDataSource:self];
[clubPicker setDelegate:self];
[clubPicker setShowsSelectionIndicator:YES];
[clubField setInputView:clubPicker];
// Club Type picker
clubTypePicker = [[UIPickerView alloc] init];
[clubTypePicker setDataSource:self];
[clubTypePicker setDelegate:self];
[clubTypePicker setShowsSelectionIndicator:YES];
[typeField setInputView:clubTypePicker];
[typeField setText:clubType[[clubTypePicker selectedRowInComponent:0]]];
// Date Picker
datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:@selector(datePickerValueChanged) forControlEvents:UIControlEventValueChanged];
[datePicker setDatePickerMode:UIDatePickerModeDateAndTime];
[dateField setInputView:datePicker];
[postLabel setFont:[UIFont fontWithName:@"Museo Slab" size:17]];
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 30)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15,5, 100, 30)];
[headerView addSubview:label];
[label setFont:[UIFont fontWithName:@"Museo Slab" size:18]];
[label setBackgroundColor:[UIColor clearColor]];
switch (section) {
case 0:
[label setText:@"Details"];
break;
case 1:
[label setFrame:CGRectMake(15,-5, 100, 30)];
[label setText:@"Other info"];
break;
default:
break;
}
return headerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{ if (section == 0)
return 35;
else if (section == 1)
return 30;
return 0;
}
// Pickers
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
if (pickerView == clubPicker) return 2;
if (pickerView == clubTypePicker) return 1;
return 1;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (pickerView == clubPicker) {
if (component == 0) {
switch (row) {
case 0:
return @"Academic Clubs";
break;
case 1:
return @"Club Sports";
break;
case 2:
return @"Multicultural Clubs";
break;
case 3:
return @"Special Interest Groups";
break;
}
} else if (component == 1){
switch ([clubPicker selectedRowInComponent:0]) {
case 0:
return [[[clubs objectForKey:@"academic"] objectAtIndex:row] objectForKey:@"name"];
break;
case 1:
return [[[clubs objectForKey:@"clubSports"] objectAtIndex:row] objectForKey:@"name"];
break;
case 2:
return [[[clubs objectForKey:@"diversity"] objectAtIndex:row] objectForKey:@"name"];
break;
case 3:
return [[[clubs objectForKey:@"special"] objectAtIndex:row] objectForKey:@"name"];
break;
}
}
} else if (pickerView == clubTypePicker) {
return clubType[row];
}
return @"meow";
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (pickerView == clubPicker) {
if (component == 0) {
return 4;
}
// switch ([clubPicker selectedRowInComponent:0]) {
// case 0:
// return [[clubs objectForKey:@"academic"] count];
// break;
// case 1:
// return [[clubs objectForKey:@"clubSports"] count];
// break;
// case 2:
// return [[clubs objectForKey:@"diversity"] count];
// break;
// case 3:
// return [[clubs objectForKey:@"special"] count];
// break;
// }
return 30;
} else if (pickerView == clubTypePicker) {
return 2;
}
return 0;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (pickerView == clubPicker) {
if (component == 0) {
[clubPicker selectRow:0 inComponent:1 animated:YES];
[clubPicker reloadComponent:1];
}
if (component == 1) {
if ([clubPicker selectedRowInComponent:0] == 0 && row >[[clubs objectForKey:@"academic"] count]-1) {
[clubPicker selectRow:[[clubs objectForKey:@"academic"] count]-1 inComponent:1 animated:NO]; return;
} else if ([clubPicker selectedRowInComponent:0] == 1 && row >[[clubs objectForKey:@"clubSports"] count]-1) {
[clubPicker selectRow:[[clubs objectForKey:@"clubSports"] count]-1 inComponent:1 animated:NO]; return;
} else if ([clubPicker selectedRowInComponent:0] == 2 && row >[[clubs objectForKey:@"diversity"] count]-1) {
[clubPicker selectRow:[[clubs objectForKey:@"diversity"] count]-1 inComponent:1 animated:NO]; return;
} else if ([clubPicker selectedRowInComponent:0] == 3 && row >[[clubs objectForKey:@"special"] count]-1) {
[clubPicker selectRow:[[clubs objectForKey:@"special"] count]-1 inComponent:1 animated:NO]; return;
}
[clubField setText:[[[clubs objectForKey:[self categoryKey]] objectAtIndex:row] objectForKey:@"name"]];
}
} else if (pickerView == clubTypePicker) {
[typeField setText:clubType[row]];
}
}
- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (pickerView == clubPicker) {
NSMutableAttributedString *attributedString;
if (component == 0) {
switch (row) {
case 0:
attributedString = [[NSMutableAttributedString alloc] initWithString:@"Academic Clubs"];
break;
case 1:
attributedString = [[NSMutableAttributedString alloc] initWithString:@"Club Sports"];
break;
case 2:
attributedString = [[NSMutableAttributedString alloc] initWithString:@"Multicultural Clubs"];
break;
case 3:
attributedString = [[NSMutableAttributedString alloc] initWithString:@"Special Interests"];
break;
}
} else if (component == 1){
if ([clubPicker selectedRowInComponent:0] == 0 && row >[[clubs objectForKey:@"academic"] count]-1) {
attributedString = [[NSMutableAttributedString alloc] initWithString:@""];
return attributedString;
} else if ([clubPicker selectedRowInComponent:0] == 1 && row >[[clubs objectForKey:@"clubSports"] count]-1) {
attributedString = [[NSMutableAttributedString alloc] initWithString:@""];
return attributedString;
} else if ([clubPicker selectedRowInComponent:0] == 2 && row >[[clubs objectForKey:@"diversity"] count]-1) {
attributedString = [[NSMutableAttributedString alloc] initWithString:@""];
return attributedString;
} else if ([clubPicker selectedRowInComponent:0] == 3 && row >[[clubs objectForKey:@"special"] count]-1) {
attributedString = [[NSMutableAttributedString alloc] initWithString:@""];
return attributedString;
}
switch ([clubPicker selectedRowInComponent:0]) {
case 0:
attributedString = [[NSMutableAttributedString alloc] initWithString:[[[clubs objectForKey:@"academic"] objectAtIndex:row] objectForKey:@"name"]];
break;
case 1:
attributedString = [[NSMutableAttributedString alloc] initWithString:[[[clubs objectForKey:@"clubSports"] objectAtIndex:row] objectForKey:@"name"]];
break;
case 2:
attributedString = [[NSMutableAttributedString alloc] initWithString:[[[clubs objectForKey:@"diversity"] objectAtIndex:row] objectForKey:@"name"]];
break;
case 3:
attributedString = [[NSMutableAttributedString alloc] initWithString:[[[clubs objectForKey:@"special"] objectAtIndex:row] objectForKey:@"name"]];
break;
default:
break;
}
}
if (component == 0)
[attributedString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:12] range:NSMakeRange(0, attributedString.length)];
else
[attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:10] range:NSMakeRange(0, attributedString.length)];
return attributedString;
}
return nil;
}
- (void)datePickerValueChanged
{
NSDate *departDate = [datePicker date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MMMM d, h:mm a"];
NSString *theDate = [dateFormatter stringFromDate:departDate];
[dateField setText:theDate];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[prevNext setUp:textField];
}
- (void)donePressed
{
[self hideKeyboard];
}
-(void)prevNextPressed:(UITextField *)textField
{
NSIndexPath *index = [NSIndexPath indexPathForRow:[prevNext currentIndex] inSection:0];
[[self tableView] scrollToRowAtIndexPath:index atScrollPosition:UITableViewScrollPositionMiddle
animated:YES];
[textField becomeFirstResponder];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/* Limit the number of characters for different fields */
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSUInteger newLength = [[textField text] length] + [string length] - range.length;
if (textField == locationField) {
return !(newLength > 45);
}
return YES;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([indexPath section] == 2) { // List Item
// Check for some errors!
NSMutableString *errors = [[NSMutableString alloc] initWithFormat:@""];
if ([locationField text].length < 1) {
[errors appendString:@"Enter a location. "];
}
if ([clubField text]) {
}
if ([detailsTextView text].length < 5) {
[errors appendString:@"Add some details!"];
}
if ([errors isEqualToString:@""]) {
PFObject *club = [PFObject objectWithClassName:@"WMEvent"];
// Set stuff
// Event type
[club setObject:CLUBS forKey:@"eventType"];
// The club
[club setObject:[[[clubs objectForKey:[self categoryKey]] objectAtIndex:[clubPicker selectedRowInComponent:1]] objectForKey:@"name"] forKey:@"club"];
// Club category
[club setObject:[self categoryKey] forKey:@"clubCategory"];
[club setObject:[datePicker date] forKey:@"eventDate"];
[club setObject:[locationField text] forKey:@"location"];
[club setObject:[detailsTextView text] forKey:@"details"];
[club setObject:[PFUser currentUser] forKey:@"user"];
[club setObject:[typeField text] forKey:@"meeting"];
PFACL *listingACL = [PFACL ACLWithUser:[PFUser currentUser]];
[listingACL setPublicReadAccess:YES];
[listingACL setPublicWriteAccess:NO];
[club setACL:listingACL];
[club saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
UIAlertView *av;
if (succeeded) {
[[self addEventDelegate] eventAdded];
[[self navigationController] popToRootViewControllerAnimated:YES];
} else {
av = [[UIAlertView alloc] initWithTitle:@"Error" message:[error description] delegate:self cancelButtonTitle:@"Okay" otherButtonTitles: nil];
[av show];
}
}];
[[self navigationController] popToRootViewControllerAnimated:YES];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!" message:errors delegate:self cancelButtonTitle:@"OKAY" otherButtonTitles:nil];
[alert show];
}
}
}
- (NSString *)categoryKey
{
int firstRow = [clubPicker selectedRowInComponent:0];
switch (firstRow) {
case 0:
return @"academic";
case 1:
return @"clubSports";
case 2:
return @"diversity";
case 3:
return @"special";
}
return nil;
}
// Workaround to hide keyboard when Done is tapped
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
- (void)hideKeyboard
{
[self.view endEditing:YES];
}
@end