author | unc0rr |
Mon, 16 Aug 2010 18:43:32 +0400 | |
changeset 3741 | 73246d25dfe1 |
parent 3697 | d5b30d6373fc |
child 3829 | 81db3c85784b |
permissions | -rw-r--r-- |
3547 | 1 |
// |
2 |
// SchemeSettingsViewController.m |
|
3 |
// HedgewarsMobile |
|
4 |
// |
|
5 |
// Created by Vittorio on 19/04/10. |
|
6 |
// Copyright 2010 __MyCompanyName__. All rights reserved. |
|
7 |
// |
|
8 |
||
9 |
#import "SchemeSettingsViewController.h" |
|
10 |
#import "CommodityFunctions.h" |
|
11 |
#import "SingleSchemeViewController.h" |
|
12 |
||
13 |
@implementation SchemeSettingsViewController |
|
14 |
@synthesize listOfSchemes; |
|
15 |
||
16 |
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { |
|
17 |
return rotationManager(interfaceOrientation); |
|
18 |
} |
|
19 |
||
20 |
#pragma mark - |
|
21 |
#pragma mark View lifecycle |
|
22 |
-(void) viewDidLoad { |
|
23 |
[super viewDidLoad]; |
|
3697 | 24 |
|
3547 | 25 |
UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Edit",@"from the scheme panel") |
26 |
style:UIBarButtonItemStyleBordered |
|
27 |
target:self |
|
28 |
action:@selector(toggleEdit:)]; |
|
29 |
self.navigationItem.rightBarButtonItem = editButton; |
|
30 |
[editButton release]; |
|
3697 | 31 |
|
3547 | 32 |
} |
33 |
||
34 |
-(void) viewWillAppear:(BOOL) animated { |
|
35 |
[super viewWillAppear:animated]; |
|
3697 | 36 |
|
3547 | 37 |
NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:SCHEMES_DIRECTORY() error:NULL]; |
38 |
NSMutableArray *array = [[NSMutableArray alloc] initWithArray:contentsOfDir copyItems:YES]; |
|
39 |
self.listOfSchemes = array; |
|
40 |
[array release]; |
|
3697 | 41 |
|
3547 | 42 |
[self.tableView reloadData]; |
43 |
} |
|
44 |
||
45 |
// modifies the navigation bar to add the "Add" and "Done" buttons |
|
46 |
-(void) toggleEdit:(id) sender { |
|
47 |
BOOL isEditing = self.tableView.editing; |
|
48 |
[self.tableView setEditing:!isEditing animated:YES]; |
|
3697 | 49 |
|
3547 | 50 |
if (isEditing) { |
51 |
[self.navigationItem.rightBarButtonItem setTitle:NSLocalizedString(@"Edit",@"from the scheme panel")]; |
|
52 |
[self.navigationItem.rightBarButtonItem setStyle: UIBarButtonItemStyleBordered]; |
|
53 |
self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem; |
|
54 |
} else { |
|
55 |
[self.navigationItem.rightBarButtonItem setTitle:NSLocalizedString(@"Done",@"from the scheme panel")]; |
|
56 |
[self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleDone]; |
|
57 |
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Add",@"from the scheme panel") |
|
58 |
style:UIBarButtonItemStyleBordered |
|
59 |
target:self |
|
60 |
action:@selector(addScheme:)]; |
|
61 |
self.navigationItem.leftBarButtonItem = addButton; |
|
62 |
[addButton release]; |
|
63 |
} |
|
64 |
} |
|
65 |
||
66 |
-(void) addScheme:(id) sender { |
|
67 |
NSString *fileName = [[NSString alloc] initWithFormat:@"Scheme %u.plist", [self.listOfSchemes count]]; |
|
3697 | 68 |
|
3547 | 69 |
createSchemeNamed([fileName stringByDeletingPathExtension]); |
3697 | 70 |
|
3547 | 71 |
[self.listOfSchemes addObject:fileName]; |
72 |
[fileName release]; |
|
3697 | 73 |
|
3547 | 74 |
// order the array alphabetically, so schemes will keep their position |
75 |
[self.listOfSchemes sortUsingSelector:@selector(compare:)]; |
|
3697 | 76 |
|
3547 | 77 |
[self.tableView reloadData]; |
78 |
} |
|
79 |
||
80 |
#pragma mark - |
|
81 |
#pragma mark Table view data source |
|
82 |
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { |
|
83 |
return 1; |
|
84 |
} |
|
85 |
||
86 |
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
|
87 |
return [self.listOfSchemes count]; |
|
88 |
} |
|
89 |
||
90 |
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
|
91 |
static NSString *CellIdentifier = @"Cell"; |
|
3697 | 92 |
|
3547 | 93 |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
94 |
if (cell == nil) { |
|
95 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
|
96 |
} |
|
3697 | 97 |
|
98 |
NSUInteger row = [indexPath row]; |
|
99 |
NSString *rowString = [[self.listOfSchemes objectAtIndex:row] stringByDeletingPathExtension]; |
|
100 |
cell.textLabel.text = rowString; |
|
3547 | 101 |
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
3697 | 102 |
|
3547 | 103 |
return cell; |
104 |
} |
|
105 |
||
106 |
// delete the row and the file |
|
107 |
-(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { |
|
108 |
NSUInteger row = [indexPath row]; |
|
3697 | 109 |
|
3547 | 110 |
NSString *schemeFile = [[NSString alloc] initWithFormat:@"%@/%@",SCHEMES_DIRECTORY(),[self.listOfSchemes objectAtIndex:row]]; |
111 |
[[NSFileManager defaultManager] removeItemAtPath:schemeFile error:NULL]; |
|
112 |
[schemeFile release]; |
|
3697 | 113 |
|
3547 | 114 |
[self.listOfSchemes removeObjectAtIndex:row]; |
115 |
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; |
|
116 |
} |
|
117 |
||
118 |
#pragma mark - |
|
119 |
#pragma mark Table view delegate |
|
120 |
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
|
121 |
if (childController == nil) { |
|
122 |
childController = [[SingleSchemeViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
123 |
} |
|
3697 | 124 |
|
3547 | 125 |
NSInteger row = [indexPath row]; |
126 |
NSString *selectedSchemeFile = [self.listOfSchemes objectAtIndex:row]; |
|
3697 | 127 |
|
3547 | 128 |
// this must be set so childController can load the correct plist |
3659 | 129 |
childController.schemeName = [selectedSchemeFile stringByDeletingPathExtension]; |
3547 | 130 |
[childController.tableView setContentOffset:CGPointMake(0,0) animated:NO]; |
131 |
||
132 |
[self.navigationController pushViewController:childController animated:YES]; |
|
133 |
} |
|
134 |
||
135 |
||
136 |
#pragma mark - |
|
137 |
#pragma mark Memory management |
|
138 |
-(void)didReceiveMemoryWarning { |
|
139 |
[super didReceiveMemoryWarning]; |
|
140 |
if (childController.view.superview == nil ) |
|
141 |
childController = nil; |
|
3659 | 142 |
MSG_MEMCLEAN(); |
3547 | 143 |
} |
144 |
||
145 |
-(void) viewDidUnload { |
|
146 |
self.listOfSchemes = nil; |
|
147 |
childController = nil; |
|
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3659
diff
changeset
|
148 |
MSG_DIDUNLOAD(); |
3547 | 149 |
[super viewDidUnload]; |
150 |
} |
|
151 |
||
152 |
||
153 |
-(void) dealloc { |
|
3659 | 154 |
[listOfSchemes release]; |
3547 | 155 |
[childController release]; |
156 |
[super dealloc]; |
|
157 |
} |
|
158 |
||
159 |
||
160 |
@end |
|
161 |