6 // Copyright 2010 __MyCompanyName__. All rights reserved. |
6 // Copyright 2010 __MyCompanyName__. All rights reserved. |
7 // |
7 // |
8 |
8 |
9 #import "SingleTeamViewController.h" |
9 #import "SingleTeamViewController.h" |
10 #import "HogHatViewController.h" |
10 #import "HogHatViewController.h" |
|
11 #import "GravesViewController.h" |
|
12 #import "VoicesViewController.h" |
|
13 #import "FortsViewController.h" |
11 #import "FlagsViewController.h" |
14 #import "FlagsViewController.h" |
12 #import "FortsViewController.h" |
15 #import "LevelViewController.h" |
13 #import "CommodityFunctions.h" |
16 #import "CommodityFunctions.h" |
14 |
17 |
15 #define TEAMNAME_TAG 1234 |
18 #define TEAMNAME_TAG 1234 |
16 |
19 |
17 @implementation SingleTeamViewController |
20 @implementation SingleTeamViewController |
18 @synthesize teamDictionary, hatArray, secondaryItems, secondaryControllers, textFieldBeingEdited, teamName; |
21 @synthesize teamDictionary, hatArray, secondaryItems, textFieldBeingEdited, teamName; |
19 |
22 |
20 |
23 |
21 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation { |
24 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation { |
22 return rotationManager(interfaceOrientation); |
25 return rotationManager(interfaceOrientation); |
23 } |
26 } |
32 } |
35 } |
33 |
36 |
34 // set the new value |
37 // set the new value |
35 -(BOOL) save:(id) sender { |
38 -(BOOL) save:(id) sender { |
36 NSInteger index = textFieldBeingEdited.tag; |
39 NSInteger index = textFieldBeingEdited.tag; |
|
40 |
37 if (textFieldBeingEdited != nil) { |
41 if (textFieldBeingEdited != nil) { |
38 if (TEAMNAME_TAG == index) { |
42 if (TEAMNAME_TAG == index) { |
39 NSLog(@"%@", textFieldBeingEdited.text); |
|
40 [self.teamDictionary setObject:textFieldBeingEdited.text forKey:@"teamname"]; |
43 [self.teamDictionary setObject:textFieldBeingEdited.text forKey:@"teamname"]; |
41 } else { |
44 } else { |
42 //replace the old value with the new one |
45 //replace the old value with the new one |
43 NSDictionary *oldHog = [[teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:index]; |
46 NSMutableDictionary *hog = [[teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:index]; |
44 NSMutableDictionary *newHog = [[NSMutableDictionary alloc] initWithDictionary: oldHog]; |
47 [hog setObject:textFieldBeingEdited.text forKey:@"hogname"]; |
45 [newHog setObject:textFieldBeingEdited.text forKey:@"hogname"]; |
|
46 [[teamDictionary objectForKey:@"hedgehogs"] replaceObjectAtIndex:index withObject:newHog]; |
|
47 [newHog release]; |
|
48 } |
48 } |
49 |
49 |
50 isWriteNeeded = YES; |
50 isWriteNeeded = YES; |
51 [self.textFieldBeingEdited resignFirstResponder]; |
51 [self.textFieldBeingEdited resignFirstResponder]; |
52 return YES; |
52 return YES; |
95 } |
95 } |
96 |
96 |
97 |
97 |
98 #pragma mark - |
98 #pragma mark - |
99 #pragma mark View lifecycle |
99 #pragma mark View lifecycle |
100 - (void)viewDidLoad { |
100 -(void) viewDidLoad { |
101 [super viewDidLoad]; |
101 [super viewDidLoad]; |
102 |
102 |
103 // labels for the entries |
103 // labels for the entries |
104 NSArray *array = [[NSArray alloc] initWithObjects: |
104 NSArray *array = [[NSArray alloc] initWithObjects: |
105 NSLocalizedString(@"Grave",@""), |
105 NSLocalizedString(@"Grave",@""), |
106 NSLocalizedString(@"Voice",@""), |
106 NSLocalizedString(@"Voice",@""), |
107 NSLocalizedString(@"Fort",@""), |
107 NSLocalizedString(@"Fort",@""), |
108 NSLocalizedString(@"Flag",@""), |
108 NSLocalizedString(@"Flag",@""), |
109 NSLocalizedString(@"Level",@""),nil]; |
109 NSLocalizedString(@"Level",@""),nil]; |
110 self.secondaryItems = array; |
110 self.secondaryItems = array; |
111 [array release]; |
111 [array release]; |
112 |
|
113 // insert controllers here |
|
114 NSMutableArray *controllersArray = [[NSMutableArray alloc] initWithCapacity:[secondaryItems count]]; |
|
115 |
|
116 FlagsViewController *flagsViewController = [[FlagsViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
117 [controllersArray addObject:flagsViewController]; |
|
118 [flagsViewController release]; |
|
119 |
|
120 FortsViewController *fortsViewController = [[FortsViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
121 [controllersArray addObject:fortsViewController]; |
|
122 [fortsViewController release]; |
|
123 |
|
124 self.secondaryControllers = controllersArray; |
|
125 [controllersArray release]; |
|
126 |
112 |
127 // listen if any childController modifies the plist and write it if needed |
113 // listen if any childController modifies the plist and write it if needed |
128 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setWriteNeeded) name:@"setWriteNeedTeams" object:nil]; |
114 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setWriteNeeded) name:@"setWriteNeedTeams" object:nil]; |
129 isWriteNeeded = NO; |
115 isWriteNeeded = NO; |
130 } |
116 } |
131 |
117 |
132 - (void)viewWillAppear:(BOOL)animated { |
118 -(void) viewWillAppear:(BOOL)animated { |
133 [super viewWillAppear:animated]; |
119 [super viewWillAppear:animated]; |
134 |
120 |
135 // load data about the team and write if there has been a change |
121 // load data about the team and write if there has been a change |
136 if (isWriteNeeded) |
122 if (isWriteNeeded) |
137 [self writeFile]; |
123 [self writeFile]; |
232 } |
225 } |
233 return rows; |
226 return rows; |
234 } |
227 } |
235 |
228 |
236 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
229 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
237 static NSString *CellIdentifier = @"Cell"; |
230 static NSString *CellIdentifier0 = @"Cell0"; |
238 |
231 static NSString *CellIdentifier1 = @"Cell1"; |
239 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
232 static NSString *CellIdentifier2 = @"Cell2"; |
240 if (cell == nil) { |
233 |
241 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault |
|
242 reuseIdentifier:CellIdentifier] autorelease]; |
|
243 if ([indexPath section] != 2) { |
|
244 // create a uitextfield for each row, expand it to take the maximum size |
|
245 UITextField *aTextField; |
|
246 if ([indexPath section] == 1) { |
|
247 aTextField = [[UITextField alloc] |
|
248 initWithFrame:CGRectMake(42, 12, (cell.frame.size.width + cell.frame.size.width/3) - 42, 25)]; |
|
249 } else { |
|
250 aTextField = [[UITextField alloc] |
|
251 initWithFrame:CGRectMake(5, 12, (cell.frame.size.width + cell.frame.size.width/3) - 42, 25)]; |
|
252 } |
|
253 |
|
254 aTextField.clearsOnBeginEditing = NO; |
|
255 aTextField.returnKeyType = UIReturnKeyDone; |
|
256 aTextField.adjustsFontSizeToFitWidth = YES; |
|
257 aTextField.delegate = self; |
|
258 aTextField.tag = [indexPath row]; |
|
259 aTextField.font = [UIFont boldSystemFontOfSize:[UIFont systemFontSize] + 2]; |
|
260 aTextField.clearButtonMode = UITextFieldViewModeWhileEditing; |
|
261 [aTextField addTarget:self action:@selector(save:) forControlEvents:UIControlEventEditingDidEndOnExit]; |
|
262 [cell.contentView addSubview:aTextField]; |
|
263 [aTextField release]; |
|
264 } |
|
265 } |
|
266 |
|
267 NSArray *hogArray; |
234 NSArray *hogArray; |
|
235 UITableViewCell *cell; |
268 NSInteger row = [indexPath row]; |
236 NSInteger row = [indexPath row]; |
|
237 |
269 switch ([indexPath section]) { |
238 switch ([indexPath section]) { |
270 case 0: |
239 case 0: |
|
240 |
|
241 cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier0]; |
|
242 if (cell == nil) { |
|
243 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault |
|
244 reuseIdentifier:CellIdentifier0] autorelease]; |
|
245 // create a uitextfield for each row, expand it to take the maximum size |
|
246 UITextField *aTextField = [[UITextField alloc] |
|
247 initWithFrame:CGRectMake(5, 12, (cell.frame.size.width + cell.frame.size.width/3) - 42, 25)]; |
|
248 aTextField.clearsOnBeginEditing = NO; |
|
249 aTextField.returnKeyType = UIReturnKeyDone; |
|
250 aTextField.adjustsFontSizeToFitWidth = YES; |
|
251 aTextField.delegate = self; |
|
252 aTextField.tag = [indexPath row]; |
|
253 aTextField.font = [UIFont boldSystemFontOfSize:[UIFont systemFontSize] + 2]; |
|
254 aTextField.clearButtonMode = UITextFieldViewModeWhileEditing; |
|
255 [aTextField addTarget:self action:@selector(save:) forControlEvents:UIControlEventEditingDidEndOnExit]; |
|
256 [cell.contentView addSubview:aTextField]; |
|
257 [aTextField release]; |
|
258 } |
|
259 |
271 cell.imageView.image = nil; |
260 cell.imageView.image = nil; |
272 cell.accessoryType = UITableViewCellAccessoryNone; |
261 cell.accessoryType = UITableViewCellAccessoryNone; |
273 for (UIView *oneView in cell.contentView.subviews) { |
262 for (UIView *oneView in cell.contentView.subviews) { |
274 if ([oneView isMemberOfClass:[UITextField class]]) { |
263 if ([oneView isMemberOfClass:[UITextField class]]) { |
275 // we find the uitextfied and we'll use its tag to understand which one is being edited |
264 // we find the uitextfied and we'll use its tag to understand which one is being edited |
278 textFieldFound.tag = TEAMNAME_TAG; |
267 textFieldFound.tag = TEAMNAME_TAG; |
279 } |
268 } |
280 } |
269 } |
281 break; |
270 break; |
282 case 1: |
271 case 1: |
|
272 cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; |
|
273 if (cell == nil) { |
|
274 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault |
|
275 reuseIdentifier:CellIdentifier1] autorelease]; |
|
276 |
|
277 // create a uitextfield for each row, expand it to take the maximum size |
|
278 UITextField *aTextField = [[UITextField alloc] |
|
279 initWithFrame:CGRectMake(42, 12, (cell.frame.size.width + cell.frame.size.width/3) - 42, 25)]; |
|
280 aTextField.clearsOnBeginEditing = NO; |
|
281 aTextField.returnKeyType = UIReturnKeyDone; |
|
282 aTextField.adjustsFontSizeToFitWidth = YES; |
|
283 aTextField.delegate = self; |
|
284 aTextField.tag = [indexPath row]; |
|
285 aTextField.font = [UIFont boldSystemFontOfSize:[UIFont systemFontSize] + 2]; |
|
286 aTextField.clearButtonMode = UITextFieldViewModeWhileEditing; |
|
287 [aTextField addTarget:self action:@selector(save:) forControlEvents:UIControlEventEditingDidEndOnExit]; |
|
288 [cell.contentView addSubview:aTextField]; |
|
289 [aTextField release]; |
|
290 } |
|
291 |
283 hogArray = [self.teamDictionary objectForKey:@"hedgehogs"]; |
292 hogArray = [self.teamDictionary objectForKey:@"hedgehogs"]; |
284 |
293 |
285 cell.imageView.image = [self.hatArray objectAtIndex:row]; |
294 cell.imageView.image = [self.hatArray objectAtIndex:row]; |
286 |
295 |
287 for (UIView *oneView in cell.contentView.subviews) { |
296 for (UIView *oneView in cell.contentView.subviews) { |
288 if ([oneView isMemberOfClass:[UITextField class]]) { |
297 if ([oneView isMemberOfClass:[UITextField class]]) { |
289 // we find the uitextfied and we'll use its tag to understand which one is being edited |
298 // we find the uitextfied and we'll use its tag to understand which one is being edited |
290 UITextField *textFieldFound = (UITextField *)oneView; |
299 UITextField *textFieldFound = (UITextField *)oneView; |
291 textFieldFound.text = [[hogArray objectAtIndex:row] objectForKey:@"hogname"]; |
300 textFieldFound.text = [[hogArray objectAtIndex:row] objectForKey:@"hogname"]; |
292 } |
301 } |
293 } |
302 } |
294 |
303 |
295 cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; |
304 cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; |
296 break; |
305 break; |
297 case 2: |
306 case 2: |
|
307 cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2]; |
|
308 if (cell == nil) { |
|
309 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault |
|
310 reuseIdentifier:CellIdentifier2] autorelease]; |
|
311 } |
|
312 |
298 cell.textLabel.text = [self.secondaryItems objectAtIndex:row]; |
313 cell.textLabel.text = [self.secondaryItems objectAtIndex:row]; |
299 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
314 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
300 switch (row) { |
315 switch (row) { |
301 case 3: // flags |
316 case 3: // flags |
302 cell.imageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.png", |
317 cell.imageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.png", |
305 default: |
320 default: |
306 cell.imageView.image = nil; |
321 cell.imageView.image = nil; |
307 break; |
322 break; |
308 } |
323 } |
309 break; |
324 break; |
310 default: |
|
311 break; |
|
312 } |
325 } |
313 |
326 |
314 return cell; |
327 return cell; |
315 } |
328 } |
316 |
329 |
317 |
330 |
318 #pragma mark - |
331 #pragma mark - |
319 #pragma mark Table view delegate |
332 #pragma mark Table view delegate |
320 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
333 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
321 NSInteger row = [indexPath row]; |
334 NSInteger row = [indexPath row]; |
|
335 NSInteger section = [indexPath section]; |
322 UITableViewController *nextController; |
336 UITableViewController *nextController; |
323 UITableViewCell *cell; |
337 UITableViewCell *cell; |
324 switch ([indexPath section]) { |
338 |
325 case 2: |
339 switch (section) { |
326 //TODO: this part should be rewrittend with lazy loading instead of an array of controllers |
340 case 2: //secondary items |
327 nextController = [secondaryControllers objectAtIndex:row%2 ]; //TODO: fix the objectAtIndex |
341 switch (row) { |
|
342 case 0: // grave |
|
343 if (nil == gravesViewController) |
|
344 gravesViewController = [[GravesViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
345 |
|
346 nextController = gravesViewController; |
|
347 break; |
|
348 case 1: // voice |
|
349 if (nil == voicesViewController) |
|
350 voicesViewController = [[VoicesViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
351 |
|
352 nextController = voicesViewController; |
|
353 break; |
|
354 case 2: // fort |
|
355 if (nil == fortsViewController) |
|
356 fortsViewController = [[FortsViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
357 |
|
358 nextController = fortsViewController; |
|
359 break; |
|
360 case 3: // flag |
|
361 if (nil == flagsViewController) |
|
362 flagsViewController = [[FlagsViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
363 |
|
364 nextController = flagsViewController; |
|
365 break; |
|
366 case 4: // level |
|
367 if (nil == levelViewController) |
|
368 levelViewController = [[LevelViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
369 |
|
370 nextController = levelViewController; |
|
371 break; |
|
372 } |
|
373 |
328 nextController.title = [secondaryItems objectAtIndex:row]; |
374 nextController.title = [secondaryItems objectAtIndex:row]; |
329 [nextController setTeamDictionary:teamDictionary]; |
375 [nextController setTeamDictionary:teamDictionary]; |
330 [self.navigationController pushViewController:nextController animated:YES]; |
376 [self.navigationController pushViewController:nextController animated:YES]; |
331 break; |
377 break; |
332 default: |
378 default: |
333 cell = [aTableView cellForRowAtIndexPath:indexPath]; |
379 cell = [aTableView cellForRowAtIndexPath:indexPath]; |
334 for (UIView *oneView in cell.contentView.subviews) { |
380 for (UIView *oneView in cell.contentView.subviews) { |
335 if ([oneView isMemberOfClass:[UITextField class]]) { |
381 if ([oneView isMemberOfClass:[UITextField class]]) { |
336 textFieldBeingEdited = (UITextField *)oneView; |
382 textFieldBeingEdited = (UITextField *)oneView; |
338 } |
384 } |
339 } |
385 } |
340 [aTableView deselectRowAtIndexPath:indexPath animated:NO]; |
386 [aTableView deselectRowAtIndexPath:indexPath animated:NO]; |
341 break; |
387 break; |
342 } |
388 } |
|
389 |
343 } |
390 } |
344 |
391 |
345 // action to perform when you want to change a hog hat |
392 // action to perform when you want to change a hog hat |
346 -(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath { |
393 -(void) tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath { |
347 if (nil == hogChildController) { |
394 if (nil == hogHatViewController) { |
348 hogChildController = [[HogHatViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
395 hogHatViewController = [[HogHatViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
349 } |
396 } |
350 |
397 |
351 // cache the dictionary file of the team, so that other controllers can modify it |
398 // cache the dictionary file of the team, so that other controllers can modify it |
352 hogChildController.teamDictionary = self.teamDictionary; |
399 hogHatViewController.teamDictionary = self.teamDictionary; |
353 hogChildController.selectedHog = [indexPath row]; |
400 hogHatViewController.selectedHog = [indexPath row]; |
354 |
401 |
355 [self.navigationController pushViewController:hogChildController animated:YES]; |
402 [self.navigationController pushViewController:hogHatViewController animated:YES]; |
356 } |
403 } |
357 |
404 |
358 |
405 |
359 #pragma mark - |
406 #pragma mark - |
360 #pragma mark Memory management |
407 #pragma mark Memory management |
368 self.teamDictionary = nil; |
415 self.teamDictionary = nil; |
369 self.textFieldBeingEdited = nil; |
416 self.textFieldBeingEdited = nil; |
370 self.teamName = nil; |
417 self.teamName = nil; |
371 self.hatArray = nil; |
418 self.hatArray = nil; |
372 self.secondaryItems = nil; |
419 self.secondaryItems = nil; |
373 self.secondaryControllers = nil; |
420 hogHatViewController = nil; |
374 hogChildController = nil; |
421 flagsViewController = nil; |
|
422 fortsViewController = nil; |
|
423 gravesViewController = nil; |
375 [super viewDidUnload]; |
424 [super viewDidUnload]; |
376 } |
425 } |
377 |
426 |
378 -(void) dealloc { |
427 -(void) dealloc { |
379 [teamDictionary release]; |
428 [teamDictionary release]; |
380 [textFieldBeingEdited release]; |
429 [textFieldBeingEdited release]; |
381 [teamName release]; |
430 [teamName release]; |
382 [hatArray release]; |
431 [hatArray release]; |
383 [secondaryItems release]; |
432 [secondaryItems release]; |
384 [secondaryControllers release]; |
433 [hogHatViewController release]; |
385 [hogChildController release]; |
434 [fortsViewController release]; |
|
435 [gravesViewController release]; |
|
436 [flagsViewController release]; |
386 [super dealloc]; |
437 [super dealloc]; |
387 } |
438 } |
388 |
439 |
389 |
440 |
390 @end |
441 @end |