8 |
8 |
9 #import "TeamSettingsViewController.h" |
9 #import "TeamSettingsViewController.h" |
10 #import "SingleTeamViewController.h" |
10 #import "SingleTeamViewController.h" |
11 |
11 |
12 @implementation TeamSettingsViewController |
12 @implementation TeamSettingsViewController |
13 @synthesize list; |
13 @synthesize listOfTeams; |
|
14 |
|
15 |
|
16 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { |
|
17 return (interfaceOrientation == UIInterfaceOrientationLandscapeRight); |
|
18 } |
14 |
19 |
15 #pragma mark - |
20 #pragma mark - |
16 #pragma mark View lifecycle |
21 #pragma mark View lifecycle |
17 - (void)viewDidLoad { |
22 - (void)viewDidLoad { |
18 [super viewDidLoad]; |
23 [super viewDidLoad]; |
19 |
24 |
|
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 |
20 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); |
32 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); |
21 NSString *teamsDirectory = [[paths objectAtIndex:0] stringByAppendingString:@"/Teams/"]; |
33 NSString *teamsDirectory = [[paths objectAtIndex:0] stringByAppendingString:@"/Teams/"]; |
22 |
34 |
23 NSArray *contents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:teamsDirectory |
35 NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:teamsDirectory error:NULL]; |
24 error:NULL]; |
36 NSMutableArray *array = [[NSMutableArray alloc] initWithArray: contentsOfDir copyItems:YES]; |
25 self.list = contents; |
37 self.listOfTeams = array; |
26 //NSLog(@"%@\n%@", teamsDirectory, contents); |
38 [array release]; |
27 |
39 NSLog(@"files: %@", self.listOfTeams); |
28 // Uncomment the following line to preserve selection between presentations. |
40 } |
29 // self.clearsSelectionOnViewWillAppear = NO; |
41 |
30 // Uncomment the following line to display an Edit button in the navigation bar for this view controller. |
42 // modifies the navigation bar to add the "Add" and "Done" buttons |
31 self.navigationItem.rightBarButtonItem = self.editButtonItem; |
43 -(void) toggleEdit:(id) sender { |
32 } |
44 BOOL isEditing = self.tableView.editing; |
33 |
45 [self.tableView setEditing:!isEditing animated:YES]; |
34 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { |
46 |
35 return (interfaceOrientation == UIInterfaceOrientationLandscapeRight); |
47 if (isEditing) { |
36 } |
48 [self.navigationItem.rightBarButtonItem setTitle:NSLocalizedString(@"Edit",@"from the team navigation")]; |
37 |
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 } |
|
61 } |
|
62 |
|
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 |
|
100 } |
38 |
101 |
39 #pragma mark - |
102 #pragma mark - |
40 #pragma mark Table view data source |
103 #pragma mark Table view data source |
41 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { |
104 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
42 // Return the number of sections. |
105 // Return the number of sections. |
43 return 1; |
106 return 1; |
44 } |
107 } |
45 |
108 |
46 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
109 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
47 // Return the number of rows in the section. |
110 // Return the number of rows in the section. |
48 return [list count]; |
111 return [listOfTeams count]; |
49 } |
112 } |
50 |
113 |
51 // Customize the appearance of table view cells. |
114 // Customize the appearance of table view cells. |
52 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
115 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
53 |
|
54 static NSString *CellIdentifier = @"Cell"; |
116 static NSString *CellIdentifier = @"Cell"; |
55 |
117 |
56 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
118 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
57 if (cell == nil) { |
119 if (cell == nil) { |
58 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
120 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
59 } |
121 } |
60 |
122 |
61 NSUInteger row = [indexPath row]; |
123 NSUInteger row = [indexPath row]; |
62 NSString *rowString = [[list objectAtIndex:row] stringByDeletingPathExtension]; |
124 NSString *rowString = [[listOfTeams objectAtIndex:row] stringByDeletingPathExtension]; |
63 cell.textLabel.text = rowString; |
125 cell.textLabel.text = rowString; |
64 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
126 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
65 |
127 |
66 return cell; |
128 return cell; |
67 } |
129 } |
68 |
130 |
69 |
131 // delete the row and the file |
70 /* |
132 -(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { |
71 // Override to support conditional editing of the table view. |
133 NSUInteger row = [indexPath row]; |
72 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { |
134 |
73 // Return NO if you do not want the specified item to be editable. |
135 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); |
74 return YES; |
136 NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/Teams/%@",[paths objectAtIndex:0],[self.listOfTeams objectAtIndex:row]]; |
75 } |
137 |
76 */ |
138 [[NSFileManager defaultManager] removeItemAtPath:teamFile error:NULL]; |
77 |
139 [teamFile release]; |
|
140 |
|
141 [self.listOfTeams removeObjectAtIndex:row]; |
|
142 [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; |
|
143 } |
78 |
144 |
79 /* |
145 /* |
80 // Override to support editing the table view. |
146 // Override to support editing the table view. |
81 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { |
147 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { |
82 |
148 |