author | koda |
Mon, 26 Mar 2012 03:24:50 +0200 | |
changeset 6822 | 206db098f8c5 |
parent 6700 | e04da46ee43c |
child 6832 | fae8fd118da9 |
permissions | -rw-r--r-- |
3829 | 1 |
/* |
2 |
* Hedgewars-iOS, a Hedgewars port for iOS devices |
|
6700 | 3 |
* Copyright (c) 2009-2012 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 02/04/2010. |
|
19 |
*/ |
|
20 |
||
3547 | 21 |
|
22 |
#import "TeamSettingsViewController.h" |
|
23 |
#import "SingleTeamViewController.h" |
|
24 |
||
25 |
@implementation TeamSettingsViewController |
|
26 |
@synthesize listOfTeams; |
|
27 |
||
28 |
||
29 |
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation { |
|
30 |
return rotationManager(interfaceOrientation); |
|
31 |
} |
|
32 |
||
33 |
#pragma mark - |
|
34 |
#pragma mark View lifecycle |
|
35 |
// add an edit button |
|
36 |
-(void) viewDidLoad { |
|
37 |
[super viewDidLoad]; |
|
38 |
||
3916
e7d665a4ef42
implemented endless turns support and added Timeless scheme (also fixed a crasher)
koda
parents:
3829
diff
changeset
|
39 |
UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Edit",@"") |
3547 | 40 |
style:UIBarButtonItemStyleBordered |
41 |
target:self |
|
42 |
action:@selector(toggleEdit:)]; |
|
43 |
self.navigationItem.rightBarButtonItem = editButton; |
|
44 |
[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:
5208
diff
changeset
|
45 |
|
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:
5208
diff
changeset
|
46 |
self.navigationItem.title = @"List of teams"; |
3547 | 47 |
} |
48 |
||
49 |
// load the list of teams in the teams directory |
|
50 |
-(void) viewWillAppear:(BOOL)animated { |
|
51 |
[super viewWillAppear:animated]; |
|
3697 | 52 |
|
3547 | 53 |
NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:TEAMS_DIRECTORY() error:NULL]; |
54 |
NSMutableArray *array = [[NSMutableArray alloc] initWithArray:contentsOfDir copyItems:YES]; |
|
55 |
self.listOfTeams = array; |
|
56 |
[array release]; |
|
3697 | 57 |
|
3547 | 58 |
[self.tableView reloadData]; |
59 |
} |
|
60 |
||
61 |
// modifies the navigation bar to add the "Add" and "Done" buttons |
|
62 |
-(void) toggleEdit:(id) sender { |
|
63 |
BOOL isEditing = self.tableView.editing; |
|
64 |
[self.tableView setEditing:!isEditing animated:YES]; |
|
3697 | 65 |
|
3547 | 66 |
if (isEditing) { |
67 |
[self.navigationItem.rightBarButtonItem setTitle:NSLocalizedString(@"Edit",@"from the team panel")]; |
|
68 |
[self.navigationItem.rightBarButtonItem setStyle: UIBarButtonItemStyleBordered]; |
|
69 |
self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem; |
|
70 |
} else { |
|
71 |
[self.navigationItem.rightBarButtonItem setTitle:NSLocalizedString(@"Done",@"from the team panel")]; |
|
72 |
[self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleDone]; |
|
73 |
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Add",@"from the team panel") |
|
74 |
style:UIBarButtonItemStyleBordered |
|
75 |
target:self |
|
76 |
action:@selector(addTeam:)]; |
|
77 |
self.navigationItem.leftBarButtonItem = addButton; |
|
78 |
[addButton release]; |
|
79 |
} |
|
80 |
} |
|
81 |
||
82 |
// add a team file with default values and updates the table |
|
83 |
-(void) addTeam:(id) sender { |
|
84 |
NSString *fileName = [[NSString alloc] initWithFormat:@"Default Team %u.plist", [self.listOfTeams count]]; |
|
3697 | 85 |
|
6103 | 86 |
[CreationChamber createTeamNamed:[fileName stringByDeletingPathExtension]]; |
3697 | 87 |
|
3547 | 88 |
[self.listOfTeams addObject:fileName]; |
3697 | 89 |
|
3547 | 90 |
// order the array alphabetically, so teams will keep their position |
91 |
[self.listOfTeams sortUsingSelector:@selector(compare:)]; |
|
4287 | 92 |
[self.tableView reloadData]; |
3697 | 93 |
|
4287 | 94 |
NSInteger index = [self.listOfTeams indexOfObject:fileName]; |
95 |
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; |
|
96 |
[fileName release]; |
|
3547 | 97 |
} |
98 |
||
99 |
#pragma mark - |
|
100 |
#pragma mark Table view data source |
|
101 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
|
102 |
return 1; |
|
103 |
} |
|
104 |
||
105 |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
|
106 |
return [self.listOfTeams count]; |
|
107 |
} |
|
108 |
||
109 |
// Customize the appearance of table view cells. |
|
110 |
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
|
111 |
static NSString *CellIdentifier = @"Cell"; |
|
3697 | 112 |
|
3547 | 113 |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
114 |
if (cell == nil) { |
|
115 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
|
116 |
} |
|
3697 | 117 |
|
118 |
NSUInteger row = [indexPath row]; |
|
119 |
NSString *rowString = [[self.listOfTeams objectAtIndex:row] stringByDeletingPathExtension]; |
|
120 |
cell.textLabel.text = rowString; |
|
3547 | 121 |
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
3697 | 122 |
|
3547 | 123 |
return cell; |
124 |
} |
|
125 |
||
126 |
// delete the row and the file |
|
127 |
-(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { |
|
128 |
NSUInteger row = [indexPath row]; |
|
3697 | 129 |
|
3547 | 130 |
NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@",TEAMS_DIRECTORY(),[self.listOfTeams objectAtIndex:row]]; |
131 |
[[NSFileManager defaultManager] removeItemAtPath:teamFile error:NULL]; |
|
132 |
[teamFile release]; |
|
3697 | 133 |
|
3547 | 134 |
[self.listOfTeams 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:
5208
diff
changeset
|
135 |
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; |
3547 | 136 |
} |
137 |
||
138 |
||
139 |
#pragma mark - |
|
140 |
#pragma mark Table view delegate |
|
141 |
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
|
142 |
if (childController == nil) { |
|
143 |
childController = [[SingleTeamViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
144 |
} |
|
3697 | 145 |
|
3547 | 146 |
NSInteger row = [indexPath row]; |
147 |
NSString *selectedTeamFile = [listOfTeams objectAtIndex:row]; |
|
3697 | 148 |
|
3547 | 149 |
// this must be set so childController can load the correct plist |
3660 | 150 |
childController.teamName = [selectedTeamFile stringByDeletingPathExtension]; |
3547 | 151 |
[childController.tableView setContentOffset:CGPointMake(0,0) animated:NO]; |
152 |
||
153 |
[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:
5208
diff
changeset
|
154 |
[tableView deselectRowAtIndexPath:indexPath animated:YES]; |
3547 | 155 |
} |
156 |
||
157 |
||
158 |
#pragma mark - |
|
159 |
#pragma mark Memory management |
|
160 |
-(void) didReceiveMemoryWarning { |
|
161 |
// Releases the view if it doesn't have a superview. |
|
162 |
[super didReceiveMemoryWarning]; |
|
163 |
// Relinquish ownership any cached data, images, etc that aren't in use. |
|
164 |
if (childController.view.superview == nil ) |
|
165 |
childController = nil; |
|
166 |
} |
|
167 |
||
168 |
-(void) viewDidUnload { |
|
169 |
self.listOfTeams = nil; |
|
170 |
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:
3660
diff
changeset
|
171 |
MSG_DIDUNLOAD(); |
3547 | 172 |
[super viewDidUnload]; |
173 |
} |
|
174 |
||
175 |
-(void) dealloc { |
|
5208 | 176 |
releaseAndNil(listOfTeams); |
177 |
releaseAndNil(childController); |
|
3547 | 178 |
[super dealloc]; |
179 |
} |
|
180 |
||
181 |
||
182 |
@end |
|
183 |