forked from rpetrich/AppList
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathALApplicationPreferenceViewController.m
More file actions
292 lines (249 loc) · 9.58 KB
/
ALApplicationPreferenceViewController.m
File metadata and controls
292 lines (249 loc) · 9.58 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
#import "ALApplicationTableDataSource.h"
#import "ALApplicationList.h"
#import "ALValueCell.h"
#import <Preferences/Preferences.h>
#include <notify.h>
__attribute__((visibility("hidden")))
@interface ALApplicationPreferenceViewController : PSViewController<UITableViewDelegate> {
@private
ALApplicationTableDataSource *_dataSource;
UITableView *_tableView;
NSString *_navigationTitle;
NSArray *descriptors;
id settingsDefaultValue;
NSString *settingsPath;
NSMutableDictionary *settings;
NSString *settingsKeyPrefix;
NSString *settingsChangeNotification;
BOOL singleEnabledMode;
}
- (id)initForContentSize:(CGSize)size;
@property (nonatomic, retain) NSString *navigationTitle;
@property (nonatomic, readonly) UITableView *tableView;
@property (nonatomic, readonly) ALApplicationTableDataSource *dataSource;
- (void)cellAtIndexPath:(NSIndexPath *)indexPath didChangeToValue:(id)newValue;
- (id)valueForCellAtIndexPath:(NSIndexPath *)indexPath;
@end
__attribute__((visibility("hidden")))
@interface ALPreferencesTableDataSource : ALApplicationTableDataSource<ALValueCellDelegate> {
@private
ALApplicationPreferenceViewController *_controller;
}
- (id)initWithController:(ALApplicationPreferenceViewController *)controller;
@end
@implementation ALPreferencesTableDataSource
- (id)initWithController:(ALApplicationPreferenceViewController *)controller
{
if ((self = [super init])) {
_controller = controller;
}
return self;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
id cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
if ([cell isKindOfClass:[ALValueCell class]]) {
[cell setDelegate:self];
[cell loadValue:[_controller valueForCellAtIndexPath:indexPath]];
}
return cell;
}
- (void)valueCell:(ALValueCell *)valueCell didChangeToValue:(id)newValue
{
[_controller cellAtIndexPath:[self.tableView indexPathForCell:valueCell] didChangeToValue:newValue];
}
@end
@interface PSViewController (OS32)
- (void)setSpecifier:(PSSpecifier *)specifier;
@end
@implementation ALApplicationPreferenceViewController
- (id)initForContentSize:(CGSize)size
{
if ([[PSViewController class] instancesRespondToSelector:@selector(initForContentSize:)])
self = [super initForContentSize:size];
else
self = [super init];
if (self) {
CGRect frame;
frame.origin = CGPointZero;
frame.size = size;
_tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStyleGrouped];
_dataSource = [[ALPreferencesTableDataSource alloc] initWithController:self];
[_tableView setDataSource:_dataSource];
[_tableView setDelegate:self];
[_tableView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
_dataSource.tableView = _tableView;
}
return self;
}
- (void)dealloc
{
[_tableView setDelegate:nil];
[_tableView setDataSource:nil];
[_tableView release];
_dataSource.tableView = nil;
[_dataSource release];
[settingsDefaultValue release];
[settingsPath release];
[settingsKeyPrefix release];
[settingsChangeNotification release];
[_navigationTitle release];
[super dealloc];
}
@synthesize tableView = _tableView, dataSource = _dataSource, navigationTitle = _navigationTitle;
- (void)setNavigationTitle:(NSString *)navigationTitle
{
[_navigationTitle autorelease];
_navigationTitle = [navigationTitle retain];
if ([self respondsToSelector:@selector(navigationItem)])
[[self navigationItem] setTitle:_navigationTitle];
}
- (void)_updateSections
{
NSInteger index = 0;
for (NSDictionary *descriptor in descriptors) {
NSString *predicateFormat = [descriptor objectForKey:ALSectionDescriptorVisibilityPredicateKey];
if (!predicateFormat) {
index++;
continue;
}
NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateFormat];
BOOL visible = [predicate evaluateWithObject:settings];
NSArray *existingDescriptors = [_dataSource sectionDescriptors];
BOOL already = [existingDescriptors count] > index ? [existingDescriptors objectAtIndex:index] == descriptor : NO;
if (visible) {
if (!already) {
[_dataSource insertSectionDescriptor:descriptor atIndex:index];
}
index++;
} else {
if (already) {
[_dataSource removeSectionDescriptorAtIndex:index];
}
}
}
}
- (void)loadFromSpecifier:(PSSpecifier *)specifier
{
[self setNavigationTitle:[specifier propertyForKey:@"ALNavigationTitle"] ?: [specifier name]];
singleEnabledMode = [[specifier propertyForKey:@"ALSingleEnabledMode"] boolValue];
[descriptors release];
descriptors = [specifier propertyForKey:@"ALSectionDescriptors"];
if (descriptors == nil) {
NSString *defaultCellClass = singleEnabledMode ? @"ALCheckCell" : @"ALSwitchCell";
NSNumber *iconSize = [NSNumber numberWithUnsignedInteger:ALApplicationIconSizeSmall];
descriptors = [NSArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:
@"System Applications", ALSectionDescriptorTitleKey,
@"isSystemApplication = TRUE", ALSectionDescriptorPredicateKey,
defaultCellClass, ALSectionDescriptorCellClassNameKey,
iconSize, ALSectionDescriptorIconSizeKey,
nil],
[NSDictionary dictionaryWithObjectsAndKeys:
@"User Applications", ALSectionDescriptorTitleKey,
@"isSystemApplication = FALSE", ALSectionDescriptorPredicateKey,
defaultCellClass, ALSectionDescriptorCellClassNameKey,
iconSize, ALSectionDescriptorIconSizeKey,
nil],
nil];
}
[_dataSource setSectionDescriptors:descriptors];
[descriptors retain];
NSString *bundlePath = [specifier propertyForKey:@"ALLocalizationBundle"];
_dataSource.localizationBundle = bundlePath ? [NSBundle bundleWithPath:bundlePath] : nil;
[settingsDefaultValue release];
settingsDefaultValue = [[specifier propertyForKey:@"ALSettingsDefaultValue"] retain];
[settingsPath release];
settingsPath = [[specifier propertyForKey:@"ALSettingsPath"] retain];
[settingsKeyPrefix release];
settingsKeyPrefix = [[specifier propertyForKey:singleEnabledMode ? @"ALSettingsKey" : @"ALSettingsKeyPrefix"] ?: @"ALValue-" retain];
settings = [[NSMutableDictionary alloc] initWithContentsOfFile:settingsPath] ?: [[NSMutableDictionary alloc] init];
[settingsChangeNotification release];
settingsChangeNotification = [[specifier propertyForKey:@"ALChangeNotification"] retain];
id temp = [specifier propertyForKey:@"ALAllowsSelection"];
[_tableView setAllowsSelection:temp ? [temp boolValue] : singleEnabledMode];
[self _updateSections];
[_tableView reloadData];
}
- (void)setSpecifier:(PSSpecifier *)specifier
{
[self loadFromSpecifier:specifier];
[super setSpecifier:specifier];
}
- (void)viewWillBecomeVisible:(void *)source
{
if (source)
[self loadFromSpecifier:(PSSpecifier *)source];
[super viewWillBecomeVisible:source];
}
- (UIView *)view
{
return _tableView;
}
- (CGSize)contentSize
{
return [_tableView frame].size;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
id cell = [_tableView cellForRowAtIndexPath:indexPath];
if ([cell respondsToSelector:@selector(didSelect)])
[cell didSelect];
id cellDescriptor = [_dataSource cellDescriptorForIndexPath:indexPath];
if ([cellDescriptor isKindOfClass:[NSDictionary class]]) {
SEL action = NSSelectorFromString([[cellDescriptor objectForKey:@"action"] stringByAppendingString:@"FromCellDescriptor:"]);
if ([self respondsToSelector:action])
objc_msgSend(self, action, cellDescriptor);
}
[_tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)cellAtIndexPath:(NSIndexPath *)indexPath didChangeToValue:(id)newValue
{
id cellDescriptor = [_dataSource cellDescriptorForIndexPath:indexPath];
if ([cellDescriptor isKindOfClass:[NSDictionary class]]) {
[settings setObject:newValue forKey:[cellDescriptor objectForKey:@"ALSettingsKey"]];
} else if (singleEnabledMode) {
if ([newValue boolValue]) {
[settings setObject:cellDescriptor forKey:settingsKeyPrefix];
for (NSIndexPath *otherIndexPath in [_tableView indexPathsForVisibleRows]) {
if (![otherIndexPath isEqual:indexPath]) {
ALValueCell *otherCell = (ALValueCell *)[_tableView cellForRowAtIndexPath:otherIndexPath];
[otherCell loadValue:(id)kCFBooleanFalse];
}
}
} else if ([[settings objectForKey:settingsKeyPrefix] isEqual:cellDescriptor]) {
[settings removeObjectForKey:settingsKeyPrefix];
}
} else {
NSString *key = [settingsKeyPrefix stringByAppendingString:cellDescriptor];
[settings setObject:newValue forKey:key];
}
if (settingsPath)
[settings writeToFile:settingsPath atomically:YES];
if (settingsChangeNotification)
notify_post([settingsChangeNotification UTF8String]);
[self _updateSections];
}
- (id)valueForCellAtIndexPath:(NSIndexPath *)indexPath
{
id cellDescriptor = [_dataSource cellDescriptorForIndexPath:indexPath];
if ([cellDescriptor isKindOfClass:[NSDictionary class]]) {
return [settings objectForKey:[cellDescriptor objectForKey:@"ALSettingsKey"]] ?: [cellDescriptor objectForKey:@"ALSettingsDefaultValue"];
}
if (singleEnabledMode) {
return [[settings objectForKey:settingsKeyPrefix] isEqualToString:cellDescriptor] ? (id)kCFBooleanTrue : (id)kCFBooleanFalse;
} else {
NSString *key = [settingsKeyPrefix stringByAppendingString:cellDescriptor];
return [settings objectForKey:key] ?: settingsDefaultValue;
}
}
- (void)pushController:(id<PSBaseView>)controller
{
[super pushController:controller];
[controller setParentController:self];
}
- (void)launchURLFromCellDescriptor:(NSDictionary *)cellDescriptor
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[cellDescriptor objectForKey:@"url"]]];
}
@end