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