author | sheepluva |
Sat, 30 Oct 2010 23:25:41 +0200 | |
changeset 4026 | afae5a3b8424 |
parent 3971 | 5c82ee165ed5 |
child 4478 | 05029b4d8490 |
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 08/04/2010. |
|
19 |
*/ |
|
20 |
||
3547 | 21 |
|
22 |
#import "FlagsViewController.h" |
|
23 |
#import "CommodityFunctions.h" |
|
24 |
||
25 |
@implementation FlagsViewController |
|
3950 | 26 |
@synthesize teamDictionary, flagArray, communityArray, lastIndexPath; |
3547 | 27 |
|
28 |
||
29 |
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation { |
|
30 |
return rotationManager(interfaceOrientation); |
|
31 |
} |
|
32 |
||
33 |
||
34 |
#pragma mark - |
|
35 |
#pragma mark View lifecycle |
|
36 |
-(void) viewDidLoad { |
|
37 |
[super viewDidLoad]; |
|
38 |
||
3950 | 39 |
NSMutableArray *array_na = [[NSMutableArray alloc] init]; |
40 |
NSMutableArray *array_cm = [[NSMutableArray alloc] init]; |
|
41 |
||
42 |
for (NSString *name in [[NSFileManager defaultManager] contentsOfDirectoryAtPath:FLAGS_DIRECTORY() error:NULL]) { |
|
43 |
if ([name hasPrefix:@"cm_"]) { |
|
44 |
NSString *processed = [name substringFromIndex:3]; |
|
45 |
[array_cm addObject:processed]; |
|
46 |
} else |
|
47 |
[array_na addObject:name]; |
|
48 |
} |
|
49 |
||
50 |
self.flagArray = array_na; |
|
51 |
[array_na release]; |
|
52 |
self.communityArray = array_cm; |
|
53 |
[array_cm release]; |
|
3697 | 54 |
|
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3547
diff
changeset
|
55 |
self.title = NSLocalizedString(@"Set team flag",@""); |
3547 | 56 |
} |
57 |
||
58 |
-(void) viewWillAppear:(BOOL)animated { |
|
59 |
[super viewWillAppear:animated]; |
|
3950 | 60 |
// reloadData needed because team might change |
3547 | 61 |
[self.tableView reloadData]; |
3950 | 62 |
//[self.tableView setContentOffset:CGPointMake(0,0) animated:NO]; |
3547 | 63 |
} |
64 |
||
65 |
||
66 |
#pragma mark - |
|
67 |
#pragma mark Table view data source |
|
68 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
|
3950 | 69 |
return 2; |
3547 | 70 |
} |
71 |
||
72 |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
|
3950 | 73 |
if (section == 0) |
74 |
return [self.flagArray count]; |
|
75 |
else |
|
76 |
return [self.communityArray count]; |
|
3547 | 77 |
} |
78 |
||
79 |
// Customize the appearance of table view cells. |
|
80 |
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
|
81 |
static NSString *CellIdentifier = @"Cell"; |
|
3950 | 82 |
NSInteger row = [indexPath row]; |
3697 | 83 |
|
3547 | 84 |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
85 |
if (cell == nil) { |
|
86 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
|
87 |
} |
|
3697 | 88 |
|
3950 | 89 |
NSString *flagName = nil; |
90 |
NSArray *source = nil; |
|
91 |
if ([indexPath section] == 0) { |
|
92 |
source = self.flagArray; |
|
93 |
flagName = [source objectAtIndex:row]; |
|
94 |
} else { |
|
95 |
source = self.communityArray; |
|
96 |
flagName = [NSString stringWithFormat:@"cm_%@",[source objectAtIndex:row]]; |
|
97 |
} |
|
98 |
NSString *flagFile = [[NSString alloc] initWithFormat:@"%@/%@", FLAGS_DIRECTORY(), flagName]; |
|
3547 | 99 |
UIImage *flagSprite = [[UIImage alloc] initWithContentsOfFile:flagFile]; |
100 |
[flagFile release]; |
|
101 |
cell.imageView.image = flagSprite; |
|
102 |
[flagSprite release]; |
|
3697 | 103 |
|
3950 | 104 |
cell.textLabel.text = [[source objectAtIndex:row] stringByDeletingPathExtension]; |
105 |
if ([[flagName stringByDeletingPathExtension] isEqualToString:[self.teamDictionary objectForKey:@"flag"]]) { |
|
3547 | 106 |
cell.accessoryType = UITableViewCellAccessoryCheckmark; |
107 |
self.lastIndexPath = indexPath; |
|
108 |
} else { |
|
109 |
cell.accessoryType = UITableViewCellAccessoryNone; |
|
110 |
} |
|
3697 | 111 |
|
3547 | 112 |
return cell; |
113 |
} |
|
114 |
||
3950 | 115 |
-(NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section { |
116 |
NSString *sectionTitle = nil; |
|
117 |
switch (section) { |
|
118 |
case 0: |
|
119 |
sectionTitle = NSLocalizedString(@"Worldwide", @""); |
|
120 |
break; |
|
121 |
case 1: |
|
122 |
sectionTitle = NSLocalizedString(@"Community", @""); |
|
123 |
break; |
|
124 |
default: |
|
125 |
DLog(@"nope"); |
|
126 |
break; |
|
127 |
} |
|
128 |
return sectionTitle; |
|
129 |
} |
|
130 |
||
3547 | 131 |
|
132 |
#pragma mark - |
|
133 |
#pragma mark Table view delegate |
|
134 |
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
|
135 |
int newRow = [indexPath row]; |
|
136 |
int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1; |
|
3950 | 137 |
int newSection = [indexPath section]; |
138 |
int oldSection = (lastIndexPath != nil) ? [lastIndexPath section] : -1; |
|
139 |
||
140 |
if (newRow != oldRow || newSection != oldSection) { |
|
141 |
NSString *flag = nil; |
|
142 |
if ([indexPath section] == 0) |
|
143 |
flag = [self.flagArray objectAtIndex:newRow]; |
|
144 |
else |
|
145 |
flag = [NSString stringWithFormat:@"cm_%@",[self.communityArray objectAtIndex:newRow]]; |
|
146 |
||
3547 | 147 |
// if the two selected rows differ update data on the hog dictionary and reload table content |
3950 | 148 |
[self.teamDictionary setValue:[flag stringByDeletingPathExtension] forKey:@"flag"]; |
3547 | 149 |
|
150 |
// tell our boss to write this new stuff on disk |
|
151 |
[[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil]; |
|
152 |
||
153 |
UITableViewCell *newCell = [aTableView cellForRowAtIndexPath:indexPath]; |
|
154 |
newCell.accessoryType = UITableViewCellAccessoryCheckmark; |
|
155 |
UITableViewCell *oldCell = [aTableView cellForRowAtIndexPath:lastIndexPath]; |
|
156 |
oldCell.accessoryType = UITableViewCellAccessoryNone; |
|
157 |
self.lastIndexPath = indexPath; |
|
158 |
[aTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; |
|
159 |
} |
|
160 |
[aTableView deselectRowAtIndexPath:indexPath animated:YES]; |
|
161 |
[self.navigationController popViewControllerAnimated:YES]; |
|
162 |
} |
|
163 |
||
164 |
||
165 |
#pragma mark - |
|
166 |
#pragma mark Memory management |
|
167 |
-(void) didReceiveMemoryWarning { |
|
3950 | 168 |
self.lastIndexPath = nil; |
169 |
MSG_MEMCLEAN(); |
|
3547 | 170 |
[super didReceiveMemoryWarning]; |
171 |
} |
|
172 |
||
173 |
-(void) viewDidUnload { |
|
174 |
self.teamDictionary = nil; |
|
175 |
self.lastIndexPath = nil; |
|
176 |
self.flagArray = nil; |
|
3950 | 177 |
self.communityArray = nil; |
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3547
diff
changeset
|
178 |
MSG_DIDUNLOAD(); |
3547 | 179 |
[super viewDidUnload]; |
180 |
} |
|
181 |
||
182 |
-(void) dealloc { |
|
183 |
[teamDictionary release]; |
|
184 |
[lastIndexPath release]; |
|
185 |
[flagArray release]; |
|
3950 | 186 |
[communityArray release]; |
3547 | 187 |
[super dealloc]; |
188 |
} |
|
189 |
||
190 |
||
191 |
@end |
|
192 |