5 // Created by Vittorio on 02/04/10. |
5 // Created by Vittorio on 02/04/10. |
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 "HogNameViewController.h" |
|
11 #import "HogHatViewController.h" |
10 #import "HogHatViewController.h" |
12 #import "FlagsViewController.h" |
11 #import "FlagsViewController.h" |
13 #import "FortsViewController.h" |
12 #import "FortsViewController.h" |
14 #import "CommodityFunctions.h" |
13 #import "CommodityFunctions.h" |
15 |
14 |
|
15 #define TEAMNAME_TAG 1234 |
|
16 |
16 @implementation SingleTeamViewController |
17 @implementation SingleTeamViewController |
17 @synthesize teamDictionary, hatArray, secondaryItems, secondaryControllers, textFieldBeingEdited; |
18 @synthesize teamDictionary, hatArray, secondaryItems, secondaryControllers, textFieldBeingEdited, teamName; |
18 |
19 |
19 |
20 |
20 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { |
21 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { |
21 return (interfaceOrientation == UIInterfaceOrientationLandscapeRight); |
22 return (interfaceOrientation == UIInterfaceOrientationLandscapeRight); |
22 } |
23 } |
29 if (textFieldBeingEdited != nil) |
30 if (textFieldBeingEdited != nil) |
30 [self.textFieldBeingEdited resignFirstResponder]; |
31 [self.textFieldBeingEdited resignFirstResponder]; |
31 } |
32 } |
32 |
33 |
33 // set the new value |
34 // set the new value |
34 -(void) save:(id) sender { |
35 -(BOOL) save:(id) sender { |
35 if (textFieldBeingEdited != nil) { |
36 if (textFieldBeingEdited != nil) { |
36 //replace the old value with the new one |
37 if (TEAMNAME_TAG == selectedHog) { |
37 NSDictionary *oldHog = [[teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:selectedHog]; |
38 NSLog(@"%@", textFieldBeingEdited.text); |
38 NSMutableDictionary *newHog = [[NSMutableDictionary alloc] initWithDictionary: oldHog]; |
39 [self.teamDictionary setObject:textFieldBeingEdited.text forKey:@"teamname"]; |
39 [newHog setObject:textFieldBeingEdited.text forKey:@"hogname"]; |
40 } else { |
40 [[teamDictionary objectForKey:@"hedgehogs"] replaceObjectAtIndex:selectedHog withObject:newHog]; |
41 //replace the old value with the new one |
41 [newHog release]; |
42 NSDictionary *oldHog = [[teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:selectedHog]; |
|
43 NSMutableDictionary *newHog = [[NSMutableDictionary alloc] initWithDictionary: oldHog]; |
|
44 [newHog setObject:textFieldBeingEdited.text forKey:@"hogname"]; |
|
45 [[teamDictionary objectForKey:@"hedgehogs"] replaceObjectAtIndex:selectedHog withObject:newHog]; |
|
46 [newHog release]; |
|
47 } |
42 |
48 |
43 isWriteNeeded = YES; |
49 isWriteNeeded = YES; |
44 [self.textFieldBeingEdited resignFirstResponder]; |
50 [self.textFieldBeingEdited resignFirstResponder]; |
45 } |
51 return YES; |
|
52 } |
|
53 return NO; |
46 } |
54 } |
47 |
55 |
48 // the textfield is being modified, update the navigation controller |
56 // the textfield is being modified, update the navigation controller |
49 -(void) textFieldDidBeginEditing:(UITextField *)aTextField{ |
57 -(void) textFieldDidBeginEditing:(UITextField *)aTextField{ |
50 self.textFieldBeingEdited = aTextField; |
58 self.textFieldBeingEdited = aTextField; |
51 selectedHog = aTextField.tag; |
59 selectedHog = aTextField.tag; |
52 UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cancel",@"from the hog name table") |
60 UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cancel",@"from the hog name table") |
53 style:UIBarButtonItemStylePlain |
61 style:UIBarButtonItemStylePlain |
54 target:self |
62 target:self |
62 action:@selector(save:)]; |
70 action:@selector(save:)]; |
63 self.navigationItem.rightBarButtonItem = saveButton; |
71 self.navigationItem.rightBarButtonItem = saveButton; |
64 [saveButton release]; |
72 [saveButton release]; |
65 } |
73 } |
66 |
74 |
|
75 // we save every time a textfield is edited, so we don't risk to update only the hogs or only the temname |
|
76 -(BOOL) textFieldShouldEndEditing:(UITextField *)aTextField { |
|
77 return [self save:nil]; |
|
78 } |
|
79 |
67 // the textfield has been modified, check for empty strings and restore original navigation bar |
80 // the textfield has been modified, check for empty strings and restore original navigation bar |
68 -(void) textFieldDidEndEditing:(UITextField *)aTextField{ |
81 -(void) textFieldDidEndEditing:(UITextField *)aTextField{ |
69 if ([textFieldBeingEdited.text length] == 0) |
82 if ([textFieldBeingEdited.text length] == 0) |
70 textFieldBeingEdited.text = [NSString stringWithFormat:@"hedgehog %d",textFieldBeingEdited.tag]; |
83 textFieldBeingEdited.text = [NSString stringWithFormat:@"hedgehog %d",textFieldBeingEdited.tag]; |
71 |
84 |
72 self.textFieldBeingEdited = nil; |
85 //self.textFieldBeingEdited = nil; |
73 self.navigationItem.rightBarButtonItem = self.navigationItem.backBarButtonItem; |
86 self.navigationItem.rightBarButtonItem = self.navigationItem.backBarButtonItem; |
74 self.navigationItem.leftBarButtonItem = nil; |
87 self.navigationItem.leftBarButtonItem = nil; |
75 } |
88 } |
76 |
89 |
77 // limit the size of the field to 64 characters like in original frontend |
90 // limit the size of the field to 64 characters like in original frontend |
117 |
130 |
118 - (void)viewWillAppear:(BOOL)animated { |
131 - (void)viewWillAppear:(BOOL)animated { |
119 [super viewWillAppear:animated]; |
132 [super viewWillAppear:animated]; |
120 |
133 |
121 // load data about the team and write if there has been a change |
134 // load data about the team and write if there has been a change |
|
135 if (isWriteNeeded) |
|
136 [self writeFile]; |
|
137 |
122 NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",TEAMS_DIRECTORY(),self.title]; |
138 NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",TEAMS_DIRECTORY(),self.title]; |
123 if (isWriteNeeded) { |
|
124 [self.teamDictionary writeToFile:teamFile atomically:YES]; |
|
125 NSLog(@"writing: %@",teamDictionary); |
|
126 isWriteNeeded = NO; |
|
127 } |
|
128 |
|
129 NSMutableDictionary *teamDict = [[NSMutableDictionary alloc] initWithContentsOfFile:teamFile]; |
139 NSMutableDictionary *teamDict = [[NSMutableDictionary alloc] initWithContentsOfFile:teamFile]; |
130 self.teamDictionary = teamDict; |
140 self.teamDictionary = teamDict; |
131 [teamDict release]; |
141 [teamDict release]; |
132 [teamFile release]; |
142 [teamFile release]; |
133 |
143 |
|
144 self.teamName = self.title; |
|
145 |
134 // load the images of the hat for aach hog |
146 // load the images of the hat for aach hog |
|
147 NSString *normalHogFile = [[NSString alloc] initWithFormat:@"%@/Hedgehog.png",GRAPHICS_DIRECTORY()]; |
|
148 UIImage *normalHogImage = [[UIImage alloc] initWithContentsOfFile:normalHogFile]; |
|
149 [normalHogFile release]; |
|
150 CGRect hogSpriteArea = CGRectMake(96, 0, 32, 32); |
|
151 CGImageRef cgImg = CGImageCreateWithImageInRect([normalHogImage CGImage], hogSpriteArea); |
|
152 [normalHogImage release]; |
|
153 UIImage *normalHogSprite = [[UIImage alloc] initWithCGImage:cgImg]; |
|
154 CGImageRelease(cgImg); |
|
155 |
135 NSArray *hogArray = [self.teamDictionary objectForKey:@"hedgehogs"]; |
156 NSArray *hogArray = [self.teamDictionary objectForKey:@"hedgehogs"]; |
136 NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:[hogArray count]]; |
157 NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:[hogArray count]]; |
137 for (NSDictionary *hog in hogArray) { |
158 for (NSDictionary *hog in hogArray) { |
138 NSString *hatFile = [[NSString alloc] initWithFormat:@"%@/%@.png",HATS_DIRECTORY(),[hog objectForKey:@"hat"]]; |
159 NSString *hatFile = [[NSString alloc] initWithFormat:@"%@/%@.png",HATS_DIRECTORY(),[hog objectForKey:@"hat"]]; |
139 |
160 |
142 CGRect firstSpriteArea = CGRectMake(0, 0, 32, 32); |
163 CGRect firstSpriteArea = CGRectMake(0, 0, 32, 32); |
143 CGImageRef cgImgage = CGImageCreateWithImageInRect([image CGImage], firstSpriteArea); |
164 CGImageRef cgImgage = CGImageCreateWithImageInRect([image CGImage], firstSpriteArea); |
144 [image release]; |
165 [image release]; |
145 |
166 |
146 UIImage *hatSprite = [[UIImage alloc] initWithCGImage:cgImgage]; |
167 UIImage *hatSprite = [[UIImage alloc] initWithCGImage:cgImgage]; |
147 [array addObject:hatSprite]; |
|
148 CGImageRelease(cgImgage); |
168 CGImageRelease(cgImgage); |
|
169 |
|
170 [array addObject:mergeTwoImages(normalHogSprite, hatSprite)]; |
149 [hatSprite release]; |
171 [hatSprite release]; |
150 } |
172 } |
|
173 [normalHogSprite release]; |
151 self.hatArray = array; |
174 self.hatArray = array; |
152 [array release]; |
175 [array release]; |
153 |
176 |
154 [self.tableView reloadData]; |
177 [self.tableView reloadData]; |
155 } |
178 } |
156 |
179 |
157 // write on file if there has been a change |
180 // write on file if there has been a change |
158 -(void) viewWillDisappear:(BOOL)animated { |
181 -(void) viewWillDisappear:(BOOL)animated { |
159 [super viewWillDisappear:animated]; |
182 [super viewWillDisappear:animated]; |
160 |
183 if (isWriteNeeded) |
161 NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",TEAMS_DIRECTORY(),self.title]; |
184 [self writeFile]; |
162 if (isWriteNeeded) { |
|
163 [self.teamDictionary writeToFile:teamFile atomically:YES]; |
|
164 NSLog(@"writing: %@",teamDictionary); |
|
165 isWriteNeeded = NO; |
|
166 } |
|
167 |
|
168 [teamFile release]; |
|
169 } |
185 } |
170 |
186 |
171 // needed by other classes to warn about a user change |
187 // needed by other classes to warn about a user change |
172 -(void) setWriteNeeded { |
188 -(void) setWriteNeeded { |
173 isWriteNeeded = YES; |
189 isWriteNeeded = YES; |
|
190 } |
|
191 |
|
192 -(void) writeFile { |
|
193 NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",TEAMS_DIRECTORY(),self.title]; |
|
194 |
|
195 NSString *newTeamName = [self.teamDictionary objectForKey:@"teamname"]; |
|
196 if (![newTeamName isEqualToString:self.teamName]) { |
|
197 //delete old |
|
198 [[NSFileManager defaultManager] removeItemAtPath:teamFile error:NULL]; |
|
199 [teamFile release]; |
|
200 self.title = newTeamName; |
|
201 self.teamName = newTeamName; |
|
202 teamFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",TEAMS_DIRECTORY(),newTeamName]; |
|
203 } |
|
204 |
|
205 [self.teamDictionary writeToFile:teamFile atomically:YES]; |
|
206 NSLog(@"writing: %@",teamDictionary); |
|
207 isWriteNeeded = NO; |
|
208 [teamFile release]; |
174 } |
209 } |
175 |
210 |
176 #pragma mark - |
211 #pragma mark - |
177 #pragma mark Table view data source |
212 #pragma mark Table view data source |
178 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
213 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
202 |
237 |
203 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
238 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
204 if (cell == nil) { |
239 if (cell == nil) { |
205 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault |
240 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault |
206 reuseIdentifier:CellIdentifier] autorelease]; |
241 reuseIdentifier:CellIdentifier] autorelease]; |
207 if ([indexPath section] == 1) { |
242 if ([indexPath section] != 2) { |
208 // create a uitextfield for each row, expand it to take the maximum size |
243 // create a uitextfield for each row, expand it to take the maximum size |
209 UITextField *aTextField = [[UITextField alloc] |
244 UITextField *aTextField; |
210 initWithFrame:CGRectMake(42, 12, (cell.frame.size.width + cell.frame.size.width/3) - 42, 25)]; |
245 if ([indexPath section] == 1) { |
|
246 aTextField = [[UITextField alloc] |
|
247 initWithFrame:CGRectMake(42, 12, (cell.frame.size.width + cell.frame.size.width/3) - 42, 25)]; |
|
248 } else { |
|
249 aTextField = [[UITextField alloc] |
|
250 initWithFrame:CGRectMake(5, 12, (cell.frame.size.width + cell.frame.size.width/3) - 42, 25)]; |
|
251 } |
|
252 |
211 aTextField.clearsOnBeginEditing = NO; |
253 aTextField.clearsOnBeginEditing = NO; |
212 aTextField.returnKeyType = UIReturnKeyDone; |
254 aTextField.returnKeyType = UIReturnKeyDone; |
213 aTextField.adjustsFontSizeToFitWidth = YES; |
255 aTextField.adjustsFontSizeToFitWidth = YES; |
214 aTextField.delegate = self; |
256 aTextField.delegate = self; |
215 aTextField.font = [UIFont boldSystemFontOfSize:[UIFont systemFontSize] + 2]; |
257 aTextField.font = [UIFont boldSystemFontOfSize:[UIFont systemFontSize] + 2]; |
222 |
264 |
223 NSArray *hogArray; |
265 NSArray *hogArray; |
224 NSInteger row = [indexPath row]; |
266 NSInteger row = [indexPath row]; |
225 switch ([indexPath section]) { |
267 switch ([indexPath section]) { |
226 case 0: |
268 case 0: |
227 cell.textLabel.text = self.title; |
|
228 cell.imageView.image = nil; |
269 cell.imageView.image = nil; |
229 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
270 cell.accessoryType = UITableViewCellAccessoryNone; |
|
271 for (UIView *oneView in cell.contentView.subviews) { |
|
272 if ([oneView isMemberOfClass:[UITextField class]]) { |
|
273 // we find the uitextfied and we'll use its tag to understand which one is being edited |
|
274 UITextField *textFieldFound = (UITextField *)oneView; |
|
275 textFieldFound.text = [self.teamDictionary objectForKey:@"teamname"]; |
|
276 textFieldFound.tag = TEAMNAME_TAG; |
|
277 } |
|
278 } |
230 break; |
279 break; |
231 case 1: |
280 case 1: |
232 hogArray = [self.teamDictionary objectForKey:@"hedgehogs"]; |
281 hogArray = [self.teamDictionary objectForKey:@"hedgehogs"]; |
233 |
282 |
234 cell.imageView.image = [self.hatArray objectAtIndex:row]; |
283 cell.imageView.image = [self.hatArray objectAtIndex:row]; |
268 #pragma mark - |
317 #pragma mark - |
269 #pragma mark Table view delegate |
318 #pragma mark Table view delegate |
270 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
319 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
271 NSInteger row = [indexPath row]; |
320 NSInteger row = [indexPath row]; |
272 UITableViewController *nextController; |
321 UITableViewController *nextController; |
273 if (1 == [indexPath section]) { |
322 UITableViewCell *cell; |
274 UITableViewCell *cell = [aTableView cellForRowAtIndexPath:indexPath]; |
323 switch ([indexPath section]) { |
275 for (UIView *oneView in cell.contentView.subviews) { |
324 case 2: |
276 if ([oneView isMemberOfClass:[UITextField class]]) { |
325 //TODO: this part should be rewrittend with lazy loading instead of an array of controllers |
277 textFieldBeingEdited = (UITextField *)oneView; |
326 nextController = [secondaryControllers objectAtIndex:row%2 ]; //TODO: fix the objectAtIndex |
278 textFieldBeingEdited.tag = row; |
327 nextController.title = [secondaryItems objectAtIndex:row]; |
279 [textFieldBeingEdited becomeFirstResponder]; |
328 [nextController setTeamDictionary:teamDictionary]; |
|
329 [self.navigationController pushViewController:nextController animated:YES]; |
|
330 break; |
|
331 default: |
|
332 cell = [aTableView cellForRowAtIndexPath:indexPath]; |
|
333 for (UIView *oneView in cell.contentView.subviews) { |
|
334 if ([oneView isMemberOfClass:[UITextField class]]) { |
|
335 textFieldBeingEdited = (UITextField *)oneView; |
|
336 textFieldBeingEdited.tag = row; |
|
337 [textFieldBeingEdited becomeFirstResponder]; |
|
338 } |
280 } |
339 } |
281 } |
340 [aTableView deselectRowAtIndexPath:indexPath animated:NO]; |
282 [aTableView deselectRowAtIndexPath:indexPath animated:NO]; |
341 break; |
283 } |
|
284 if (2 == [indexPath section]) { |
|
285 //TODO: this part should be rewrittend with lazy loading instead of an array of controllers |
|
286 nextController = [secondaryControllers objectAtIndex:row%2 ]; //TODO: fix the objectAtIndex |
|
287 nextController.title = [secondaryItems objectAtIndex:row]; |
|
288 [nextController setTeamDictionary:teamDictionary]; |
|
289 [self.navigationController pushViewController:nextController animated:YES]; |
|
290 } |
342 } |
291 } |
343 } |
292 |
344 |
293 // action to perform when you want to change a hog hat |
345 // action to perform when you want to change a hog hat |
294 -(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath { |
346 -(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath { |
313 } |
365 } |
314 |
366 |
315 -(void) viewDidUnload { |
367 -(void) viewDidUnload { |
316 self.teamDictionary = nil; |
368 self.teamDictionary = nil; |
317 self.textFieldBeingEdited = nil; |
369 self.textFieldBeingEdited = nil; |
|
370 self.teamName = nil; |
318 self.hatArray = nil; |
371 self.hatArray = nil; |
319 self.secondaryItems = nil; |
372 self.secondaryItems = nil; |
320 self.secondaryControllers = nil; |
373 self.secondaryControllers = nil; |
321 hogChildController = nil; |
374 hogChildController = nil; |
322 [super viewDidUnload]; |
375 [super viewDidUnload]; |
323 } |
376 } |
324 |
377 |
325 -(void) dealloc { |
378 -(void) dealloc { |
326 [teamDictionary release]; |
379 [teamDictionary release]; |
327 [textFieldBeingEdited release]; |
380 [textFieldBeingEdited release]; |
|
381 [teamName release]; |
328 [hatArray release]; |
382 [hatArray release]; |
329 [secondaryItems release]; |
383 [secondaryItems release]; |
330 [secondaryControllers release]; |
384 [secondaryControllers release]; |
331 [hogChildController release]; |
385 [hogChildController release]; |
332 [super dealloc]; |
386 [super dealloc]; |