-
Notifications
You must be signed in to change notification settings - Fork 50
/
SponsorBlockSettingsController.m
366 lines (320 loc) · 18.9 KB
/
SponsorBlockSettingsController.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
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
357
358
359
360
361
362
363
364
365
366
#import "Headers/SponsorBlockSettingsController.h"
#import "Headers/Localization.h"
@implementation SponsorBlockTableCell
- (void)colorPicker:(id)colorPicker didSelectColor:(UIColor *)color {
self.colorWell.color = color;
NSString *hexString = hexFromUIColor(color);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *settingsPath = [documentsDirectory stringByAppendingPathComponent:@"iSponsorBlock.plist"];
NSMutableDictionary *settings = [NSMutableDictionary dictionary];
[settings addEntriesFromDictionary:[NSDictionary dictionaryWithContentsOfFile:settingsPath]];
NSDictionary *categorySettings = [settings objectForKey:@"categorySettings"];
[categorySettings setValue:hexString forKey:[NSString stringWithFormat:@"%@Color", self.category]];
[settings setValue:categorySettings forKey:@"categorySettings"];
[settings writeToURL:[NSURL fileURLWithPath:settingsPath isDirectory:NO] error:nil];
CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), CFSTR("com.galacticdev.isponsorblockprefs.changed"), NULL, NULL, YES);
}
- (void)presentColorPicker:(UITableViewCell *)sender {
HBColorPickerViewController *viewController = [[objc_getClass("HBColorPickerViewController") alloc] init];
viewController.delegate = self;
viewController.popoverPresentationController.sourceView = self;
HBColorPickerConfiguration *configuration = [[objc_getClass("HBColorPickerConfiguration") alloc] initWithColor:self.colorWell.color];
configuration.supportsAlpha = NO;
viewController.configuration = configuration;
UIViewController *rootViewController = self._viewControllerForAncestor;
[rootViewController presentViewController:viewController animated:YES completion:nil];
//fixes the bottom of the color picker from getting cut off
viewController.view.frame = CGRectMake(0,-50, viewController.view.frame.size.width, viewController.view.frame.size.height);
}
@end
@implementation SponsorBlockSettingsController
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *dismissButton;
dismissButton = [[UIBarButtonItem alloc] initWithImage:[UIImage systemImageNamed:@"xmark"]
style:UIBarButtonItemStylePlain
target:self
action:@selector(dismissButtonTapped:)];
dismissButton.tintColor = [UIColor blackColor];
self.navigationItem.leftBarButtonItem = dismissButton;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
self.settingsPath = [documentsDirectory stringByAppendingPathComponent:@"iSponsorBlock.plist"];
self.settings = [NSMutableDictionary dictionary];
[self.settings addEntriesFromDictionary:[NSDictionary dictionaryWithContentsOfFile:self.settingsPath]];
self.view.backgroundColor = UIColor.systemBackgroundColor;
//detects if device is an se gen 1 or not, crude fix for text getting cut off
if ([UIScreen mainScreen].bounds.size.width > 320) {
self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleInsetGrouped];
}
else {
self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
}
[self.view addSubview:self.tableView];
self.tableView.translatesAutoresizingMaskIntoConstraints = NO;
[self.tableView.heightAnchor constraintEqualToAnchor:self.view.heightAnchor].active = YES;
[self.tableView.widthAnchor constraintEqualToAnchor:self.view.widthAnchor].active = YES;
self.tableView.delegate = self;
self.tableView.dataSource = self;
NSBundle *tweakBundle = iSponsorBlockBundle();
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:[tweakBundle pathForResource:@"LogoSponsorBlocker128px" ofType:@"png"]]];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)];
label.text = @"iSponsorBlock";
label.font = [UIFont boldSystemFontOfSize:48];
self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0,0,0,200)];
[self.tableView.tableHeaderView addSubview:imageView];
[self.tableView.tableHeaderView addSubview:label];
self.tweakTitle = label.text;
imageView.translatesAutoresizingMaskIntoConstraints = NO;
label.translatesAutoresizingMaskIntoConstraints = NO;
[imageView.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor].active = YES;
[imageView.topAnchor constraintEqualToAnchor:self.tableView.tableHeaderView.topAnchor constant:5].active = YES;
[label.centerXAnchor constraintEqualToAnchor:imageView.centerXAnchor].active = YES;
[label.topAnchor constraintEqualToAnchor:imageView.bottomAnchor constant:5].active = YES;
//for dismissing num pad when tapping anywhere on the string
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self.view action:@selector(endEditing:)];
tap.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tap];
self.sectionTitles = @[LOC(@"Sponsor"), LOC(@"Intermission/IntroAnimation"), LOC(@"Endcards/Credits"), LOC(@"InteractionReminder"), LOC(@"Unpaid/SelfPromotion"), LOC(@"Non-MusicSection"), LOC(@"SponsorBlockUserID"), LOC(@"SponsorBlockAPIInstance")];
}
//Add iSponsorBlock text to Navbar label if header text out of screen
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGRect labelCellRect = [self.tableView rectForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
CGRect visibleRect = CGRectMake(self.tableView.contentOffset.x,
self.tableView.contentOffset.y + self.navigationController.navigationBar.frame.size.height,
self.tableView.bounds.size.width,
self.tableView.bounds.size.height - self.navigationController.navigationBar.frame.size.height);
if (!CGRectContainsRect(visibleRect, labelCellRect)) {
self.title = self.tweakTitle;
[UIView animateWithDuration:0.3 animations:^{
self.navigationItem.titleView.alpha = 1.0;
}];
} else {
self.title = nil;
[UIView animateWithDuration:0.3 animations:^{
self.navigationItem.titleView.alpha = 0.0;
}];
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 18;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0) return 1;
else if (section <= 6 || section == 17) return 2;
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SponsorBlockCell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SponsorBlocKCell"];
}
if (indexPath.section == 0) {
cell.textLabel.text = LOC(@"Enabled");
UISwitch *enabledSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0,0,51,31)];
cell.accessoryView = enabledSwitch;
[enabledSwitch addTarget:self action:@selector(enabledSwitchToggled:) forControlEvents:UIControlEventValueChanged];
if ([self.settings valueForKey:@"enabled"]) {
[enabledSwitch setOn:[[self.settings valueForKey:@"enabled"] boolValue] animated:NO];
}
else {
[enabledSwitch setOn:YES animated:NO];
[self enabledSwitchToggled:enabledSwitch];
}
return cell;
}
if (indexPath.section <= 6) {
SponsorBlockTableCell *tableCell = [[SponsorBlockTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SponsorBlockCell2"];
NSDictionary *categorySettings = [self.settings objectForKey:@"categorySettings"];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:@[LOC(@"Disable"), LOC(@"AutoSkip"), LOC(@"ShowInSeekBar"), LOC(@"ManualSkip")]];
//make it so "Show in Seek Bar" text won't be cut off on certain devices
NSMutableArray *segments = [segmentedControl valueForKey:@"_segments"];
UISegment *segment = segments[2];
UILabel *label = [segment valueForKey:@"_info"];
label.adjustsFontSizeToFitWidth = YES;
switch (indexPath.section) {
case 1:
segmentedControl.selectedSegmentIndex = [[categorySettings objectForKey:@"sponsor"] intValue];
tableCell.category = @"sponsor";
break;
case 2:
segmentedControl.selectedSegmentIndex = [[categorySettings objectForKey:@"intro"] intValue];
tableCell.category = @"intro";
break;
case 3:
segmentedControl.selectedSegmentIndex = [[categorySettings objectForKey:@"outro"] intValue];
tableCell.category = @"outro";
break;
case 4:
segmentedControl.selectedSegmentIndex = [[categorySettings objectForKey:@"interaction"] intValue];
tableCell.category = @"interaction";
break;
case 5:
segmentedControl.selectedSegmentIndex = [[categorySettings objectForKey:@"selfpromo"] intValue];
tableCell.category = @"selfpromo";
break;
case 6:
segmentedControl.selectedSegmentIndex = [[categorySettings objectForKey:@"music_offtopic"] intValue];
tableCell.category = @"music_offtopic";
break;
default:
break;
}
if (indexPath.row == 0) {
[segmentedControl addTarget:self action:@selector(categorySegmentSelected:) forControlEvents:UIControlEventValueChanged];
segmentedControl.apportionsSegmentWidthsByContent = YES;
[tableCell.contentView addSubview:segmentedControl];
segmentedControl.translatesAutoresizingMaskIntoConstraints = NO;
[segmentedControl.centerYAnchor constraintEqualToAnchor:tableCell.contentView.centerYAnchor].active = YES;
[segmentedControl.widthAnchor constraintEqualToAnchor:tableCell.contentView.widthAnchor].active = YES;
}
else {
tableCell.textLabel.text = LOC(@"SetColorToShowInSeekBar");
tableCell.textLabel.adjustsFontSizeToFitWidth = YES;
HBColorWell *colorWell = [[objc_getClass("HBColorWell") alloc] initWithFrame:CGRectMake(0,0,32,32)];
[colorWell addTarget:tableCell action:@selector(presentColorPicker:) forControlEvents:UIControlEventTouchUpInside];
UIColor *color = colorWithHexString([categorySettings objectForKey:[NSString stringWithFormat:@"%@Color", tableCell.category]]);
colorWell.color = color;
tableCell.accessoryView = colorWell;
tableCell.colorWell = colorWell;
}
return tableCell;
}
if (indexPath.section == 7) {
UITableViewCell *textCell = [[UITableViewCell alloc] initWithStyle:1000 reuseIdentifier:@"SponsorBlockTextCell"];
textCell.textLabel.text = LOC(@"UserID");
textCell.textLabel.adjustsFontSizeToFitWidth = YES;
[textCell editableTextField].text = [self.settings valueForKey:@"userID"];
[textCell editableTextField].delegate = self;
return textCell;
}
if (indexPath.section == 8) {
UITableViewCell *textCell = [[UITableViewCell alloc] initWithStyle:1000 reuseIdentifier:@"SponsorBlockTextCell"];
textCell.textLabel.text = LOC(@"API_URL");
textCell.textLabel.adjustsFontSizeToFitWidth = YES;
[textCell editableTextField].text = [self.settings valueForKey:@"apiInstance"];
[textCell editableTextField].delegate = self;
return textCell;
}
if (indexPath.section == 9) {
UITableViewCell *textCell = [[UITableViewCell alloc] initWithStyle:1000 reuseIdentifier:@"SponsorBlockTextCell"];
textCell.textLabel.text = LOC(@"MinimumSegmentDuration");
textCell.textLabel.adjustsFontSizeToFitWidth = YES;
[textCell editableTextField].text = [NSString stringWithFormat:@"%.1f", [[self.settings valueForKey:@"minimumDuration"] floatValue]];
[textCell editableTextField].keyboardType = UIKeyboardTypeDecimalPad;
[textCell editableTextField].delegate = self;
return textCell;
}
if (indexPath.section == 10) {
UITableViewCell *textCell = [[UITableViewCell alloc] initWithStyle:1000 reuseIdentifier:@"SponsorBlockTextCell"];
textCell.textLabel.text = LOC(@"HowLongNoticeWillAppear");
textCell.textLabel.adjustsFontSizeToFitWidth = YES;
[textCell editableTextField].text = [NSString stringWithFormat:@"%.1f", [[self.settings valueForKey:@"skipNoticeDuration"] floatValue]];
[textCell editableTextField].keyboardType = UIKeyboardTypeDecimalPad;
[textCell editableTextField].delegate = self;
return textCell;
}
if (indexPath.section >= 11 && indexPath.section < 17) {
NSArray *titles = @[LOC(@"ShowSkipNotice"), LOC(@"ShowButtonsInPlayer"), LOC(@"HideStartEndButtonInPlayer"), LOC(@"ShowModifiedTime"), LOC(@"AudioNotificationOnSkip"), LOC(@"EnableSkipCountTracking")];
NSArray *titlesNames = @[@"showSkipNotice", @"showButtonsInPlayer", @"hideStartEndButtonInPlayer", @"showModifiedTime", @"skipAudioNotification", @"enableSkipCountTracking"];
UITableViewCell *tableCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SponsorBlockCell3"];
tableCell.textLabel.text = titles[indexPath.section-11];
tableCell.textLabel.adjustsFontSizeToFitWidth = YES;
UISwitch *toggleSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0,0,51,31)];
tableCell.accessoryView = toggleSwitch;
[toggleSwitch addTarget:self action:@selector(switchToggled:) forControlEvents:UIControlEventValueChanged];
if ([self.settings valueForKey:titlesNames[indexPath.section-11]]) {
[toggleSwitch setOn:[[self.settings valueForKey:titlesNames[indexPath.section-11]] boolValue] animated:NO];
} else {
[toggleSwitch setOn:YES animated:NO];
[self switchToggled:toggleSwitch];
}
return tableCell;
}
if (indexPath.section == 17) {
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SponsorBlockDonationCell"];
cell.textLabel.text = indexPath.row == 0 ? LOC(@"DonateOnVenmo") : LOC(@"DonateOnPayPal");
cell.imageView.image = [UIImage systemImageNamed:@"dollarsign.circle.fill"];
return cell;
}
return nil;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section == 0) return nil;
if (section <= 8) return self.sectionTitles[section-1];
return nil;
}
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
if (section == 0) return LOC(@"RestartFooter");
if (section == 7) return LOC(@"UserIDFooter");
if (section == 8) return LOC(@"APIFooter");
if (section == 15) return LOC(@"AudioFooter");
return nil;
}
//To allow highlights only for certain sections
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 17) {
return YES;
} else {
return NO;
}
}
- (void)dismissButtonTapped:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 17) {
if (indexPath.row == 0) {
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"venmo://"]]) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"venmo://venmo.com/code?user_id=3178620965093376215"] options:@{} completionHandler:nil];
} else {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://venmo.com/code?user_id=3178620965093376215"] options:@{} completionHandler:nil];
}
} else {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://paypal.me/DBrett684"] options:@{} completionHandler:nil];
}
}
[tableView deselectRowAtIndexPath:indexPath animated:YES]; //To prevent highlight sticking after pressing on buttons
}
- (void)enabledSwitchToggled:(UISwitch *)sender {
[self.settings setValue:@(sender.on) forKey:@"enabled"];
[self writeSettings];
}
- (void)switchToggled:(UISwitch *)sender {
UITableViewCell *cell = (UITableViewCell *)sender.superview;
NSArray *titlesNames = @[@"showSkipNotice", @"showButtonsInPlayer", @"hideStartEndButtonInPlayer", @"showModifiedTime", @"skipAudioNotification", @"enableSkipCountTracking"];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
[self.settings setValue:@(sender.on) forKey:titlesNames[indexPath.section-11]];
[self writeSettings];
}
- (void)categorySegmentSelected:(UISegmentedControl *)segmentedControl {
NSMutableDictionary *categorySettings = [self.settings valueForKey:@"categorySettings"];
[categorySettings setValue:@(segmentedControl.selectedSegmentIndex) forKey:[(SponsorBlockTableCell *)segmentedControl.superview.superview category]];
[self.settings setValue:categorySettings forKey:@"categorySettings"];
[self writeSettings];
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
UITableViewCell *cell = (UITableViewCell *)textField.superview.superview;
NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
f.numberStyle = NSNumberFormatterDecimalStyle;
NSString *minimumDurationTitle = LOC(@"MinimumSegmentDuration");
NSString *skipNoticeDurationTitle = LOC(@"HowLongNoticeWillAppear");
NSString *userIDTitle = LOC(@"UserID");
NSString *apiURLTitle = LOC(@"API_URL");
if ([cell.textLabel.text isEqualToString:minimumDurationTitle]) {
[self.settings setValue:[f numberFromString:textField.text] forKey:@"minimumDuration"];
} else if ([cell.textLabel.text isEqualToString:skipNoticeDurationTitle]) {
[self.settings setValue:[f numberFromString:textField.text] forKey:@"skipNoticeDuration"];
} else if ([cell.textLabel.text isEqualToString:userIDTitle]) {
[self.settings setValue:textField.text forKey:@"userID"];
} else if ([cell.textLabel.text isEqualToString:apiURLTitle]) {
[self.settings setValue:textField.text forKey:@"apiInstance"];
}
[self writeSettings];
}
- (void)writeSettings {
[self.settings writeToURL:[NSURL fileURLWithPath:self.settingsPath isDirectory:NO] error:nil];
CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), CFSTR("com.galacticdev.isponsorblockprefs.changed"), NULL, NULL, YES);
}
@end