author | koda |
Sun, 10 Oct 2010 22:32:01 +0200 | |
changeset 3948 | 24daa33a3114 |
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 02/04/2010. |
|
19 |
*/ |
|
20 |
||
3547 | 21 |
|
22 |
#import "TeamSettingsViewController.h" |
|
23 |
#import "SingleTeamViewController.h" |
|
24 |
#import "CommodityFunctions.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]; |
|
46 |
} |
|
47 |
||
48 |
// load the list of teams in the teams directory |
|
49 |
-(void) viewWillAppear:(BOOL)animated { |
|
50 |
[super viewWillAppear:animated]; |
|
3697 | 51 |
|
3547 | 52 |
NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:TEAMS_DIRECTORY() error:NULL]; |
53 |
NSMutableArray *array = [[NSMutableArray alloc] initWithArray:contentsOfDir copyItems:YES]; |
|
54 |
self.listOfTeams = array; |
|
55 |
[array release]; |
|
3697 | 56 |
|
3547 | 57 |
[self.tableView reloadData]; |
58 |
} |
|
59 |
||
60 |
// modifies the navigation bar to add the "Add" and "Done" buttons |
|
61 |
-(void) toggleEdit:(id) sender { |
|
62 |
BOOL isEditing = self.tableView.editing; |
|
63 |
[self.tableView setEditing:!isEditing animated:YES]; |
|
3697 | 64 |
|
3547 | 65 |
if (isEditing) { |
66 |
[self.navigationItem.rightBarButtonItem setTitle:NSLocalizedString(@"Edit",@"from the team panel")]; |
|
67 |
[self.navigationItem.rightBarButtonItem setStyle: UIBarButtonItemStyleBordered]; |
|
68 |
self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem; |
|
69 |
} else { |
|
70 |
[self.navigationItem.rightBarButtonItem setTitle:NSLocalizedString(@"Done",@"from the team panel")]; |
|
71 |
[self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleDone]; |
|
72 |
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Add",@"from the team panel") |
|
73 |
style:UIBarButtonItemStyleBordered |
|
74 |
target:self |
|
75 |
action:@selector(addTeam:)]; |
|
76 |
self.navigationItem.leftBarButtonItem = addButton; |
|
77 |
[addButton release]; |
|
78 |
} |
|
79 |
} |
|
80 |
||
81 |
// add a team file with default values and updates the table |
|
82 |
-(void) addTeam:(id) sender { |
|
83 |
NSString *fileName = [[NSString alloc] initWithFormat:@"Default Team %u.plist", [self.listOfTeams count]]; |
|
3697 | 84 |
|
3547 | 85 |
createTeamNamed([fileName stringByDeletingPathExtension]); |
3697 | 86 |
|
3547 | 87 |
[self.listOfTeams addObject:fileName]; |
88 |
[fileName release]; |
|
3697 | 89 |
|
3547 | 90 |
// order the array alphabetically, so teams will keep their position |
91 |
[self.listOfTeams sortUsingSelector:@selector(compare:)]; |
|
3697 | 92 |
|
3547 | 93 |
[self.tableView reloadData]; |
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.listOfTeams count]; |
|
104 |
} |
|
105 |
||
106 |
// Customize the appearance of table view cells. |
|
107 |
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
|
108 |
static NSString *CellIdentifier = @"Cell"; |
|
3697 | 109 |
|
3547 | 110 |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
111 |
if (cell == nil) { |
|
112 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
|
113 |
} |
|
3697 | 114 |
|
115 |
NSUInteger row = [indexPath row]; |
|
116 |
NSString *rowString = [[self.listOfTeams objectAtIndex:row] stringByDeletingPathExtension]; |
|
117 |
cell.textLabel.text = rowString; |
|
3547 | 118 |
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
3697 | 119 |
|
3547 | 120 |
return cell; |
121 |
} |
|
122 |
||
123 |
// delete the row and the file |
|
124 |
-(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { |
|
125 |
NSUInteger row = [indexPath row]; |
|
3697 | 126 |
|
3547 | 127 |
NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@",TEAMS_DIRECTORY(),[self.listOfTeams objectAtIndex:row]]; |
128 |
[[NSFileManager defaultManager] removeItemAtPath:teamFile error:NULL]; |
|
129 |
[teamFile release]; |
|
3697 | 130 |
|
3547 | 131 |
[self.listOfTeams removeObjectAtIndex:row]; |
132 |
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; |
|
133 |
} |
|
134 |
||
135 |
||
136 |
#pragma mark - |
|
137 |
#pragma mark Table view delegate |
|
138 |
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
|
139 |
if (childController == nil) { |
|
140 |
childController = [[SingleTeamViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
141 |
} |
|
3697 | 142 |
|
3547 | 143 |
NSInteger row = [indexPath row]; |
144 |
NSString *selectedTeamFile = [listOfTeams objectAtIndex:row]; |
|
3697 | 145 |
|
3547 | 146 |
// this must be set so childController can load the correct plist |
3660 | 147 |
childController.teamName = [selectedTeamFile stringByDeletingPathExtension]; |
3547 | 148 |
[childController.tableView setContentOffset:CGPointMake(0,0) animated:NO]; |
149 |
||
150 |
[self.navigationController pushViewController:childController animated:YES]; |
|
151 |
} |
|
152 |
||
153 |
||
154 |
#pragma mark - |
|
155 |
#pragma mark Memory management |
|
156 |
-(void) didReceiveMemoryWarning { |
|
157 |
// Releases the view if it doesn't have a superview. |
|
158 |
[super didReceiveMemoryWarning]; |
|
159 |
// Relinquish ownership any cached data, images, etc that aren't in use. |
|
160 |
if (childController.view.superview == nil ) |
|
161 |
childController = nil; |
|
162 |
} |
|
163 |
||
164 |
-(void) viewDidUnload { |
|
165 |
self.listOfTeams = nil; |
|
166 |
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
|
167 |
MSG_DIDUNLOAD(); |
3547 | 168 |
[super viewDidUnload]; |
169 |
} |
|
170 |
||
171 |
-(void) dealloc { |
|
172 |
[self.listOfTeams release]; |
|
173 |
[childController release]; |
|
174 |
[super dealloc]; |
|
175 |
} |
|
176 |
||
177 |
||
178 |
@end |
|
179 |