author | inu |
Sun, 07 Nov 2010 11:23:30 -0500 | |
changeset 4170 | 7fb7386423d8 |
parent 3916 | e7d665a4ef42 |
child 4281 | e033cf015b2c |
permissions | -rw-r--r-- |
3829 | 1 |
/* |
2 |
* Hedgewars-iOS, a Hedgewars port for iOS devices |
|
3 |
* Copyright (c) 2009-2010 Vittorio Giovara <vittorio.giovara@gmail.com> |
|
4 |
* |
|
5 |
* This program is free software; you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
17 |
* |
|
18 |
* File created on 19/04/2010. |
|
19 |
*/ |
|
20 |
||
3547 | 21 |
|
22 |
#import "SchemeSettingsViewController.h" |
|
23 |
#import "CommodityFunctions.h" |
|
24 |
#import "SingleSchemeViewController.h" |
|
25 |
||
26 |
@implementation SchemeSettingsViewController |
|
27 |
@synthesize listOfSchemes; |
|
28 |
||
29 |
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { |
|
30 |
return rotationManager(interfaceOrientation); |
|
31 |
} |
|
32 |
||
33 |
#pragma mark - |
|
34 |
#pragma mark View lifecycle |
|
35 |
-(void) viewDidLoad { |
|
36 |
[super viewDidLoad]; |
|
3697 | 37 |
|
3916
e7d665a4ef42
implemented endless turns support and added Timeless scheme (also fixed a crasher)
koda
parents:
3903
diff
changeset
|
38 |
UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Edit",@"") |
3547 | 39 |
style:UIBarButtonItemStyleBordered |
40 |
target:self |
|
41 |
action:@selector(toggleEdit:)]; |
|
42 |
self.navigationItem.rightBarButtonItem = editButton; |
|
43 |
[editButton release]; |
|
44 |
} |
|
45 |
||
46 |
-(void) viewWillAppear:(BOOL) animated { |
|
47 |
[super viewWillAppear:animated]; |
|
3697 | 48 |
|
3547 | 49 |
NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:SCHEMES_DIRECTORY() error:NULL]; |
50 |
NSMutableArray *array = [[NSMutableArray alloc] initWithArray:contentsOfDir copyItems:YES]; |
|
51 |
self.listOfSchemes = array; |
|
52 |
[array release]; |
|
3697 | 53 |
|
3547 | 54 |
[self.tableView reloadData]; |
55 |
} |
|
56 |
||
57 |
// modifies the navigation bar to add the "Add" and "Done" buttons |
|
58 |
-(void) toggleEdit:(id) sender { |
|
59 |
BOOL isEditing = self.tableView.editing; |
|
60 |
[self.tableView setEditing:!isEditing animated:YES]; |
|
3697 | 61 |
|
3547 | 62 |
if (isEditing) { |
63 |
[self.navigationItem.rightBarButtonItem setTitle:NSLocalizedString(@"Edit",@"from the scheme panel")]; |
|
64 |
[self.navigationItem.rightBarButtonItem setStyle: UIBarButtonItemStyleBordered]; |
|
65 |
self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem; |
|
66 |
} else { |
|
67 |
[self.navigationItem.rightBarButtonItem setTitle:NSLocalizedString(@"Done",@"from the scheme panel")]; |
|
68 |
[self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleDone]; |
|
69 |
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Add",@"from the scheme panel") |
|
70 |
style:UIBarButtonItemStyleBordered |
|
71 |
target:self |
|
72 |
action:@selector(addScheme:)]; |
|
73 |
self.navigationItem.leftBarButtonItem = addButton; |
|
74 |
[addButton release]; |
|
75 |
} |
|
76 |
} |
|
77 |
||
78 |
-(void) addScheme:(id) sender { |
|
79 |
NSString *fileName = [[NSString alloc] initWithFormat:@"Scheme %u.plist", [self.listOfSchemes count]]; |
|
3697 | 80 |
|
3547 | 81 |
createSchemeNamed([fileName stringByDeletingPathExtension]); |
3697 | 82 |
|
3547 | 83 |
[self.listOfSchemes addObject:fileName]; |
84 |
[fileName release]; |
|
3697 | 85 |
|
3547 | 86 |
// order the array alphabetically, so schemes will keep their position |
87 |
[self.listOfSchemes sortUsingSelector:@selector(compare:)]; |
|
3697 | 88 |
|
3547 | 89 |
[self.tableView reloadData]; |
90 |
} |
|
91 |
||
92 |
#pragma mark - |
|
93 |
#pragma mark Table view data source |
|
94 |
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { |
|
95 |
return 1; |
|
96 |
} |
|
97 |
||
98 |
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
|
99 |
return [self.listOfSchemes count]; |
|
100 |
} |
|
101 |
||
102 |
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
|
103 |
static NSString *CellIdentifier = @"Cell"; |
|
3697 | 104 |
|
3547 | 105 |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
106 |
if (cell == nil) { |
|
107 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
|
108 |
} |
|
3697 | 109 |
|
110 |
NSUInteger row = [indexPath row]; |
|
111 |
NSString *rowString = [[self.listOfSchemes objectAtIndex:row] stringByDeletingPathExtension]; |
|
112 |
cell.textLabel.text = rowString; |
|
3547 | 113 |
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
3697 | 114 |
|
3547 | 115 |
return cell; |
116 |
} |
|
117 |
||
118 |
// delete the row and the file |
|
119 |
-(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { |
|
120 |
NSUInteger row = [indexPath row]; |
|
3697 | 121 |
|
3547 | 122 |
NSString *schemeFile = [[NSString alloc] initWithFormat:@"%@/%@",SCHEMES_DIRECTORY(),[self.listOfSchemes objectAtIndex:row]]; |
123 |
[[NSFileManager defaultManager] removeItemAtPath:schemeFile error:NULL]; |
|
124 |
[schemeFile release]; |
|
3697 | 125 |
|
3547 | 126 |
[self.listOfSchemes removeObjectAtIndex:row]; |
127 |
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; |
|
128 |
} |
|
129 |
||
130 |
#pragma mark - |
|
131 |
#pragma mark Table view delegate |
|
132 |
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
|
133 |
if (childController == nil) { |
|
134 |
childController = [[SingleSchemeViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
135 |
} |
|
3697 | 136 |
|
3547 | 137 |
NSInteger row = [indexPath row]; |
138 |
NSString *selectedSchemeFile = [self.listOfSchemes objectAtIndex:row]; |
|
3697 | 139 |
|
3547 | 140 |
// this must be set so childController can load the correct plist |
3659 | 141 |
childController.schemeName = [selectedSchemeFile stringByDeletingPathExtension]; |
3547 | 142 |
[childController.tableView setContentOffset:CGPointMake(0,0) animated:NO]; |
143 |
||
144 |
[self.navigationController pushViewController:childController animated:YES]; |
|
145 |
} |
|
146 |
||
147 |
||
148 |
#pragma mark - |
|
149 |
#pragma mark Memory management |
|
150 |
-(void)didReceiveMemoryWarning { |
|
151 |
[super didReceiveMemoryWarning]; |
|
152 |
if (childController.view.superview == nil ) |
|
153 |
childController = nil; |
|
3659 | 154 |
MSG_MEMCLEAN(); |
3547 | 155 |
} |
156 |
||
157 |
-(void) viewDidUnload { |
|
158 |
self.listOfSchemes = nil; |
|
159 |
childController = nil; |
|
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3659
diff
changeset
|
160 |
MSG_DIDUNLOAD(); |
3547 | 161 |
[super viewDidUnload]; |
162 |
} |
|
163 |
||
164 |
||
165 |
-(void) dealloc { |
|
3659 | 166 |
[listOfSchemes release]; |
3547 | 167 |
[childController release]; |
168 |
[super dealloc]; |
|
169 |
} |
|
170 |
||
171 |
||
172 |
@end |
|
173 |