-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettingsViewController.m
More file actions
403 lines (352 loc) · 14.6 KB
/
SettingsViewController.m
File metadata and controls
403 lines (352 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
//
// SettingsController.m
// MakeMyStatus
//
// Created by Edward Rudd on 4/5/10.
// Copyright 2010 OutOfOrder.cc. All rights reserved.
//
#import "SettingsViewController.h"
#import "SettingsValueTable.h"
#import "SettingsSliderCell.h"
#import "SettingsTextCell.h"
#pragma mark -
#pragma mark SettingsController
@implementation SettingsViewController
@synthesize schema = _schema;
enum {
SettingsTypeUnknown,
SettingsTypeToggle,
SettingsTypeSlider,
SettingsTypeTitle,
SettingsTypeText,
SettingsTypeMulti,
SettingsTypeChild
};
typedef NSUInteger SettingsType;
#pragma mark -
#pragma mark View lifecycle
- (id)initWithSettings:(SettingsManager *)settings {
self = [super initWithStyle:UITableViewStyleGrouped];
if (self != nil) {
[self setTitle:@"Settings"];
_settingsManager = [settings retain];
_config = nil;
_predicate = nil;
_groups = nil;
_settings = [NSUserDefaults standardUserDefaults];
_schema = nil;
}
return self;
}
- (id)initWithSettings:(SettingsManager *)settings andSchema:(NSString *)schema {
self = [self initWithSettings:settings];
if (self != nil) {
_schema = [schema copy];
}
return self;
}
- (void) viewWillAppear:(BOOL)animated
{
NSIndexPath *cur = [self.tableView indexPathForSelectedRow];
if (cur != nil) {
NSInteger first = [self.allitems indexOfObjectIdenticalTo:[self.groups objectAtIndex:cur.section]];
NSDictionary *obj = [self.allitems objectAtIndex:first + 1 + cur.row];
NSString *type = [obj valueForKey:@"Type"];
if ([type isEqualToString:@"PSMultiValueSpecifier"]) {
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:cur];
id _val = [[NSUserDefaults standardUserDefaults] objectForKey:[obj objectForKey:@"Key"]];
NSInteger idx = [[obj valueForKey:@"Values"] indexOfObject:_val];
if (idx != NSNotFound) {
[[cell detailTextLabel] setText:[[obj valueForKey:@"Titles"] objectAtIndex:idx]];
// This is to adjust width and left offset of detailTextLabel BEFORE we animate back
[cell setNeedsLayout];
[cell layoutIfNeeded];
}
}
}
[super viewWillAppear:animated];
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return [self.groups count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [[self.groups objectAtIndex:section] valueForKey:@"Title"];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
NSInteger first = [self.allitems indexOfObjectIdenticalTo:[self.groups objectAtIndex:section]];
NSInteger len = [self.allitems count];
NSInteger pos;
for (pos = first + 1; pos < len; ++pos) {
if ([self.predicate evaluateWithObject:[self.allitems objectAtIndex:pos]]) {
break;
}
}
return pos - first - 1;
}
- (UITableViewCell *)fetchCellForTableView:(UITableView *)tableView ofType: (SettingsType)type withId:(NSString *)cellId {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (cell == nil) {
if (type != SettingsTypeSlider && type != SettingsTypeText) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId] autorelease];
}
switch (type) {
case SettingsTypeMulti:
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
break;
case SettingsTypeChild:
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
break;
case SettingsTypeToggle: {
UISwitch *switchObj = [[UISwitch alloc] initWithFrame:CGRectZero];
[cell setAccessoryView:switchObj];
[switchObj addTarget:self action:@selector(switchToggle:) forControlEvents:UIControlEventValueChanged];
[switchObj release];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
break;
case SettingsTypeSlider:
cell = [[[SettingsSliderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId] autorelease];
[[(SettingsSliderCell *)cell slider] addTarget:self action:@selector(sliderChange:) forControlEvents:UIControlEventValueChanged];
break;
case SettingsTypeText:
cell = [[[SettingsTextCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId] autorelease];
[[(SettingsTextCell *)cell textField] setDelegate:self];
break;
case SettingsTypeTitle:
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
break;
default:
break;
}
}
return cell;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger grpIdx = [self.allitems indexOfObjectIdenticalTo:[self.groups objectAtIndex:indexPath.section]];
NSInteger settingIdx = grpIdx + 1 + indexPath.row;
NSDictionary *option = [self.allitems objectAtIndex:settingIdx];
UITableViewCell *cell = nil;
NSString *valueText = @"";
id value = nil;
/** @todo add Localization Support */
NSString *key = [option valueForKey:@"Key"];
if (key) {
value = [_settings objectForKey:key];
if (value) {
if ([value respondsToSelector: @selector(stringValue)]) {
valueText = [value stringValue];
} else {
valueText = value;
}
}
}
NSString *type = [option valueForKey:@"Type"];
if ([type isEqualToString:@"PSMultiValueSpecifier"]) {
cell = [self fetchCellForTableView:tableView ofType: SettingsTypeMulti withId:@"CellMulti"];
[[cell textLabel] setText:[option valueForKey:@"Title"]];
NSInteger idx = [[option valueForKey:@"Values"] indexOfObject:value];
if (idx != NSNotFound) {
[[cell detailTextLabel] setText:[[option valueForKey:@"Titles"] objectAtIndex:idx]];
}
} else if ([type isEqualToString:@"PSTextFieldSpecifier"]) {
cell = [self fetchCellForTableView:tableView ofType: SettingsTypeText withId:@"CellText"];
[[cell textLabel] setText:[option valueForKey:@"Title"]];
UITextField *tf = [(SettingsTextCell *)cell textField];
tf.text = valueText;
tf.tag = settingIdx;
NSNumber *secure = [option objectForKey:@"IsSecure"];
tf.secureTextEntry = (secure && [secure boolValue]);
NSString *opt = [option objectForKey:@"KeyboardType"];
if (opt) {
if ([opt isEqualToString:@"NumbersAndPunctuation"]) {
tf.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
} else if ([opt isEqualToString:@"NumberPad"]) {
tf.keyboardType = UIKeyboardTypeNumberPad;
} else if ([opt isEqualToString:@"URL"]) {
tf.keyboardType = UIKeyboardTypeURL;
} else if ([opt isEqualToString:@"EmailAddress"]) {
tf.keyboardType = UIKeyboardTypeEmailAddress;
} else {
tf.keyboardType = UIKeyboardTypeAlphabet;
}
} else {
tf.keyboardType = UIKeyboardTypeAlphabet;
}
opt = [option objectForKey:@"AutocapitalizationType"];
if (opt) {
if ([opt isEqualToString:@"Sentences"]) {
tf.autocapitalizationType = UITextAutocapitalizationTypeSentences;
} else if ([opt isEqualToString:@"Words"]) {
tf.autocapitalizationType = UITextAutocapitalizationTypeWords;
} else if ([opt isEqualToString:@"AllCharacters"]) {
tf.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
} else {
tf.autocapitalizationType = UITextAutocapitalizationTypeNone;
}
} else {
tf.autocapitalizationType = UITextAutocapitalizationTypeNone;
}
opt = [option objectForKey:@"AutocorrectionType"];
if (opt) {
if ([opt isEqualToString:@"No"]) {
tf.autocorrectionType = UITextAutocorrectionTypeNo;
} else if ([opt isEqualToString:@"Yes"]) {
tf.autocorrectionType = UITextAutocorrectionTypeYes;
} else {
tf.autocorrectionType = UITextAutocorrectionTypeDefault;
}
} else {
tf.autocorrectionType = UITextAutocorrectionTypeDefault;
}
} else if ([type isEqualToString:@"PSSliderSpecifier"]) {
cell = [self fetchCellForTableView:tableView ofType: SettingsTypeSlider withId:@"CellSlider"];
UISlider *sl = (UISlider *)[(SettingsSliderCell *)cell slider];
sl.tag = settingIdx;
sl.value = [value floatValue];
sl.minimumValue = [[option objectForKey:@"MinimumValue"] floatValue];
sl.maximumValue = [[option objectForKey:@"MaximumValue"] floatValue];
} else if ([type isEqualToString:@"PSToggleSwitchSpecifier"]) {
cell = [self fetchCellForTableView:tableView ofType: SettingsTypeToggle withId:@"CellToggle"];
[[cell textLabel] setText:[option valueForKey:@"Title"]];
UISwitch *sw = (UISwitch *)[cell accessoryView];
[sw setTag:settingIdx];
[sw setOn:[value boolValue]];
} else if ([type isEqualToString:@"PSTitleValueSpecifier"]) {
cell = [self fetchCellForTableView:tableView ofType: SettingsTypeTitle withId:@"CellTitle"];
[[cell textLabel] setText:[option valueForKey:@"Title"]];
[[cell detailTextLabel] setText:[option valueForKey:@"defaultValue"]];
[[cell detailTextLabel] setText:valueText];
} else if ([type isEqualToString:@"PSChildPaneSpecifier"]) {
cell = [self fetchCellForTableView:tableView ofType: SettingsTypeChild withId:@"CellChild"];
[[cell textLabel] setText:[option valueForKey:@"Title"]];
} else {
NSLog(@"Unknown Type %@ for %@",type, indexPath);
cell = [self fetchCellForTableView:tableView ofType:SettingsTypeUnknown withId:@"Cell"];
}
return cell;
}
#pragma mark -
#pragma mark Callbacks
- (void)switchToggle:(UISwitch *)switchObj
{
NSDictionary *obj = [self.allitems objectAtIndex:switchObj.tag];
NSString *key = [obj objectForKey:@"Key"];
if (key) {
[_settings setBool:switchObj.on forKey:key];
}
[_settingsManager callValueChangeActionForOption:obj withValue:[NSNumber numberWithBool:switchObj.on]];
}
- (void)sliderChange:(UISlider *)sliderObj
{
NSDictionary *obj = [self.allitems objectAtIndex:sliderObj.tag];
NSNumber *stepNum = [obj objectForKey:@"StepValue"];
if (stepNum) {
float step = [stepNum floatValue];
float min = [[obj objectForKey:@"MinimumValue"] floatValue];
sliderObj.value = (round((sliderObj.value - min) / step) * step) + min;
}
NSString *key = [obj objectForKey:@"Key"];
if (sliderObj.value != [_settings floatForKey:key]) {
if (key) {
[_settings setFloat:sliderObj.value forKey:key];
}
[_settingsManager callValueChangeActionForOption:obj withValue:[NSNumber numberWithFloat:sliderObj.value]];
}
}
#pragma mark Text Field Delegate
- (void) textFieldDidEndEditing:(UITextField *)textField
{
NSDictionary *obj = [self.allitems objectAtIndex:textField.tag];
NSString *key = [obj objectForKey:@"Key"];
if (key) {
[_settings setObject:textField.text forKey:key];
}
[_settingsManager callValueChangeActionForOption:obj withValue:textField.text];
}
- (BOOL) textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger first = [self.allitems indexOfObjectIdenticalTo:[self.groups objectAtIndex:indexPath.section]];
NSDictionary *obj = [self.allitems objectAtIndex:first + 1 + indexPath.row];
NSString *type = [obj valueForKey:@"Type"];
if ([type isEqualToString:@"PSMultiValueSpecifier"]) {
SettingsValueTable *viewController = [[SettingsValueTable alloc] initWithOption:obj forSettings:_settingsManager];
[self.navigationController pushViewController:viewController animated:YES];
[viewController release];
} else if ([type isEqualToString:@"PSChildPaneSpecifier"]) {
SettingsViewController *vc = [[SettingsViewController alloc] initWithSettings:_settingsManager andSchema:[obj valueForKey:@"File"]];
[self.navigationController pushViewController:vc animated:YES];
[vc release];
}
}
#pragma mark -
#pragma mark Lazy Loading
- (NSDictionary *)config {
if (!_config) {
_config = [[_settingsManager configForSchema:_schema] retain];
}
return _config;
}
- (NSArray *)allitems {
if (!_allitems) {
_allitems = [self.config objectForKey:@"PreferenceSpecifiers"];
}
return _allitems;
}
- (NSPredicate *)predicate {
if (!_predicate) {
_predicate = [[NSPredicate predicateWithFormat:@"Type == 'PSGroupSpecifier'"] retain];
}
return _predicate;
}
- (NSArray *)groups {
if (!_groups) {
_groups = [[self.allitems filteredArrayUsingPredicate:self.predicate] retain];
}
return _groups;
}
- (void) setSchema:(NSString *)schema {
[_schema release];
_schema = [schema copy];
[_config release];
_config = nil;
[_groups release];
_groups = nil;
_allitems = nil;
[self.tableView reloadData];
}
#pragma mark -
#pragma mark Memory management
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Relinquish ownership any cached data, images, etc that aren't in use.
[_predicate release];
_predicate = nil;
[_groups release];
_groups = nil;
[_config release];
_config = nil;
_allitems = nil;
}
- (void)viewDidUnload {
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
}
- (void)dealloc {
[_predicate release];
[_groups release];
[_config release];
[_settingsManager release];
[super dealloc];
}
@end