project_files/HedgewarsMobile/Classes/SchemeSettingsViewController.m
changeset 3514 59dbd31e9953
parent 3490 016b3172b645
child 3522 156c04c6a3d8
equal deleted inserted replaced
3513:f589230fa21b 3514:59dbd31e9953
       
     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 #pragma mark -
       
   120 #pragma mark Table view delegate
       
   121 
       
   122 -(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
       
   123     if (childController == nil) {
       
   124         childController = [[SingleSchemeViewController alloc] initWithStyle:UITableViewStyleGrouped];
       
   125     }
       
   126     
       
   127     NSInteger row = [indexPath row];
       
   128     NSString *selectedSchemeFile = [self.listOfSchemes objectAtIndex:row];
       
   129     
       
   130     // this must be set so childController can load the correct plist
       
   131     childController.title = [selectedSchemeFile stringByDeletingPathExtension];
       
   132     [childController.tableView setContentOffset:CGPointMake(0,0) animated:NO];
       
   133 
       
   134     [self.navigationController pushViewController:childController animated:YES];
       
   135 }
       
   136 
       
   137 
       
   138 #pragma mark -
       
   139 #pragma mark Memory management
       
   140 -(void)didReceiveMemoryWarning {
       
   141     [super didReceiveMemoryWarning];
       
   142     if (childController.view.superview == nil )
       
   143         childController = nil;
       
   144 }
       
   145 
       
   146 -(void) viewDidUnload {
       
   147     self.listOfSchemes = nil;
       
   148     childController = nil;
       
   149     [super viewDidUnload];
       
   150     MSG_DIDUNLOAD();
       
   151 }
       
   152 
       
   153 
       
   154 -(void) dealloc {
       
   155     [self.listOfSchemes release];
       
   156     [childController release];
       
   157     [super dealloc];
       
   158 }
       
   159 
       
   160 
       
   161 @end
       
   162