-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTweak.x
190 lines (164 loc) · 8.51 KB
/
Tweak.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
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
#import "Tweak.h"
%hook YTAsyncCollectionView
- (void)layoutSubviews {
%orig;
[self removeOffendingCells];
}
%new
- (void)removeOffendingCells {
__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
__strong typeof(weakSelf) strongSelf = weakSelf;
if (!strongSelf)
return;
@try {
NSArray *visibleCells = [strongSelf visibleCells];
NSMutableArray *indexPathsToRemove = [NSMutableArray array];
for (UICollectionViewCell *cell in visibleCells) {
if (![cell isKindOfClass:NSClassFromString(@"_ASCollectionViewCell")]) {
continue;
}
_ASCollectionViewCell *asCell = (_ASCollectionViewCell *)cell;
if (![asCell respondsToSelector:@selector(node)]) {
continue;
}
id node = [asCell node];
if (![node isKindOfClass:NSClassFromString(@"YTVideoWithContextNode")]) {
continue;
}
if ([Util nodeContainsBlockedVideo:node]) {
NSIndexPath *indexPath = [strongSelf indexPathForCell:cell];
if (indexPath) {
[indexPathsToRemove addObject:indexPath];
}
}
}
if (indexPathsToRemove.count > 0) {
[strongSelf
performBatchUpdates:^{ [strongSelf deleteItemsAtIndexPaths:indexPathsToRemove]; }
completion:nil];
}
} @catch (NSException *exception) {
NSLog(@"[Gonerino] Exception in removeOffendingCells: %@", exception);
}
});
}
%end
%hook YTDefaultSheetController
- (void)addAction:(YTActionSheetAction *)action {
%orig;
static void *blockActionKey = &blockActionKey;
if (objc_getAssociatedObject(self, blockActionKey)) {
return;
}
UIView *sourceView = [self valueForKey:@"sourceView"];
id node = [sourceView valueForKey:@"asyncdisplaykit_node"];
if (!node || ![node debugDescription] || ![[node debugDescription] containsString:@"YTVideoWithContextNode"]) {
return;
}
NSInteger currentActionsCount = 3;
if ([self respondsToSelector:@selector(actions)]) {
currentActionsCount = [[self actions] count];
}
if (currentActionsCount < 3) {
return;
}
__weak typeof(self) weakSelf = self;
CGSize iconSize = CGSizeMake(24, 24);
if (action) {
UIImage *originalIcon = [action valueForKey:@"_iconImage"];
if (originalIcon) {
iconSize = originalIcon.size;
}
}
YTActionSheetAction *blockChannelAction = [%c(YTActionSheetAction)
actionWithTitle:@"Block channel"
iconImage:[Util createBlockChannelIconWithSize:iconSize]
style:0
handler:^(YTActionSheetAction *action) {
__strong typeof(self) strongSelf = weakSelf;
@try {
UIView *sourceView = [strongSelf valueForKey:@"sourceView"];
id node = [sourceView valueForKey:@"asyncdisplaykit_node"];
if ([node respondsToSelector:@selector(subnodes)]) {
for (id subnode in [node subnodes]) {
if ([subnode isKindOfClass:NSClassFromString(@"YTInlinePlaybackPlayerNode")]) {
[Util extractVideoInfoFromNode:subnode
completion:^(NSString *videoId, NSString *videoTitle,
NSString *ownerName) {
if (ownerName) {
[[ChannelManager sharedInstance]
addBlockedChannel:ownerName];
UIViewController *viewController =
(UIViewController *)strongSelf;
[[%c(YTToastResponderEvent)
eventWithMessage:[NSString
stringWithFormat:@"Blocked %@",
ownerName]
firstResponder:viewController] send];
}
}];
break;
}
}
}
} @catch (NSException *e) {
NSLog(@"[Gonerino] Exception in block action: %@", e);
}
}];
YTActionSheetAction *blockVideoAction = [%c(YTActionSheetAction)
actionWithTitle:@"Block video"
iconImage:[Util createBlockVideoIconWithSize:iconSize]
style:0
handler:^(YTActionSheetAction *action) {
__strong typeof(self) strongSelf = weakSelf;
@try {
UIView *sourceView = [strongSelf valueForKey:@"sourceView"];
id node = [sourceView valueForKey:@"asyncdisplaykit_node"];
if ([node respondsToSelector:@selector(subnodes)]) {
for (id subnode in [node subnodes]) {
if ([subnode isKindOfClass:NSClassFromString(@"YTInlinePlaybackPlayerNode")]) {
[Util
extractVideoInfoFromNode:subnode
completion:^(NSString *videoId, NSString *videoTitle,
NSString *ownerName) {
if (videoId) {
[[VideoManager sharedInstance] addBlockedVideo:videoId
title:videoTitle
channel:ownerName];
UIViewController *viewController =
(UIViewController *)strongSelf;
[[%c(YTToastResponderEvent)
eventWithMessage:
[NSString stringWithFormat:@"Blocked video: %@",
videoTitle ?: videoId]
firstResponder:viewController] send];
if ([strongSelf respondsToSelector:@selector(dismiss)]) {
[strongSelf dismiss];
}
}
}];
break;
}
}
}
} @catch (NSException *e) {
NSLog(@"[Gonerino] Exception in block action: %@", e);
}
}];
objc_setAssociatedObject(self, blockActionKey, @YES, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[self addAction:blockChannelAction];
[self addAction:blockVideoAction];
}
%new
- (UIViewController *)findViewControllerForView:(UIView *)view {
UIResponder *responder = view;
while (responder) {
if ([responder isKindOfClass:[UIViewController class]]) {
return (UIViewController *)responder;
}
responder = [responder nextResponder];
}
return nil;
}
%end