project_files/HedgewarsMobile/Classes/TeamSettingsViewController.m
author koda
Tue, 06 Jul 2010 03:33:01 +0200
changeset 3625 9f1d79e01a60
parent 3547 02875b1145b7
child 3660 bc125bea5849
permissions -rw-r--r--
icons \o/ fixed orientation and launch image issues prevent a crash when loading lobby multiple times
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     1
//
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     2
//  TeamSettingsViewController.m
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     3
//  HedgewarsMobile
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     4
//
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     5
//  Created by Vittorio on 02/04/10.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     6
//  Copyright 2010 __MyCompanyName__. All rights reserved.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     7
//
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     8
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     9
#import "TeamSettingsViewController.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    10
#import "SingleTeamViewController.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    11
#import "CommodityFunctions.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    12
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    13
@implementation TeamSettingsViewController
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    14
@synthesize listOfTeams;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    15
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    16
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    17
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    18
    return rotationManager(interfaceOrientation);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    19
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    20
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    21
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    22
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    23
#pragma mark View lifecycle
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    24
// add an edit button
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    25
-(void) viewDidLoad {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    26
    [super viewDidLoad];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    27
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    28
    UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Edit",@"from the team panel")
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    29
                                                                   style:UIBarButtonItemStyleBordered
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    30
                                                                  target:self
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    31
                                                                  action:@selector(toggleEdit:)];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    32
    self.navigationItem.rightBarButtonItem = editButton;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    33
    [editButton release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    34
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    35
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    36
// load the list of teams in the teams directory
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    37
-(void) viewWillAppear:(BOOL)animated {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    38
    [super viewWillAppear:animated];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    39
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    40
    NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:TEAMS_DIRECTORY() error:NULL];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    41
    NSMutableArray *array = [[NSMutableArray alloc] initWithArray:contentsOfDir copyItems:YES];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    42
    self.listOfTeams = array;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    43
    [array release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    44
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    45
    [self.tableView reloadData];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    46
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    47
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    48
// modifies the navigation bar to add the "Add" and "Done" buttons
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    49
-(void) toggleEdit:(id) sender {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    50
    BOOL isEditing = self.tableView.editing;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    51
    [self.tableView setEditing:!isEditing animated:YES];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    52
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    53
    if (isEditing) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    54
        [self.navigationItem.rightBarButtonItem setTitle:NSLocalizedString(@"Edit",@"from the team panel")];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    55
        [self.navigationItem.rightBarButtonItem setStyle: UIBarButtonItemStyleBordered];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    56
        self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    57
    } else {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    58
        [self.navigationItem.rightBarButtonItem setTitle:NSLocalizedString(@"Done",@"from the team panel")];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    59
        [self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleDone];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    60
        UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Add",@"from the team panel")
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    61
                                                                      style:UIBarButtonItemStyleBordered
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    62
                                                                     target:self
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    63
                                                                     action:@selector(addTeam:)];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    64
        self.navigationItem.leftBarButtonItem = addButton;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    65
        [addButton release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    66
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    67
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    68
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    69
// add a team file with default values and updates the table
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    70
-(void) addTeam:(id) sender {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    71
    NSString *fileName = [[NSString alloc] initWithFormat:@"Default Team %u.plist", [self.listOfTeams count]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    72
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    73
    createTeamNamed([fileName stringByDeletingPathExtension]);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    74
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    75
    [self.listOfTeams addObject:fileName];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    76
    [fileName release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    77
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    78
    // order the array alphabetically, so teams will keep their position
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    79
    [self.listOfTeams sortUsingSelector:@selector(compare:)];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    80
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    81
    [self.tableView reloadData];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    82
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    83
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    84
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    85
#pragma mark Table view data source
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    86
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    87
    return 1;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    88
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    89
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    90
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    91
    return [self.listOfTeams count];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    92
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    93
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    94
// Customize the appearance of table view cells.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    95
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    96
    static NSString *CellIdentifier = @"Cell";
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    97
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    98
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    99
    if (cell == nil) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   100
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   101
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   102
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   103
    NSUInteger row = [indexPath row]; 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   104
    NSString *rowString = [[self.listOfTeams objectAtIndex:row] stringByDeletingPathExtension]; 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   105
    cell.textLabel.text = rowString; 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   106
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   107
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   108
    return cell;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   109
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   110
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   111
// delete the row and the file
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   112
-(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   113
    NSUInteger row = [indexPath row];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   114
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   115
    NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@",TEAMS_DIRECTORY(),[self.listOfTeams objectAtIndex:row]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   116
    [[NSFileManager defaultManager] removeItemAtPath:teamFile error:NULL];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   117
    [teamFile release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   118
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   119
    [self.listOfTeams removeObjectAtIndex:row];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   120
    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   121
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   122
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   123
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   124
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   125
#pragma mark Table view delegate
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   126
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   127
    if (childController == nil) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   128
        childController = [[SingleTeamViewController alloc] initWithStyle:UITableViewStyleGrouped];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   129
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   130
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   131
    NSInteger row = [indexPath row];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   132
    NSString *selectedTeamFile = [listOfTeams objectAtIndex:row];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   133
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   134
    // this must be set so childController can load the correct plist
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   135
    childController.title = [selectedTeamFile stringByDeletingPathExtension];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   136
    [childController.tableView setContentOffset:CGPointMake(0,0) animated:NO];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   137
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   138
    [self.navigationController pushViewController:childController animated:YES];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   139
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   140
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   141
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   142
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   143
#pragma mark Memory management
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   144
-(void) didReceiveMemoryWarning {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   145
    // Releases the view if it doesn't have a superview.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   146
    [super didReceiveMemoryWarning];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   147
    // Relinquish ownership any cached data, images, etc that aren't in use.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   148
    if (childController.view.superview == nil )
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   149
        childController = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   150
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   151
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   152
-(void) viewDidUnload {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   153
    self.listOfTeams = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   154
    childController = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   155
    [super viewDidUnload];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   156
    MSG_DIDUNLOAD();
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   157
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   158
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   159
-(void) dealloc {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   160
    [self.listOfTeams release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   161
    [childController release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   162
    [super dealloc];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   163
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   164
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   165
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   166
@end
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   167