forked from qnblackcat/RedditFilter_Sideloaded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTweak.xm
executable file
·265 lines (247 loc) · 10.8 KB
/
Tweak.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
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
#import <Carousel.h>
#import <Comment.h>
#import <Post.h>
#import <ToggleImageTableViewCell.h>
#import <UIKit/UIKit.h>
#import <dlfcn.h>
#import <mach-o/dyld.h>
#import <objc/runtime.h>
#import <substrate.h>
#import "Preferences.h"
@interface UIImage ()
+ (UIImage *)imageNamed:(NSString *)name inBundle:(NSBundle *)bundle;
@end
static NSMutableArray *assetBundles;
extern "C" UIImage *iconWithName(NSString *iconName) {
NSArray *commonIconSizes = @[
@"24",
@"20",
@"16",
];
UIImage *iconImage;
for (NSBundle *bundle in assetBundles) {
for (NSString *iconSize in commonIconSizes) {
if (iconImage) break;
iconImage = [UIImage imageNamed:[NSString stringWithFormat:@"%@_%@", iconName, iconSize]
inBundle:bundle];
}
}
return iconImage;
}
extern "C" NSString *localizedString(NSString *key, NSString *table) {
for (NSBundle *bundle in assetBundles) {
NSString *localizedString = [bundle localizedStringForKey:key value:nil table:table];
if (![localizedString isEqualToString:key]) return localizedString;
}
return nil;
}
extern "C" Class CoreClass(NSString *name) {
Class cls = NSClassFromString(name);
NSArray *prefixes = @[
@"Reddit.",
@"RedditCore.",
@"RedditCoreModels.",
@"RedditCore_RedditCoreModels.",
@"RedditUI.",
];
for (NSString *prefix in prefixes) {
if (cls) break;
cls = NSClassFromString([prefix stringByAppendingString:name]);
}
return cls;
}
static NSArray *filteredObjects(NSArray *objects) {
return
[objects filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(
id object, NSDictionary *bindings) {
NSString *className = NSStringFromClass(object_getClass(object));
if ([NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterPromoted]) {
if ([className hasSuffix:@"AdPost"]) return NO;
if ([className hasSuffix:@"Post"] &&
[object respondsToSelector:@selector(isAdPost)] && ((Post *)object).isAdPost)
return NO;
}
if ([NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterRecommended]) {
if ([className containsString:@"Recommendation"]) return NO;
}
if ([NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterLivestreams]) {
if ([className containsString:@"Stream"]) return NO;
}
if ([NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterNSFW]) {
if ([className hasSuffix:@"Post"] &&
[object respondsToSelector:@selector(isNSFW)] && ((Post *)object).isNSFW)
return NO;
}
return YES;
}]];
}
%hook Listing
- (void)fetchNextPage:(id (^)(NSArray *, id))completion {
id (^newCompletion)(NSArray *, id) = ^id(NSArray *objects, id arg2) {
objects = filteredObjects(objects);
return completion(objects, arg2);
};
%orig(newCompletion);
}
%end
%hook FeedNetworkSource
- (NSArray *)postsAndCommentsFromData:(id)data {
return filteredObjects(%orig);
}
%end
%hook PostDetailPresenter
- (BOOL)shouldFetchCommentAdPost {
return [NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterPromoted] ? NO
: %orig;
}
%end
%hook StreamManager
- (instancetype)initWithAccountContext:(id)accountContext
source:(NSInteger)source
deeplinkSubredditName:(id)deeplinkSubredditName
streamingConfig:(id)streamingConfig {
if ([NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterLivestreams]) return nil;
return %orig;
}
- (instancetype)initWithService:(id)service
source:(NSInteger)source
deeplinkSubredditName:(id)deeplinkSubredditName
streamingConfig:(id)streamingConfig {
if ([NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterLivestreams]) return nil;
return %orig;
}
%end
%hook Carousel
- (BOOL)isHiddenByUserWithAccountSettings:(id)accountSettings {
return ([NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterRecommended] &&
([self.analyticType containsString:@"recommended"] ||
[self.analyticType containsString:@"similar"] ||
[self.analyticType containsString:@"popular"])) ||
%orig;
}
%end
%hook QuickActionViewModel
- (void)fetchActions {
if ([NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterRecommended]) return;
%orig;
}
%end
%hook Post
- (NSArray *)awardingTotals {
return [NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterAwards] ? nil
: %orig;
}
- (NSUInteger)totalAwardsReceived {
return [NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterAwards] ? 0
: %orig;
}
- (BOOL)canAward {
return [NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterAwards] ? NO
: %orig;
}
- (BOOL)isScoreHidden {
return [NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterScores] ? YES
: %orig;
}
%end
%hook Comment
- (NSArray *)awardingTotals {
return [NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterAwards] ? nil
: %orig;
}
- (NSUInteger)totalAwardsReceived {
return [NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterAwards] ? 0
: %orig;
}
- (BOOL)canAward {
return [NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterAwards] ? NO
: %orig;
}
- (BOOL)shouldHighlightForHighAward {
return [NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterAwards] ? NO
: %orig;
}
- (BOOL)isScoreHidden {
return [NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterScores] ? YES
: %orig;
}
- (BOOL)shouldAutoCollapse {
return [NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterAutoCollapseAutoMod] &&
[((Comment *)self).authorPk isEqualToString:@"t2_6l4z3"]
? YES
: %orig;
}
%end
%hook ToggleImageTableViewCell
- (void)updateConstraints {
%orig;
UIStackView *horizontalStackView =
[self respondsToSelector:@selector(imageLabelView)]
? [self imageLabelView].horizontalStackView
: object_getIvar(self,
class_getInstanceVariable(object_getClass(self), "horizontalStackView"));
UILabel *detailLabel = [self respondsToSelector:@selector(imageLabelView)]
? [self imageLabelView].detailLabel
: [self detailLabel];
if (!horizontalStackView || !detailLabel) return;
if (detailLabel.text) {
UIView *contentView = [self contentView];
[contentView addConstraint:[NSLayoutConstraint constraintWithItem:detailLabel
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:horizontalStackView
attribute:NSLayoutAttributeHeight
multiplier:.33
constant:0]];
[contentView addConstraint:[NSLayoutConstraint constraintWithItem:horizontalStackView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:contentView
attribute:NSLayoutAttributeHeight
multiplier:1
constant:0]];
[contentView addConstraint:[NSLayoutConstraint constraintWithItem:horizontalStackView
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:contentView
attribute:NSLayoutAttributeCenterY
multiplier:1
constant:0]];
}
}
%end
static BOOL initialized = NO;
static void add_image(const struct mach_header *mh, intptr_t vmaddr_slide) {
if (!initialized && %c(Listing)) {
%init(Comment = CoreClass(@"Comment"), Post = CoreClass(@"Post"),
QuickActionViewModel = CoreClass(@"QuickActionViewModel"),
StreamManager = CoreClass(@"StreamManager"),
ToggleImageTableViewCell = CoreClass(@"ToggleImageTableViewCell"));
initialized = YES;
}
}
%ctor {
assetBundles = [NSMutableArray new];
[assetBundles addObject:NSBundle.mainBundle];
for (NSString *file in
[NSFileManager.defaultManager contentsOfDirectoryAtPath:NSBundle.mainBundle.bundlePath
error:nil]) {
if (![file hasSuffix:@"bundle"]) continue;
NSBundle *bundle = [NSBundle
bundleWithPath:[NSBundle.mainBundle pathForResource:[file stringByDeletingPathExtension]
ofType:@"bundle"]];
if (bundle) [assetBundles addObject:bundle];
}
for (NSString *file in [NSFileManager.defaultManager
contentsOfDirectoryAtPath:[NSBundle.mainBundle.bundlePath
stringByAppendingPathComponent:@"Frameworks"]
error:nil]) {
if (![file hasSuffix:@"framework"]) continue;
NSBundle *bundle = [NSBundle
bundleWithPath:[NSBundle.mainBundle pathForResource:[file stringByDeletingPathExtension]
ofType:@"framework"
inDirectory:@"Frameworks"]];
if (bundle) [assetBundles addObject:bundle];
}
_dyld_register_func_for_add_image(add_image);
}