blob: e4d0e1c9b6678883dca9e459dbb2119aada0dd45 (
plain) (
blame)
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
|
//
// MasterViewController.m
// Juick
//
// Created by Vitaly Takmazov on 26.10.13.
// Copyright (c) 2013 com.juick. All rights reserved.
//
#import "MessagesViewController.h"
#import "MessageCell.h"
#import "Message.h"
#import "ColorScheme.h"
#import "NewPostViewController.h"
#import "LoginViewController.h"
#import "ThreadViewController.h"
#import "NSURL+PathParameters.h"
@interface MessagesViewController ()
@property(nonatomic, strong) NSMutableArray *messages;
@property(nonatomic, assign) Boolean dataLoading;
@end
@implementation MessagesViewController
- (void) refreshData {
self.dataLoading = YES;
[[APIClient sharedClient] pullNextFromPath:self.path params:self.params callback:^(NSArray *next, NSError *err) {
NSArray *newMsgs = next;
NSUInteger oldCount = [self.messages count];
if ([self.path isEqualToString:[APIClient threadUrl]]) {
NSUInteger lastRid = [((Message *)[self.messages lastObject]).rid unsignedIntegerValue];
NSUInteger count = [next count] - 1;
if (oldCount > 0) {
if (lastRid && lastRid < count) {
newMsgs = [next subarrayWithRange:NSMakeRange(lastRid, count - lastRid)];
} else {
return;
}
}
}
[self.messages addObjectsFromArray:newMsgs];
NSMutableArray *indexPaths = [NSMutableArray new];
for (NSUInteger index = oldCount; index <= oldCount + [newMsgs count] - 1; index++) {
[indexPaths addObject:[NSIndexPath indexPathForRow:index inSection: 0]];
}
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:YES];
[self.tableView endUpdates];
[self.refreshControl endRefreshing];
self.dataLoading = NO;
}];
}
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.dataLoading = NO;
[self.view setBackgroundColor:[ColorScheme mainBackground]];
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 500.0f;
self.messages = [NSMutableArray array];
self.params = [NSMutableDictionary new];
self.refreshControl = [UIRefreshControl new];
[self.refreshControl addTarget:self action:@selector(refreshData) forControlEvents:UIControlEventValueChanged];
if ([User isAuthenticated]) {
[User checkIsValid:^(BOOL success) {
if (success) {
self.path = [APIClient feedUrl];
[self refreshData];
} else {
[User throwUnableToLogin];
}
}];
} else {
self.path = [APIClient messagesUrl];
[self refreshData];
}
}
- (void) composePressed {
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.messages.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
Message *msg = [self.messages objectAtIndex:indexPath.row];
NSString * cellIdentifier = @"messageCell";
MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
[cell configureWithMessage:msg];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([self.path isEqualToString:[APIClient threadUrl]])
return;
Message *msg = [self.messages objectAtIndex:indexPath.row];
ThreadViewController *threadViewController = [[ThreadViewController alloc] init];
[threadViewController setPath:[APIClient threadUrl]];
[threadViewController setParams:[NSMutableDictionary dictionaryWithObjectsAndKeys:msg.mid, @"mid", nil]];
[self.navigationController pushViewController:threadViewController animated:NO];
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView_ {
CGFloat actualPosition = scrollView_.contentOffset.y;
CGFloat contentHeight = scrollView_.contentSize.height - scrollView_.contentSize.height / 2;
if (actualPosition >= contentHeight && !self.dataLoading && ![self.tableView isTracking]) {
Message *lastMsg = [self.messages lastObject];
if (lastMsg != nil) {
NSNumber *lastMid = lastMsg.mid;
if (![self.path isEqualToString:[APIClient threadUrl]]) {
[self.params setObject:lastMid forKey:@"before_mid"];
}
[self refreshData];
}
}
}
@end
|