3305
|
1 |
//
|
|
2 |
// TeamSettingsViewController.m
|
|
3 |
// HedgewarsMobile
|
|
4 |
//
|
|
5 |
// Created by Vittorio on 02/04/10.
|
|
6 |
// Copyright 2010 __MyCompanyName__. All rights reserved.
|
|
7 |
//
|
|
8 |
|
|
9 |
#import "TeamSettingsViewController.h"
|
|
10 |
#import "SingleTeamViewController.h"
|
|
11 |
|
|
12 |
@implementation TeamSettingsViewController
|
3323
|
13 |
@synthesize listOfTeams;
|
|
14 |
|
|
15 |
|
|
16 |
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
|
|
17 |
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
|
|
18 |
}
|
3305
|
19 |
|
|
20 |
#pragma mark -
|
|
21 |
#pragma mark View lifecycle
|
|
22 |
- (void)viewDidLoad {
|
|
23 |
[super viewDidLoad];
|
3308
|
24 |
|
3323
|
25 |
UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Edit",@"from the team navigation")
|
|
26 |
style:UIBarButtonItemStyleBordered
|
|
27 |
target:self
|
|
28 |
action:@selector(toggleEdit:)];
|
|
29 |
self.navigationItem.rightBarButtonItem = editButton;
|
|
30 |
[editButton release];
|
|
31 |
|
3305
|
32 |
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
|
3308
|
33 |
NSString *teamsDirectory = [[paths objectAtIndex:0] stringByAppendingString:@"/Teams/"];
|
3305
|
34 |
|
3323
|
35 |
NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:teamsDirectory error:NULL];
|
|
36 |
NSMutableArray *array = [[NSMutableArray alloc] initWithArray: contentsOfDir copyItems:YES];
|
|
37 |
self.listOfTeams = array;
|
|
38 |
[array release];
|
|
39 |
NSLog(@"files: %@", self.listOfTeams);
|
|
40 |
}
|
3308
|
41 |
|
3323
|
42 |
// modifies the navigation bar to add the "Add" and "Done" buttons
|
|
43 |
-(void) toggleEdit:(id) sender {
|
|
44 |
BOOL isEditing = self.tableView.editing;
|
|
45 |
[self.tableView setEditing:!isEditing animated:YES];
|
|
46 |
|
|
47 |
if (isEditing) {
|
|
48 |
[self.navigationItem.rightBarButtonItem setTitle:NSLocalizedString(@"Edit",@"from the team navigation")];
|
|
49 |
[self.navigationItem.rightBarButtonItem setStyle: UIBarButtonItemStyleBordered];
|
|
50 |
self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem;
|
|
51 |
} else {
|
|
52 |
[self.navigationItem.rightBarButtonItem setTitle:NSLocalizedString(@"Done",@"from the team navigation")];
|
|
53 |
[self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleDone];
|
|
54 |
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Add",@"from the team navigation")
|
|
55 |
style:UIBarButtonItemStyleBordered
|
|
56 |
target:self
|
|
57 |
action:@selector(addTeam:)];
|
|
58 |
self.navigationItem.leftBarButtonItem = addButton;
|
|
59 |
[addButton release];
|
|
60 |
}
|
3305
|
61 |
}
|
|
62 |
|
3323
|
63 |
// add a team file with default values and updates the table
|
|
64 |
-(void) addTeam:(id) sender {
|
|
65 |
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
|
|
66 |
NSString *teamsDirectory = [[paths objectAtIndex:0] stringByAppendingString:@"/Teams/"];
|
|
67 |
[[NSFileManager defaultManager] createDirectoryAtPath:teamsDirectory
|
|
68 |
withIntermediateDirectories:NO
|
|
69 |
attributes:nil
|
|
70 |
error:NULL];
|
|
71 |
|
|
72 |
NSMutableArray *hedgehogs = [[NSMutableArray alloc] init];
|
|
73 |
|
|
74 |
for (int i = 0; i < 8; i++) {
|
|
75 |
NSString *hogName = [[NSString alloc] initWithFormat:@"hedgehog %d",i];
|
|
76 |
NSDictionary *hog = [[NSDictionary alloc] initWithObjectsAndKeys:@"100",@"health",@"0",@"level",
|
|
77 |
hogName,@"hogname",@"NoHat",@"hat",nil];
|
|
78 |
[hogName release];
|
|
79 |
[hedgehogs addObject:hog];
|
|
80 |
[hog release];
|
|
81 |
}
|
|
82 |
|
|
83 |
NSString *fileName = [[NSString alloc] initWithFormat:@"Default Team %u.plist", [self.listOfTeams count]];
|
|
84 |
|
|
85 |
NSDictionary *defaultTeam = [[NSDictionary alloc] initWithObjectsAndKeys:@"4421353",@"color",@"0",@"hash",
|
|
86 |
[fileName stringByDeletingPathExtension],@"teamname",@"Statue",@"grave",@"Plane",@"fort",
|
|
87 |
@"Default",@"voicepack",@"hedgewars",@"flag",hedgehogs,@"hedgehogs",nil];
|
|
88 |
[hedgehogs release];
|
|
89 |
|
|
90 |
NSString *defaultTeamFile = [[NSString alloc] initWithFormat:@"%@/%@",teamsDirectory, fileName];
|
|
91 |
[defaultTeam writeToFile:defaultTeamFile atomically:YES];
|
|
92 |
[defaultTeamFile release];
|
|
93 |
[defaultTeam release];
|
|
94 |
|
|
95 |
[self.listOfTeams addObject:fileName];
|
|
96 |
[fileName release];
|
|
97 |
|
|
98 |
[self.tableView reloadData];
|
|
99 |
|
3305
|
100 |
}
|
|
101 |
|
|
102 |
#pragma mark -
|
|
103 |
#pragma mark Table view data source
|
3323
|
104 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
|
3305
|
105 |
// Return the number of sections.
|
|
106 |
return 1;
|
|
107 |
}
|
|
108 |
|
3323
|
109 |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
|
3305
|
110 |
// Return the number of rows in the section.
|
3323
|
111 |
return [listOfTeams count];
|
3305
|
112 |
}
|
|
113 |
|
|
114 |
// Customize the appearance of table view cells.
|
3323
|
115 |
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
|
3305
|
116 |
static NSString *CellIdentifier = @"Cell";
|
|
117 |
|
|
118 |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
|
|
119 |
if (cell == nil) {
|
|
120 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
|
|
121 |
}
|
|
122 |
|
|
123 |
NSUInteger row = [indexPath row];
|
3323
|
124 |
NSString *rowString = [[listOfTeams objectAtIndex:row] stringByDeletingPathExtension];
|
3305
|
125 |
cell.textLabel.text = rowString;
|
|
126 |
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
|
|
127 |
|
|
128 |
return cell;
|
|
129 |
}
|
|
130 |
|
3323
|
131 |
// delete the row and the file
|
|
132 |
-(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
133 |
NSUInteger row = [indexPath row];
|
|
134 |
|
|
135 |
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
|
|
136 |
NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/Teams/%@",[paths objectAtIndex:0],[self.listOfTeams objectAtIndex:row]];
|
3305
|
137 |
|
3323
|
138 |
[[NSFileManager defaultManager] removeItemAtPath:teamFile error:NULL];
|
|
139 |
[teamFile release];
|
|
140 |
|
|
141 |
[self.listOfTeams removeObjectAtIndex:row];
|
|
142 |
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
|
3305
|
143 |
}
|
|
144 |
|
|
145 |
/*
|
|
146 |
// Override to support editing the table view.
|
|
147 |
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
148 |
|
|
149 |
if (editingStyle == UITableViewCellEditingStyleDelete) {
|
|
150 |
// Delete the row from the data source
|
|
151 |
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
|
|
152 |
}
|
|
153 |
else if (editingStyle == UITableViewCellEditingStyleInsert) {
|
|
154 |
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
|
|
155 |
}
|
|
156 |
}
|
|
157 |
*/
|
|
158 |
|
|
159 |
/*
|
|
160 |
// Override to support rearranging the table view.
|
|
161 |
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
|
|
162 |
}
|
|
163 |
*/
|
|
164 |
|
|
165 |
/*
|
|
166 |
// Override to support conditional rearranging of the table view.
|
|
167 |
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
168 |
// Return NO if you do not want the item to be re-orderable.
|
|
169 |
return YES;
|
|
170 |
}
|
|
171 |
*/
|
|
172 |
|
|
173 |
|
|
174 |
#pragma mark -
|
|
175 |
#pragma mark Table view delegate
|
|
176 |
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
177 |
if (childController == nil) {
|
|
178 |
childController = [[SingleTeamViewController alloc] initWithStyle:UITableViewStyleGrouped];
|
|
179 |
}
|
|
180 |
|
|
181 |
NSInteger row = [indexPath row];
|
3323
|
182 |
NSString *selectedTeamFile = [listOfTeams objectAtIndex:row];
|
|
183 |
NSLog(@"%@",selectedTeamFile);
|
3305
|
184 |
|
3323
|
185 |
// load data about the team and extract info
|
|
186 |
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
|
|
187 |
NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/Teams/%@",[paths objectAtIndex:0],selectedTeamFile];
|
|
188 |
NSMutableDictionary *teamDict = [[NSMutableDictionary alloc] initWithContentsOfFile:teamFile];
|
|
189 |
[teamFile release];
|
|
190 |
childController.teamDictionary = teamDict;
|
|
191 |
[teamDict release];
|
|
192 |
|
|
193 |
// this must be set so childController can load the correct plist
|
|
194 |
//childController.title = [[listOfTeams objectAtIndex:row] stringByDeletingPathExtension];
|
3305
|
195 |
[self.navigationController pushViewController:childController animated:YES];
|
|
196 |
}
|
|
197 |
|
|
198 |
/*
|
|
199 |
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
|
|
200 |
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Hey, do you see the disclosure button?"
|
|
201 |
message:@"If you're trying to drill down, touch that instead"
|
|
202 |
delegate:nil
|
|
203 |
cancelButtonTitle:@"Won't happen again"
|
|
204 |
otherButtonTitles:nil];
|
|
205 |
[alert show];
|
|
206 |
[alert release];
|
|
207 |
}
|
|
208 |
*/
|
|
209 |
|
|
210 |
#pragma mark -
|
|
211 |
#pragma mark Memory management
|
3323
|
212 |
-(void) didReceiveMemoryWarning {
|
3305
|
213 |
// Releases the view if it doesn't have a superview.
|
|
214 |
[super didReceiveMemoryWarning];
|
|
215 |
|
|
216 |
// Relinquish ownership any cached data, images, etc that aren't in use.
|
|
217 |
}
|
|
218 |
|
3323
|
219 |
-(void) viewDidUnload {
|
|
220 |
self.listOfTeams = nil;
|
3305
|
221 |
}
|
3315
|
222 |
|
3323
|
223 |
-(void) dealloc {
|
|
224 |
[listOfTeams release];
|
|
225 |
[childController release];
|
3305
|
226 |
[super dealloc];
|
|
227 |
}
|
|
228 |
|
|
229 |
|
|
230 |
@end
|
|
231 |
|