Repository navigation
Expand file tree
/
Copy pathSCISettingsViewController.m
More file actions
356 lines (264 loc) · 13.3 KB
/
Copy pathSCISettingsViewController.m
File metadata and controls
356 lines (264 loc) · 13.3 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
#import "SCISettingsViewController.h"
static char rowStaticRef[] = "row";
@interface SCISettingsViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, copy) NSArray *sections;
@property (nonatomic) BOOL reduceMargin;
@end
///
@implementation SCISettingsViewController
- (instancetype)initWithTitle:(NSString *)title sections:(NSArray *)sections reduceMargin:(BOOL)reduceMargin {
self = [super init];
if (self) {
self.title = title;
self.reduceMargin = reduceMargin;
// Exclude development cells from release builds
NSMutableArray *mutableSections = [sections mutableCopy];
[mutableSections enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(NSDictionary *section, NSUInteger index, BOOL *stop) {
if ([section[@"header"] hasPrefix:@"_"] && [section[@"footer"] hasPrefix:@"_"]) {
if (![[SCIUtils IGVersionString] isEqualToString:@"0.0.0"]) {
[mutableSections removeObjectAtIndex:index];
}
}
else if ([section[@"header"] isEqualToString:@"Experimental"]) {
if (![[SCIUtils IGVersionString] hasSuffix:@"-dev"]) {
[mutableSections removeObjectAtIndex:index];
}
}
}];
self.sections = [mutableSections copy];
}
return self;
}
- (instancetype)init {
return [self initWithTitle:[SCITweakSettings title] sections:[SCITweakSettings sections] reduceMargin:YES];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationController.navigationBar.prefersLargeTitles = NO;
self.view.backgroundColor = UIColor.systemBackgroundColor;
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleInsetGrouped];
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.tableView.dataSource = self;
self.tableView.contentInset = UIEdgeInsetsMake(self.reduceMargin ? -30 : -10, 0, 0, 0);
self.tableView.delegate = self;
[self.view addSubview:self.tableView];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
if (![[[NSUserDefaults standardUserDefaults] objectForKey:@"SCInstaFirstRun"] isEqualToString:SCIVersionString]) {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"SCInsta Settings Info"
message:@"In the future: Hold down on the three lines at the top right of your profile page, to re-open SCInsta settings."
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"I understand!"
style:UIAlertActionStyleDefault
handler:nil]];
UIViewController *presenter = self.presentingViewController;
[presenter presentViewController:alert animated:YES completion:nil];
// Done with first-time setup for this version
[[NSUserDefaults standardUserDefaults] setValue:SCIVersionString forKey:@"SCInstaFirstRun"];
}
}
// MARK: - UITableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
SCISetting *row = self.sections[indexPath.section][@"rows"][indexPath.row];
if (!row) return nil;
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
UIListContentConfiguration *cellContentConfig = cell.defaultContentConfiguration;
cellContentConfig.text = row.title;
// Subtitle
if (row.subtitle.length) {
cellContentConfig.secondaryText = row.subtitle;
cellContentConfig.textToSecondaryTextVerticalPadding = 4.5;
}
// Icon
if (row.icon != nil) {
cellContentConfig.image = [row.icon image];
cellContentConfig.imageProperties.tintColor = row.icon.color;
}
// Image url
if (row.imageUrl != nil) {
[self loadImageFromURL:row.imageUrl atIndexPath:indexPath forTableView:tableView];
cellContentConfig.imageToTextPadding = 14;
}
switch (row.type) {
case SCITableCellStatic: {
cell.selectionStyle = UITableViewCellSelectionStyleNone;
break;
}
case SCITableCellLink: {
cellContentConfig.textProperties.color = [UIColor systemBlueColor];
cellContentConfig.textProperties.font = [UIFont systemFontOfSize:[UIFont preferredFontForTextStyle:UIFontTextStyleBody].pointSize
weight:UIFontWeightMedium];
cell.selectionStyle = UITableViewCellSelectionStyleDefault;
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage systemImageNamed:@"safari"]];
imageView.tintColor = [UIColor systemGray3Color];
cell.accessoryView = imageView;
break;
}
case SCITableCellSwitch: {
UISwitch *toggle = [UISwitch new];
toggle.on = [[NSUserDefaults standardUserDefaults] boolForKey:row.defaultsKey];
toggle.onTintColor = [SCIUtils SCIColor_Primary];
objc_setAssociatedObject(toggle, rowStaticRef, row, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[toggle addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
cell.accessoryView = toggle;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
break;
}
case SCITableCellStepper: {
UIStepper *stepper = [UIStepper new];
stepper.minimumValue = row.min;
stepper.maximumValue = row.max;
stepper.stepValue = row.step;
stepper.value = [[NSUserDefaults standardUserDefaults] doubleForKey:row.defaultsKey];
objc_setAssociatedObject(stepper, rowStaticRef, row, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[stepper addTarget:self
action:@selector(stepperChanged:)
forControlEvents:UIControlEventValueChanged];
// Template subtitle
if (row.subtitle.length) {
cellContentConfig.secondaryText = [self formatString:row.subtitle withValue:stepper.value label:row.label singularLabel:row.singularLabel];
}
cell.accessoryView = stepper;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
break;
}
case SCITableCellButton: {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
break;
}
case SCITableCellMenu: {
UIButton *menuButton = [UIButton buttonWithType:UIButtonTypeSystem];
[menuButton setTitle:@"•••" forState:UIControlStateNormal];
menuButton.menu = [row menuForButton:menuButton];
menuButton.showsMenuAsPrimaryAction = YES;
menuButton.titleLabel.font = [UIFont systemFontOfSize:[UIFont preferredFontForTextStyle:UIFontTextStyleBody].pointSize
weight:UIFontWeightMedium];
UIButtonConfiguration *config = menuButton.configuration ?: [UIButtonConfiguration plainButtonConfiguration];
menuButton.configuration.contentInsets = NSDirectionalEdgeInsetsMake(8, 8, 8, 8);
menuButton.configuration = config;
[menuButton sizeToFit];
cell.accessoryView = menuButton;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
break;
}
case SCITableCellNavigation: {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
break;
}
}
cell.contentConfiguration = cellContentConfig;
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.sections[section][@"rows"] count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return self.sections[section][@"header"];
}
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
return self.sections[section][@"footer"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.sections.count;
}
// MARK: - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
SCISetting *row = self.sections[indexPath.section][@"rows"][indexPath.row];
if (!row) return;
if (row.type == SCITableCellLink) {
[[UIApplication sharedApplication] openURL:row.url options:@{} completionHandler:nil];
}
else if (row.type == SCITableCellButton) {
if (row.action != nil) {
row.action();
}
}
else if (row.type == SCITableCellNavigation) {
if (row.navSections.count > 0) {
UIViewController *vc = [[SCISettingsViewController alloc] initWithTitle:row.title sections:row.navSections reduceMargin:NO];
vc.title = row.title;
[self.navigationController pushViewController:vc animated:YES];
}
else if (row.navViewController) {
[self.navigationController pushViewController:row.navViewController animated:YES];
}
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
// MARK: - Actions
- (void)switchChanged:(UISwitch *)sender {
SCISetting *row = objc_getAssociatedObject(sender, rowStaticRef);
[[NSUserDefaults standardUserDefaults] setBool:sender.isOn forKey:row.defaultsKey];
NSLog(@"Switch changed: %@", sender.isOn ? @"ON" : @"OFF");
if (row.requiresRestart) {
[SCIUtils showRestartConfirmation];
}
}
- (void)stepperChanged:(UIStepper *)sender {
SCISetting *row = objc_getAssociatedObject(sender, rowStaticRef);
[[NSUserDefaults standardUserDefaults] setDouble:sender.value forKey:row.defaultsKey];
NSLog(@"Stepper changed: %f", sender.value);
[self reloadCellForView:sender];
}
- (void)menuChanged:(UICommand *)command {
NSDictionary *properties = command.propertyList;
[[NSUserDefaults standardUserDefaults] setValue:properties[@"value"] forKey:properties[@"defaultsKey"]];
NSLog(@"Menu changed: %@", command.propertyList[@"value"]);
[self reloadCellForView:command.sender animated:YES];
if (properties[@"requiresRestart"]) {
[SCIUtils showRestartConfirmation];
}
}
// MARK: - Helper
- (NSString *)formatString:(NSString *)template withValue:(double)value label:(NSString *)label singularLabel:(NSString *)singularLabel {
// Singular or plural labels
NSString *applicableLabel = fabs(value - 1.0) < 0.00001 ? singularLabel : label;
// Force value to 0 to prevent it being -0
if (fabs(value) < 0.00001) {
value = 0.0;
}
// Get correct decimal value based on step value
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterDecimalStyle;
formatter.minimumFractionDigits = 0;
formatter.maximumFractionDigits = [SCIUtils decimalPlacesInDouble:value];
NSString *stringValue = [formatter stringFromNumber:@(value)];
return [NSString stringWithFormat:template, stringValue, applicableLabel];
}
- (void)reloadCellForView:(UIView *)view animated:(BOOL)animated {
UITableViewCell *cell = (UITableViewCell *)view.superview;
while (cell && ![cell isKindOfClass:[UITableViewCell class]]) {
cell = (UITableViewCell *)cell.superview;
}
if (!cell) return;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
if (!indexPath) return;
[self.tableView reloadRowsAtIndexPaths:@[indexPath]
withRowAnimation:animated ? UITableViewRowAnimationAutomatic : UITableViewRowAnimationNone];
}
- (void)reloadCellForView:(UIView *)view {
[self reloadCellForView:view animated:NO];
}
- (void)loadImageFromURL:(NSURL *)url atIndexPath:(NSIndexPath *)indexPath forTableView:(UITableView *)tableView
{
if (!url) return;
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithURL:url
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
if (!data || error) return;
UIImage *image = [UIImage imageWithData:data];
if (!image) return;
dispatch_async(dispatch_get_main_queue(), ^{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (!cell) return;
UIListContentConfiguration *config = (UIListContentConfiguration *)cell.contentConfiguration;
config.image = image;
config.imageProperties.maximumSize = CGSizeMake(45, 45);
cell.contentConfiguration = config;
});
}];
[task resume];
}
@end