forked from Apollo-Reborn/Apollo-Reborn
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApolloRecentlyRead.xm
More file actions
1165 lines (1016 loc) · 52.1 KB
/
Copy pathApolloRecentlyRead.xm
File metadata and controls
1165 lines (1016 loc) · 52.1 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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
#import <objc/message.h>
#import "ApolloCommon.h"
#import "ApolloState.h"
#import "Tweak.h"
#import "UserDefaultConstants.h"
#import "fishhook.h"
// MARK: - Recently Read Posts
// Direct access to ReadPostsTracker's in-memory ordered set via fishhook + ObjC runtime
static __unsafe_unretained id sReadPostsTracker = nil;
static Ivar sReadPostIDsIvar = NULL;
static void *sTrackerTypeMetadata = NULL;
// fishhook: briefly hook swift_allocObject to capture the ReadPostsTracker singleton
static void *(*orig_swift_allocObject)(void *type, size_t size, size_t alignMask);
static void *hooked_swift_allocObject(void *type, size_t size, size_t alignMask) {
void *obj = orig_swift_allocObject(type, size, alignMask);
if (type == sTrackerTypeMetadata && !sReadPostsTracker) {
sReadPostsTracker = (__bridge id)obj;
// Unhook immediately – only need one capture
rebind_symbols((struct rebinding[1]){{"swift_allocObject", (void *)orig_swift_allocObject, NULL}}, 1);
}
return obj;
}
// Retrieve the in-memory NSMutableOrderedSet of read post IDs from the tracker
static NSMutableOrderedSet *getTrackerReadPostIDs(void) {
if (!sReadPostsTracker) return nil;
// Lazily find the ivar by name
if (!sReadPostIDsIvar) {
unsigned int ivarCount = 0;
Ivar *ivars = class_copyIvarList([sReadPostsTracker class], &ivarCount);
if (ivars) {
for (unsigned int i = 0; i < ivarCount; i++) {
const char *name = ivar_getName(ivars[i]);
if (name && strstr(name, "readPostIDs")) {
sReadPostIDsIvar = ivars[i];
break;
}
}
free(ivars);
}
if (!sReadPostIDsIvar) {
ApolloLog(@"[RecentlyRead] readPostIDs ivar not found");
return nil;
}
}
id value = object_getIvar(sReadPostsTracker, sReadPostIDsIvar);
if ([value isKindOfClass:[NSMutableOrderedSet class]]) {
return (NSMutableOrderedSet *)value;
}
return nil;
}
// Flush the in-memory ReadPostIDs to NSUserDefaults so backup captures current state
void ApolloFlushReadPostIDsToDefaults(void) {
NSMutableOrderedSet *trackerSet = getTrackerReadPostIDs();
if (trackerSet && trackerSet.count > 0) {
ApolloLog(@"[RecentlyRead] Flushing %lu in-memory ReadPostIDs to NSUserDefaults", (unsigned long)trackerSet.count);
[[NSUserDefaults standardUserDefaults] setObject:[trackerSet array] forKey:@"ReadPostIDs"];
} else {
ApolloLog(@"[RecentlyRead] Flush skipped — tracker %s, count: %lu",
sReadPostsTracker ? "available" : "nil",
(unsigned long)(trackerSet ? trackerSet.count : 0));
}
}
@interface RecentlyReadViewController : UITableViewController <UISearchResultsUpdating>
@property (nonatomic, strong) NSMutableArray *posts;
@property (nonatomic, strong) NSMutableArray *filteredPosts;
@property (nonatomic, strong) UISearchController *searchController;
@property (nonatomic, strong) NSArray<NSString *> *allPostFullNames;
@property (nonatomic, assign) NSUInteger nextFetchIndex;
@property (nonatomic, assign) BOOL hasMorePages;
@property (nonatomic, assign) BOOL isFetchingPage;
@property (nonatomic, assign) BOOL hasLoadedOnce;
@property (nonatomic, strong) UIActivityIndicatorView *spinner;
@property (nonatomic, strong) UIActivityIndicatorView *footerSpinner;
@end
static char kNavPathKey;
static char kThumbURLKey;
static char kThumbTaskKey;
static char kThumbWidthConstraintKey;
static char kStackLeadingWithThumbConstraintKey;
static char kStackLeadingNoThumbConstraintKey;
static const NSUInteger kRecentlyReadPageSize = 40;
static const CGFloat kRecentlyReadThumbnailSmallSize = 55.0;
static const CGFloat kRecentlyReadThumbnailPlaceholderInset = 15.0;
static const CGFloat kRecentlyReadCellVerticalInset = 12.0;
static const CGFloat kRecentlyReadDefaultTopGap = 11.0;
static const CGFloat kRecentlyReadExpandedTopGap = 11.0;
static NSCache<NSString *, UIImage *> *RecentlyReadThumbnailCache(void) {
static NSCache<NSString *, UIImage *> *cache = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
cache = [[NSCache alloc] init];
cache.countLimit = 300;
});
return cache;
}
static CGRect RecentlyReadAspectFitRect(CGSize contentSize, CGRect bounds) {
if (contentSize.width <= 0.0 || contentSize.height <= 0.0 || CGRectIsEmpty(bounds)) {
return bounds;
}
CGFloat scale = MIN(bounds.size.width / contentSize.width, bounds.size.height / contentSize.height);
CGSize fitted = CGSizeMake(contentSize.width * scale, contentSize.height * scale);
CGFloat x = CGRectGetMidX(bounds) - fitted.width * 0.5;
CGFloat y = CGRectGetMidY(bounds) - fitted.height * 0.5;
return CGRectMake(x, y, fitted.width, fitted.height);
}
static NSURLSession *RecentlyReadThumbnailSession(void) {
static NSURLSession *session = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSURLSessionConfiguration *cfg = [NSURLSessionConfiguration defaultSessionConfiguration];
cfg.requestCachePolicy = NSURLRequestReturnCacheDataElseLoad;
cfg.timeoutIntervalForRequest = 15.0;
session = [NSURLSession sessionWithConfiguration:cfg];
});
return session;
}
static UIImage *RecentlyReadPlaceholderImageForAsset(NSString *assetName, CGFloat inset) {
UIImage *base = [UIImage imageNamed:assetName];
if (!base) return nil;
// Match Apollo compact placeholder tone (#76787f) and give
// the glyph extra breathing room inside the compact-small thumbnail.
UIColor *tint = [UIColor colorWithRed:(118.0 / 255.0)
green:(120.0 / 255.0)
blue:(127.0 / 255.0)
alpha:1.0];
UIImage *templated = [base imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
CGSize canvasSize = CGSizeMake(kRecentlyReadThumbnailSmallSize, kRecentlyReadThumbnailSmallSize);
CGRect canvas = (CGRect){CGPointZero, canvasSize};
CGRect paddedBounds = CGRectInset(canvas, inset, inset);
CGRect drawRect = RecentlyReadAspectFitRect(base.size, paddedBounds);
UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:canvasSize];
return [renderer imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull context) {
[tint setFill];
[templated drawInRect:drawRect];
}];
}
static UIImage *RecentlyReadNoThumbnailPlaceholderImage(BOOL isSelfPost) {
static UIImage *selfPostPlaceholder = nil;
static UIImage *linkPlaceholder = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
selfPostPlaceholder = RecentlyReadPlaceholderImageForAsset(@"self-post-indicator", kRecentlyReadThumbnailPlaceholderInset);
linkPlaceholder = RecentlyReadPlaceholderImageForAsset(@"link-button-reddit", 10.0);
// Fallbacks
if (!selfPostPlaceholder) selfPostPlaceholder = linkPlaceholder;
if (!linkPlaceholder) linkPlaceholder = selfPostPlaceholder;
});
return isSelfPost ? selfPostPlaceholder : linkPlaceholder;
}
static UIColor *RecentlyReadMetaColor(void) {
return [UIColor colorWithDynamicProvider:^UIColor *(UITraitCollection *tc) {
UIColor *secondary = [UIColor secondaryLabelColor];
UIColor *primary = [UIColor labelColor];
CGFloat r1, g1, b1, a1, r2, g2, b2, a2;
[secondary getRed:&r1 green:&g1 blue:&b1 alpha:&a1];
[primary getRed:&r2 green:&g2 blue:&b2 alpha:&a2];
CGFloat t = 0.3;
return [UIColor colorWithRed:r1 + (r2 - r1) * t green:g1 + (g2 - g1) * t
blue:b1 + (b2 - b1) * t alpha:a1 + (a2 - a1) * t];
}];
}
static UIImage *RecentlyReadNSFWBadgeImage(CGFloat fontSize) {
NSString *text = @"NSFW";
UIFont *badgeFont = [UIFont systemFontOfSize:fontSize * 0.9 weight:UIFontWeightMedium];
NSDictionary *attrs = @{NSFontAttributeName: badgeFont, NSForegroundColorAttributeName: [UIColor whiteColor]};
CGSize textSize = [text sizeWithAttributes:attrs];
CGFloat hPad = 4.25;
CGFloat vPad = 1.5;
CGFloat badgeHeight = textSize.height + vPad * 2;
CGFloat badgeWidth = textSize.width + hPad * 2;
CGFloat cornerRadius = badgeHeight * 0.325;
CGSize canvasSize = CGSizeMake(badgeWidth, badgeHeight);
UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:canvasSize];
return [renderer imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull context) {
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, badgeWidth, badgeHeight)
cornerRadius:cornerRadius];
// Apollo's native NSFW badge red (#E60000)
[[UIColor colorWithRed:(0xE6 / 255.0) green:0.0 blue:0.0 alpha:1.0] setFill];
[path fill];
[text drawAtPoint:CGPointMake(hPad, vPad) withAttributes:attrs];
}];
}
@implementation RecentlyReadViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"Recently Read";
self.posts = [NSMutableArray array];
self.filteredPosts = [NSMutableArray array];
self.allPostFullNames = @[];
self.nextFetchIndex = 0;
self.hasMorePages = NO;
self.isFetchingPage = NO;
self.hasLoadedOnce = NO;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 86;
self.tableView.backgroundColor = [UIColor systemGroupedBackgroundColor];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.obscuresBackgroundDuringPresentation = NO;
self.searchController.searchBar.placeholder = @"Search Recently Read";
self.navigationItem.searchController = self.searchController;
self.navigationItem.hidesSearchBarWhenScrolling = YES;
self.definesPresentationContext = YES;
UIBarButtonItem *clearItem = [[UIBarButtonItem alloc]
initWithImage:[UIImage systemImageNamed:@"trash"]
style:UIBarButtonItemStylePlain
target:self
action:@selector(_clearAllTapped)];
self.navigationItem.rightBarButtonItem = clearItem;
self.spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleMedium];
self.spinner.hidesWhenStopped = YES;
self.tableView.backgroundView = self.spinner;
self.footerSpinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleMedium];
self.footerSpinner.hidesWhenStopped = YES;
self.tableView.tableFooterView = [UIView new];
self.refreshControl = [[UIRefreshControl alloc] init];
[self.refreshControl addTarget:self
action:@selector(_pullToRefreshTriggered)
forControlEvents:UIControlEventValueChanged];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (!self.hasLoadedOnce) {
self.hasLoadedOnce = YES;
[self refreshPosts];
}
}
- (void)_pullToRefreshTriggered {
if (self.isFetchingPage) {
[self.refreshControl endRefreshing];
return;
}
[self refreshPosts];
}
- (NSArray<NSString *> *)recentReadFullNames {
NSMutableOrderedSet *trackerSet = getTrackerReadPostIDs();
NSArray *postIDs = nil;
if (trackerSet && trackerSet.count > 0) {
postIDs = [trackerSet array];
} else {
postIDs = [[NSUserDefaults standardUserDefaults] stringArrayForKey:@"ReadPostIDs"];
}
if (postIDs.count == 0) return @[];
NSUInteger maxCount = postIDs.count;
if (sReadPostMaxCount > 0) {
maxCount = MIN(postIDs.count, (NSUInteger)sReadPostMaxCount);
}
NSArray *recentIDs = [postIDs subarrayWithRange:NSMakeRange(postIDs.count - maxCount, maxCount)];
NSMutableArray<NSString *> *fullNames = [NSMutableArray arrayWithCapacity:recentIDs.count];
for (NSString *postID in recentIDs) {
if ([postID hasPrefix:@"t3_"]) {
[fullNames addObject:postID];
} else {
[fullNames addObject:[@"t3_" stringByAppendingString:postID]];
}
}
return [[[fullNames reverseObjectEnumerator] allObjects] copy];
}
- (void)setFooterLoading:(BOOL)loading {
if (loading) {
[self.footerSpinner startAnimating];
UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, 44)];
self.footerSpinner.translatesAutoresizingMaskIntoConstraints = NO;
[footer addSubview:self.footerSpinner];
[NSLayoutConstraint activateConstraints:@[
[self.footerSpinner.centerXAnchor constraintEqualToAnchor:footer.centerXAnchor],
[self.footerSpinner.centerYAnchor constraintEqualToAnchor:footer.centerYAnchor],
]];
self.tableView.tableFooterView = footer;
} else {
[self.footerSpinner stopAnimating];
self.tableView.tableFooterView = [UIView new];
}
}
- (void)refreshPosts {
self.posts = [NSMutableArray array];
self.filteredPosts = [NSMutableArray array];
self.allPostFullNames = [self recentReadFullNames];
self.nextFetchIndex = 0;
self.hasMorePages = (self.allPostFullNames.count > 0);
self.isFetchingPage = NO;
[self.spinner startAnimating];
self.tableView.backgroundView = self.spinner;
[self setFooterLoading:NO];
[self.tableView reloadData];
if (!self.hasMorePages) {
[self.spinner stopAnimating];
[self.refreshControl endRefreshing];
[self showEmptyState];
return;
}
[self fetchNextPageIfNeeded];
}
- (void)_clearAllTapped {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Clear Read History"
message:@"This will remove all recently read post entries and unmark all read posts. This cannot be undone."
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]];
[alert addAction:[UIAlertAction actionWithTitle:@"Clear All" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
// Clear the tracker's in-memory set directly
NSMutableOrderedSet *trackerSet = getTrackerReadPostIDs();
if (trackerSet) {
[trackerSet removeAllObjects];
}
// Also clear NSUserDefaults
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"ReadPostIDs"];
if (self.searchController.isActive) {
self.searchController.active = NO;
}
self.posts = [NSMutableArray array];
self.filteredPosts = [NSMutableArray array];
self.allPostFullNames = @[];
self.nextFetchIndex = 0;
self.hasMorePages = NO;
self.isFetchingPage = NO;
[self setFooterLoading:NO];
[self.tableView reloadData];
[self showEmptyState];
}]];
[self presentViewController:alert animated:YES completion:nil];
}
- (void)fetchNextPageIfNeeded {
if (self.isFetchingPage || !self.hasMorePages) return;
if (self.nextFetchIndex >= self.allPostFullNames.count) {
self.hasMorePages = NO;
[self.spinner stopAnimating];
[self setFooterLoading:NO];
[self.refreshControl endRefreshing];
if (self.posts.count == 0) {
[self showEmptyState];
}
return;
}
Class RDKClientClass = objc_getClass("RDKClient");
id client = [RDKClientClass sharedClient];
if (!client) {
ApolloLog(@"[RecentlyRead] RDKClient sharedClient is nil");
[self.spinner stopAnimating];
[self.refreshControl endRefreshing];
if (self.posts.count == 0) {
[self showEmptyState];
}
return;
}
self.isFetchingPage = YES;
BOOL initialPage = (self.posts.count == 0);
if (!initialPage) {
[self setFooterLoading:YES];
}
NSUInteger pageStart = self.nextFetchIndex;
NSUInteger remaining = self.allPostFullNames.count - pageStart;
NSUInteger pageCount = MIN((NSUInteger)kRecentlyReadPageSize, remaining);
NSArray<NSString *> *pageFullNames = [self.allPostFullNames subarrayWithRange:NSMakeRange(pageStart, pageCount)];
[client thingsByFullNames:pageFullNames completion:^(NSArray *things, NSError *fetchError) {
dispatch_async(dispatch_get_main_queue(), ^{
self.isFetchingPage = NO;
[self.spinner stopAnimating];
[self setFooterLoading:NO];
[self.refreshControl endRefreshing];
if (fetchError || !things) {
ApolloLog(@"[RecentlyRead] Fetch error: %@", fetchError);
if (self.posts.count == 0) {
[self showEmptyState];
}
return;
}
self.nextFetchIndex = pageStart + pageCount;
self.hasMorePages = (self.nextFetchIndex < self.allPostFullNames.count);
NSMutableDictionary *thingsByName = [NSMutableDictionary dictionaryWithCapacity:things.count];
for (id thing in things) {
if ([thing isKindOfClass:objc_getClass("RDKLink")]) {
NSString *fn = [(RDKLink *)thing fullName];
if (fn) thingsByName[fn] = thing;
}
}
NSMutableArray *ordered = [NSMutableArray arrayWithCapacity:pageFullNames.count];
for (NSString *fn in pageFullNames) {
id thing = thingsByName[fn];
if (thing) [ordered addObject:thing];
}
[self.posts addObjectsFromArray:ordered];
[self _refilterPosts];
if (self.activePosts.count == 0 && ![self isSearchActive]) {
[self showEmptyState];
} else if (self.activePosts.count > 0) {
self.tableView.backgroundView = nil;
} else {
[self _updateBackgroundForSearch];
}
[self.tableView reloadData];
});
}];
}
- (void)showEmptyState {
[self setFooterLoading:NO];
UILabel *emptyLabel = [[UILabel alloc] init];
emptyLabel.text = @"No recently read posts";
emptyLabel.textAlignment = NSTextAlignmentCenter;
emptyLabel.textColor = [UIColor secondaryLabelColor];
emptyLabel.font = [UIFont systemFontOfSize:17 weight:UIFontWeightRegular];
self.tableView.backgroundView = emptyLabel;
}
- (BOOL)isSearchActive {
return self.searchController.isActive && self.searchController.searchBar.text.length > 0;
}
- (NSArray *)activePosts {
if ([self isSearchActive] || [[NSUserDefaults standardUserDefaults] boolForKey:UDKeyFilterNSFWRecentlyRead]) {
return self.filteredPosts;
}
return self.posts;
}
- (void)_refilterPosts {
BOOL filterNSFW = [[NSUserDefaults standardUserDefaults] boolForKey:UDKeyFilterNSFWRecentlyRead];
NSString *query = self.searchController.searchBar.text;
BOOL searching = [self isSearchActive] && query.length > 0;
if (!searching && !filterNSFW) {
self.filteredPosts = [self.posts mutableCopy];
return;
}
NSString *lower = searching ? query.lowercaseString : nil;
NSMutableArray *filtered = [NSMutableArray array];
for (RDKLink *link in self.posts) {
if (filterNSFW && link.isNSFW) continue;
if (searching &&
!(link.title && [link.title.lowercaseString containsString:lower]) &&
!(link.subreddit && [link.subreddit.lowercaseString containsString:lower]) &&
!(link.author && [link.author.lowercaseString containsString:lower]) &&
!(link.isNSFW && [@"nsfw" containsString:lower])) {
continue;
}
[filtered addObject:link];
}
self.filteredPosts = filtered;
}
- (void)_updateBackgroundForSearch {
if ([self isSearchActive] && self.activePosts.count == 0 && self.posts.count > 0) {
UILabel *noResults = [[UILabel alloc] init];
noResults.text = @"No matching posts";
noResults.textAlignment = NSTextAlignmentCenter;
noResults.textColor = [UIColor secondaryLabelColor];
noResults.font = [UIFont systemFontOfSize:17 weight:UIFontWeightRegular];
self.tableView.backgroundView = noResults;
} else if (self.activePosts.count > 0) {
self.tableView.backgroundView = nil;
}
}
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
[self _refilterPosts];
[self.tableView reloadData];
[self _updateBackgroundForSearch];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.activePosts.count;
}
- (NSString *)timeAgoStringFromDate:(NSDate *)date {
if (!date) return @"";
NSTimeInterval elapsed = -[date timeIntervalSinceNow];
if (elapsed < 60) return @"now";
if (elapsed < 3600) return [NSString stringWithFormat:@"%ldm", (long)(elapsed / 60)];
if (elapsed < 86400) return [NSString stringWithFormat:@"%ldh", (long)(elapsed / 3600)];
if (elapsed < 2592000) return [NSString stringWithFormat:@"%ldd", (long)(elapsed / 86400)];
double months = elapsed / 2592000.0;
if (months < 12) return [NSString stringWithFormat:@"%.0fmo", months];
return [NSString stringWithFormat:@"%.1fy", elapsed / 31556736.0];
}
- (NSString *)compactScoreString:(NSInteger)score {
if (score >= 100000) return [NSString stringWithFormat:@"%.1fK", score / 1000.0];
if (score >= 1000) return [NSString stringWithFormat:@"%.1fK", score / 1000.0];
return [NSString stringWithFormat:@"%ld", (long)score];
}
- (NSAttributedString *)statsAttributedStringForLink:(RDKLink *)link {
NSMutableAttributedString *result = [[NSMutableAttributedString alloc] init];
UIColor *metaColor = RecentlyReadMetaColor();
UIFont *metaFont = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
NSDictionary *textAttrs = @{NSFontAttributeName: metaFont, NSForegroundColorAttributeName: metaColor};
CGFloat iconSize = 11.0;
CGFloat baselineOffset = -1.5;
UIImageSymbolConfiguration *config = [UIImageSymbolConfiguration configurationWithPointSize:iconSize weight:UIImageSymbolWeightMedium];
// Upvote arrow
UIImage *upIcon = [[UIImage systemImageNamed:@"arrow.up" withConfiguration:config]
imageWithTintColor:metaColor renderingMode:UIImageRenderingModeAlwaysOriginal];
NSTextAttachment *upAtt = [[NSTextAttachment alloc] init];
upAtt.image = upIcon;
upAtt.bounds = CGRectMake(0, baselineOffset, iconSize, iconSize);
[result appendAttributedString:[NSAttributedString attributedStringWithAttachment:upAtt]];
[result appendAttributedString:[[NSAttributedString alloc] initWithString:
[NSString stringWithFormat:@"\u00A0%@\u00A0\u00A0", [self compactScoreString:link.score]]
attributes:textAttrs]];
// Comment bubble
UIImage *commentIcon = [[UIImage systemImageNamed:@"bubble.right" withConfiguration:config]
imageWithTintColor:metaColor renderingMode:UIImageRenderingModeAlwaysOriginal];
NSTextAttachment *commentAtt = [[NSTextAttachment alloc] init];
commentAtt.image = commentIcon;
commentAtt.bounds = CGRectMake(0, baselineOffset, iconSize + 1, iconSize);
[result appendAttributedString:[NSAttributedString attributedStringWithAttachment:commentAtt]];
NSString *commentsStr = [(id)link respondsToSelector:@selector(totalComments)]
? [self compactScoreString:link.totalComments] : @"0";
[result appendAttributedString:[[NSAttributedString alloc] initWithString:
[NSString stringWithFormat:@"\u00A0%@\u00A0\u00A0", commentsStr]
attributes:textAttrs]];
// Clock (mirrored so hand points to 3:00)
UIImage *clockIconBase = [UIImage systemImageNamed:@"clock" withConfiguration:config];
UIImage *clockFlipped = [UIImage imageWithCGImage:clockIconBase.CGImage
scale:clockIconBase.scale orientation:UIImageOrientationUpMirrored];
UIImage *clockIcon = [clockFlipped imageWithTintColor:metaColor renderingMode:UIImageRenderingModeAlwaysOriginal];
NSTextAttachment *clockAtt = [[NSTextAttachment alloc] init];
clockAtt.image = clockIcon;
clockAtt.bounds = CGRectMake(0, baselineOffset, iconSize, iconSize);
[result appendAttributedString:[NSAttributedString attributedStringWithAttachment:clockAtt]];
[result appendAttributedString:[[NSAttributedString alloc] initWithString:
[NSString stringWithFormat:@"\u00A0%@", [self timeAgoStringFromDate:link.createdUTC]]
attributes:textAttrs]];
return result;
}
- (void)_navigateToAssociatedPath:(UIButton *)sender {
NSString *path = objc_getAssociatedObject(sender, &kNavPathKey);
if (!path.length) return;
NSString *urlStr = [NSString stringWithFormat:@"https://reddit.com%@", path];
NSURL *url = [NSURL URLWithString:urlStr];
if (url) ApolloRouteResolvedURLViaApolloScheme(url);
}
- (NSURL *)thumbnailURLForLink:(RDKLink *)link {
SEL thumbSel = NSSelectorFromString(@"thumbnailURL");
if (![(id)link respondsToSelector:thumbSel]) return nil;
NSURL *url = ((id (*)(id, SEL))objc_msgSend)(link, thumbSel);
if (!url) return nil;
NSString *scheme = url.scheme.lowercaseString;
if (![scheme isEqualToString:@"http"] && ![scheme isEqualToString:@"https"]) {
return nil;
}
return url;
}
- (void)configureThumbnailImageView:(UIImageView *)thumbnailView forLink:(RDKLink *)link {
NSURLSessionDataTask *oldTask = objc_getAssociatedObject(thumbnailView, &kThumbTaskKey);
if (oldTask) {
[oldTask cancel];
objc_setAssociatedObject(thumbnailView, &kThumbTaskKey, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
NSURL *thumbURL = [self thumbnailURLForLink:link];
if (!thumbURL) {
thumbnailView.contentMode = UIViewContentModeCenter;
thumbnailView.image = RecentlyReadNoThumbnailPlaceholderImage(link.isSelfPost);
objc_setAssociatedObject(thumbnailView, &kThumbURLKey, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
return;
}
NSString *urlString = thumbURL.absoluteString ?: @"";
objc_setAssociatedObject(thumbnailView, &kThumbURLKey, urlString, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
NSCache<NSString *, UIImage *> *cache = RecentlyReadThumbnailCache();
UIImage *cached = [cache objectForKey:urlString];
if (cached) {
thumbnailView.contentMode = UIViewContentModeScaleAspectFill;
thumbnailView.image = cached;
return;
}
thumbnailView.contentMode = UIViewContentModeScaleAspectFill;
thumbnailView.image = RecentlyReadNoThumbnailPlaceholderImage(NO);
__weak UIImageView *weakThumb = thumbnailView;
NSURLSessionDataTask *task = [RecentlyReadThumbnailSession() dataTaskWithURL:thumbURL
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error || data.length == 0) {
dispatch_async(dispatch_get_main_queue(), ^{
UIImageView *strongThumb = weakThumb;
if (!strongThumb) return;
NSString *current = objc_getAssociatedObject(strongThumb, &kThumbURLKey);
if ([current isEqualToString:urlString]) {
strongThumb.image = RecentlyReadNoThumbnailPlaceholderImage(NO);
}
objc_setAssociatedObject(strongThumb, &kThumbTaskKey, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
});
return;
}
UIImage *image = [UIImage imageWithData:data];
if (!image) {
dispatch_async(dispatch_get_main_queue(), ^{
UIImageView *strongThumb = weakThumb;
if (!strongThumb) return;
NSString *current = objc_getAssociatedObject(strongThumb, &kThumbURLKey);
if ([current isEqualToString:urlString]) {
strongThumb.image = RecentlyReadNoThumbnailPlaceholderImage(NO);
}
objc_setAssociatedObject(strongThumb, &kThumbTaskKey, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
});
return;
}
[cache setObject:image forKey:urlString];
dispatch_async(dispatch_get_main_queue(), ^{
UIImageView *strongThumb = weakThumb;
if (!strongThumb) return;
NSString *current = objc_getAssociatedObject(strongThumb, &kThumbURLKey);
if ([current isEqualToString:urlString]) {
strongThumb.image = image;
}
objc_setAssociatedObject(strongThumb, &kThumbTaskKey, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
});
}];
objc_setAssociatedObject(thumbnailView, &kThumbTaskKey, task, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[task resume];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellID = @"RecentPostCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
static const NSInteger kStackTag = 200;
static const NSInteger kSubHeaderTag = 201;
static const NSInteger kTitleTag = 202;
static const NSInteger kSubFooterTag = 203;
static const NSInteger kBottomTag = 204;
static const NSInteger kSepTag = 205;
static const NSInteger kSubFooterSubredditTag = 207;
static const NSInteger kSubFooterByTag = 208;
static const NSInteger kSubFooterAuthorTag = 209;
static const NSInteger kAuthorTopTag = 210;
static const NSInteger kThumbTag = 211;
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
cell.selectionStyle = UITableViewCellSelectionStyleDefault;
cell.backgroundColor = [UIColor secondarySystemGroupedBackgroundColor];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
UIView *selectedBg = [[UIView alloc] init];
selectedBg.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.15];
cell.selectedBackgroundView = selectedBg;
UIColor *metaColor = RecentlyReadMetaColor();
UIColor *metaHighlight = [metaColor colorWithAlphaComponent:0.4];
UIFont *mediumFont = [UIFont systemFontOfSize:13 weight:UIFontWeightMedium];
CGFloat metaLineHeight = ceil(mediumFont.lineHeight);
// Subreddit header button (shown above title when SubredditAtTop)
UIButton *subHeaderBtn = [UIButton buttonWithType:UIButtonTypeCustom];
subHeaderBtn.tag = kSubHeaderTag;
subHeaderBtn.titleLabel.font = mediumFont;
[subHeaderBtn setTitleColor:metaColor forState:UIControlStateNormal];
[subHeaderBtn setTitleColor:metaHighlight forState:UIControlStateHighlighted];
subHeaderBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeading;
subHeaderBtn.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
[subHeaderBtn.heightAnchor constraintEqualToConstant:metaLineHeight].active = YES;
[subHeaderBtn addTarget:self action:@selector(_navigateToAssociatedPath:) forControlEvents:UIControlEventTouchUpInside];
// Title
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.tag = kTitleTag;
titleLabel.numberOfLines = 3;
titleLabel.font = [UIFont systemFontOfSize:15 weight:UIFontWeightRegular];
titleLabel.textColor = [UIColor labelColor];
// Footer stack (subreddit + by + author, shown below title when !SubredditAtTop)
UIButton *subredditFooterBtn = [UIButton buttonWithType:UIButtonTypeCustom];
subredditFooterBtn.tag = kSubFooterSubredditTag;
subredditFooterBtn.titleLabel.font = mediumFont;
[subredditFooterBtn setTitleColor:metaColor forState:UIControlStateNormal];
[subredditFooterBtn setTitleColor:metaHighlight forState:UIControlStateHighlighted];
subredditFooterBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeading;
[subredditFooterBtn.heightAnchor constraintEqualToConstant:metaLineHeight].active = YES;
[subredditFooterBtn addTarget:self action:@selector(_navigateToAssociatedPath:) forControlEvents:UIControlEventTouchUpInside];
UILabel *byLabel = [[UILabel alloc] init];
byLabel.tag = kSubFooterByTag;
byLabel.text = @" by ";
byLabel.font = [UIFont systemFontOfSize:13 weight:UIFontWeightRegular];
byLabel.textColor = metaColor;
[byLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
UIButton *authorFooterBtn = [UIButton buttonWithType:UIButtonTypeCustom];
authorFooterBtn.tag = kSubFooterAuthorTag;
authorFooterBtn.titleLabel.font = mediumFont;
[authorFooterBtn setTitleColor:metaColor forState:UIControlStateNormal];
[authorFooterBtn setTitleColor:metaHighlight forState:UIControlStateHighlighted];
authorFooterBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeading;
[authorFooterBtn.heightAnchor constraintEqualToConstant:metaLineHeight].active = YES;
[authorFooterBtn addTarget:self action:@selector(_navigateToAssociatedPath:) forControlEvents:UIControlEventTouchUpInside];
UIStackView *footerStack = [[UIStackView alloc] initWithArrangedSubviews:@[subredditFooterBtn, byLabel, authorFooterBtn]];
footerStack.tag = kSubFooterTag;
footerStack.axis = UILayoutConstraintAxisHorizontal;
footerStack.spacing = 0;
footerStack.alignment = UIStackViewAlignmentCenter;
// Author button (shown between title and stats when SubredditAtTop + Usernames)
UIButton *authorTopBtn = [UIButton buttonWithType:UIButtonTypeCustom];
authorTopBtn.tag = kAuthorTopTag;
authorTopBtn.titleLabel.font = mediumFont;
[authorTopBtn setTitleColor:metaColor forState:UIControlStateNormal];
[authorTopBtn setTitleColor:metaHighlight forState:UIControlStateHighlighted];
authorTopBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeading;
authorTopBtn.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
[authorTopBtn.heightAnchor constraintEqualToConstant:metaLineHeight].active = YES;
[authorTopBtn addTarget:self action:@selector(_navigateToAssociatedPath:) forControlEvents:UIControlEventTouchUpInside];
// Bottom line: stats
UILabel *statsLabel = [[UILabel alloc] init];
statsLabel.tag = kBottomTag;
statsLabel.numberOfLines = 1;
UIImageView *thumbnailView = [[UIImageView alloc] init];
thumbnailView.tag = kThumbTag;
thumbnailView.contentMode = UIViewContentModeScaleAspectFill;
thumbnailView.clipsToBounds = YES;
thumbnailView.layer.cornerRadius = 6.0;
thumbnailView.backgroundColor = [UIColor tertiarySystemFillColor];
thumbnailView.translatesAutoresizingMaskIntoConstraints = NO;
[cell.contentView addSubview:thumbnailView];
UIStackView *stack = [[UIStackView alloc] initWithArrangedSubviews:@[
subHeaderBtn, titleLabel, footerStack, authorTopBtn, statsLabel
]];
stack.tag = kStackTag;
stack.axis = UILayoutConstraintAxisVertical;
stack.spacing = 3;
stack.alignment = UIStackViewAlignmentLeading;
stack.translatesAutoresizingMaskIntoConstraints = NO;
[stack setCustomSpacing:kRecentlyReadDefaultTopGap afterView:subHeaderBtn];
[stack setCustomSpacing:kRecentlyReadDefaultTopGap afterView:titleLabel];
[stack setCustomSpacing:0 afterView:footerStack];
[stack setCustomSpacing:0 afterView:authorTopBtn];
[cell.contentView addSubview:stack];
UIView *sep = [[UIView alloc] init];
sep.tag = kSepTag;
sep.backgroundColor = [UIColor separatorColor];
sep.translatesAutoresizingMaskIntoConstraints = NO;
[cell.contentView addSubview:sep];
NSLayoutConstraint *thumbWidth = [thumbnailView.widthAnchor constraintEqualToConstant:kRecentlyReadThumbnailSmallSize];
NSLayoutConstraint *stackLeadingWithThumb = [stack.leadingAnchor constraintEqualToAnchor:thumbnailView.trailingAnchor constant:12];
NSLayoutConstraint *stackLeadingNoThumb = [stack.leadingAnchor constraintEqualToAnchor:cell.contentView.leadingAnchor constant:12];
stackLeadingNoThumb.active = YES;
[NSLayoutConstraint activateConstraints:@[
[thumbnailView.leadingAnchor constraintEqualToAnchor:cell.contentView.leadingAnchor constant:12],
[thumbnailView.topAnchor constraintEqualToAnchor:cell.contentView.topAnchor constant:kRecentlyReadCellVerticalInset],
[thumbnailView.heightAnchor constraintEqualToConstant:kRecentlyReadThumbnailSmallSize],
thumbWidth,
[thumbnailView.bottomAnchor constraintLessThanOrEqualToAnchor:cell.contentView.bottomAnchor constant:-kRecentlyReadCellVerticalInset],
[stack.topAnchor constraintEqualToAnchor:cell.contentView.topAnchor constant:kRecentlyReadCellVerticalInset],
[stack.trailingAnchor constraintEqualToAnchor:cell.contentView.trailingAnchor constant:-8],
[stack.bottomAnchor constraintEqualToAnchor:cell.contentView.bottomAnchor constant:-kRecentlyReadCellVerticalInset],
[sep.leadingAnchor constraintEqualToAnchor:cell.contentView.leadingAnchor constant:16],
[sep.trailingAnchor constraintEqualToAnchor:cell.contentView.trailingAnchor],
[sep.bottomAnchor constraintEqualToAnchor:cell.contentView.bottomAnchor],
[sep.heightAnchor constraintEqualToConstant:1.0 / [UIScreen mainScreen].scale],
]];
objc_setAssociatedObject(cell, &kThumbWidthConstraintKey, thumbWidth, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
objc_setAssociatedObject(cell, &kStackLeadingWithThumbConstraintKey, stackLeadingWithThumb, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
objc_setAssociatedObject(cell, &kStackLeadingNoThumbConstraintKey, stackLeadingNoThumb, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
RDKLink *link = self.activePosts[indexPath.row];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL subAtTop = [defaults boolForKey:@"ShowSubredditAtTop"];
BOOL showUsernames = [defaults boolForKey:@"AlwaysShowUsernames"];
UIStackView *stack = (UIStackView *)[cell.contentView viewWithTag:kStackTag];
UIButton *subHeaderBtn = (UIButton *)[cell.contentView viewWithTag:kSubHeaderTag];
UILabel *titleLabel = [cell.contentView viewWithTag:kTitleTag];
UIStackView *footerStack = (UIStackView *)[cell.contentView viewWithTag:kSubFooterTag];
UIButton *subredditFooterBtn = (UIButton *)[cell.contentView viewWithTag:kSubFooterSubredditTag];
UILabel *byLabel = [cell.contentView viewWithTag:kSubFooterByTag];
UIButton *authorFooterBtn = (UIButton *)[cell.contentView viewWithTag:kSubFooterAuthorTag];
UIButton *authorTopBtn = (UIButton *)[cell.contentView viewWithTag:kAuthorTopTag];
UILabel *statsLabel = [cell.contentView viewWithTag:kBottomTag];
UIImageView *thumbnailView = (UIImageView *)[cell.contentView viewWithTag:kThumbTag];
NSLayoutConstraint *thumbWidth = objc_getAssociatedObject(cell, &kThumbWidthConstraintKey);
NSLayoutConstraint *stackLeadingWithThumb = objc_getAssociatedObject(cell, &kStackLeadingWithThumbConstraintKey);
NSLayoutConstraint *stackLeadingNoThumb = objc_getAssociatedObject(cell, &kStackLeadingNoThumbConstraintKey);
BOOL showThumbnails = sShowRecentlyReadThumbnails;
if (showThumbnails) {
thumbnailView.hidden = NO;
thumbWidth.constant = kRecentlyReadThumbnailSmallSize;
stackLeadingNoThumb.active = NO;
stackLeadingWithThumb.active = YES;
[self configureThumbnailImageView:thumbnailView forLink:link];
} else {
NSURLSessionDataTask *task = objc_getAssociatedObject(thumbnailView, &kThumbTaskKey);
if (task) {
[task cancel];
objc_setAssociatedObject(thumbnailView, &kThumbTaskKey, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
objc_setAssociatedObject(thumbnailView, &kThumbURLKey, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
thumbnailView.image = nil;
thumbnailView.hidden = YES;
thumbWidth.constant = 0;
stackLeadingWithThumb.active = NO;
stackLeadingNoThumb.active = YES;
}
// Title with optional NSFW badge
NSString *titleText = link.title ?: @"(untitled)";
UIFont *titleFont = titleLabel.font;
NSMutableParagraphStyle *titlePara = [[NSMutableParagraphStyle alloc] init];
titlePara.lineSpacing = 1.5;
NSDictionary *titleAttrs = @{NSFontAttributeName: titleFont,
NSForegroundColorAttributeName: [UIColor labelColor],
NSParagraphStyleAttributeName: titlePara};
if (link.isNSFW) {
NSMutableAttributedString *titleAttr = [[NSMutableAttributedString alloc] initWithString:titleText
attributes:titleAttrs];
// Space before badge
[titleAttr appendAttributedString:[[NSAttributedString alloc] initWithString:@" "]];
// NSFW badge as inline image
UIImage *badge = RecentlyReadNSFWBadgeImage(titleFont.pointSize);
NSTextAttachment *att = [[NSTextAttachment alloc] init];
att.image = badge;
// Vertically center the badge with the text midline
CGFloat fontMid = (titleFont.ascender + titleFont.descender) / 2.0;
CGFloat yOffset = fontMid - badge.size.height / 2.0;
att.bounds = CGRectMake(0, yOffset, badge.size.width, badge.size.height);
[titleAttr appendAttributedString:[NSAttributedString attributedStringWithAttachment:att]];
titleLabel.attributedText = titleAttr;
} else {
titleLabel.attributedText = [[NSAttributedString alloc] initWithString:titleText attributes:titleAttrs];
}
NSString *subPath = link.subreddit.length > 0 ? [NSString stringWithFormat:@"/r/%@", link.subreddit] : nil;
NSString *authorPath = link.author.length > 0 ? [NSString stringWithFormat:@"/u/%@", link.author] : nil;
if (subAtTop) {
[stack setCustomSpacing:kRecentlyReadExpandedTopGap afterView:subHeaderBtn];
[stack setCustomSpacing:kRecentlyReadExpandedTopGap afterView:titleLabel];
// Subreddit above title
subHeaderBtn.hidden = NO;
subHeaderBtn.titleLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
[subHeaderBtn setTitle:link.subreddit ?: @"" forState:UIControlStateNormal];
objc_setAssociatedObject(subHeaderBtn, &kNavPathKey, subPath, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
footerStack.hidden = YES;
if (showUsernames && link.author.length > 0) {
authorTopBtn.hidden = NO;
[authorTopBtn setTitle:link.author forState:UIControlStateNormal];
objc_setAssociatedObject(authorTopBtn, &kNavPathKey, authorPath, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
} else {
authorTopBtn.hidden = YES;
}
} else {
[stack setCustomSpacing:kRecentlyReadDefaultTopGap afterView:subHeaderBtn];
[stack setCustomSpacing:kRecentlyReadDefaultTopGap afterView:titleLabel];
// Subreddit below title with optional author
subHeaderBtn.hidden = YES;
authorTopBtn.hidden = YES;
footerStack.hidden = NO;
[subredditFooterBtn setTitle:link.subreddit ?: @"" forState:UIControlStateNormal];
objc_setAssociatedObject(subredditFooterBtn, &kNavPathKey, subPath, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
if (showUsernames && link.author.length > 0) {
byLabel.hidden = NO;
authorFooterBtn.hidden = NO;
[authorFooterBtn setTitle:link.author forState:UIControlStateNormal];
objc_setAssociatedObject(authorFooterBtn, &kNavPathKey, authorPath, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
} else {
byLabel.hidden = YES;
authorFooterBtn.hidden = YES;
}
}
statsLabel.attributedText = [self statsAttributedStringForLink:link];
return cell;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (!self.hasMorePages || self.isFetchingPage || self.activePosts.count == 0) return;
CGFloat contentHeight = scrollView.contentSize.height;
if (contentHeight <= 0) return;
CGFloat triggerOffset = MAX(0.0, contentHeight * 0.65 - scrollView.bounds.size.height);
if (scrollView.contentOffset.y >= triggerOffset) {
[self fetchNextPageIfNeeded];
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
RDKLink *link = self.activePosts[indexPath.row];
NSString *permalink = link.permalink;
if (!permalink) return;
// Route through apollo:// scheme to open natively in-app
NSString *urlString = [NSString stringWithFormat:@"https://reddit.com%@", permalink];
NSURL *url = [NSURL URLWithString:urlString];
if (!url) return;
ApolloRouteResolvedURLViaApolloScheme(url);
}
- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row >= (NSInteger)self.activePosts.count) return nil;
UIContextualAction *deleteAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive
title:@""
handler:^(UIContextualAction *action, UIView *sourceView, void (^completionHandler)(BOOL)) {
[self _deletePostAtIndexPath:indexPath];
completionHandler(YES);
}];
deleteAction.image = [UIImage systemImageNamed:@"trash.fill"];
deleteAction.backgroundColor = [UIColor systemRedColor];
UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[deleteAction]];
config.performsFirstActionWithFullSwipe = YES;
return config;
}
- (void)_deletePostAtIndexPath:(NSIndexPath *)indexPath {
NSArray *active = self.activePosts;
if (indexPath.row >= (NSInteger)active.count) return;
RDKLink *link = active[indexPath.row];
NSString *fullName = link.fullName; // e.g. "t3_abc123"
NSString *bareID = [fullName hasPrefix:@"t3_"] ? [fullName substringFromIndex:3] : fullName;
// Remove from tracker's in-memory ordered set (stores bare IDs)
NSMutableOrderedSet *trackerSet = getTrackerReadPostIDs();
if (trackerSet) {
[trackerSet removeObject:fullName];
[trackerSet removeObject:bareID];
}
// Remove from NSUserDefaults fallback