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