cocoaTouch/SingleTeamViewController.m
author koda
Fri, 09 Apr 2010 21:08:27 +0000
changeset 3329 d8e41ee0b3ae
parent 3328 fe87c2242984
child 3330 987ec27b6042
permissions -rw-r--r--
add support to editing hog names
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3305
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
     1
//
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
     2
//  SingleTeamViewController.m
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
     3
//  HedgewarsMobile
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
     4
//
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
     5
//  Created by Vittorio on 02/04/10.
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
     6
//  Copyright 2010 __MyCompanyName__. All rights reserved.
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
     7
//
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
     8
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
     9
#import "SingleTeamViewController.h"
3329
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    10
//#import "HogNameViewController.h"
3305
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    11
#import "HogHatViewController.h"
3325
652a8ebdf667 moved around team creation
koda
parents: 3323
diff changeset
    12
#import "FlagsViewController.h"
652a8ebdf667 moved around team creation
koda
parents: 3323
diff changeset
    13
#import "FortsViewController.h"
652a8ebdf667 moved around team creation
koda
parents: 3323
diff changeset
    14
#import "CommodityFunctions.h"
3305
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    15
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    16
@implementation SingleTeamViewController
3329
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    17
@synthesize teamDictionary, hatArray, secondaryItems, secondaryControllers, textFieldBeingEdited;
3325
652a8ebdf667 moved around team creation
koda
parents: 3323
diff changeset
    18
3305
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    19
3325
652a8ebdf667 moved around team creation
koda
parents: 3323
diff changeset
    20
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
652a8ebdf667 moved around team creation
koda
parents: 3323
diff changeset
    21
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
652a8ebdf667 moved around team creation
koda
parents: 3323
diff changeset
    22
}
3305
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    23
3329
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    24
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    25
#pragma mark -
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    26
#pragma mark textfield methods
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    27
// return to previous table
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    28
-(void) cancel:(id) sender {
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    29
    if (textFieldBeingEdited != nil)
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    30
        [self.textFieldBeingEdited resignFirstResponder];
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    31
}
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    32
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    33
// set the new value
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    34
-(void) save:(id) sender {
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    35
    if (textFieldBeingEdited != nil) {
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    36
        //replace the old value with the new one
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    37
        NSDictionary *oldHog = [[teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:selectedHog];
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    38
        NSMutableDictionary *newHog = [[NSMutableDictionary alloc] initWithDictionary: oldHog];
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    39
        [newHog setObject:textFieldBeingEdited.text forKey:@"hogname"];
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    40
        [[teamDictionary objectForKey:@"hedgehogs"] replaceObjectAtIndex:selectedHog withObject:newHog];
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    41
        [newHog release];
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    42
        
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    43
        isWriteNeeded = YES;
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    44
        [self.textFieldBeingEdited resignFirstResponder];
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    45
    }
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    46
}
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    47
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    48
// the textfield is being modified, update the navigation controller
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    49
-(void) textFieldDidBeginEditing:(UITextField *)aTextField{
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    50
    self.textFieldBeingEdited = aTextField;
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    51
    selectedHog = aTextField.tag;
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    52
    UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cancel",@"from the hog name table")
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    53
                                                                     style:UIBarButtonItemStylePlain
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    54
                                                                    target:self
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    55
                                                                    action:@selector(cancel:)];
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    56
    self.navigationItem.leftBarButtonItem = cancelButton;
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    57
    [cancelButton release];
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    58
    
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    59
    UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Save",@"from the hog name table")
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    60
                                                                     style:UIBarButtonItemStyleDone
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    61
                                                                    target:self
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    62
                                                                    action:@selector(save:)];
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    63
    self.navigationItem.rightBarButtonItem = saveButton;
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    64
    [saveButton release];
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    65
}
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    66
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    67
// the textfield has been modified, check for empty strings and restore original navigation bar
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    68
-(void) textFieldDidEndEditing:(UITextField *)aTextField{
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    69
    if ([textFieldBeingEdited.text length] == 0) 
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    70
        textFieldBeingEdited.text = [NSString stringWithFormat:@"hedgehog %d",textFieldBeingEdited.tag];
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    71
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    72
    self.textFieldBeingEdited = nil;
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    73
    self.navigationItem.rightBarButtonItem = self.navigationItem.backBarButtonItem;
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    74
    self.navigationItem.leftBarButtonItem = nil;
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    75
}
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    76
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    77
// limit the size of the field to 64 characters like in original frontend
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    78
-(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    79
    int limit = 64;
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    80
    return !([textField.text length] > limit && [string length] > range.length);
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    81
}
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    82
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    83
3305
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    84
#pragma mark -
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    85
#pragma mark View lifecycle
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    86
- (void)viewDidLoad {
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    87
    [super viewDidLoad];
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    88
   
3329
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
    89
    // labels for the entries
3305
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    90
    NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    91
                             NSLocalizedString(@"Grave",@""),
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    92
                             NSLocalizedString(@"Voice",@""),
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    93
                             NSLocalizedString(@"Fort",@""),
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    94
                             NSLocalizedString(@"Flag",@""),
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    95
                             NSLocalizedString(@"Level",@""),nil];
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    96
    self.secondaryItems = array;
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    97
    [array release];
3312
6d8f1c76756d restore and update the old general settings
koda
parents: 3308
diff changeset
    98
    
3325
652a8ebdf667 moved around team creation
koda
parents: 3323
diff changeset
    99
    // insert controllers here
652a8ebdf667 moved around team creation
koda
parents: 3323
diff changeset
   100
    NSMutableArray *controllersArray = [[NSMutableArray alloc] initWithCapacity:[secondaryItems count]];
652a8ebdf667 moved around team creation
koda
parents: 3323
diff changeset
   101
    
652a8ebdf667 moved around team creation
koda
parents: 3323
diff changeset
   102
    FlagsViewController *flagsViewController = [[FlagsViewController alloc] initWithStyle:UITableViewStyleGrouped];
652a8ebdf667 moved around team creation
koda
parents: 3323
diff changeset
   103
    [controllersArray addObject:flagsViewController];
652a8ebdf667 moved around team creation
koda
parents: 3323
diff changeset
   104
    [flagsViewController release];
652a8ebdf667 moved around team creation
koda
parents: 3323
diff changeset
   105
    
652a8ebdf667 moved around team creation
koda
parents: 3323
diff changeset
   106
    FortsViewController *fortsViewController = [[FortsViewController alloc] initWithStyle:UITableViewStyleGrouped];
652a8ebdf667 moved around team creation
koda
parents: 3323
diff changeset
   107
    [controllersArray addObject:fortsViewController];
652a8ebdf667 moved around team creation
koda
parents: 3323
diff changeset
   108
    [fortsViewController release];
652a8ebdf667 moved around team creation
koda
parents: 3323
diff changeset
   109
    
652a8ebdf667 moved around team creation
koda
parents: 3323
diff changeset
   110
    self.secondaryControllers = controllersArray;
652a8ebdf667 moved around team creation
koda
parents: 3323
diff changeset
   111
    [controllersArray release];
3315
4e2813713358 usa a cached dictionary and write only when needed
koda
parents: 3312
diff changeset
   112
4e2813713358 usa a cached dictionary and write only when needed
koda
parents: 3312
diff changeset
   113
    // listen if any childController modifies the plist and write it if needed
4e2813713358 usa a cached dictionary and write only when needed
koda
parents: 3312
diff changeset
   114
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setWriteNeeded) name:@"setWriteNeedTeams" object:nil];
4e2813713358 usa a cached dictionary and write only when needed
koda
parents: 3312
diff changeset
   115
    isWriteNeeded = NO;
4e2813713358 usa a cached dictionary and write only when needed
koda
parents: 3312
diff changeset
   116
}
4e2813713358 usa a cached dictionary and write only when needed
koda
parents: 3312
diff changeset
   117
4e2813713358 usa a cached dictionary and write only when needed
koda
parents: 3312
diff changeset
   118
- (void)viewWillAppear:(BOOL)animated {
4e2813713358 usa a cached dictionary and write only when needed
koda
parents: 3312
diff changeset
   119
    [super viewWillAppear:animated];
3323
091cf214bdd5 implement an add/remove team in the ifrontend
koda
parents: 3316
diff changeset
   120
    
3329
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   121
    // load data about the team and write if there has been a change
3328
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   122
    NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",TEAMS_DIRECTORY(),self.title];
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   123
    if (isWriteNeeded) {
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   124
        [self.teamDictionary writeToFile:teamFile atomically:YES];
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   125
        NSLog(@"writing: %@",teamDictionary);
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   126
        isWriteNeeded = NO;
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   127
    }
3312
6d8f1c76756d restore and update the old general settings
koda
parents: 3308
diff changeset
   128
    
3328
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   129
	NSMutableDictionary *teamDict = [[NSMutableDictionary alloc] initWithContentsOfFile:teamFile];
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   130
    self.teamDictionary = teamDict;
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   131
    [teamDict release];
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   132
	[teamFile release];
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   133
        
3312
6d8f1c76756d restore and update the old general settings
koda
parents: 3308
diff changeset
   134
    // load the images of the hat for aach hog
3315
4e2813713358 usa a cached dictionary and write only when needed
koda
parents: 3312
diff changeset
   135
    NSArray *hogArray = [self.teamDictionary objectForKey:@"hedgehogs"];
4e2813713358 usa a cached dictionary and write only when needed
koda
parents: 3312
diff changeset
   136
    NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:[hogArray count]];
4e2813713358 usa a cached dictionary and write only when needed
koda
parents: 3312
diff changeset
   137
    for (NSDictionary *hog in hogArray) {
3328
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   138
        NSString *hatFile = [[NSString alloc] initWithFormat:@"%@/%@.png",HATS_DIRECTORY(),[hog objectForKey:@"hat"]];
3312
6d8f1c76756d restore and update the old general settings
koda
parents: 3308
diff changeset
   139
6d8f1c76756d restore and update the old general settings
koda
parents: 3308
diff changeset
   140
        UIImage *image = [[UIImage alloc] initWithContentsOfFile: hatFile];
3316
52f0482b043f fix a couple of leaks in hte ifrontend
koda
parents: 3315
diff changeset
   141
        [hatFile release];
3312
6d8f1c76756d restore and update the old general settings
koda
parents: 3308
diff changeset
   142
        CGRect firstSpriteArea = CGRectMake(0, 0, 32, 32);
6d8f1c76756d restore and update the old general settings
koda
parents: 3308
diff changeset
   143
        CGImageRef cgImgage = CGImageCreateWithImageInRect([image CGImage], firstSpriteArea);
6d8f1c76756d restore and update the old general settings
koda
parents: 3308
diff changeset
   144
        [image release];
6d8f1c76756d restore and update the old general settings
koda
parents: 3308
diff changeset
   145
        
6d8f1c76756d restore and update the old general settings
koda
parents: 3308
diff changeset
   146
        UIImage *hatSprite = [[UIImage alloc] initWithCGImage:cgImgage];
6d8f1c76756d restore and update the old general settings
koda
parents: 3308
diff changeset
   147
        [array addObject:hatSprite];
6d8f1c76756d restore and update the old general settings
koda
parents: 3308
diff changeset
   148
        CGImageRelease(cgImgage);
6d8f1c76756d restore and update the old general settings
koda
parents: 3308
diff changeset
   149
        [hatSprite release];
6d8f1c76756d restore and update the old general settings
koda
parents: 3308
diff changeset
   150
    }
3315
4e2813713358 usa a cached dictionary and write only when needed
koda
parents: 3312
diff changeset
   151
    self.hatArray = array;
3312
6d8f1c76756d restore and update the old general settings
koda
parents: 3308
diff changeset
   152
    [array release];
3315
4e2813713358 usa a cached dictionary and write only when needed
koda
parents: 3312
diff changeset
   153
    
4e2813713358 usa a cached dictionary and write only when needed
koda
parents: 3312
diff changeset
   154
    [self.tableView reloadData];
3305
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   155
}
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   156
3329
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   157
// write on file if there has been a change
3328
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   158
-(void) viewWillDisappear:(BOOL)animated {
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   159
	[super viewWillDisappear:animated];
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   160
	
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   161
	NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",TEAMS_DIRECTORY(),self.title];
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   162
    if (isWriteNeeded) {
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   163
        [self.teamDictionary writeToFile:teamFile atomically:YES];
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   164
        NSLog(@"writing: %@",teamDictionary);
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   165
        isWriteNeeded = NO;
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   166
    }
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   167
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   168
	[teamFile release];
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   169
}
3329
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   170
3325
652a8ebdf667 moved around team creation
koda
parents: 3323
diff changeset
   171
// needed by other classes to warn about a user change
3315
4e2813713358 usa a cached dictionary and write only when needed
koda
parents: 3312
diff changeset
   172
-(void) setWriteNeeded {
4e2813713358 usa a cached dictionary and write only when needed
koda
parents: 3312
diff changeset
   173
    isWriteNeeded = YES;
4e2813713358 usa a cached dictionary and write only when needed
koda
parents: 3312
diff changeset
   174
}
4e2813713358 usa a cached dictionary and write only when needed
koda
parents: 3312
diff changeset
   175
3305
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   176
#pragma mark -
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   177
#pragma mark Table view data source
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   178
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   179
    return 3;
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   180
}
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   181
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   182
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
3312
6d8f1c76756d restore and update the old general settings
koda
parents: 3308
diff changeset
   183
    NSInteger rows = 0;
3305
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   184
    switch (section) {
3325
652a8ebdf667 moved around team creation
koda
parents: 3323
diff changeset
   185
        case 0: // team name
3305
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   186
            rows = 1;
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   187
            break;
3325
652a8ebdf667 moved around team creation
koda
parents: 3323
diff changeset
   188
        case 1: // team members
652a8ebdf667 moved around team creation
koda
parents: 3323
diff changeset
   189
            rows = MAX_HOGS;
3305
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   190
            break;
3325
652a8ebdf667 moved around team creation
koda
parents: 3323
diff changeset
   191
        case 2: // team details
652a8ebdf667 moved around team creation
koda
parents: 3323
diff changeset
   192
            rows = [self.secondaryItems count];
3305
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   193
            break;
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   194
        default:
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   195
            break;
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   196
    }
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   197
    return rows;
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   198
}
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   199
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   200
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   201
    static NSString *CellIdentifier = @"Cell";
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   202
    
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   203
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   204
    if (cell == nil) {
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   205
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   206
                                       reuseIdentifier:CellIdentifier] autorelease];
3329
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   207
        if ([indexPath section] == 1) {
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   208
            // create a uitextfield for each row, expand it to take the maximum size
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   209
            UITextField *aTextField = [[UITextField alloc] 
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   210
                                       initWithFrame:CGRectMake(42, 12, (cell.frame.size.width + cell.frame.size.width/3) - 42, 25)];
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   211
            aTextField.clearsOnBeginEditing = NO;
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   212
            aTextField.returnKeyType = UIReturnKeyDone;
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   213
            aTextField.adjustsFontSizeToFitWidth = YES;
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   214
            aTextField.delegate = self;
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   215
            aTextField.font = [UIFont boldSystemFontOfSize:[UIFont systemFontSize] + 2];
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   216
            aTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   217
            [aTextField addTarget:self action:@selector(save:) forControlEvents:UIControlEventEditingDidEndOnExit];
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   218
            [cell.contentView addSubview:aTextField];
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   219
            [aTextField release];
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   220
        }
3305
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   221
    }
3325
652a8ebdf667 moved around team creation
koda
parents: 3323
diff changeset
   222
3315
4e2813713358 usa a cached dictionary and write only when needed
koda
parents: 3312
diff changeset
   223
    NSArray *hogArray;
3305
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   224
    NSInteger row = [indexPath row];
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   225
    switch ([indexPath section]) {
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   226
        case 0:
3315
4e2813713358 usa a cached dictionary and write only when needed
koda
parents: 3312
diff changeset
   227
            cell.textLabel.text = self.title;
4e2813713358 usa a cached dictionary and write only when needed
koda
parents: 3312
diff changeset
   228
            cell.imageView.image = nil;
3328
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   229
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
3305
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   230
            break;
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   231
        case 1:
3315
4e2813713358 usa a cached dictionary and write only when needed
koda
parents: 3312
diff changeset
   232
            hogArray = [self.teamDictionary objectForKey:@"hedgehogs"];
3329
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   233
3315
4e2813713358 usa a cached dictionary and write only when needed
koda
parents: 3312
diff changeset
   234
            cell.imageView.image = [self.hatArray objectAtIndex:row];
3329
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   235
            
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   236
            for (UIView *oneView in cell.contentView.subviews) {
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   237
                if ([oneView isMemberOfClass:[UITextField class]]) {
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   238
                    // we find the uitextfied and we'll use its tag to understand which one is being edited
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   239
                    UITextField *textFieldFound = (UITextField *)oneView;
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   240
                    textFieldFound.text = [[hogArray objectAtIndex:row] objectForKey:@"hogname"];
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   241
                    textFieldFound.tag = row;
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   242
                }
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   243
            }
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   244
3328
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   245
            cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
3305
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   246
            break;
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   247
        case 2:
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   248
            cell.textLabel.text = [self.secondaryItems objectAtIndex:row];
3328
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   249
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
3325
652a8ebdf667 moved around team creation
koda
parents: 3323
diff changeset
   250
            switch (row) {
652a8ebdf667 moved around team creation
koda
parents: 3323
diff changeset
   251
                case 3: // flags
652a8ebdf667 moved around team creation
koda
parents: 3323
diff changeset
   252
                    cell.imageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.png",
652a8ebdf667 moved around team creation
koda
parents: 3323
diff changeset
   253
                                                                             FLAGS_DIRECTORY(),[teamDictionary objectForKey:@"flag"]]];
652a8ebdf667 moved around team creation
koda
parents: 3323
diff changeset
   254
                    break;
652a8ebdf667 moved around team creation
koda
parents: 3323
diff changeset
   255
                default:
652a8ebdf667 moved around team creation
koda
parents: 3323
diff changeset
   256
                    cell.imageView.image = nil;
652a8ebdf667 moved around team creation
koda
parents: 3323
diff changeset
   257
                    break;
652a8ebdf667 moved around team creation
koda
parents: 3323
diff changeset
   258
            }
3305
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   259
            break;
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   260
        default:
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   261
            break;
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   262
    }
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   263
    
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   264
    return cell;
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   265
}
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   266
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   267
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   268
#pragma mark -
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   269
#pragma mark Table view delegate
3329
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   270
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
3325
652a8ebdf667 moved around team creation
koda
parents: 3323
diff changeset
   271
    NSInteger row = [indexPath row];
652a8ebdf667 moved around team creation
koda
parents: 3323
diff changeset
   272
    UITableViewController *nextController;
3305
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   273
    if (1 == [indexPath section]) {
3329
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   274
        UITableViewCell *cell = [aTableView cellForRowAtIndexPath:indexPath];
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   275
        for (UIView *oneView in cell.contentView.subviews) {
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   276
            if ([oneView isMemberOfClass:[UITextField class]]) {
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   277
                textFieldBeingEdited = (UITextField *)oneView;
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   278
                textFieldBeingEdited.tag = row;
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   279
                [textFieldBeingEdited becomeFirstResponder];
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   280
            }
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   281
        }
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   282
        [aTableView deselectRowAtIndexPath:indexPath animated:NO];
3305
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   283
    }
3325
652a8ebdf667 moved around team creation
koda
parents: 3323
diff changeset
   284
    if (2 == [indexPath section]) {
652a8ebdf667 moved around team creation
koda
parents: 3323
diff changeset
   285
        //TODO: this part should be rewrittend with lazy loading instead of an array of controllers
652a8ebdf667 moved around team creation
koda
parents: 3323
diff changeset
   286
        nextController = [secondaryControllers objectAtIndex:row%2 ];              //TODO: fix the objectAtIndex
652a8ebdf667 moved around team creation
koda
parents: 3323
diff changeset
   287
        nextController.title = [secondaryItems objectAtIndex:row];
3328
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   288
        [nextController setTeamDictionary:teamDictionary];
3329
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   289
        [self.navigationController pushViewController:nextController animated:YES];
3325
652a8ebdf667 moved around team creation
koda
parents: 3323
diff changeset
   290
    }
3305
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   291
}
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   292
3329
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   293
// action to perform when you want to change a hog hat
3328
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   294
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   295
    if (nil == hogChildController) {
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   296
        hogChildController = [[HogHatViewController alloc] initWithStyle:UITableViewStyleGrouped];
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   297
    }
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   298
    
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   299
    // cache the dictionary file of the team, so that other controllers can modify it
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   300
    hogChildController.teamDictionary = self.teamDictionary;
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   301
    hogChildController.selectedHog = [indexPath row];
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   302
    
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   303
    [self.navigationController pushViewController:hogChildController animated:YES];
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   304
}
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   305
3305
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   306
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   307
#pragma mark -
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   308
#pragma mark Memory management
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   309
-(void) didReceiveMemoryWarning {
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   310
    // Releases the view if it doesn't have a superview.
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   311
    [super didReceiveMemoryWarning];
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   312
    // Relinquish ownership any cached data, images, etc that aren't in use.
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   313
}
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   314
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   315
-(void) viewDidUnload {
3329
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   316
    self.teamDictionary = nil;
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   317
    self.textFieldBeingEdited = nil;
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   318
    self.hatArray = nil;
3325
652a8ebdf667 moved around team creation
koda
parents: 3323
diff changeset
   319
    self.secondaryItems = nil;
3329
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   320
    self.secondaryControllers = nil;
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   321
    hogChildController = nil;
3315
4e2813713358 usa a cached dictionary and write only when needed
koda
parents: 3312
diff changeset
   322
    [super viewDidUnload];
3305
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   323
}
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   324
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   325
-(void) dealloc {
3329
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   326
    [teamDictionary release];
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   327
    [textFieldBeingEdited release];
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   328
    [hatArray release];
3305
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   329
    [secondaryItems release];
3329
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   330
    [secondaryControllers release];
d8e41ee0b3ae add support to editing hog names
koda
parents: 3328
diff changeset
   331
    [hogChildController release];
3305
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   332
    [super dealloc];
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   333
}
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   334
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   335
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   336
@end
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   337