3356
|
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"
|
3479
|
10 |
#import "CommodityFunctions.h"
|
|
11 |
#import "SingleSchemeViewController.h"
|
3356
|
12 |
|
|
13 |
@implementation SchemeSettingsViewController
|
3479
|
14 |
@synthesize listOfSchemes;
|
3356
|
15 |
|
3479
|
16 |
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
|
|
17 |
return rotationManager(interfaceOrientation);
|
3356
|
18 |
}
|
|
19 |
|
|
20 |
|
|
21 |
#pragma mark -
|
|
22 |
#pragma mark View lifecycle
|
3479
|
23 |
-(void) viewDidLoad {
|
3356
|
24 |
[super viewDidLoad];
|
3479
|
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 |
|
3356
|
33 |
}
|
|
34 |
|
3479
|
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];
|
3356
|
44 |
}
|
|
45 |
|
3479
|
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 |
}
|
3356
|
80 |
|
|
81 |
#pragma mark -
|
|
82 |
#pragma mark Table view data source
|
3479
|
83 |
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
|
3356
|
84 |
return 1;
|
|
85 |
}
|
|
86 |
|
3479
|
87 |
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
|
|
88 |
return [self.listOfSchemes count];
|
3356
|
89 |
}
|
|
90 |
|
3479
|
91 |
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
|
3356
|
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 |
|
3479
|
99 |
NSUInteger row = [indexPath row];
|
|
100 |
NSString *rowString = [[self.listOfSchemes objectAtIndex:row] stringByDeletingPathExtension];
|
|
101 |
cell.textLabel.text = rowString;
|
|
102 |
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
|
3356
|
103 |
|
|
104 |
return cell;
|
|
105 |
}
|
|
106 |
|
3479
|
107 |
// delete the row and the file
|
|
108 |
-(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
109 |
NSUInteger row = [indexPath row];
|
3356
|
110 |
|
3479
|
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];
|
3356
|
117 |
}
|
|
118 |
|
|
119 |
#pragma mark -
|
|
120 |
#pragma mark Table view delegate
|
3479
|
121 |
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
122 |
if (childController == nil) {
|
|
123 |
childController = [[SingleSchemeViewController alloc] initWithStyle:UITableViewStyleGrouped];
|
|
124 |
}
|
|
125 |
|
|
126 |
NSInteger row = [indexPath row];
|
|
127 |
NSString *selectedSchemeFile = [self.listOfSchemes objectAtIndex:row];
|
|
128 |
|
|
129 |
// this must be set so childController can load the correct plist
|
|
130 |
childController.title = [selectedSchemeFile stringByDeletingPathExtension];
|
|
131 |
[childController.tableView setContentOffset:CGPointMake(0,0) animated:NO];
|
|
132 |
|
|
133 |
[self.navigationController pushViewController:childController animated:YES];
|
3356
|
134 |
}
|
|
135 |
|
|
136 |
|
|
137 |
#pragma mark -
|
|
138 |
#pragma mark Memory management
|
3479
|
139 |
-(void)didReceiveMemoryWarning {
|
3356
|
140 |
[super didReceiveMemoryWarning];
|
3490
|
141 |
if (childController.view.superview == nil )
|
|
142 |
childController = nil;
|
3356
|
143 |
}
|
|
144 |
|
3479
|
145 |
-(void) viewDidUnload {
|
|
146 |
self.listOfSchemes = nil;
|
|
147 |
childController = nil;
|
3490
|
148 |
[super viewDidUnload];
|
|
149 |
MSG_DIDUNLOAD();
|
3356
|
150 |
}
|
|
151 |
|
|
152 |
|
3479
|
153 |
-(void) dealloc {
|
|
154 |
[self.listOfSchemes release];
|
|
155 |
[childController release];
|
3356
|
156 |
[super dealloc];
|
|
157 |
}
|
|
158 |
|
|
159 |
|
|
160 |
@end
|
|
161 |
|