cocoaTouch/SingleTeamViewController.m
changeset 3329 d8e41ee0b3ae
parent 3328 fe87c2242984
child 3330 987ec27b6042
equal deleted inserted replaced
3328:fe87c2242984 3329:d8e41ee0b3ae
     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"
    10 #import "HogHatViewController.h"
    11 #import "HogHatViewController.h"
    11 #import "FlagsViewController.h"
    12 #import "FlagsViewController.h"
    12 #import "FortsViewController.h"
    13 #import "FortsViewController.h"
    13 #import "CommodityFunctions.h"
    14 #import "CommodityFunctions.h"
    14 
    15 
    15 @implementation SingleTeamViewController
    16 @implementation SingleTeamViewController
    16 @synthesize teamDictionary, hatArray, secondaryItems, secondaryControllers;
    17 @synthesize teamDictionary, hatArray, secondaryItems, secondaryControllers, textFieldBeingEdited;
    17 
    18 
    18 
    19 
    19 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    20 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    20     return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
    21     return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
    21 }
    22 }
       
    23 
       
    24 
       
    25 #pragma mark -
       
    26 #pragma mark textfield methods
       
    27 // return to previous table
       
    28 -(void) cancel:(id) sender {
       
    29     if (textFieldBeingEdited != nil)
       
    30         [self.textFieldBeingEdited resignFirstResponder];
       
    31 }
       
    32 
       
    33 // set the new value
       
    34 -(void) save:(id) sender {
       
    35     if (textFieldBeingEdited != nil) {
       
    36         //replace the old value with the new one
       
    37         NSDictionary *oldHog = [[teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:selectedHog];
       
    38         NSMutableDictionary *newHog = [[NSMutableDictionary alloc] initWithDictionary: oldHog];
       
    39         [newHog setObject:textFieldBeingEdited.text forKey:@"hogname"];
       
    40         [[teamDictionary objectForKey:@"hedgehogs"] replaceObjectAtIndex:selectedHog withObject:newHog];
       
    41         [newHog release];
       
    42         
       
    43         isWriteNeeded = YES;
       
    44         [self.textFieldBeingEdited resignFirstResponder];
       
    45     }
       
    46 }
       
    47 
       
    48 // the textfield is being modified, update the navigation controller
       
    49 -(void) textFieldDidBeginEditing:(UITextField *)aTextField{
       
    50     self.textFieldBeingEdited = aTextField;
       
    51     selectedHog = aTextField.tag;
       
    52     UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cancel",@"from the hog name table")
       
    53                                                                      style:UIBarButtonItemStylePlain
       
    54                                                                     target:self
       
    55                                                                     action:@selector(cancel:)];
       
    56     self.navigationItem.leftBarButtonItem = cancelButton;
       
    57     [cancelButton release];
       
    58     
       
    59     UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Save",@"from the hog name table")
       
    60                                                                      style:UIBarButtonItemStyleDone
       
    61                                                                     target:self
       
    62                                                                     action:@selector(save:)];
       
    63     self.navigationItem.rightBarButtonItem = saveButton;
       
    64     [saveButton release];
       
    65 }
       
    66 
       
    67 // the textfield has been modified, check for empty strings and restore original navigation bar
       
    68 -(void) textFieldDidEndEditing:(UITextField *)aTextField{
       
    69     if ([textFieldBeingEdited.text length] == 0) 
       
    70         textFieldBeingEdited.text = [NSString stringWithFormat:@"hedgehog %d",textFieldBeingEdited.tag];
       
    71 
       
    72     self.textFieldBeingEdited = nil;
       
    73     self.navigationItem.rightBarButtonItem = self.navigationItem.backBarButtonItem;
       
    74     self.navigationItem.leftBarButtonItem = nil;
       
    75 }
       
    76 
       
    77 // limit the size of the field to 64 characters like in original frontend
       
    78 -(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
       
    79     int limit = 64;
       
    80     return !([textField.text length] > limit && [string length] > range.length);
       
    81 }
       
    82 
    22 
    83 
    23 #pragma mark -
    84 #pragma mark -
    24 #pragma mark View lifecycle
    85 #pragma mark View lifecycle
    25 - (void)viewDidLoad {
    86 - (void)viewDidLoad {
    26     [super viewDidLoad];
    87     [super viewDidLoad];
    27    
    88    
       
    89     // labels for the entries
    28     NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:
    90     NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:
    29                              NSLocalizedString(@"Grave",@""),
    91                              NSLocalizedString(@"Grave",@""),
    30                              NSLocalizedString(@"Voice",@""),
    92                              NSLocalizedString(@"Voice",@""),
    31                              NSLocalizedString(@"Fort",@""),
    93                              NSLocalizedString(@"Fort",@""),
    32                              NSLocalizedString(@"Flag",@""),
    94                              NSLocalizedString(@"Flag",@""),
    54 }
   116 }
    55 
   117 
    56 - (void)viewWillAppear:(BOOL)animated {
   118 - (void)viewWillAppear:(BOOL)animated {
    57     [super viewWillAppear:animated];
   119     [super viewWillAppear:animated];
    58     
   120     
    59     // load data about the team and extract info
   121     // load data about the team and write if there has been a change
    60     NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",TEAMS_DIRECTORY(),self.title];
   122     NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",TEAMS_DIRECTORY(),self.title];
    61     if (isWriteNeeded) {
   123     if (isWriteNeeded) {
    62         [self.teamDictionary writeToFile:teamFile atomically:YES];
   124         [self.teamDictionary writeToFile:teamFile atomically:YES];
    63         NSLog(@"writing: %@",teamDictionary);
   125         NSLog(@"writing: %@",teamDictionary);
    64         isWriteNeeded = NO;
   126         isWriteNeeded = NO;
    65     }
   127     }
    66     
   128     
    67 	NSMutableDictionary *teamDict = [[NSMutableDictionary alloc] initWithContentsOfFile:teamFile];
   129 	NSMutableDictionary *teamDict = [[NSMutableDictionary alloc] initWithContentsOfFile:teamFile];
    68     self.teamDictionary = teamDict;
   130     self.teamDictionary = teamDict;
    69     [teamDict release];
   131     [teamDict release];
    70     
       
    71 	[teamFile release];
   132 	[teamFile release];
    72         
   133         
    73     // load the images of the hat for aach hog
   134     // load the images of the hat for aach hog
    74     NSArray *hogArray = [self.teamDictionary objectForKey:@"hedgehogs"];
   135     NSArray *hogArray = [self.teamDictionary objectForKey:@"hedgehogs"];
    75     NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:[hogArray count]];
   136     NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:[hogArray count]];
    91     [array release];
   152     [array release];
    92     
   153     
    93     [self.tableView reloadData];
   154     [self.tableView reloadData];
    94 }
   155 }
    95 
   156 
       
   157 // write on file if there has been a change
    96 -(void) viewWillDisappear:(BOOL)animated {
   158 -(void) viewWillDisappear:(BOOL)animated {
    97 	[super viewWillDisappear:animated];
   159 	[super viewWillDisappear:animated];
    98 	
   160 	
    99 	NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",TEAMS_DIRECTORY(),self.title];
   161 	NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",TEAMS_DIRECTORY(),self.title];
   100     if (isWriteNeeded) {
   162     if (isWriteNeeded) {
   103         isWriteNeeded = NO;
   165         isWriteNeeded = NO;
   104     }
   166     }
   105 
   167 
   106 	[teamFile release];
   168 	[teamFile release];
   107 }
   169 }
       
   170 
   108 // needed by other classes to warn about a user change
   171 // needed by other classes to warn about a user change
   109 -(void) setWriteNeeded {
   172 -(void) setWriteNeeded {
   110     isWriteNeeded = YES;
   173     isWriteNeeded = YES;
   111 }
   174 }
   112 
   175 
   139     
   202     
   140     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   203     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   141     if (cell == nil) {
   204     if (cell == nil) {
   142         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
   205         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
   143                                        reuseIdentifier:CellIdentifier] autorelease];
   206                                        reuseIdentifier:CellIdentifier] autorelease];
   144     }
   207         if ([indexPath section] == 1) {
   145 
   208             // create a uitextfield for each row, expand it to take the maximum size
       
   209             UITextField *aTextField = [[UITextField alloc] 
       
   210                                        initWithFrame:CGRectMake(42, 12, (cell.frame.size.width + cell.frame.size.width/3) - 42, 25)];
       
   211             aTextField.clearsOnBeginEditing = NO;
       
   212             aTextField.returnKeyType = UIReturnKeyDone;
       
   213             aTextField.adjustsFontSizeToFitWidth = YES;
       
   214             aTextField.delegate = self;
       
   215             aTextField.font = [UIFont boldSystemFontOfSize:[UIFont systemFontSize] + 2];
       
   216             aTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
       
   217             [aTextField addTarget:self action:@selector(save:) forControlEvents:UIControlEventEditingDidEndOnExit];
       
   218             [cell.contentView addSubview:aTextField];
       
   219             [aTextField release];
       
   220         }
       
   221     }
   146 
   222 
   147     NSArray *hogArray;
   223     NSArray *hogArray;
   148     NSInteger row = [indexPath row];
   224     NSInteger row = [indexPath row];
   149     switch ([indexPath section]) {
   225     switch ([indexPath section]) {
   150         case 0:
   226         case 0:
   152             cell.imageView.image = nil;
   228             cell.imageView.image = nil;
   153             cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
   229             cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
   154             break;
   230             break;
   155         case 1:
   231         case 1:
   156             hogArray = [self.teamDictionary objectForKey:@"hedgehogs"];
   232             hogArray = [self.teamDictionary objectForKey:@"hedgehogs"];
   157             cell.textLabel.text = [[hogArray objectAtIndex:row] objectForKey:@"hogname"];
   233 
   158             cell.imageView.image = [self.hatArray objectAtIndex:row];
   234             cell.imageView.image = [self.hatArray objectAtIndex:row];
       
   235             
       
   236             for (UIView *oneView in cell.contentView.subviews) {
       
   237                 if ([oneView isMemberOfClass:[UITextField class]]) {
       
   238                     // we find the uitextfied and we'll use its tag to understand which one is being edited
       
   239                     UITextField *textFieldFound = (UITextField *)oneView;
       
   240                     textFieldFound.text = [[hogArray objectAtIndex:row] objectForKey:@"hogname"];
       
   241                     textFieldFound.tag = row;
       
   242                 }
       
   243             }
       
   244 
   159             cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
   245             cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
   160             break;
   246             break;
   161         case 2:
   247         case 2:
   162             cell.textLabel.text = [self.secondaryItems objectAtIndex:row];
   248             cell.textLabel.text = [self.secondaryItems objectAtIndex:row];
   163             cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
   249             cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
   177     
   263     
   178     return cell;
   264     return cell;
   179 }
   265 }
   180 
   266 
   181 
   267 
   182 /*
       
   183 // Override to support conditional editing of the table view.
       
   184 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
       
   185     // Return NO if you do not want the specified item to be editable.
       
   186     return YES;
       
   187 }
       
   188 */
       
   189 
       
   190 /*
       
   191 // Override to support editing the table view.
       
   192 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
       
   193  forRowAtIndexPath:(NSIndexPath *)indexPath {
       
   194     
       
   195     if (editingStyle == UITableViewCellEditingStyleDelete) {
       
   196         // Delete the row from the data source
       
   197         [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
       
   198     }   
       
   199     else if (editingStyle == UITableViewCellEditingStyleInsert) {
       
   200         // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
       
   201     }   
       
   202 }
       
   203 */
       
   204 
       
   205 /*
       
   206 // Override to support rearranging the table view.
       
   207 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
       
   208 }
       
   209 */
       
   210 
       
   211 
       
   212 /*
       
   213 // Override to support conditional rearranging of the table view.
       
   214 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
       
   215     // Return NO if you do not want the item to be re-orderable.
       
   216     return YES;
       
   217 }
       
   218 */
       
   219 
       
   220 
       
   221 #pragma mark -
   268 #pragma mark -
   222 #pragma mark Table view delegate
   269 #pragma mark Table view delegate
   223 -(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   270 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   224     NSInteger row = [indexPath row];
   271     NSInteger row = [indexPath row];
   225     UITableViewController *nextController;
   272     UITableViewController *nextController;
   226     if (1 == [indexPath section]) {
   273     if (1 == [indexPath section]) {
   227         //TODO: hog name pref, causes segfault
   274         UITableViewCell *cell = [aTableView cellForRowAtIndexPath:indexPath];
       
   275         for (UIView *oneView in cell.contentView.subviews) {
       
   276             if ([oneView isMemberOfClass:[UITextField class]]) {
       
   277                 textFieldBeingEdited = (UITextField *)oneView;
       
   278                 textFieldBeingEdited.tag = row;
       
   279                 [textFieldBeingEdited becomeFirstResponder];
       
   280             }
       
   281         }
       
   282         [aTableView deselectRowAtIndexPath:indexPath animated:NO];
   228     }
   283     }
   229     if (2 == [indexPath section]) {
   284     if (2 == [indexPath section]) {
   230         //TODO: this part should be rewrittend with lazy loading instead of an array of controllers
   285         //TODO: this part should be rewrittend with lazy loading instead of an array of controllers
   231         nextController = [secondaryControllers objectAtIndex:row%2 ];              //TODO: fix the objectAtIndex
   286         nextController = [secondaryControllers objectAtIndex:row%2 ];              //TODO: fix the objectAtIndex
   232         nextController.title = [secondaryItems objectAtIndex:row];
   287         nextController.title = [secondaryItems objectAtIndex:row];
   233         [nextController setTeamDictionary:teamDictionary];
   288         [nextController setTeamDictionary:teamDictionary];
   234     }
   289         [self.navigationController pushViewController:nextController animated:YES];
   235     [self.navigationController pushViewController:nextController animated:YES];
   290     }
   236 }
   291 }
   237 
   292 
       
   293 // action to perform when you want to change a hog hat
   238 -(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
   294 -(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
   239     if (nil == hogChildController) {
   295     if (nil == hogChildController) {
   240         hogChildController = [[HogHatViewController alloc] initWithStyle:UITableViewStyleGrouped];
   296         hogChildController = [[HogHatViewController alloc] initWithStyle:UITableViewStyleGrouped];
   241     }
   297     }
   242     
   298     
   255     [super didReceiveMemoryWarning];
   311     [super didReceiveMemoryWarning];
   256     // Relinquish ownership any cached data, images, etc that aren't in use.
   312     // Relinquish ownership any cached data, images, etc that aren't in use.
   257 }
   313 }
   258 
   314 
   259 -(void) viewDidUnload {
   315 -(void) viewDidUnload {
       
   316     self.teamDictionary = nil;
       
   317     self.textFieldBeingEdited = nil;
       
   318     self.hatArray = nil;
       
   319     self.secondaryItems = nil;
   260     self.secondaryControllers = nil;
   320     self.secondaryControllers = nil;
   261     self.secondaryItems = nil;
   321     hogChildController = nil;
   262     self.hatArray = nil;
       
   263     self.teamDictionary = nil;
       
   264     [super viewDidUnload];
   322     [super viewDidUnload];
   265 }
   323 }
   266 
   324 
   267 -(void) dealloc {
   325 -(void) dealloc {
       
   326     [teamDictionary release];
       
   327     [textFieldBeingEdited release];
       
   328     [hatArray release];
       
   329     [secondaryItems release];
   268     [secondaryControllers release];
   330     [secondaryControllers release];
   269     [secondaryItems release];
   331     [hogChildController release];
   270     [hatArray release];
       
   271     [teamDictionary release];
       
   272     [super dealloc];
   332     [super dealloc];
   273 }
   333 }
   274 
   334 
   275 
   335 
   276 @end
   336 @end