02063186d368774191c5d2e2d638750491548b24
[atutor.git] / mods / atsocial_iphone_app / InAppSettingsKit / Controllers / IASKSpecifierValuesViewController.m
1 //
2 //  IASKSpecifierValuesViewController.m
3 //  http://www.inappsettingskit.com
4 //
5 //  Copyright (c) 2009:
6 //  Luc Vandal, Edovia Inc., http://www.edovia.com
7 //  Ortwin Gentz, FutureTap GmbH, http://www.futuretap.com
8 //  All rights reserved.
9 // 
10 //  It is appreciated but not required that you give credit to Luc Vandal and Ortwin Gentz, 
11 //  as the original authors of this code. You can give credit in a blog post, a tweet or on 
12 //  a info page of your app. Also, the original authors appreciate letting them know if you use this code.
13 //
14 //  This code is licensed under the BSD license that is available at: http://www.opensource.org/licenses/bsd-license.php
15 //
16
17 #import "IASKSpecifierValuesViewController.h"
18 #import "IASKSpecifier.h"
19 #import "IASKSettingsReader.h"
20
21 #define kCellValue      @"kCellValue"
22
23 @implementation IASKSpecifierValuesViewController
24
25 @synthesize currentSpecifier=_currentSpecifier;
26 @synthesize checkedItem=_checkedItem;
27 @synthesize settingsReader = _settingsReader;
28
29 /*
30  // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
31 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
32     if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
33         // Custom initialization
34     }
35     return self;
36 }
37 */
38
39
40 /*- (void)viewDidLoad {
41     [super viewDidLoad];
42 }*/
43
44 - (void)viewWillAppear:(BOOL)animated {
45     if (_currentSpecifier) {
46         [self setTitle:[_currentSpecifier title]];
47     }
48     
49     if (_tableView) {
50         [_tableView reloadData];
51     }
52         [super viewWillAppear:animated];
53 }
54
55 - (void)viewDidAppear:(BOOL)animated {
56         [_tableView flashScrollIndicators];
57         [super viewDidAppear:animated];
58 }
59
60
61 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
62     return (interfaceOrientation == UIInterfaceOrientationPortrait) || (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
63 }
64
65 - (void)didReceiveMemoryWarning {
66         // Releases the view if it doesn't have a superview.
67     [super didReceiveMemoryWarning];
68         
69         // Release any cached data, images, etc that aren't in use.
70 }
71
72 - (void)viewDidUnload {
73         // Release any retained subviews of the main view.
74         // e.g. self.myOutlet = nil;
75 }
76
77
78 - (void)dealloc {
79     [_currentSpecifier release];
80         [_settingsReader release];
81         _settingsReader = nil;
82
83     [super dealloc];
84 }
85
86
87 #pragma mark -
88 #pragma mark UITableView delegates
89
90 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
91         return 1;
92 }
93
94 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
95     return [_currentSpecifier multipleValuesCount];
96 }
97
98 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
99     UITableViewCell *cell   = [tableView dequeueReusableCellWithIdentifier:kCellValue];
100     NSArray *values         = [_currentSpecifier multipleValues];
101     NSArray *titles         = [_currentSpecifier multipleTitles];
102
103     if (!cell) {
104         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellValue] autorelease];
105     }
106
107     if ([[NSUserDefaults standardUserDefaults] objectForKey:[_currentSpecifier key]] ?
108                 [[[NSUserDefaults standardUserDefaults] objectForKey:[_currentSpecifier key]] isEqual:[values objectAtIndex:indexPath.row]] :
109                 [[_currentSpecifier defaultValue] isEqual:[values objectAtIndex:indexPath.row]]) {
110         [self setCheckedItem:indexPath];
111         [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
112     }
113     else {
114         [cell setAccessoryType:UITableViewCellAccessoryNone];
115     }
116
117         @try {
118                 [[cell textLabel] setText:[self.settingsReader titleForStringId:[titles objectAtIndex:indexPath.row]]];
119         }
120         @catch (NSException * e) {}
121     return cell;
122 }
123
124 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
125     if (indexPath == [self checkedItem]) {
126         [tableView deselectRowAtIndexPath:indexPath animated:YES];
127         return;
128     }
129     
130     NSArray *values         = [_currentSpecifier multipleValues];
131     
132     [tableView deselectRowAtIndexPath:indexPath animated:YES];
133     [[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
134     [[tableView cellForRowAtIndexPath:[self checkedItem]] setAccessoryType:UITableViewCellAccessoryNone];
135     [self setCheckedItem:indexPath];
136
137     [[NSUserDefaults standardUserDefaults] setObject:[values objectAtIndex:indexPath.row] forKey:[_currentSpecifier key]];
138     [[NSNotificationCenter defaultCenter] postNotificationName:kIASKAppSettingChanged object:[_currentSpecifier key]];
139 }
140
141
142 @end