blob: fb3f63b27ae1aee62e7b628b8286029becc2c1e1 (
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
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
|
//
// 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 "NewPostViewController.h"
#import "LoginViewController.h"
#import "ThreadViewController.h"
#import "BlogViewController.h"
#import "User+UIView.h"
NSString* const messageCellIdentifier = @"messageCell";
@interface MessagesViewController()
@property(nonatomic, assign) Boolean initialDataLoaded;
-(void) applicationActivated;
@end
@implementation MessagesViewController
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.navigationController.visibleViewController.title = self.title;
}
-(void) refreshData {
UIApplicationState state = [UIApplication sharedApplication].applicationState;
if (state == UIApplicationStateActive && self.dataLoading == NO) {
if ([self.path length] > 0) {
self.dataLoading = YES;
if (self.messages.count == 0) {
[self.tableView reloadData];
}
[[AppDelegate shared].api pullNextFromPath:self.path params:self.params callback:^(NSArray *next, NSError *err) {
if (!self.initialDataLoaded) {
self.initialDataLoaded = YES;
}
self.dataLoading = NO;
if (err) {
[User throwUnableToLogin:self sourceView:self.navigationItem.titleView error:err path:self.path params:self.params];
return;
}
NSArray *newMsgs = next;
if ([self isAtTop:self.params]) {
if (![self.path isEqualToString:[API threadUrl]]) {
[self.messages removeAllObjects];
[self.tableView reloadData];
}
}
NSUInteger oldCount = [self.messages count];
if ([self.path isEqualToString:[API threadUrl]]) {
NSUInteger lastRid = [((Message *)[self.messages lastObject]).rid unsignedIntegerValue] + 1;
NSUInteger count = [next count];
if (oldCount > 0) {
if (lastRid && lastRid < count) {
newMsgs = [next subarrayWithRange:NSMakeRange(lastRid, count - lastRid)];
} else {
[self.refreshControl endRefreshing];
return;
}
}
}
if (self.messages.count == 0) {
[self.tableView reloadData];
}
[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:NO];
[self.tableView endUpdates];
[self.refreshControl endRefreshing];
if (self.shouldScrollToUnreadOnRefresh) {
NSInteger messagesCount = [self.messages count];
if (messagesCount > 0) {
NSInteger itemToScroll = MIN([self.firstUnread integerValue], self.messages.count - 1);
NSIndexPath *lastRow = [NSIndexPath indexPathForRow:itemToScroll inSection:0];
[self.tableView scrollToRowAtIndexPath:lastRow atScrollPosition:UITableViewScrollPositionBottom animated:NO];
}
self.firstUnread = nil;
}
}];
}
}
}
-(void) refreshControlActivated {
NSMutableDictionary *refreshedParams = [self.params mutableCopy];
refreshedParams[@"before_mid"] = nil;
refreshedParams[@"to"] = nil;
self.params = refreshedParams;
[self refreshData];
}
-(BOOL) isAtTop:(NSDictionary *)params {
return [params objectForKey:@"before_mid"] == nil && [params objectForKey:@"to"] == nil;
}
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor colorNamed:@"Background"]];
[self.tableView registerNib:[UINib nibWithNibName:@"MessageCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:messageCellIdentifier];
[self.tableView registerNib:[UINib nibWithNibName:@"ContentLoadingCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"contentLoadingCell"];
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 500.0f;
if (!self.messages) {
self.messages = [NSMutableArray array];
}
#if !TARGET_OS_MACCATALYST
self.refreshControl = [UIRefreshControl new];
[self.refreshControl addTarget:self action:@selector(refreshControlActivated) forControlEvents:UIControlEventValueChanged];
#endif
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(newMessage:) name:NewMessageNotificationName object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationActivated) name:UIApplicationDidBecomeActiveNotification object:nil];
[self refreshData];
}
- (void) composePressed {
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.dataLoading && self.messages.count == 0 ? 4 : self.messages.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.dataLoading && self.messages.count == 0) {
return [tableView dequeueReusableCellWithIdentifier:@"contentLoadingCell"];
} else {
Message *msg = [self.messages objectAtIndex:indexPath.row];
MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:messageCellIdentifier forIndexPath:indexPath];
[cell configureWithMessage:msg selectable:self.shouldAllowToSelectText];
return cell;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return self.dataLoading && self.messages.count == 0 ? 200.0f : [super tableView:tableView heightForRowAtIndexPath:indexPath];
}
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Message *msg = [self.messages objectAtIndex:indexPath.row];
[self viewThreadForMessage:msg mid:msg.mid scrollTo:0];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
/*
if ([segue.identifier isEqual: @"profileSegue"]) {
BlogViewController *blogVC = (BlogViewController *)segue.destinationViewController;
[blogVC setUname:self.selectedUser];
[blogVC setTitle:self.selectedUser];
blogVC.path = [NSString stringWithFormat:@"/messages?uname=%@", self.selectedUser];
[blogVC setShouldScrollToBottomOnRefresh: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]) {
[_messagesDelegate loadMore];
}
}
-(void) viewThreadForMessage:(Message *)msg mid:(NSNumber *)mid scrollTo:(NSNumber *)rid {
[[AppDelegate shared] presentThread:self];
}
-(void) newMessage:(NSNotification *)obj {
Message *msg = (Message *)[obj object];
[self viewThreadForMessage:msg mid:msg.mid scrollTo:0];
}
- (void)applicationActivated {
if (!self.initialDataLoaded) {
[self refreshData];
}
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
}
@end
|