cocoaTouch/TeamSettingsViewController.m
changeset 3323 091cf214bdd5
parent 3315 4e2813713358
child 3325 652a8ebdf667
equal deleted inserted replaced
3322:80afcb97eb46 3323:091cf214bdd5
     8 
     8 
     9 #import "TeamSettingsViewController.h"
     9 #import "TeamSettingsViewController.h"
    10 #import "SingleTeamViewController.h"
    10 #import "SingleTeamViewController.h"
    11 
    11 
    12 @implementation TeamSettingsViewController
    12 @implementation TeamSettingsViewController
    13 @synthesize list;
    13 @synthesize listOfTeams;
       
    14 
       
    15 
       
    16 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
       
    17     return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
       
    18 }
    14 
    19 
    15 #pragma mark -
    20 #pragma mark -
    16 #pragma mark View lifecycle
    21 #pragma mark View lifecycle
    17 - (void)viewDidLoad {
    22 - (void)viewDidLoad {
    18     [super viewDidLoad];
    23     [super viewDidLoad];
    19 
    24 
       
    25     UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Edit",@"from the team navigation")
       
    26                                                                    style:UIBarButtonItemStyleBordered
       
    27                                                                   target:self
       
    28                                                                   action:@selector(toggleEdit:)];
       
    29     self.navigationItem.rightBarButtonItem = editButton;
       
    30     [editButton release];
       
    31     
    20     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    32     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    21     NSString *teamsDirectory = [[paths objectAtIndex:0] stringByAppendingString:@"/Teams/"];
    33     NSString *teamsDirectory = [[paths objectAtIndex:0] stringByAppendingString:@"/Teams/"];
    22     
    34     
    23     NSArray *contents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:teamsDirectory
    35     NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:teamsDirectory error:NULL];
    24                                                                             error:NULL];
    36     NSMutableArray *array = [[NSMutableArray alloc] initWithArray: contentsOfDir copyItems:YES];
    25     self.list = contents;
    37     self.listOfTeams = array;
    26     //NSLog(@"%@\n%@", teamsDirectory, contents);
    38     [array release];
    27 
    39     NSLog(@"files: %@", self.listOfTeams);
    28     // Uncomment the following line to preserve selection between presentations.
    40 }
    29     // self.clearsSelectionOnViewWillAppear = NO;
    41 
    30     // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    42 // modifies the navigation bar to add the "Add" and "Done" buttons
    31     self.navigationItem.rightBarButtonItem = self.editButtonItem;
    43 -(void) toggleEdit:(id) sender {
    32 }
    44     BOOL isEditing = self.tableView.editing;
    33 
    45     [self.tableView setEditing:!isEditing animated:YES];
    34 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    46     
    35     return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
    47     if (isEditing) {
    36 }
    48         [self.navigationItem.rightBarButtonItem setTitle:NSLocalizedString(@"Edit",@"from the team navigation")];
    37 
    49         [self.navigationItem.rightBarButtonItem setStyle: UIBarButtonItemStyleBordered];
       
    50         self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem;
       
    51     } else {
       
    52         [self.navigationItem.rightBarButtonItem setTitle:NSLocalizedString(@"Done",@"from the team navigation")];
       
    53         [self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleDone];
       
    54         UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Add",@"from the team navigation")
       
    55                                                                       style:UIBarButtonItemStyleBordered
       
    56                                                                      target:self
       
    57                                                                      action:@selector(addTeam:)];
       
    58         self.navigationItem.leftBarButtonItem = addButton;
       
    59         [addButton release];
       
    60     }
       
    61 }
       
    62 
       
    63 // add a team file with default values and updates the table
       
    64 -(void) addTeam:(id) sender {
       
    65     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
       
    66     NSString *teamsDirectory = [[paths objectAtIndex:0] stringByAppendingString:@"/Teams/"];
       
    67     [[NSFileManager defaultManager] createDirectoryAtPath:teamsDirectory 
       
    68                               withIntermediateDirectories:NO 
       
    69                                                attributes:nil 
       
    70                                                     error:NULL];
       
    71     
       
    72     NSMutableArray *hedgehogs = [[NSMutableArray alloc] init];
       
    73     
       
    74     for (int i = 0; i < 8; i++) {
       
    75         NSString *hogName = [[NSString alloc] initWithFormat:@"hedgehog %d",i];
       
    76         NSDictionary *hog = [[NSDictionary alloc] initWithObjectsAndKeys:@"100",@"health",@"0",@"level",
       
    77                              hogName,@"hogname",@"NoHat",@"hat",nil];
       
    78         [hogName release];
       
    79         [hedgehogs addObject:hog];
       
    80         [hog release];
       
    81     }
       
    82     
       
    83     NSString *fileName = [[NSString alloc] initWithFormat:@"Default Team %u.plist", [self.listOfTeams count]];
       
    84 
       
    85     NSDictionary *defaultTeam = [[NSDictionary alloc] initWithObjectsAndKeys:@"4421353",@"color",@"0",@"hash",
       
    86                                  [fileName stringByDeletingPathExtension],@"teamname",@"Statue",@"grave",@"Plane",@"fort",
       
    87                                  @"Default",@"voicepack",@"hedgewars",@"flag",hedgehogs,@"hedgehogs",nil];
       
    88     [hedgehogs release];
       
    89     
       
    90     NSString *defaultTeamFile = [[NSString alloc] initWithFormat:@"%@/%@",teamsDirectory, fileName];
       
    91     [defaultTeam writeToFile:defaultTeamFile atomically:YES];
       
    92     [defaultTeamFile release];    
       
    93     [defaultTeam release];
       
    94     
       
    95     [self.listOfTeams addObject:fileName];
       
    96     [fileName release];
       
    97     
       
    98     [self.tableView reloadData];
       
    99     
       
   100 }
    38 
   101 
    39 #pragma mark -
   102 #pragma mark -
    40 #pragma mark Table view data source
   103 #pragma mark Table view data source
    41 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
   104 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    42     // Return the number of sections.
   105     // Return the number of sections.
    43     return 1;
   106     return 1;
    44 }
   107 }
    45 
   108 
    46 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   109 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    47     // Return the number of rows in the section.
   110     // Return the number of rows in the section.
    48     return [list count];
   111     return [listOfTeams count];
    49 }
   112 }
    50 
   113 
    51 // Customize the appearance of table view cells.
   114 // Customize the appearance of table view cells.
    52 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   115 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    53     
       
    54     static NSString *CellIdentifier = @"Cell";
   116     static NSString *CellIdentifier = @"Cell";
    55     
   117     
    56     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   118     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    57     if (cell == nil) {
   119     if (cell == nil) {
    58         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
   120         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    59     }
   121     }
    60     
   122     
    61     NSUInteger row = [indexPath row]; 
   123     NSUInteger row = [indexPath row]; 
    62     NSString *rowString = [[list objectAtIndex:row] stringByDeletingPathExtension]; 
   124     NSString *rowString = [[listOfTeams objectAtIndex:row] stringByDeletingPathExtension]; 
    63     cell.textLabel.text = rowString; 
   125     cell.textLabel.text = rowString; 
    64     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
   126     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    65     
   127     
    66     return cell;
   128     return cell;
    67 }
   129 }
    68 
   130 
    69 
   131 // delete the row and the file
    70 /*
   132 -(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    71 // Override to support conditional editing of the table view.
   133     NSUInteger row = [indexPath row];
    72 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
   134     
    73     // Return NO if you do not want the specified item to be editable.
   135     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    74     return YES;
   136     NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/Teams/%@",[paths objectAtIndex:0],[self.listOfTeams objectAtIndex:row]];
    75 }
   137 
    76 */
   138     [[NSFileManager defaultManager] removeItemAtPath:teamFile error:NULL];
    77 
   139     [teamFile release];
       
   140     
       
   141     [self.listOfTeams removeObjectAtIndex:row];
       
   142     [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
       
   143 }
    78 
   144 
    79 /*
   145 /*
    80 // Override to support editing the table view.
   146 // Override to support editing the table view.
    81 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
   147 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    82     
   148     
    88         // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
   154         // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    89     }   
   155     }   
    90 }
   156 }
    91 */
   157 */
    92 
   158 
    93 
       
    94 /*
   159 /*
    95 // Override to support rearranging the table view.
   160 // Override to support rearranging the table view.
    96 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
   161 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
    97 }
   162 }
    98 */
   163 */
    99 
       
   100 
   164 
   101 /*
   165 /*
   102 // Override to support conditional rearranging of the table view.
   166 // Override to support conditional rearranging of the table view.
   103 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
   167 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
   104     // Return NO if you do not want the item to be re-orderable.
   168     // Return NO if you do not want the item to be re-orderable.
   113     if (childController == nil) {
   177     if (childController == nil) {
   114         childController = [[SingleTeamViewController alloc] initWithStyle:UITableViewStyleGrouped];
   178         childController = [[SingleTeamViewController alloc] initWithStyle:UITableViewStyleGrouped];
   115     }
   179     }
   116     
   180     
   117     NSInteger row = [indexPath row];
   181     NSInteger row = [indexPath row];
   118     NSString *selectedTeam = [[list objectAtIndex:row] stringByDeletingPathExtension];
   182     NSString *selectedTeamFile = [listOfTeams objectAtIndex:row];
   119     
   183     NSLog(@"%@",selectedTeamFile);
   120     childController.title = selectedTeam;
   184     
       
   185         // load data about the team and extract info
       
   186     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
       
   187     NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/Teams/%@",[paths objectAtIndex:0],selectedTeamFile];
       
   188     NSMutableDictionary *teamDict = [[NSMutableDictionary alloc] initWithContentsOfFile:teamFile];
       
   189     [teamFile release];
       
   190     childController.teamDictionary = teamDict;
       
   191     [teamDict release];
       
   192     
       
   193     // this must be set so childController can load the correct plist
       
   194     //childController.title = [[listOfTeams objectAtIndex:row] stringByDeletingPathExtension];
   121     [self.navigationController pushViewController:childController animated:YES];
   195     [self.navigationController pushViewController:childController animated:YES];
   122 }
   196 }
   123 
   197 
   124 /*
   198 /*
   125 -(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
   199 -(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
   133 }
   207 }
   134 */
   208 */
   135 
   209 
   136 #pragma mark -
   210 #pragma mark -
   137 #pragma mark Memory management
   211 #pragma mark Memory management
   138 
   212 -(void) didReceiveMemoryWarning {
   139 - (void)didReceiveMemoryWarning {
       
   140     // Releases the view if it doesn't have a superview.
   213     // Releases the view if it doesn't have a superview.
   141     [super didReceiveMemoryWarning];
   214     [super didReceiveMemoryWarning];
   142     
   215     
   143     // Relinquish ownership any cached data, images, etc that aren't in use.
   216     // Relinquish ownership any cached data, images, etc that aren't in use.
   144 }
   217 }
   145 
   218 
   146 - (void)viewDidUnload {
   219 -(void) viewDidUnload {
   147     self.list = nil;
   220     self.listOfTeams = nil;
   148 }
   221 }
   149 
   222 
   150 
   223 -(void) dealloc {
   151 - (void)dealloc {
   224     [listOfTeams release];
   152     [list release];
   225     [childController release];
   153     if (nil != childController)
       
   154         [childController release];
       
   155     [super dealloc];
   226     [super dealloc];
   156 }
   227 }
   157 
   228 
   158 
   229 
   159 @end
   230 @end