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