-
Notifications
You must be signed in to change notification settings - Fork 1
/
EventEditorViewController.m
553 lines (414 loc) · 15.6 KB
/
EventEditorViewController.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
//
// EventEditorViewController.m
// MilesTracker
//
// Created by Mike Glazer on 3/14/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import <RoutingRequest.h>
#import "EventEditNameViewController.h"
#import "EventEditorViewController.h"
#import "LocationSelectionViewController.h"
#import "Event.h"
#import "Tag.h"
#import "MilesTracker.h"
#import "MilesTrackerAppDelegate.h"
#import "UnitConverter.h"
#import "DistancePickerViewController.h"
#import "TagSelectionController.h"
#import "RoutingRequestAdditions.h"
#import <TokenManager.h>
#import <JsonRoutingParser.h>
@implementation EventEditorViewController
@synthesize event;
@synthesize editingStartLocation;
@synthesize tableView;
@synthesize appDelegate;
/*
- (id)initWithStyle:(UITableViewStyle)style {
// Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
if (self = [super initWithStyle:style]) {
}
return self;
}
*/
- (void)viewDidLoad {
[super viewDidLoad];
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
UIBarButtonItem *cancelButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"History" style:UIBarButtonItemStyleBordered target:self action:@selector(cancel)];
self.navigationItem.backBarButtonItem = cancelButtonItem;
UIBarButtonItem *acceptButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Save" style:UIBarButtonItemStyleDone target:self action:@selector(save)];
self.navigationItem.rightBarButtonItem = acceptButtonItem;
tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]
style:UITableViewStyleGrouped];
tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
tableView.delegate = self;
tableView.dataSource = self;
self.navigationItem.title = self.event.name;
[tableView reloadData];
self.view = tableView;
tokenManager_ = [[MilesTracker sharedInstance] tokenManager];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[tableView reloadData];
}
/*
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
appDelegate = nil;
}
#pragma mark Table view methods
enum TableViewSections {
SECTION_DISTANCE = 0,
SECTION_NAME_START_END = 1,
SECTION_DATE_TAGS = 2,
SECTION_COUNT = 3
};
enum DistanceSectionRows {
SECTION_DISTANCE_DISTANCE = 0,
SECTION_DISTANCE_COUNT = 1
};
enum NameStartEndSectionRows {
SECTION_NAME_START_END_NAME = 0,
SECTION_NAME_START_END_START = 1,
SECTION_NAME_START_END_END = 2,
SECTION_NAME_START_END_COUNT = 3
};
enum SectionDateTagsRows {
SECTION_DATE_TAGS_DATE = 0,
SECTION_DATE_TAGS_TAGS = 1,
SECTION_DATE_TAGS_COUNT = 2
};
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return SECTION_COUNT;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
switch ( section ) {
case SECTION_DISTANCE:
return SECTION_DISTANCE_COUNT;
case SECTION_NAME_START_END:
return SECTION_NAME_START_END_COUNT;
case SECTION_DATE_TAGS:
return SECTION_DATE_TAGS_COUNT;
}
return 0;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
switch ( indexPath.section )
{
case SECTION_DISTANCE:
cell = [self cellForDistanceSection:cell withRow:indexPath.row];
break;
case SECTION_NAME_START_END:
cell = [self cellForNameSection:cell withRow:indexPath.row];
break;
case SECTION_DATE_TAGS:
cell = [self cellForDateSection:cell withRow:indexPath.row];
break;
}
return cell;
}
- (UITableViewCell*)cellForDistanceSection:(UITableViewCell*)cell withRow:(NSUInteger)row {
NSNumber* localeDistance = [[appDelegate unitConverter] distanceToLocale:event.distance];
[cell detailTextLabel].text = [NSString stringWithFormat:@"%.1f %@", [localeDistance floatValue],
[[appDelegate unitConverter] localeDistanceString]];
[cell textLabel].text = @"Distance";
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
return cell;
}
- (UITableViewCell*)cellForNameSection:(UITableViewCell*)cell withRow:(NSUInteger)row {
switch ( row ) {
case SECTION_NAME_START_END_NAME:
[cell detailTextLabel].text = event.name;
[cell textLabel].text = @"Name";
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
break;
case SECTION_NAME_START_END_START:
[cell detailTextLabel].text = event.startLocationDescription;
[cell textLabel].text = @"Start";
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
break;
case SECTION_NAME_START_END_END:
[cell detailTextLabel].text = event.endLocationDescription;
[cell textLabel].text = @"End";
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
break;
}
return cell;
}
- (NSString*)textForTagsCell:(NSSet*)tags {
Tag* tag = nil;
NSEnumerator* enumerator = [tags objectEnumerator];
NSString* result = @"";
NSMutableString* buffer = [NSMutableString string];
while ( (tag = [enumerator nextObject]) ) {
[buffer appendString:tag.name];
[buffer appendString:@", "];
}
// Delete final comma+space from the string
if (![buffer isEqualToString:@""])
{
[buffer deleteCharactersInRange:NSMakeRange(
[buffer length]-2, 2)];
result = [NSString stringWithString:buffer];
}
return result;
}
- (UITableViewCell*)cellForDateSection:(UITableViewCell*)cell withRow:(NSUInteger)row {
NSDateFormatter* formatter = [MilesTracker createDateFormatter];
switch ( row ) {
case SECTION_DATE_TAGS_DATE:
[cell detailTextLabel].text = [formatter stringFromDate:event.timeStamp];
[cell textLabel].text = @"Date";
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
break;
case SECTION_DATE_TAGS_TAGS:
[cell detailTextLabel].text = [self textForTagsCell:event.tags];
[cell textLabel].text = @"Tags";
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
break;
}
[formatter release];
return cell;
}
- (void)didSelectRowInDistanceSection:(NSUInteger)row {
if ( row != 0 ) {
return;
}
DistancePickerViewController* controller = [[DistancePickerViewController alloc] init];
controller.distance = [[appDelegate unitConverter] distanceToLocale:event.distance];
controller.delegate = self;
[self.navigationController pushViewController:controller animated:YES];
[controller release];
}
- (void)didSelectRowInNameSection:(NSUInteger)row {
if ( row == SECTION_NAME_START_END_NAME ) {
EventEditNameViewController* editController = [[EventEditNameViewController alloc] initWithNibName:@"EventAddView" bundle:nil];
editController.delegate = self;
editController.name = event.name;
editController.title = @"Edit Event Name";
[self.navigationController pushViewController:editController animated:YES];
} else {
LocationSelectionViewController* nextController = [[LocationSelectionViewController alloc] initWithNibName:@"LocationSelectionView" bundle:nil];
nextController.delegate = self;
NSString* existingAddress;
CLLocationCoordinate2D existingCoordinate;
if ( row == SECTION_NAME_START_END_START ) {
existingAddress = event.startLocationDescription;
existingCoordinate.latitude = [event.startLocationLat floatValue];
existingCoordinate.longitude = [event.startLocationLon floatValue];
} else {
existingAddress = event.endLocationDescription;
existingCoordinate.latitude = [event.endLocationLat floatValue];
existingCoordinate.longitude = [event.endLocationLon floatValue];
}
nextController.existingAddress = existingAddress;
nextController.existingCoordinate = existingCoordinate;
self.editingStartLocation = (row == SECTION_NAME_START_END_START);
UINavigationController* baseController = [[UINavigationController alloc] initWithRootViewController:nextController];
//[self.navigationController pushViewController:nextController animated:YES];
[self presentModalViewController:baseController animated:YES];
[nextController release];
[baseController release];
}
}
- (void)didSelectRowInDateSection:(NSUInteger)row {
if ( row == SECTION_DATE_TAGS_DATE ) {
DatePickerViewController* controller = [[DatePickerViewController alloc] initWithNibName:@"DatePickerView" bundle:nil];
controller.delegate = self;
controller.date = event.timeStamp;
[self.navigationController pushViewController:controller animated:YES];
[controller release];
} else {
TagSelectionController* controller = [[TagSelectionController alloc] initWithStyle:UITableViewStyleGrouped];
controller.event = event;
[self.navigationController pushViewController:controller animated:YES];
[controller release];
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
switch ( indexPath.section ) {
case SECTION_DISTANCE:
[self didSelectRowInDistanceSection:indexPath.row];
break;
case SECTION_NAME_START_END:
[self didSelectRowInNameSection:indexPath.row];
break;
case SECTION_DATE_TAGS:
[self didSelectRowInDateSection:indexPath.row];
break;
}
// Navigation logic may go here. Create and push another view controller.
// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
// [self.navigationController pushViewController:anotherViewController];
// [anotherViewController release];
}
#pragma mark -
#pragma mark LocationSelectionViewDelegate methods
- (bool)eventHasStartAndEndPositionsDefined {
return self.event.startLocationLat != nil && self.event.startLocationLon != nil &&
self.event.endLocationLat != nil && self.event.endLocationLon != nil;
}
- (void)updateDistanceFromEvent {
CLLocationCoordinate2D startLocation;
startLocation.latitude = [self.event.startLocationLat doubleValue];
startLocation.longitude = [self.event.startLocationLon doubleValue];
CLLocationCoordinate2D endLocation;
endLocation.latitude = [self.event.endLocationLat doubleValue];
endLocation.longitude = [self.event.endLocationLon doubleValue];
RoutingRequest* request = [MilesTracker createRoutingRequest];
request.delegate = self;
[request findRoute:startLocation to:endLocation vehicle:@"car" tokenManager:tokenManager_];
[request release];
}
- (void)didAcceptLocationSelection:(CLLocationCoordinate2D)coordinate withAddress:(NSString*)name {
//[self dismissModalViewControllerAnimated:YES];
NSLog(@"User selected location %f:%f at %@", coordinate.latitude, coordinate.longitude, name);
if ( self.editingStartLocation ) {
self.event.startLocationLat = [NSNumber numberWithDouble:coordinate.latitude];
self.event.startLocationLon = [NSNumber numberWithDouble:coordinate.longitude];
self.event.startLocationDescription = name;
} else {
self.event.endLocationLat = [NSNumber numberWithDouble:coordinate.latitude];
self.event.endLocationLon = [NSNumber numberWithDouble:coordinate.longitude];
self.event.endLocationDescription = name;
}
[self dismissModalViewControllerAnimated:YES];
if ( [self eventHasStartAndEndPositionsDefined] ) {
[self updateDistanceFromEvent];
}
}
- (bool)isStartingPoint {
return self.editingStartLocation;
}
- (void)didCancelLocationSelection {
[self dismissModalViewControllerAnimated:YES];
}
#pragma mark -
#pragma mark EventNameDelegate
- (void)eventEditNameViewController:(EventEditNameViewController*)source didSaveName:(NSString*)name {
if ( name != nil ) {
event.name = name;
}
[self.navigationController popViewControllerAnimated:YES];
}
#pragma mark -
#pragma mark DistancePickerViewControllerDelegate
- (void)didSaveDistance:(NSNumber*)distance {
if ( distance != nil ) {
event.distance = [[appDelegate unitConverter] distanceFromLocale:distance];
}
[self.navigationController popViewControllerAnimated:YES];
}
#pragma mark -
#pragma mark ServiceRequestResult
- (void) serviceServerResponse:(NSString *)jsonResponse {
JsonRoutingParser* parser = [[JsonRoutingParser alloc] init];
if ( [parser responceStatus:jsonResponse] ) {
RouteSummary* result = [parser routeSummury:jsonResponse];
event.distance = [NSNumber numberWithFloat:(float)result.totalDistance];
[self.tableView reloadData];
[result release];
}
[parser release];
}
- (void) serviceServerError:(NSString *)error {
NSLog(@"Error: %@", error);
}
#pragma mark -
#pragma mark Saving and cancelling
- (void)save {
NSError* error;
if ( ![event.managedObjectContext save:&error] ) {
// TODO: Replace with better error handling code
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
[self.navigationController popViewControllerAnimated:YES];
}
- (void)cancel {
[self.navigationController popViewControllerAnimated:YES];
}
#pragma mark -
#pragma mark DatePickerViewControllerDelegate
-(void) datePickerAcceptedDateSelection:(NSDate*)date {
event.timeStamp = date;
[self.navigationController popViewControllerAnimated:YES];
}
-(void) datePickerCancelDateSelection {
[self.navigationController popViewControllerAnimated:YES];
}
/*
// 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:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
}
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;
}
*/
- (void)dealloc {
[event release];
[tableView release];
[super dealloc];
}
@end