forked from Awful/Awful.app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAwfulThreadListController.m
453 lines (416 loc) · 18 KB
/
AwfulThreadListController.m
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
//
// AwfulThreadListController.m
// Awful
//
// Created by Sean Berry on 7/27/10.
// Copyright 2010 Regular Berry Software LLC. All rights reserved.
//
#import "AwfulThreadListController.h"
#import "AwfulFetchedTableViewControllerSubclass.h"
#import "AwfulActionSheet.h"
#import "AwfulAlertView.h"
#import "AwfulDataStack.h"
#import "AwfulDisclosureIndicatorView.h"
#import "AwfulHTTPClient.h"
#import "AwfulLoginController.h"
#import "AwfulModels.h"
#import "AwfulPostsViewController.h"
#import "AwfulProfileViewController.h"
#import "AwfulSettings.h"
#import "AwfulSplitViewController.h"
#import "AwfulTabBarController.h"
#import "AwfulTheme.h"
#import "AwfulThreadCell.h"
#import "AwfulThreadTags.h"
#import "NSString+CollapseWhitespace.h"
#import "SVProgressHUD.h"
#import "SVPullToRefresh.h"
#import "UIViewController+NavigationEnclosure.h"
@interface AwfulThreadListController ()
@property (nonatomic) NSMutableSet *cellsMissingThreadTags;
@end
@implementation AwfulThreadListController
- (id)init
{
self = [super init];
if (!(self = [super init])) return nil;
_cellsMissingThreadTags = [NSMutableSet new];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(settingsChanged:)
name:AwfulSettingsDidChangeNotification
object:nil];
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (NSFetchedResultsController *)createFetchedResultsController
{
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:[AwfulThread entityName]];
request.predicate = [NSPredicate predicateWithFormat:@"hideFromList == NO AND forum == %@",
self.forum];
request.sortDescriptors = @[
[NSSortDescriptor sortDescriptorWithKey:@"stickyIndex" ascending:YES],
[NSSortDescriptor sortDescriptorWithKey:@"lastPostDate" ascending:NO]
];
return [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:[AwfulDataStack sharedDataStack].context
sectionNameKeyPath:nil
cacheName:nil];
}
- (void)setForum:(AwfulForum *)forum
{
if (_forum == forum) return;
_forum = forum;
self.title = _forum.name;
self.navigationItem.backBarButtonItem = [self abbreviatedBackBarButtonItem];
self.fetchedResultsController = [self createFetchedResultsController];
}
- (UIBarButtonItem *)abbreviatedBackBarButtonItem
{
NSURL *abbreviationsURL = [[NSBundle mainBundle] URLForResource:@"Forum Abbreviations"
withExtension:@"plist"];
NSDictionary *abbreviations = [NSDictionary dictionaryWithContentsOfURL:abbreviationsURL];
NSString *text = abbreviations[self.forum.forumID];
if (!text) return nil;
return [[UIBarButtonItem alloc] initWithTitle:text style:UIBarButtonItemStyleBordered
target:nil
action:NULL];
}
- (void)settingsChanged:(NSNotification *)note
{
if (![self isViewLoaded]) return;
NSArray *keys = note.userInfo[AwfulSettingsDidChangeSettingsKey];
if ([keys containsObject:AwfulSettingsKeys.showThreadTags]) {
[self.tableView reloadData];
}
}
#pragma mark - Table view controller
- (void)refresh
{
[super refresh];
[self.cellsMissingThreadTags removeAllObjects];
[self loadPageNum:1];
CGFloat refreshViewHeight = self.tableView.pullToRefreshView.bounds.size.height;
[self.tableView setContentOffset:CGPointMake(0, -refreshViewHeight)];
}
- (BOOL)canPullForNextPage
{
return YES;
}
- (BOOL)refreshOnAppear
{
if (![AwfulHTTPClient client].reachable) return NO;
if (!self.forum.lastRefresh) return YES;
if ([self.fetchedResultsController.fetchedObjects count] == 0) return YES;
return [[NSDate date] timeIntervalSinceDate:self.forum.lastRefresh] > 60 * 15;
}
- (void)nextPage
{
[super nextPage];
[self loadPageNum:self.currentPage + 1];
}
- (void)retheme
{
[super retheme];
self.view.backgroundColor = [AwfulTheme currentTheme].threadListBackgroundColor;
self.tableView.separatorColor = [AwfulTheme currentTheme].threadListSeparatorColor;
}
- (void)loadPageNum:(NSUInteger)pageNum
{
[self.networkOperation cancel];
__block id op;
op = [[AwfulHTTPClient client] listThreadsInForumWithID:self.forum.forumID
onPage:pageNum
andThen:^(NSError *error, NSArray *threads)
{
if (![self.networkOperation isEqual:op]) return;
if (error) {
[AwfulAlertView showWithTitle:@"Network Error" error:error buttonTitle:@"OK"];
} else {
if (pageNum == 1) {
[self.forum.threads setValue:@YES forKey:@"hideFromList"];
}
[threads setValue:@NO forKey:@"hideFromList"];
self.forum.lastRefresh = [NSDate date];
self.ignoreUpdates = YES;
[[AwfulDataStack sharedDataStack] save];
self.ignoreUpdates = NO;
self.currentPage = pageNum;
}
self.refreshing = NO;
}];
self.networkOperation = op;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.currentPage = 1;
self.tableView.rowHeight = 75;
}
- (void)showThreadActionsForThread:(AwfulThread *)thread
{
AwfulActionSheet *sheet = [AwfulActionSheet new];
sheet.title = [thread.title stringByCollapsingWhitespace];
[sheet addButtonWithTitle:@"Jump to First Page" block:^{
AwfulPostsViewController *page = [AwfulPostsViewController new];
page.thread = thread;
[self displayPage:page];
[page loadPage:1];
}];
[sheet addButtonWithTitle:@"Jump to Last Page" block:^{
AwfulPostsViewController *page = [AwfulPostsViewController new];
page.thread = thread;
[self displayPage:page];
[page loadPage:AwfulThreadPageLast];
}];
if (thread.beenSeen) {
[sheet addButtonWithTitle:@"Mark as Unread" block:^{
[self markThreadUnseen:thread];
}];
}
NSString *bookmarkTitle = [NSString stringWithFormat:@"%@ Thread",
thread.isBookmarkedValue ? @"Unbookmark" : @"Bookmark"];
[sheet addButtonWithTitle:bookmarkTitle block:^{
[[AwfulHTTPClient client] setThreadWithID:thread.threadID
isBookmarked:!thread.isBookmarkedValue
andThen:^(NSError *error)
{
if (error) {
[AwfulAlertView showWithTitle:@"Network Error" error:error buttonTitle:@"OK"];
} else {
NSString *status = thread.isBookmarkedValue ? @"Bookmarked" : @"Unbookmarked";
[SVProgressHUD showSuccessWithStatus:status];
}
}];
}];
[sheet addButtonWithTitle:@"View OP's Profile" block:^{
AwfulProfileViewController *profile = [AwfulProfileViewController new];
profile.hidesBottomBarWhenPushed = YES;
profile.userID = thread.author.userID;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self action:@selector(doneWithProfile)];
profile.navigationItem.leftBarButtonItem = done;
UINavigationController *nav = [profile enclosingNavigationController];
nav.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:nav animated:YES completion:nil];
} else {
[self.navigationController pushViewController:profile animated:YES];
}
}];
[sheet addCancelButtonWithTitle:@"Cancel"];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
[sheet showFromRect:self.awfulTabBarController.tabBar.frame
inView:self.awfulTabBarController.view
animated:YES];
} else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
NSUInteger index = [self.fetchedResultsController.fetchedObjects indexOfObject:thread];
if (index != NSNotFound) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
[sheet showFromRect:cell.frame inView:self.tableView animated:YES];
}
}
}
- (void)doneWithProfile
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)markThreadUnseen:(AwfulThread *)thread
{
[[AwfulHTTPClient client] forgetReadPostsInThreadWithID:thread.threadID
andThen:^(NSError *error)
{
if (error) {
[AwfulAlertView showWithTitle:@"Network Error" error:error buttonTitle:@"OK"];
} else {
thread.seenPostsValue = 0;
[[AwfulDataStack sharedDataStack] save];
}
}];
}
- (void)displayPage:(AwfulPostsViewController *)page
{
AwfulSplitViewController *split = (AwfulSplitViewController *)self.splitViewController;
if (!split) {
[self.navigationController pushViewController:page animated:YES];
} else {
UINavigationController *nav = (UINavigationController *)split.viewControllers[1];
nav.viewControllers = @[ page ];
[split.masterPopoverController dismissPopoverAnimated:YES];
}
}
- (void)pop
{
[self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - UITableViewDataSource and UITableViewDelegate
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * const Identifier = @"AwfulThreadCell";
AwfulThreadCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
if (!cell) {
cell = [[AwfulThreadCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:Identifier];
cell.stickyImageViewOffset = CGSizeMake(1, 1);
UILongPressGestureRecognizer *longPress = [UILongPressGestureRecognizer new];
[longPress addTarget:self action:@selector(showThreadActions:)];
[cell addGestureRecognizer:longPress];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
cell.accessoryView = [AwfulDisclosureIndicatorView new];
}
}
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (void)configureCell:(UITableViewCell *)genericCell atIndexPath:(NSIndexPath *)indexPath
{
AwfulThreadCell *cell = (AwfulThreadCell *)genericCell;
AwfulThread *thread = [self.fetchedResultsController objectAtIndexPath:indexPath];
if ([AwfulSettings settings].showThreadTags) {
cell.imageView.hidden = NO;
cell.imageView.image = [[AwfulThreadTags sharedThreadTags]
threadTagNamed:thread.firstIconName];
if (!cell.imageView.image && thread.firstIconName) {
[self updateThreadTagsForCellAtIndexPath:indexPath];
}
cell.secondaryTagImageView.hidden = NO;
cell.secondaryTagImageView.image = [[AwfulThreadTags sharedThreadTags]
threadTagNamed:thread.secondIconName];
if (thread.secondIconName && !cell.secondaryTagImageView.image) {
[self updateThreadTagsForCellAtIndexPath:indexPath];
}
if (thread.isStickyValue) {
cell.stickyImageView.hidden = NO;
cell.stickyImageView.image = [UIImage imageNamed:@"sticky.png"];
} else {
cell.stickyImageView.hidden = YES;
}
// Hardcode Film Dump to never show ratings; its thread tags are the ratings.
if ([thread.forum.forumID isEqualToString:@"133"]) {
cell.rating = 0;
} else {
cell.rating = [thread.threadRating floatValue];
}
} else {
cell.imageView.image = nil;
cell.imageView.hidden = YES;
cell.secondaryTagImageView.image = nil;
cell.secondaryTagImageView.hidden = YES;
cell.stickyImageView.hidden = YES;
cell.closed = thread.isClosedValue;
cell.rating = 0;
}
cell.textLabel.text = [thread.title stringByCollapsingWhitespace];
if (thread.isStickyValue || !thread.isClosedValue) {
cell.imageView.alpha = 1;
cell.ratingImageView.alpha = 1;
cell.textLabel.textColor = [AwfulTheme currentTheme].threadCellTextColor;
} else {
cell.imageView.alpha = 0.5;
cell.ratingImageView.alpha = 0.5;
cell.textLabel.textColor = [AwfulTheme currentTheme].threadCellClosedThreadColor;
}
NSNumberFormatterStyle numberStyle = NSNumberFormatterDecimalStyle;
NSString *pagesFormatted = [NSNumberFormatter localizedStringFromNumber:thread.numberOfPages
numberStyle:numberStyle];
NSString *plural = thread.numberOfPagesValue == 1 ? @"" : @"s";
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ page%@", pagesFormatted, plural];
cell.detailTextLabel.textColor = [AwfulTheme currentTheme].threadCellPagesTextColor;
if (thread.beenSeen) {
cell.originalPosterTextLabel.text = [NSString stringWithFormat:@"Killed by %@",
thread.lastPostAuthorName];
} else {
cell.originalPosterTextLabel.text = [NSString stringWithFormat:@"Posted by %@",
thread.author.username];
}
AwfulTheme *theme = [AwfulTheme currentTheme];
cell.originalPosterTextLabel.textColor = theme.threadCellOriginalPosterTextColor;
if (thread.starCategoryValue == AwfulStarCategoryRed) {
cell.unreadCountBadgeView.badgeColor = theme.threadListUnreadBadgeRedColor;
cell.unreadCountBadgeView.offBadgeColor = theme.threadListUnreadBadgeRedOffColor;
} else if (thread.starCategoryValue == AwfulStarCategoryYellow) {
cell.unreadCountBadgeView.badgeColor = theme.threadListUnreadBadgeYellowColor;
cell.unreadCountBadgeView.offBadgeColor = theme.threadListUnreadBadgeYellowOffColor;
} else {
cell.unreadCountBadgeView.badgeColor = theme.threadListUnreadBadgeBlueColor;
cell.unreadCountBadgeView.offBadgeColor = theme.threadListUnreadBadgeBlueOffColor;
}
cell.unreadCountBadgeView.highlightedBadgeColor = theme.threadListUnreadBadgeHighlightedColor;
NSInteger unreadPosts = thread.totalRepliesValue + 1 - thread.seenPostsValue;
cell.unreadCountBadgeView.badgeText = [@(unreadPosts) stringValue];
cell.unreadCountBadgeView.on = unreadPosts > 0;
cell.showsUnread = thread.seenPostsValue > 0;
cell.backgroundColor = theme.threadCellBackgroundColor;
cell.selectionStyle = theme.cellSelectionStyle;
AwfulDisclosureIndicatorView *disclosure = (AwfulDisclosureIndicatorView *)cell.accessoryView;
disclosure.color = theme.disclosureIndicatorColor;
disclosure.highlightedColor = theme.disclosureIndicatorHighlightedColor;
}
- (void)updateThreadTagsForCellAtIndexPath:(NSIndexPath *)indexPath
{
if ([self.cellsMissingThreadTags count] == 0) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(newThreadTags:)
name:AwfulNewThreadTagsAvailableNotification
object:nil];
}
[self.cellsMissingThreadTags addObject:indexPath];
}
- (void)newThreadTags:(NSNotification *)note
{
if ([self.cellsMissingThreadTags count] == 0) return;
[self.cellsMissingThreadTags removeAllObjects];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:AwfulNewThreadTagsAvailableNotification
object:nil];
[self.tableView reloadRowsAtIndexPaths:[self.cellsMissingThreadTags allObjects]
withRowAnimation:UITableViewRowAnimationNone];
}
- (void)showThreadActions:(UILongPressGestureRecognizer *)longPress
{
if (longPress.state == UIGestureRecognizerStateBegan) {
UITableViewCell *cell = (UITableViewCell *)longPress.view;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
AwfulThread *thread = [self.fetchedResultsController objectAtIndexPath:indexPath];
[self showThreadActionsForThread:thread];
}
}
- (void)tableView:(UITableView *)tableView
willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.backgroundColor = [AwfulTheme currentTheme].threadCellBackgroundColor;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
AwfulThread *thread = [self.fetchedResultsController objectAtIndexPath:indexPath];
return thread.seenPostsValue > 0;
}
- (NSString *)tableView:(UITableView *)tableView
titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return @"Mark Unread";
}
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
AwfulThread *thread = [self.fetchedResultsController objectAtIndexPath:indexPath];
[self markThreadUnseen:thread];
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
AwfulPostsViewController *page = [AwfulPostsViewController new];
AwfulThread *thread = [self.fetchedResultsController objectAtIndexPath:indexPath];
page.thread = thread;
// For an unread thread, the Forums will interpret "next unread page" to mean "last page",
// which is not very helpful.
[page loadPage:thread.beenSeen ? AwfulThreadPageNextUnread : 1];
[self displayPage:page];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
@end