author | unc0rr |
Fri, 03 Feb 2012 14:21:07 +0400 | |
changeset 6618 | 2d3232069c4b |
parent 6103 | 55ecfbf834e3 |
child 6700 | e04da46ee43c |
permissions | -rw-r--r-- |
3829 | 1 |
/* |
2 |
* Hedgewars-iOS, a Hedgewars port for iOS devices |
|
4976 | 3 |
* Copyright (c) 2009-2011 Vittorio Giovara <vittorio.giovara@gmail.com> |
3829 | 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" |
|
4281 | 23 |
#import "CreationChamber.h" |
3547 | 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]; |
|
6074
047eaed35cbb
ios major refactoring for ios settings, now they are presented differently on iphone/ipad, code is simplified and optimized, and ui is a little refreshed (eg. no more stuck selected fields)
koda
parents:
5984
diff
changeset
|
44 |
|
047eaed35cbb
ios major refactoring for ios settings, now they are presented differently on iphone/ipad, code is simplified and optimized, and ui is a little refreshed (eg. no more stuck selected fields)
koda
parents:
5984
diff
changeset
|
45 |
self.navigationItem.title = @"List of schemes"; |
3547 | 46 |
} |
47 |
||
48 |
-(void) viewWillAppear:(BOOL) animated { |
|
49 |
[super viewWillAppear:animated]; |
|
3697 | 50 |
|
3547 | 51 |
NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:SCHEMES_DIRECTORY() error:NULL]; |
52 |
NSMutableArray *array = [[NSMutableArray alloc] initWithArray:contentsOfDir copyItems:YES]; |
|
53 |
self.listOfSchemes = array; |
|
54 |
[array release]; |
|
3697 | 55 |
|
3547 | 56 |
[self.tableView reloadData]; |
57 |
} |
|
58 |
||
59 |
// modifies the navigation bar to add the "Add" and "Done" buttons |
|
60 |
-(void) toggleEdit:(id) sender { |
|
61 |
BOOL isEditing = self.tableView.editing; |
|
62 |
[self.tableView setEditing:!isEditing animated:YES]; |
|
3697 | 63 |
|
3547 | 64 |
if (isEditing) { |
65 |
[self.navigationItem.rightBarButtonItem setTitle:NSLocalizedString(@"Edit",@"from the scheme panel")]; |
|
66 |
[self.navigationItem.rightBarButtonItem setStyle: UIBarButtonItemStyleBordered]; |
|
67 |
self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem; |
|
68 |
} else { |
|
69 |
[self.navigationItem.rightBarButtonItem setTitle:NSLocalizedString(@"Done",@"from the scheme panel")]; |
|
70 |
[self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleDone]; |
|
71 |
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Add",@"from the scheme panel") |
|
72 |
style:UIBarButtonItemStyleBordered |
|
73 |
target:self |
|
74 |
action:@selector(addScheme:)]; |
|
75 |
self.navigationItem.leftBarButtonItem = addButton; |
|
76 |
[addButton release]; |
|
77 |
} |
|
78 |
} |
|
79 |
||
80 |
-(void) addScheme:(id) sender { |
|
81 |
NSString *fileName = [[NSString alloc] initWithFormat:@"Scheme %u.plist", [self.listOfSchemes count]]; |
|
3697 | 82 |
|
6103 | 83 |
[CreationChamber createSchemeNamed:[fileName stringByDeletingPathExtension]]; |
3697 | 84 |
|
3547 | 85 |
[self.listOfSchemes addObject:fileName]; |
3697 | 86 |
|
3547 | 87 |
// order the array alphabetically, so schemes will keep their position |
88 |
[self.listOfSchemes sortUsingSelector:@selector(compare:)]; |
|
4287 | 89 |
[self.tableView reloadData]; |
3697 | 90 |
|
4287 | 91 |
NSInteger index = [self.listOfSchemes indexOfObject:fileName]; |
92 |
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; |
|
93 |
[fileName release]; |
|
3547 | 94 |
} |
95 |
||
96 |
#pragma mark - |
|
97 |
#pragma mark Table view data source |
|
98 |
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { |
|
99 |
return 1; |
|
100 |
} |
|
101 |
||
102 |
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
|
103 |
return [self.listOfSchemes count]; |
|
104 |
} |
|
105 |
||
106 |
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
|
107 |
static NSString *CellIdentifier = @"Cell"; |
|
3697 | 108 |
|
3547 | 109 |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
110 |
if (cell == nil) { |
|
111 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
|
112 |
} |
|
3697 | 113 |
|
114 |
NSUInteger row = [indexPath row]; |
|
115 |
NSString *rowString = [[self.listOfSchemes objectAtIndex:row] stringByDeletingPathExtension]; |
|
116 |
cell.textLabel.text = rowString; |
|
3547 | 117 |
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
3697 | 118 |
|
3547 | 119 |
return cell; |
120 |
} |
|
121 |
||
122 |
// delete the row and the file |
|
123 |
-(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { |
|
124 |
NSUInteger row = [indexPath row]; |
|
3697 | 125 |
|
3547 | 126 |
NSString *schemeFile = [[NSString alloc] initWithFormat:@"%@/%@",SCHEMES_DIRECTORY(),[self.listOfSchemes objectAtIndex:row]]; |
127 |
[[NSFileManager defaultManager] removeItemAtPath:schemeFile error:NULL]; |
|
128 |
[schemeFile release]; |
|
3697 | 129 |
|
3547 | 130 |
[self.listOfSchemes removeObjectAtIndex:row]; |
6074
047eaed35cbb
ios major refactoring for ios settings, now they are presented differently on iphone/ipad, code is simplified and optimized, and ui is a little refreshed (eg. no more stuck selected fields)
koda
parents:
5984
diff
changeset
|
131 |
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; |
3547 | 132 |
} |
133 |
||
134 |
#pragma mark - |
|
135 |
#pragma mark Table view delegate |
|
136 |
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
|
137 |
if (childController == nil) { |
|
138 |
childController = [[SingleSchemeViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
139 |
} |
|
3697 | 140 |
|
3547 | 141 |
NSInteger row = [indexPath row]; |
142 |
NSString *selectedSchemeFile = [self.listOfSchemes objectAtIndex:row]; |
|
3697 | 143 |
|
3547 | 144 |
// this must be set so childController can load the correct plist |
3659 | 145 |
childController.schemeName = [selectedSchemeFile stringByDeletingPathExtension]; |
3547 | 146 |
[childController.tableView setContentOffset:CGPointMake(0,0) animated:NO]; |
147 |
||
148 |
[self.navigationController pushViewController:childController animated:YES]; |
|
6074
047eaed35cbb
ios major refactoring for ios settings, now they are presented differently on iphone/ipad, code is simplified and optimized, and ui is a little refreshed (eg. no more stuck selected fields)
koda
parents:
5984
diff
changeset
|
149 |
[tableView deselectRowAtIndexPath:indexPath animated:YES]; |
3547 | 150 |
} |
151 |
||
152 |
||
153 |
#pragma mark - |
|
154 |
#pragma mark Memory management |
|
155 |
-(void)didReceiveMemoryWarning { |
|
156 |
[super didReceiveMemoryWarning]; |
|
157 |
if (childController.view.superview == nil ) |
|
158 |
childController = nil; |
|
3659 | 159 |
MSG_MEMCLEAN(); |
3547 | 160 |
} |
161 |
||
162 |
-(void) viewDidUnload { |
|
163 |
self.listOfSchemes = nil; |
|
164 |
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
|
165 |
MSG_DIDUNLOAD(); |
3547 | 166 |
[super viewDidUnload]; |
167 |
} |
|
168 |
||
169 |
||
170 |
-(void) dealloc { |
|
5208 | 171 |
releaseAndNil(listOfSchemes); |
172 |
releaseAndNil(childController); |
|
3547 | 173 |
[super dealloc]; |
174 |
} |
|
175 |
||
176 |
||
177 |
@end |
|
178 |