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