-
Notifications
You must be signed in to change notification settings - Fork 36
/
TWAdBlockSettingsViewController.xm
191 lines (182 loc) · 7.71 KB
/
TWAdBlockSettingsViewController.xm
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
#import "TWAdBlockSettingsViewController.h"
extern "C" NSBundle *tweakBundle;
extern "C" NSUserDefaults *tweakDefaults;
#define LOC(x, d) [tweakBundle localizedStringForKey:x value:d table:nil]
%hook _TtC6Twitch27SettingsSwitchTableViewCell
%new
- (id)delegate {
return MSHookIvar<id>(self, "delegate");
}
%new
- (void)setDelegate:(id)delegate {
MSHookIvar<id>(self, "delegate") = delegate;
}
%new
- (BOOL)isOn {
return MSHookIvar<UISwitch *>(self, "$__lazy_storage_$_switchView").isOn;
}
%new
- (void)configureWithTitle:(NSString *)title
subtitle:(NSString *)subtitle
isEnabled:(BOOL)isEnabled
isOn:(BOOL)isOn
accessibilityIdentifier:(NSString *)accessibilityIdentifier {
self.textLabel.text = title;
self.detailTextLabel.text = subtitle;
UISwitch *switchView = MSHookIvar<UISwitch *>(self, "$__lazy_storage_$_switchView");
switchView.enabled = isEnabled;
switchView.on = isOn;
self.accessibilityIdentifier = accessibilityIdentifier;
}
- (void)settingsSwitchToggled {
id<SettingsSwitchTableViewCellDelegate> delegate =
MSHookIvar<id<SettingsSwitchTableViewCellDelegate>>(self, "delegate");
if (![delegate respondsToSelector:@selector(settingsCellSwitchToggled:)]) return %orig;
[delegate settingsCellSwitchToggled:self];
}
%end
%subclass TWAdBlockSettingsViewController : TWBaseTableViewController
%property(nonatomic, assign) BOOL adblockEnabled;
%property(nonatomic, assign) BOOL proxyEnabled;
%property(nonatomic, assign) BOOL customProxyEnabled;
- (instancetype)initWithTableViewStyle:(NSInteger)tableViewStyle themeManager:(id)themeManager {
if ((self = %orig)) {
self.adblockEnabled = [tweakDefaults boolForKey:@"TWAdBlockEnabled"];
self.proxyEnabled = [tweakDefaults boolForKey:@"TWAdBlockProxyEnabled"];
self.customProxyEnabled = [tweakDefaults boolForKey:@"TWAdBlockCustomProxyEnabled"];
}
return self;
}
- (void)viewDidLoad {
%orig;
self.title = @"TwitchAdBlock";
}
%new
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.adblockEnabled ? 2 : 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
switch (section) {
case 0:
return 1;
case 1:
return self.proxyEnabled ? self.customProxyEnabled ? 3 : 2 : 1;
default:
return 0;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
switch (indexPath.section) {
case 0:
cell = [[objc_getClass("_TtC6Twitch27SettingsSwitchTableViewCell") alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"AdBlockSwitchCell"];
[(_TtC6Twitch27SettingsSwitchTableViewCell *)cell
configureWithTitle:LOC(@"settings.adblock.title", @"Ad Block")
subtitle:nil
isEnabled:YES
isOn:[tweakDefaults boolForKey:@"TWAdBlockEnabled"]
accessibilityIdentifier:@"AdBlockSwitchCell"];
[(_TtC6Twitch27SettingsSwitchTableViewCell *)cell setDelegate:self];
return cell;
case 1:
switch (indexPath.row) {
case 0:
cell = [[objc_getClass("_TtC6Twitch27SettingsSwitchTableViewCell") alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"AdBlockProxySwitchCell"];
[(_TtC6Twitch27SettingsSwitchTableViewCell *)cell
configureWithTitle:LOC(@"settings.proxy.title", @"Ad Block Proxy")
subtitle:nil
isEnabled:YES
isOn:[tweakDefaults boolForKey:@"TWAdBlockProxyEnabled"]
accessibilityIdentifier:@"AdBlockProxySwitchCell"];
[(_TtC6Twitch27SettingsSwitchTableViewCell *)cell setDelegate:self];
return cell;
case 1:
cell = [[objc_getClass("_TtC6Twitch27SettingsSwitchTableViewCell") alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"AdBlockCustomProxySwitchCell"];
[(_TtC6Twitch27SettingsSwitchTableViewCell *)cell
configureWithTitle:LOC(@"settings.custom_proxy.title", @"Custom Proxy")
subtitle:nil
isEnabled:YES
isOn:[tweakDefaults boolForKey:@"TWAdBlockCustomProxyEnabled"]
accessibilityIdentifier:@"AdBlockCustomProxySwitchCell"];
[(_TtC6Twitch27SettingsSwitchTableViewCell *)cell setDelegate:self];
return cell;
case 2:
cell = [[objc_getClass("TWAdBlockSettingsTextFieldTableViewCell") alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"TWAdBlockProxy"];
TWAdBlockSettingsTextField *textField =
((TWAdBlockSettingsTextFieldTableViewCell *)cell).textField;
textField.textField.placeholder = PROXY_URL;
textField.textField.text = [tweakDefaults stringForKey:@"TWAdBlockProxy"];
textField.delegate = self;
return cell;
}
default:
return nil;
}
}
%new
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
switch (section) {
case 0:
return LOC(@"settings.adblock.footer", @"Choose whether or not you want to block ads");
case 1:
return LOC(@"settings.proxy.footer",
@"Proxy specific requests through a proxy server based in an ad-free country");
default:
return nil;
}
}
%new
- (void)settingsCellSwitchToggled:(UISwitch *)sender {
if ([sender.accessibilityIdentifier isEqualToString:@"AdBlockSwitchCell"]) {
[tweakDefaults setBool:sender.isOn forKey:@"TWAdBlockEnabled"];
self.adblockEnabled = sender.isOn;
NSIndexSet *sections = [NSIndexSet indexSetWithIndex:1];
if (sender.isOn)
[self.tableView insertSections:sections withRowAnimation:UITableViewRowAnimationFade];
else
[self.tableView deleteSections:sections withRowAnimation:UITableViewRowAnimationFade];
} else if ([sender.accessibilityIdentifier isEqualToString:@"AdBlockProxySwitchCell"]) {
[tweakDefaults setBool:sender.isOn forKey:@"TWAdBlockProxyEnabled"];
self.proxyEnabled = sender.isOn;
NSMutableArray *indexPaths = [NSMutableArray array];
[indexPaths addObject:[NSIndexPath indexPathForRow:1 inSection:1]];
if (self.customProxyEnabled) [indexPaths addObject:[NSIndexPath indexPathForRow:2 inSection:1]];
if (sender.isOn)
[self.tableView insertRowsAtIndexPaths:indexPaths
withRowAnimation:UITableViewRowAnimationFade];
else
[self.tableView deleteRowsAtIndexPaths:indexPaths
withRowAnimation:UITableViewRowAnimationFade];
} else if ([sender.accessibilityIdentifier isEqualToString:@"AdBlockCustomProxySwitchCell"]) {
[tweakDefaults setBool:sender.isOn forKey:@"TWAdBlockCustomProxyEnabled"];
self.customProxyEnabled = sender.isOn;
NSArray *indexPaths = @[ [NSIndexPath indexPathForRow:2 inSection:1] ];
if (sender.isOn)
[self.tableView insertRowsAtIndexPaths:indexPaths
withRowAnimation:UITableViewRowAnimationFade];
else
[self.tableView deleteRowsAtIndexPaths:indexPaths
withRowAnimation:UITableViewRowAnimationFade];
}
[tweakDefaults synchronize];
notify_post("com.level3tjg.twitchadblock/updatePrefs");
}
%new
- (void)textFieldDidEndEditing:(UITextField *)textField {
[tweakDefaults setValue:textField.text forKey:@"TWAdBlockProxy"];
[tweakDefaults synchronize];
notify_post("com.level3tjg.twitchadblock/updatePrefs");
}
%end
%ctor {
if (![NSProcessInfo.processInfo.processName isEqualToString:@"mediaserverd"]) %init;
}