forked from rpetrich/AppList
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathALApplicationPreferenceViewController.m
245 lines (212 loc) · 8.05 KB
/
ALApplicationPreferenceViewController.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
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
#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;
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)loadFromSpecifier:(PSSpecifier *)specifier
{
[self setNavigationTitle:[specifier propertyForKey:@"ALNavigationTitle"] ?: [specifier name]];
singleEnabledMode = [[specifier propertyForKey:@"ALSingleEnabledMode"] boolValue];
NSArray *descriptors = [specifier propertyForKey:@"ALSectionDescriptors"];
if (!descriptors) {
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];
[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];
[_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 displayIdentifierForIndexPath: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
{
NSString *displayIdentifier = [_dataSource displayIdentifierForIndexPath:indexPath];
if (singleEnabledMode) {
if ([newValue boolValue]) {
[settings setObject:displayIdentifier 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:displayIdentifier]) {
[settings removeObjectForKey:settingsKeyPrefix];
}
} else {
NSString *key = [settingsKeyPrefix stringByAppendingString:displayIdentifier];
[settings setObject:newValue forKey:key];
}
if (settingsPath)
[settings writeToFile:settingsPath atomically:YES];
if (settingsChangeNotification)
notify_post([settingsChangeNotification UTF8String]);
}
- (id)valueForCellAtIndexPath:(NSIndexPath *)indexPath
{
NSString *displayIdentifier = [_dataSource displayIdentifierForIndexPath:indexPath];
if (singleEnabledMode) {
return [[settings objectForKey:settingsKeyPrefix] isEqualToString:displayIdentifier] ? (id)kCFBooleanTrue : (id)kCFBooleanFalse;
} else {
NSString *key = [settingsKeyPrefix stringByAppendingString:displayIdentifier];
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