forked from level3tjg/RedditFilter
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathFeedFilterSettingsViewController.x
More file actions
456 lines (424 loc) · 18.7 KB
/
Copy pathFeedFilterSettingsViewController.x
File metadata and controls
456 lines (424 loc) · 18.7 KB
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
#import <CoreFoundation/CoreFoundation.h>
#import "FeedFilterSettingsViewController.h"
#import "DebugMenu.h"
extern NSString *localizedString(NSString *key, NSString *table);
extern UIImage *iconWithName(NSString *iconName);
extern Class CoreClass(NSString *name);
#define LOC(x, d) (localizedString(x, nil) ?: d)
// Helper to broadcast preferences update
static void postPrefsUpdatedNotification() {
CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), CFSTR("com.level3tjg.redditfilter/prefsUpdated"), NULL, NULL, true);
}
#if REDDITFILTER_DEBUG
// Visible declarations for the debug-only helpers so the direct call site in
// -cellForRowAtIndexPath: and the @selector(...) references are fully typed.
// (The implementations are added to the class at runtime by Logos below.)
@interface FeedFilterSettingsViewController (RFSchemaDebug)
- (UITableViewCell *)debugCellForRow:(NSInteger)row inTableView:(UITableView *)tableView;
- (void)rfCopyDiscoveredPath:(UIButton *)sender;
- (void)rfCopyFailedJSON:(UIButton *)sender;
- (void)rfResetCounters:(UIButton *)sender;
@end
#endif
%subclass FeedFilterSettingsViewController : BaseTableViewController
%new
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#if REDDITFILTER_DEBUG
return 2;
#else
return 1;
#endif
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
switch (section) {
case 0:
return 6;
#if REDDITFILTER_DEBUG
case 1:
// One row per tracked schema path, plus a trailing "reset" row.
return [[RFSchemaDebug shared] snapshot].count + 1;
#endif
default:
return 0;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *mainLabelText;
NSString *detailLabelText;
NSArray *iconNames;
ToggleImageTableViewCell *toggleCell;
ImageLabelTableViewCell *cell;
switch (indexPath.section) {
case 0: {
toggleCell = [tableView dequeueReusableCellWithIdentifier:kToggleCellID
forIndexPath:indexPath];
// Fixed Switch Handler Accumulation
[toggleCell.accessorySwitch removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents];
switch (indexPath.row) {
case 0:
mainLabelText = LOC(@"filter.settings.promoted.title", @"Promoted");
iconNames = @[ @"rpl3/tag", @"icon_tag" ];
toggleCell.accessorySwitch.on =
![NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterPromoted];
[toggleCell.accessorySwitch addTarget:self
action:@selector(didTogglePromotedSwitch:)
forControlEvents:UIControlEventValueChanged];
break;
case 1:
mainLabelText = LOC(@"filter.settings.recommended.title", @"Recommended");
iconNames = @[ @"rpl3/spam", @"icon_spam" ];
toggleCell.accessorySwitch.on =
![NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterRecommended];
[toggleCell.accessorySwitch addTarget:self
action:@selector(didToggleRecommendedSwitch:)
forControlEvents:UIControlEventValueChanged];
break;
case 2:
mainLabelText = LOC(@"filter.settings.nsfw.title", @"NSFW");
iconNames = @[ @"rpl3/nsfw", @"icon_nsfw_outline", @"icon_nsfw" ];
toggleCell.accessorySwitch.on =
![NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterNSFW];
[toggleCell.accessorySwitch addTarget:self
action:@selector(didToggleNsfwSwitch:)
forControlEvents:UIControlEventValueChanged];
break;
case 3:
mainLabelText = LOC(@"filter.settings.awards.title", @"Awards");
detailLabelText =
LOC(@"filter.settings.awards.subtitle", @"Show awards on posts and comments");
iconNames = @[ @"rpl3/award", @"icon_gift_fill", @"icon_award", @"icon-award-outline" ];
toggleCell.accessorySwitch.on =
![NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterAwards];
[toggleCell.accessorySwitch addTarget:self
action:@selector(didToggleAwardsSwitch:)
forControlEvents:UIControlEventValueChanged];
break;
case 4:
mainLabelText = LOC(@"filter.settings.scores.title", @"Scores");
detailLabelText =
LOC(@"filter.settings.scores.subtitle", @"Show vote count on posts and comments");
iconNames = @[ @"rpl3/upvote", @"icon_upvote" ];
toggleCell.accessorySwitch.on =
![NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterScores];
[toggleCell.accessorySwitch addTarget:self
action:@selector(didToggleScoresSwitch:)
forControlEvents:UIControlEventValueChanged];
break;
case 5:
mainLabelText = LOC(@"filter.settings.automod.title", @"AutoMod");
detailLabelText =
LOC(@"filter.settings.automod.subtitle", @"Auto collapse AutoMod comments");
iconNames = @[ @"rpl3/mod", @"icon_mod" ];
toggleCell.accessorySwitch.on =
[NSUserDefaults.standardUserDefaults boolForKey:kRedditFilterAutoCollapseAutoMod];
[toggleCell.accessorySwitch addTarget:self
action:@selector(didToggleAutoCollapseAutoModSwitch:)
forControlEvents:UIControlEventValueChanged];
break;
default:
return nil;
}
cell = toggleCell;
break;
}
#if REDDITFILTER_DEBUG
case 1:
return [self debugCellForRow:indexPath.row inTableView:tableView];
#endif
default:
return nil;
}
([cell respondsToSelector:@selector(mainLabel)] ? cell.mainLabel : cell.imageLabelView.mainLabel)
.text = mainLabelText;
([cell respondsToSelector:@selector(detailLabel)] ? cell.detailLabel
: cell.imageLabelView.detailLabel)
.text = detailLabelText;
UIImage *iconImage;
for (NSString *iconName in iconNames) {
iconImage = iconWithName(iconName);
if (iconImage) break;
}
if (iconImage) {
UIImage *displayImage = [[iconImage imageScaledToSize:CGSizeMake(20, 20)]
imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
if ([cell respondsToSelector:@selector(setDisplayImage:)])
cell.displayImage = displayImage;
else
cell.imageLabelView.imageView.image = displayImage;
}
return cell;
}
%new
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
BaseLabel *label = [%c(BaseLabel) labelWithSubheaderFont];
LayoutGuidance *layoutGuidance = [%c(LayoutGuidance) currentGuidance];
label.frame = CGRectMake(layoutGuidance.gridPadding, 0,
layoutGuidance.maxContentWidth - layoutGuidance.gridPaddingDouble, 40.0);
[label associatePropertySetter:@selector(setTextColor:)
withThemePropertyGetter:@selector(metaTextColor)];
BaseTableReusableView *headerView = [[%c(BaseTableReusableView) alloc]
initWithFrame:CGRectMake(0, 0, tableView.frameWidth, 40.0)];
[headerView.contentView addSubview:label];
[headerView associatePropertySetter:@selector(setBackgroundColor:)
withThemePropertyGetter:@selector(canvasColor)];
switch (section) {
case 0:
label.text = [LOC(@"filter.settings.header", @"Filters") uppercaseString];
break;
#if REDDITFILTER_DEBUG
case 1:
label.text = [@"Schema Paths · Debug" uppercaseString];
break;
#endif
default:
return nil;
}
return headerView;
}
%new
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 40.0;
}
%new
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
CGFloat footerHeight = [self tableView:tableView heightForFooterInSection:section];
BaseLabel *label = [%c(BaseLabel) labelWithSubheaderFont];
LayoutGuidance *layoutGuidance = [%c(LayoutGuidance) currentGuidance];
label.frame = CGRectMake(layoutGuidance.gridPadding, 0,
layoutGuidance.maxContentWidth - layoutGuidance.gridPaddingDouble,
footerHeight);
[label associatePropertySetter:@selector(setTextColor:)
withThemePropertyGetter:@selector(metaTextColor)];
BaseTableReusableView *footerView = [[%c(BaseTableReusableView) alloc]
initWithFrame:CGRectMake(0, 0, tableView.frameWidth, footerHeight)];
[footerView.contentView addSubview:label];
[footerView associatePropertySetter:@selector(setBackgroundColor:)
withThemePropertyGetter:@selector(canvasColor)];
switch (section) {
case 0:
label.text = LOC(@"filter.settings.footer", @"Filter specific types of posts from your feed");
break;
#if REDDITFILTER_DEBUG
case 1:
label.numberOfLines = 0;
label.text = @"✓ resolved · ✗ broke (structural fallback is now filtering). "
@"Tap Copy on a ✗ row to grab the auto-discovered replacement path.";
break;
#endif
default:
return nil;
}
return footerView;
}
%new
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
#if REDDITFILTER_DEBUG
if (section == 1) return 76.0;
#endif
return 40.0;
}
- (void)viewDidLoad {
%orig;
self.title = @"RedditFilter";
// Provide safe fallbacks to prevent crashes if Reddit removes/renames classes
Class toggleCellClass = CoreClass(@"ToggleImageTableViewCell") ?: [UITableViewCell class];
Class labelCellClass = CoreClass(@"ImageLabelTableViewCell") ?: [UITableViewCell class];
[self.tableView registerClass:toggleCellClass
forCellReuseIdentifier:kToggleCellID];
[self.tableView registerClass:labelCellClass
forCellReuseIdentifier:kLabelCellID];
#if REDDITFILTER_DEBUG
// Debug rows carry multi-line detail text, so let them self-size.
self.tableView.estimatedRowHeight = 60.0;
self.tableView.rowHeight = UITableViewAutomaticDimension;
#endif
}
// Emitting notifications across tweak processes allows updates without querying
%new
- (void)didTogglePromotedSwitch:(UISwitch *)sender {
[NSUserDefaults.standardUserDefaults setBool:!sender.on forKey:kRedditFilterPromoted];
postPrefsUpdatedNotification();
}
%new
- (void)didToggleRecommendedSwitch:(UISwitch *)sender {
[NSUserDefaults.standardUserDefaults setBool:!sender.on forKey:kRedditFilterRecommended];
postPrefsUpdatedNotification();
}
%new
- (void)didToggleNsfwSwitch:(UISwitch *)sender {
[NSUserDefaults.standardUserDefaults setBool:!sender.on forKey:kRedditFilterNSFW];
postPrefsUpdatedNotification();
}
%new
- (void)didToggleAwardsSwitch:(UISwitch *)sender {
[NSUserDefaults.standardUserDefaults setBool:!sender.on forKey:kRedditFilterAwards];
postPrefsUpdatedNotification();
}
%new
- (void)didToggleScoresSwitch:(UISwitch *)sender {
[NSUserDefaults.standardUserDefaults setBool:!sender.on forKey:kRedditFilterScores];
postPrefsUpdatedNotification();
}
%new
- (void)didToggleAutoCollapseAutoModSwitch:(UISwitch *)sender {
[NSUserDefaults.standardUserDefaults setBool:sender.on forKey:kRedditFilterAutoCollapseAutoMod];
postPrefsUpdatedNotification();
}
// ---------------------------------------------------------------------------
// Schema-path debug section.
//
// All of the method *declarations* below are compiled unconditionally so that
// Logos always registers them on the subclass; only their bodies are gated on
// REDDITFILTER_DEBUG. In a release build the bodies collapse to no-ops, the
// section is never shown (numberOfSections returns 1), and these methods are
// never invoked.
// ---------------------------------------------------------------------------
%new
- (UITableViewCell *)debugCellForRow:(NSInteger)row inTableView:(UITableView *)tableView {
#if REDDITFILTER_DEBUG
static NSString *const kRFDebugCellID = @"RFSchemaDebugCell";
// Deliberately a plain UIKit cell, not a Reddit class: the whole point of
// this screen is to keep working when Reddit's own classes/schema change.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kRFDebugCellID];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:kRFDebugCellID];
cell.detailTextLabel.numberOfLines = 0;
cell.detailTextLabel.font = [UIFont monospacedSystemFontOfSize:11.0
weight:UIFontWeightRegular];
cell.textLabel.font = [UIFont systemFontOfSize:15.0 weight:UIFontWeightSemibold];
}
cell.accessoryView = nil;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
NSArray<NSDictionary *> *snapshot = [[RFSchemaDebug shared] snapshot];
// Trailing "reset" row.
if (row >= (NSInteger)snapshot.count) {
cell.textLabel.textColor = [UIColor systemBlueColor];
cell.textLabel.text = @"Reset counters";
cell.detailTextLabel.textColor = [UIColor secondaryLabelColor];
cell.detailTextLabel.text = @"Clear all stats and re-arm path discovery";
UIButton *resetButton = [UIButton buttonWithType:UIButtonTypeSystem];
[resetButton setTitle:@"Reset" forState:UIControlStateNormal];
resetButton.titleLabel.font = [UIFont systemFontOfSize:14.0 weight:UIFontWeightSemibold];
[resetButton addTarget:self
action:@selector(rfResetCounters:)
forControlEvents:UIControlEventTouchUpInside];
[resetButton sizeToFit];
cell.accessoryView = resetButton;
return cell;
}
NSDictionary *record = snapshot[row];
NSString *op = record[kRFDebugOp];
NSString *expected = record[kRFDebugExpected];
NSString *discovered = record[kRFDebugDiscovered];
NSInteger hits = [record[kRFDebugHits] integerValue];
NSInteger misses = [record[kRFDebugMisses] integerValue];
BOOL seen = [record[kRFDebugSeen] boolValue];
BOOL lastResolved = [record[kRFDebugLastResolved] boolValue];
cell.textLabel.textColor = [UIColor labelColor];
cell.textLabel.text = op;
NSString *detail;
UIColor *detailColor;
if (!seen) {
detail = [NSString stringWithFormat:@"untested\nexpected: %@", expected];
detailColor = [UIColor secondaryLabelColor];
} else if (lastResolved) {
detail = [NSString stringWithFormat:@"\u2713 OK \u00b7 %ld hit%@",
(long)hits, hits == 1 ? @"" : @"s"];
if (misses > 0)
detail = [detail stringByAppendingFormat:@" (recovered after %ld miss%@)",
(long)misses, misses == 1 ? @"" : @"es"];
detailColor = [UIColor systemGreenColor];
} else {
detail = [NSString stringWithFormat:@"\u2717 MISS \u00b7 %ld miss%@ \u00b7 fallback active",
(long)misses, misses == 1 ? @"" : @"es"];
if (discovered.length) {
detail = [detail stringByAppendingFormat:@"\n\u2192 %@", discovered];
UIButton *copyButton = [UIButton buttonWithType:UIButtonTypeSystem];
[copyButton setTitle:@"Copy" forState:UIControlStateNormal];
copyButton.titleLabel.font = [UIFont systemFontOfSize:14.0 weight:UIFontWeightSemibold];
copyButton.tag = row;
[copyButton addTarget:self
action:@selector(rfCopyDiscoveredPath:)
forControlEvents:UIControlEventTouchUpInside];
[copyButton sizeToFit];
cell.accessoryView = copyButton;
} else {
detail = [detail stringByAppendingFormat:@"\ncould not auto-locate a new path\nexpected: %@",
expected];
// Show the "Copy JSON" button if we captured a payload
NSString *failedJSON = record[kRFDebugFailedJSON];
if (failedJSON.length > 0) {
detail = [detail stringByAppendingString:@"\n\u2192 raw payload captured"];
UIButton *copyJsonButton = [UIButton buttonWithType:UIButtonTypeSystem];
[copyJsonButton setTitle:@"Copy JSON" forState:UIControlStateNormal];
copyJsonButton.titleLabel.font = [UIFont systemFontOfSize:14.0 weight:UIFontWeightSemibold];
copyJsonButton.tag = row;
[copyJsonButton addTarget:self
action:@selector(rfCopyFailedJSON:)
forControlEvents:UIControlEventTouchUpInside];
[copyJsonButton sizeToFit];
cell.accessoryView = copyJsonButton;
}
}
detailColor = [UIColor systemRedColor];
}
cell.detailTextLabel.text = detail;
cell.detailTextLabel.textColor = detailColor;
return cell;
#else
return nil;
#endif
}
%new
- (void)rfCopyDiscoveredPath:(UIButton *)sender {
#if REDDITFILTER_DEBUG
NSArray<NSDictionary *> *snapshot = [[RFSchemaDebug shared] snapshot];
if (sender.tag < 0 || sender.tag >= (NSInteger)snapshot.count) return;
NSString *discovered = snapshot[sender.tag][kRFDebugDiscovered];
if (!discovered.length) return;
UIPasteboard.generalPasteboard.string = discovered;
[sender setTitle:@"Copied" forState:UIControlStateNormal];
[sender sizeToFit];
__weak UIButton *weakSender = sender;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.2 * NSEC_PER_SEC)),
dispatch_get_main_queue(), ^{
[weakSender setTitle:@"Copy" forState:UIControlStateNormal];
[weakSender sizeToFit];
});
#endif
}
%new
- (void)rfCopyFailedJSON:(UIButton *)sender {
#if REDDITFILTER_DEBUG
NSArray<NSDictionary *> *snapshot = [[RFSchemaDebug shared] snapshot];
if (sender.tag < 0 || sender.tag >= (NSInteger)snapshot.count) return;
NSString *failedJSON = snapshot[sender.tag][kRFDebugFailedJSON];
if (!failedJSON.length) return;
UIPasteboard.generalPasteboard.string = failedJSON;
[sender setTitle:@"Copied" forState:UIControlStateNormal];
[sender sizeToFit];
__weak UIButton *weakSender = sender;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.2 * NSEC_PER_SEC)),
dispatch_get_main_queue(), ^{
[weakSender setTitle:@"Copy JSON" forState:UIControlStateNormal];
[weakSender sizeToFit];
});
#endif
}
%new
- (void)rfResetCounters:(UIButton *)sender {
#if REDDITFILTER_DEBUG
[[RFSchemaDebug shared] reset];
[self.tableView reloadData];
#endif
}
- (void)viewWillAppear:(BOOL)animated {
%orig;
#if REDDITFILTER_DEBUG
// Stats accrue while the app runs; refresh them each time the screen opens.
[self.tableView reloadData];
#endif
}
%end