-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClientDetailViewController.m
346 lines (267 loc) · 10.5 KB
/
ClientDetailViewController.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
//
// ClientDetailViewController.m
// TimeClock
//
// Created by Matthew Baker on 10/9/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "ClientDetailViewController.h"
#import "EditingViewController.h"
#import "TimeEntry.h"
@implementation ClientDetailViewController
@synthesize client, dateFormatter, undoManager;
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
// Configure the title, title bar, and table view.
self.title = @"Info";
self.navigationItem.rightBarButtonItem = self.editButtonItem;
self.tableView.allowsSelectionDuringEditing = YES;
}
- (void)viewWillAppear:(BOOL)animated {
// Redisplay the data.
[self.tableView reloadData];
[self updateRightBarButtonItemState];
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
// Hide the back button when editing starts, and show it again when editing finishes.
[self.navigationItem setHidesBackButton:editing animated:animated];
[self.tableView reloadData];
/*
When editing starts, create and set an undo manager to track edits. Then register as an observer of undo manager change notifications, so that if an undo or redo operation is performed, the table view can be reloaded.
When editing ends, de-register from the notification center and remove the undo manager, and save the changes.
*/
if (editing) {
[self setUpUndoManager];
}
else {
[self cleanUpUndoManager];
// Save the changes.
NSError *error;
if (![client.managedObjectContext save:&error]) {
// Update to handle the error appropriately.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
exit(-1); // Fail
}
}
}
- (void)viewDidUnload {
// Release any properties that are loaded in viewDidLoad or can be recreated lazily.
self.dateFormatter = nil;
}
- (void)updateRightBarButtonItemState {
// Conditionally enable the right bar button item -- it should only be enabled if the book is in a valid state for saving.
self.navigationItem.rightBarButtonItem.enabled = [client validateForUpdate:NULL];
}
#pragma mark -
#pragma mark Table view data source methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if ([client.entries count] > 0) return 2;
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// 4 rows
if (section == 0)
return 4;
return [client.entries count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section == 0) return @"Details";
return @"Shifts";
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"Index path row: %d", indexPath.row);
switch (indexPath.section) {
case 0:
return [self configureDetailsCell:tableView atIndexPath:indexPath];
break;
case 1:
return [self configureTimeCell:tableView atIndexPath:indexPath];
break;
default:
break;
}
return nil;
}
- (UITableViewCell *)configureDetailsCell:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"DetailCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease];
cell.editingAccessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
switch (indexPath.row) {
case 0:
cell.textLabel.text = @"Name";
cell.detailTextLabel.text = client.name;
break;
case 1:
cell.textLabel.text = @"Rate";
cell.detailTextLabel.text = client.rate == nil ? @"" : [NSString stringWithFormat:@"$%@", client.rate];
break;
case 2:
cell.textLabel.text = @"POC Name";
cell.detailTextLabel.text = client.pocName;
break;
case 3:
cell.textLabel.text = @"POC Phone";
cell.detailTextLabel.text = client.pocPhone;
break;
default:
break;
}
return cell;
}
- (UITableViewCell *)configureTimeCell:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"TimeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
// A date formatter for the time stamp
NSDateFormatter *shortDate = [[NSDateFormatter alloc] init];
[shortDate setTimeStyle:NSDateFormatterMediumStyle];
[shortDate setDateStyle:NSDateFormatterShortStyle];
// A number formatter for the latitude and longitude
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setMaximumFractionDigits:2];
TimeEntry *entry = [[client.entries allObjects] objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:@"%@-%@",
[shortDate stringFromDate:[entry inTime]],
[entry outTime] == nil ? @"" : [shortDate stringFromDate:[entry outTime]]];
NSString *string = [NSString stringWithFormat:@"%@, %@hrs",
entry.client.name,
[numberFormatter stringFromNumber:[entry totalTime]]];
cell.detailTextLabel.text = string;
[numberFormatter release];
[shortDate release];
return cell;
}
- (NSIndexPath *)tableView:(UITableView *)tv willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Only allow selection if editing or if you select the POC Phone cell
return (self.editing || (indexPath.row == 3 && indexPath.section == 0)) ? indexPath : nil;
}
/**
Manage row selection: If a row is selected, create a new editing view controller to edit the property associated with the selected row.
*/
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"Cell selected");
if (!self.editing) {
if (indexPath.section == 0 && indexPath.row == 3) {
NSString *number = [NSString stringWithFormat:@"tel://%@", client.pocPhone];
NSLog(@"Calling %@", number);
BOOL success = [[UIApplication sharedApplication] openURL:[NSURL URLWithString:number]];
NSLog(@"Success: %d", success);
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
return;
}
EditingViewController *controller = [[EditingViewController alloc] initWithNibName:@"EditingView" bundle:nil];
controller.editedObject = client;
switch (indexPath.row) {
case 0: {
controller.editedFieldKey = @"name";
controller.editedFieldName = NSLocalizedString(@"clientName", @"display name for client");
controller.editType = EditingViewControllerTypeString;
} break;
case 1: {
controller.editedFieldKey = @"rate";
controller.editedFieldName = NSLocalizedString(@"clientRate", @"rate for client");
controller.editType = EditingViewControllerTypeDouble;
} break;
case 2: {
controller.editedFieldKey = @"pocName";
controller.editedFieldName = NSLocalizedString(@"pocName", @"display name for POC");
controller.editType = EditingViewControllerTypeString;
} break;
case 3: {
controller.editedFieldKey = @"pocPhone";
controller.editedFieldName = NSLocalizedString(@"pocPhone", @"display POC phone number");
controller.editType = EditingViewControllerTypePhone;
}
}
[self.navigationController pushViewController:controller animated:YES];
[controller release];
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleNone;
}
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
#pragma mark -
#pragma mark Undo support
- (void)setUpUndoManager {
/*
If the client's managed object context doesn't already have an undo manager, then create one and set it for the context and self.
The view controller needs to keep a reference to the undo manager it creates so that it can determine whether to remove the undo manager when editing finishes.
*/
if (client.managedObjectContext.undoManager == nil) {
NSUndoManager *anUndoManager = [[NSUndoManager alloc] init];
[anUndoManager setLevelsOfUndo:3];
self.undoManager = anUndoManager;
[anUndoManager release];
client.managedObjectContext.undoManager = undoManager;
}
// Register as an observer of the client's context's undo manager.
NSUndoManager *clientUndoManager = client.managedObjectContext.undoManager;
NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter];
[dnc addObserver:self selector:@selector(undoManagerDidUndo:) name:NSUndoManagerDidUndoChangeNotification object:clientUndoManager];
[dnc addObserver:self selector:@selector(undoManagerDidRedo:) name:NSUndoManagerDidRedoChangeNotification object:clientUndoManager];
}
- (void)cleanUpUndoManager {
// Remove self as an observer.
[[NSNotificationCenter defaultCenter] removeObserver:self];
if (client.managedObjectContext.undoManager == undoManager) {
client.managedObjectContext.undoManager = nil;
self.undoManager = nil;
}
}
- (NSUndoManager *)undoManager {
return client.managedObjectContext.undoManager;
}
- (void)undoManagerDidUndo:(NSNotification *)notification {
[self.tableView reloadData];
[self updateRightBarButtonItemState];
}
- (void)undoManagerDidRedo:(NSNotification *)notification {
[self.tableView reloadData];
[self updateRightBarButtonItemState];
}
/*
The view controller must be first responder in order to be able to receive shake events for undo. It should resign first responder status when it disappears.
*/
- (BOOL)canBecomeFirstResponder {
return YES;
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self becomeFirstResponder];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self resignFirstResponder];
}
#pragma mark -
#pragma mark Date Formatter
- (NSDateFormatter *)dateFormatter {
if (dateFormatter == nil) {
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
}
return dateFormatter;
}
#pragma mark -
#pragma mark Memory management
- (void)dealloc {
[undoManager release];
[dateFormatter release];
[client release];
[super dealloc];
}
@end