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