/
FCSettingsViewController.m
117 lines (91 loc) · 3.86 KB
/
FCSettingsViewController.m
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
//
// FCSettingsViewController.m
// FCSettingsBooster
//
// Created by Fabian Canas on 10/27/12.
// Copyright (c) 2012 Fabian Canas. All rights reserved.
//
#import "FCSettingsViewController.h"
#import "FCSwitchCell.h"
#import "FCColorPickerCell.h"
#import "NSUserDefaults+UIColor.h"
@interface FCSettingsViewController () {
NSString *controllerKey;
}
@property (nonatomic, strong) NSArray *configuration;
@end
@implementation FCSettingsViewController
@synthesize controllerKey=_controllerKey;
- (id)initWithConfiguration:(NSArray *) config andStyle:(UITableViewStyle)style {
self = [super initWithStyle:style];
if (self) {
if (self.tableView == nil) {
self.tableView = [[UITableView alloc] init];
self.modalPresentationStyle = UIModalPresentationCurrentContext;
self.definesPresentationContext = YES;
}
[self configure:config];
}
return self;
}
- (void)configure:(NSArray *) config {
[self.tableView registerClass:[FCSwitchCell class] forCellReuseIdentifier:@"FCSwitchCell"];
[self.tableView registerClass:[FCColorPickerCell class] forCellReuseIdentifier:@"FCColorPickerCell"];
self.configuration = config;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return _configuration.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return ((NSArray *)_configuration[section][@"settings"]).count;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return _configuration[section][@"sectionName"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *cellSettings = ((NSArray *)((NSDictionary *)_configuration[indexPath.section])[@"settings"])[indexPath.row];
NSString *cellIdentifier = cellSettings[@"type"];
UITableViewCell<FCBoosterCell> *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
cell.labelText = cellSettings[@"name"];
cell.key = cellSettings[@"key"];
return cell;
}
#pragma mark - Table view delegate
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell<FCBoosterCell> *cell = (UITableViewCell<FCBoosterCell> *)[tableView cellForRowAtIndexPath:indexPath];
if ([cell conformsToProtocol:@protocol(FCBoosterCell)]) {
if ([cell respondsToSelector:@selector(valueControllerForSettingsController:)]) {
return YES;
}
}
return NO;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell<FCBoosterCell> *cell = (UITableViewCell<FCBoosterCell> *)[tableView cellForRowAtIndexPath:indexPath];
if ([cell conformsToProtocol:@protocol(FCBoosterCell)]) {
if ([cell respondsToSelector:@selector(valueControllerForSettingsController:)]) {
[self presentViewController:[cell valueControllerForSettingsController:self] animated:YES completion:nil];
}
}
}
#pragma mark - Configuration Convenience Methods
- (NSDictionary *)cellConfigurationAtIndexPath:(NSIndexPath *)indexPath {
return ((NSArray *)((NSDictionary *)_configuration[indexPath.section])[@"settings"])[indexPath.row];
}
#pragma mark - Color Picker Delegate Methods
- (void)colorPickerViewController:(FCColorPickerViewController *)colorPicker didSelectColor:(UIColor *)color {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setColor:color forKey:self.controllerKey];
[self dismissViewControllerAnimated:YES completion:nil];
self.controllerKey = nil;
[self.tableView reloadData];
}
- (void)colorPickerViewControllerDidCancel:(FCColorPickerViewController *)colorPicker {
[self dismissViewControllerAnimated:YES completion:nil];
self.controllerKey = nil;
}
@end