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