-
Notifications
You must be signed in to change notification settings - Fork 1
/
PurchaseStatsPreferences.x
118 lines (101 loc) · 5.08 KB
/
PurchaseStatsPreferences.x
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
#import <Preferences/Preferences.h>
#import "PurchaseStatsStore.h"
@interface PurchaseStatsProductCell: PSControlTableCell
@end
@interface PurchaseStatsSettingsController: PSListController
@end
@implementation PurchaseStatsSettingsController
- (NSArray *)specifiers {
NSMutableArray *specs = (NSMutableArray *)self->_specifiers;
if (!specs) {
specs = [[NSMutableArray alloc] init];
PSSpecifier *autoRefresh = [PSSpecifier preferenceSpecifierNamed:@"Refresh Automatically" target:self
set:@selector(setPreferenceValue:specifier:)
get:@selector(readPreferenceValue:)
detail:nil cell:PSSwitchCell edit:nil];
[autoRefresh setProperty:SETTINGS_DOMAIN forKey:@"defaults"];
[autoRefresh setProperty:SETTINGS_KEY_AUTOREFRESH forKey:@"key"];
[specs addObject:autoRefresh];
[specs addObject:PSSpecifier.emptyGroupSpecifier];
PSSpecifier *authProvider = [PSSpecifier preferenceSpecifierNamed:@"Auth Provider" target:self
set:@selector(setPreferenceValue:specifier:)
get:@selector(readPreferenceValue:)
detail:PSListItemsController.class cell:PSLinkListCell edit:nil];
[authProvider setProperty:SETTINGS_DOMAIN forKey:@"defaults"];
[authProvider setProperty:SETTINGS_KEY_AUTH_PROVIDER forKey:@"key"];
[authProvider setProperty:@"Google" forKey:@"default"];
NSArray *authProviderValues = @[@"Google", @"Facebook"];
[authProvider setValues:authProviderValues titles:authProviderValues];
[specs addObject:authProvider];
PSSpecifier *username = [PSSpecifier preferenceSpecifierNamed:@"Username" target:self
set:@selector(setPreferenceValue:specifier:)
get:@selector(readPreferenceValue:)
detail:nil cell:PSEditTextCell edit:nil];
[username setKeyboardType:UIKeyboardTypeEmailAddress autoCaps:UITextAutocapitalizationTypeNone autoCorrection:UITextAutocorrectionTypeDefault];
[username setProperty:SETTINGS_DOMAIN forKey:@"defaults"];
[username setProperty:SETTINGS_KEY_USERNAME forKey:@"key"];
[username setProperty:@"username" forKey:@"prompt"];
[specs addObject:username];
PSSpecifier *password = [PSSpecifier preferenceSpecifierNamed:@"Password" target:self
set:@selector(setPreferenceValue:specifier:)
get:@selector(readPreferenceValue:)
detail:nil cell:PSSecureEditTextCell edit:nil];
[password setProperty:SETTINGS_DOMAIN forKey:@"defaults"];
[password setProperty:SETTINGS_KEY_PASSWORD forKey:@"key"];
[password setProperty:@"password" forKey:@"prompt"];
[specs addObject:password];
PurchaseStatsStore *store = [[PurchaseStatsStore alloc] init];
NSMutableArray *products = [NSMutableArray arrayWithArray:store.allProducts];
[products sortUsingComparator:^(PurchaseStatsProduct *p1, PurchaseStatsProduct *p2) {
return [p1.name localizedCaseInsensitiveCompare:p2.name];
}];
if (products.count) {
[specs addObject:PSSpecifier.emptyGroupSpecifier];
for (PurchaseStatsProduct *product in products) {
PSSpecifier *productSpecifier = [PSSpecifier preferenceSpecifierNamed:product.name target:self
set:@selector(setPreferenceValue:specifier:)
get:@selector(readPreferenceValue:)
detail:nil cell:PSSwitchCell edit:nil];
[productSpecifier setProperty:SETTINGS_DOMAIN forKey:@"defaults"];
[productSpecifier setProperty:product.productURL forKey:@"key"];
[productSpecifier setProperty:@YES forKey:@"default"];
if (%c(PurchaseStatsProductCell)) {
[productSpecifier setProperty:%c(PurchaseStatsProductCell) forKey:@"cellClass"];
}
UIImage *icon = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:product.iconDataURL]]];
if (icon) {
[productSpecifier setProperty:icon forKey:@"iconImage"];
}
[specs addObject:productSpecifier];
}
}
self->_specifiers = specs;
}
return self->_specifiers;
}
@end
static UITableViewCell *setupProductCell(UITableViewCell *self) {
self.textLabel.adjustsFontSizeToFitWidth = YES;
return self;
}
%group PSSwitchTableCell // iOS6
%subclass PurchaseStatsProductCell: PSSwitchTableCell
- (id)initWithStyle:(int)style reuseIdentifier:(NSString *)identifier specifier:(PSSpecifier *)specifier {
return setupProductCell(%orig(style, nil, specifier));
}
%end
%end
%group PSControlTableCell // iOS5
%subclass PurchaseStatsProductCell: PSControlTableCell
- (id)initWithStyle:(int)style reuseIdentifier:(NSString *)identifier specifier:(PSSpecifier *)specifier {
return setupProductCell(%orig(style, nil, specifier));
}
%end
%end
%ctor {
if (%c(PSSwitchTableCell)) {
%init(PSSwitchTableCell);
} else if (%c(PSControlTableCell)) {
%init(PSControlTableCell);
}
}