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