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