Skip to content

Commit f7e62f8

Browse files
committed
move settings cell to account page
1 parent 60aa103 commit f7e62f8

13 files changed

+167
-84
lines changed

Settings.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1+
#import <Twitch/AccountMenuViewController.h>
12
#import <Twitch/AppSettingsViewController.h>
3+
#import <Twitch/ConfigurableAccessoryTableViewCell.h>
24
#import <Twitch/SettingsDisclosureCell.h>
5+
6+
#import "TWAdBlockSettingsViewController.h"

Settings.x

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#import "Settings.h"
2+
3+
extern NSUserDefaults *tweakDefaults;
4+
5+
%hook _TtC6Twitch25AccountMenuViewController
6+
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
7+
if (indexPath.section == [self numberOfSectionsInTableView:tableView] - 1 &&
8+
indexPath.row == [self tableView:tableView numberOfRowsInSection:indexPath.section] - 1) {
9+
UITableViewStyle tableViewStyle = UITableViewStyleGrouped;
10+
if (@available(iOS 13, *)) tableViewStyle = UITableViewStyleInsetGrouped;
11+
TWAdBlockSettingsViewController *adblockSettingsViewController =
12+
[[objc_getClass("TWAdBlockSettingsViewController") alloc]
13+
initWithTableViewStyle:tableViewStyle
14+
themeManager:[objc_getClass("_TtC12TwitchCoreUI21TWDefaultThemeManager")
15+
defaultThemeManager]];
16+
adblockSettingsViewController.tableView.separatorStyle =
17+
UITableViewCellSeparatorStyleSingleLine;
18+
return [self.navigationController pushViewController:adblockSettingsViewController
19+
20+
animated:YES];
21+
}
22+
%orig;
23+
}
24+
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
25+
NSInteger numberOfRows = %orig;
26+
if (section == [self numberOfSectionsInTableView:tableView] - 1) numberOfRows++;
27+
return numberOfRows;
28+
}
29+
- (UITableViewCell *)tableView:(UITableView *)tableView
30+
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
31+
if (indexPath.section == [self numberOfSectionsInTableView:tableView] - 1 &&
32+
indexPath.row == [self tableView:tableView numberOfRowsInSection:indexPath.section] - 1) {
33+
_TtC6Twitch34ConfigurableAccessoryTableViewCell *cell =
34+
[[objc_getClass("_TtC6Twitch34ConfigurableAccessoryTableViewCell") alloc]
35+
initWithStyle:UITableViewCellStyleSubtitle
36+
reuseIdentifier:@"Twitch.ConfigurableAccessoryTableViewCell"];
37+
[cell configureWithTitle:@"TwitchAdBlock"];
38+
NSBundle *twCoreUIBundle = [NSBundle bundleWithIdentifier:@"twitch.TwitchCoreUI"];
39+
cell.accessoryView =
40+
[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow-forward-Icon"
41+
inBundle:twCoreUIBundle
42+
compatibleWithTraitCollection:nil]];
43+
cell.useDefaultBackgroundColor = YES;
44+
Ivar customImageViewIvar = class_getInstanceVariable(object_getClass(cell), "customImageView");
45+
if (!customImageViewIvar) return cell;
46+
UIImageView *customImageView = object_getIvar(cell, customImageViewIvar);
47+
customImageView.image = [UIImage imageNamed:@"Un-Host-Icon"
48+
inBundle:twCoreUIBundle
49+
compatibleWithTraitCollection:nil];
50+
customImageView.hidden = NO;
51+
return cell;
52+
}
53+
return %orig;
54+
}
55+
%end

Settings.xm

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#import <AVFoundation/AVFoundation.h>
2+
#import <Foundation/Foundation.h>
3+
#import "Config.h"
4+
#import "NSURL+TwitchAdBlock.h"
5+
#import "NSURLSession+TwitchAdBlock.h"
6+
7+
@interface TWAdBlockAssetResourceLoaderDelegate : NSObject <AVAssetResourceLoaderDelegate>
8+
@end
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#import "TWAdBlockAssetResourceLoaderDelegate.h"
2+
3+
extern NSUserDefaults *tweakDefaults;
4+
5+
@implementation TWAdBlockAssetResourceLoaderDelegate
6+
- (BOOL)handleLoadingRequest:(AVAssetResourceLoadingRequest *)loadingRequest {
7+
NSURL *URL = loadingRequest.request.URL;
8+
if (![URL.scheme isEqualToString:@"twab"]) return NO;
9+
AVAssetResourceLoadingDataRequest *dataRequest = loadingRequest.dataRequest;
10+
NSURLComponents *components = [NSURLComponents componentsWithURL:URL resolvingAgainstBaseURL:YES];
11+
components.scheme = @"https";
12+
NSMutableURLRequest *request = loadingRequest.request.mutableCopy;
13+
request.URL = components.URL;
14+
NSString *proxy = [tweakDefaults boolForKey:@"TWAdBlockCustomProxyEnabled"]
15+
? [tweakDefaults stringForKey:@"TWAdBlockProxy"]
16+
: PROXY_ADDR;
17+
NSURLSession *session = [[NSURLSession alloc] twab_proxySessionWithAddress:proxy];
18+
[[session dataTaskWithRequest:request
19+
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
20+
if (error) return [loadingRequest finishLoadingWithError:error];
21+
loadingRequest.contentInformationRequest.contentType = AVFileTypeMPEG4;
22+
[dataRequest respondWithData:data];
23+
[loadingRequest finishLoading];
24+
}] resume];
25+
return YES;
26+
}
27+
- (BOOL)resourceLoader:(AVAssetResourceLoader *)resourceLoader
28+
shouldWaitForLoadingOfRequestedResource:(AVAssetResourceLoadingRequest *)loadingRequest {
29+
return [self handleLoadingRequest:loadingRequest];
30+
}
31+
- (BOOL)resourceLoader:(AVAssetResourceLoader *)resourceLoader
32+
shouldWaitForRenewalOfRequestedResource:(AVAssetResourceRenewalRequest *)renewalRequest {
33+
return [self handleLoadingRequest:renewalRequest];
34+
}
35+
@end
Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
%subclass TWAdBlockSettingsTextField : _TtC12TwitchCoreUI17StandardTextField
44
%new
55
- (id<UITextFieldDelegate>)delegate {
6-
return MSHookIvar<id<UITextFieldDelegate>>(self, "delegate");
6+
return object_getIvar(self, class_getInstanceVariable(object_getClass(self), "delegate"));
77
}
88
%new
99
- (void)setDelegate:(id<UITextFieldDelegate>)delegate {
10-
MSHookIvar<id<UITextFieldDelegate>>(self, "delegate") = delegate;
10+
object_setIvar(self, class_getInstanceVariable(object_getClass(self), "delegate"), delegate);
1111
}
1212
%new
1313
- (UITextField *)textField {
14-
return MSHookIvar<UITextField *>(self, "textField");
14+
return object_getIvar(self, class_getInstanceVariable(object_getClass(self), "textField"));
1515
}
1616
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
1717
if (![self.delegate respondsToSelector:@selector(textFieldShouldBeginEditing:)]) return YES;
@@ -20,7 +20,6 @@
2020
- (void)textFieldDidBeginEditing:(UITextField *)textField {
2121
if ([self.delegate respondsToSelector:@selector(textFieldDidBeginEditing:)])
2222
[self textFieldDidBeginEditing:textField];
23-
MSHookIvar<BOOL>(self, "isEditing") = YES;
2423
self.backgroundColor = self.lastConfiguredTheme.backgroundBodyColor;
2524
self.layer.borderColor = self.lastConfiguredTheme.backgroundAccentColor.CGColor;
2625
self.layer.borderWidth = 2;
@@ -42,7 +41,6 @@
4241
- (void)textFieldDidEndEditing:(UITextField *)textField {
4342
if ([self.delegate respondsToSelector:@selector(textFieldDidEndEditing:)])
4443
[self.delegate textFieldDidEndEditing:textField];
45-
MSHookIvar<BOOL>(self, "isEditing") = NO;
4644
self.backgroundColor = self.lastConfiguredTheme.backgroundInputColor;
4745
self.layer.borderWidth = 0;
4846
}
@@ -55,15 +53,13 @@
5553
}
5654
- (instancetype)initWithFrame:(CGRect)frame
5755
themeManager:(_TtC12TwitchCoreUI21TWDefaultThemeManager *)themeManager {
58-
MSHookIvar<int>(self, "maximumLength") = INT_MAX;
5956
Class originalClass = object_setClass(self, UIView.class);
6057
if ((self = [self initWithFrame:frame])) {
6158
object_setClass(self, originalClass);
6259
self.themeManager = themeManager;
6360
self.applyShadowPathForElevation = YES;
64-
MSHookIvar<UITextField *>(self, "textField") =
65-
[[objc_getClass("_TtC12TwitchCoreUI13BaseTextField") alloc] init];
66-
UITextField *textField = MSHookIvar<UITextField *>(self, "textField");
61+
UITextField *textField = [[objc_getClass("_TtC12TwitchCoreUI13BaseTextField") alloc] init];
62+
object_setIvar(self, class_getInstanceVariable(object_getClass(self), "textField"), textField);
6763
textField.borderStyle = UITextBorderStyleNone;
6864
textField.spellCheckingType = UITextSpellCheckingTypeNo;
6965
textField.returnKeyType = UIReturnKeyGo;
@@ -77,17 +73,12 @@
7773
forControlEvents:UIControlEventEditingChanged];
7874
[self addSubview:textField];
7975
CGFloat inputPadding = textField.intrinsicContentSize.width * 2;
80-
MSHookIvar<CGFloat>(self, "inputPadding") = inputPadding;
8176
NSArray<NSLayoutConstraint *> *textFieldConstraints = @[
8277
[self.leftAnchor constraintEqualToAnchor:textField.leftAnchor constant:-inputPadding],
8378
[self.rightAnchor constraintEqualToAnchor:textField.rightAnchor constant:inputPadding],
8479
[self.topAnchor constraintEqualToAnchor:textField.topAnchor],
8580
[self.bottomAnchor constraintEqualToAnchor:textField.bottomAnchor],
8681
];
87-
[NSLayoutConstraint deactivateConstraints:MSHookIvar<NSArray<NSLayoutConstraint *> *>(
88-
self, "_textFieldConstraints")];
89-
MSHookIvar<NSArray<NSLayoutConstraint *> *>(self, "_textFieldConstraints") =
90-
textFieldConstraints;
9182
[NSLayoutConstraint activateConstraints:textFieldConstraints];
9283
}
9384
return self;
@@ -98,7 +89,3 @@
9889
%orig;
9990
}
10091
%end
101-
102-
%ctor {
103-
if (![NSProcessInfo.processInfo.processName isEqualToString:@"mediaserverd"]) %init;
104-
}

TWAdBlockSettingsTextFieldTableViewCell.xm renamed to TWAdBlockSettingsTextFieldTableViewCell.x

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
initWithFrame:self.frame
99
themeManager:[objc_getClass("_TtC12TwitchCoreUI21TWDefaultThemeManager")
1010
defaultThemeManager]];
11-
UITextField *textField = MSHookIvar<UITextField *>(self.textField, "textField");
11+
UITextField *textField = object_getIvar(
12+
self.textField, class_getInstanceVariable(object_getClass(self.textField), "textField"));
1213
textField.returnKeyType = UIReturnKeyDone;
1314
[self addSubview:self.textField];
1415
}
@@ -22,8 +23,7 @@
2223
%end
2324

2425
%ctor {
25-
if (![NSProcessInfo.processInfo.processName isEqualToString:@"mediaserverd"])
26-
%init(BaseTableViewCell =
27-
objc_getClass("TWBaseTableViewCell")
28-
?: objc_getClass("_TtC12TwitchCoreUI17BaseTableViewCell"));
26+
%init(BaseTableViewCell =
27+
objc_getClass("TWBaseTableViewCell")
28+
?: objc_getClass("_TtC12TwitchCoreUI17BaseTableViewCell"));
2929
}

TWAdBlockSettingsViewController.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#import <Twitch/SettingsSwitchTableViewCell.h>
2+
#import <Twitch/VersionLabel.h>
23
#import <TwitchCoreUI/TWBaseTableViewController.h>
3-
#import <notify.h>
4+
#import "Config.h"
45
#import "TWAdBlockSettingsTextFieldTableViewCell.h"
56

67
@interface TWAdBlockSettingsViewController
Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
1+
#import <dlfcn.h>
12
#import "TWAdBlockSettingsViewController.h"
23

3-
extern "C" NSBundle *tweakBundle;
4-
extern "C" NSUserDefaults *tweakDefaults;
4+
extern NSBundle *tweakBundle;
5+
extern NSUserDefaults *tweakDefaults;
56

67
#define LOC(x, d) [tweakBundle localizedStringForKey:x value:d table:nil]
78

89
%hook _TtC6Twitch27SettingsSwitchTableViewCell
910
%new
1011
- (id)delegate {
11-
return MSHookIvar<id>(self, "delegate");
12+
return object_getIvar(self, class_getInstanceVariable(object_getClass(self), "delegate"));
1213
}
1314
%new
1415
- (void)setDelegate:(id)delegate {
15-
MSHookIvar<id>(self, "delegate") = delegate;
16+
object_setIvar(self, class_getInstanceVariable(object_getClass(self), "delegate"), delegate);
1617
}
1718
%new
1819
- (BOOL)isOn {
19-
return MSHookIvar<UISwitch *>(self, "$__lazy_storage_$_switchView").isOn;
20+
return [object_getIvar(
21+
self, class_getInstanceVariable(object_getClass(self), "$__lazy_storage_$_switchView")) isOn];
2022
}
2123
%new
2224
- (void)configureWithTitle:(NSString *)title
@@ -26,16 +28,16 @@ extern "C" NSUserDefaults *tweakDefaults;
2628
accessibilityIdentifier:(NSString *)accessibilityIdentifier {
2729
self.textLabel.text = title;
2830
self.detailTextLabel.text = subtitle;
29-
UISwitch *switchView = MSHookIvar<UISwitch *>(self, "$__lazy_storage_$_switchView");
31+
UISwitch *switchView = object_getIvar(
32+
self, class_getInstanceVariable(object_getClass(self), "$__lazy_storage_$_switchView"));
3033
switchView.enabled = isEnabled;
3134
switchView.on = isOn;
3235
self.accessibilityIdentifier = accessibilityIdentifier;
3336
}
3437
- (void)settingsSwitchToggled {
35-
id<SettingsSwitchTableViewCellDelegate> delegate =
36-
MSHookIvar<id<SettingsSwitchTableViewCellDelegate>>(self, "delegate");
37-
if (![delegate respondsToSelector:@selector(settingsCellSwitchToggled:)]) return %orig;
38-
[delegate settingsCellSwitchToggled:self];
38+
if (![self.delegate respondsToSelector:@selector(settingsCellSwitchToggled:)])
39+
return %orig;
40+
[self.delegate settingsCellSwitchToggled:self];
3941
}
4042
%end
4143

@@ -57,14 +59,14 @@ extern "C" NSUserDefaults *tweakDefaults;
5759
}
5860
%new
5961
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
60-
return self.adblockEnabled ? 2 : 1;
62+
return self.adblockEnabled ? 3 : 2;
6163
}
6264
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
6365
switch (section) {
6466
case 0:
6567
return 1;
6668
case 1:
67-
return self.proxyEnabled ? self.customProxyEnabled ? 3 : 2 : 1;
69+
return self.adblockEnabled ? self.proxyEnabled ? self.customProxyEnabled ? 3 : 2 : 1 : 0;
6870
default:
6971
return 0;
7072
}
@@ -117,7 +119,7 @@ extern "C" NSUserDefaults *tweakDefaults;
117119
reuseIdentifier:@"TWAdBlockProxy"];
118120
TWAdBlockSettingsTextField *textField =
119121
((TWAdBlockSettingsTextFieldTableViewCell *)cell).textField;
120-
textField.textField.placeholder = PROXY_URL;
122+
textField.textField.placeholder = PROXY_ADDR;
121123
textField.textField.text = [tweakDefaults stringForKey:@"TWAdBlockProxy"];
122124
textField.delegate = self;
123125
return cell;
@@ -127,16 +129,32 @@ extern "C" NSUserDefaults *tweakDefaults;
127129
}
128130
}
129131
%new
130-
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
132+
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
133+
NSString *title;
131134
switch (section) {
132135
case 0:
133-
return LOC(@"settings.adblock.footer", @"Choose whether or not you want to block ads");
136+
title = LOC(@"settings.adblock.footer", @"Choose whether or not you want to block ads");
137+
break;
134138
case 1:
135-
return LOC(@"settings.proxy.footer",
136-
@"Proxy specific requests through a proxy server based in an ad-free country");
139+
title = LOC(@"settings.proxy.footer",
140+
@"Proxy specific requests through a proxy server based in an ad-free country");
141+
if (self.adblockEnabled) break;
142+
case 2: {
143+
_TtC6Twitch12VersionLabel *versionLabel =
144+
[[objc_getClass("_TtC6Twitch12VersionLabel") alloc] initWithFrame:CGRectZero];
145+
versionLabel.text = @"TwitchAdBlock v" PACKAGE_VERSION;
146+
UIStackView *footerStackView =
147+
[[UIStackView alloc] initWithArrangedSubviews:@[ versionLabel ]];
148+
return footerStackView;
149+
}
137150
default:
138151
return nil;
139152
}
153+
UITableViewHeaderFooterView *footerView =
154+
[[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:nil];
155+
footerView.textLabel.text = title;
156+
footerView.textLabel.numberOfLines = 0;
157+
return footerView;
140158
}
141159
%new
142160
- (void)settingsCellSwitchToggled:(UISwitch *)sender {
@@ -176,16 +194,9 @@ extern "C" NSUserDefaults *tweakDefaults;
176194
}
177195

178196
[tweakDefaults synchronize];
179-
notify_post("com.level3tjg.twitchadblock/updatePrefs");
180197
}
181198
%new
182199
- (void)textFieldDidEndEditing:(UITextField *)textField {
183200
[tweakDefaults setValue:textField.text forKey:@"TWAdBlockProxy"];
184-
[tweakDefaults synchronize];
185-
notify_post("com.level3tjg.twitchadblock/updatePrefs");
186201
}
187202
%end
188-
189-
%ctor {
190-
if (![NSProcessInfo.processInfo.processName isEqualToString:@"mediaserverd"]) %init;
191-
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#import "SimpleTableViewCell.h"
2+
3+
@interface _TtC6Twitch34ConfigurableAccessoryTableViewCell : _TtC6Twitch19SimpleTableViewCell
4+
@property (nonatomic, assign) BOOL useDefaultBackgroundColor;
5+
@end

0 commit comments

Comments
 (0)