diff options
author | Vitaly Takmazov | 2017-03-27 01:22:24 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2017-06-28 21:26:00 +0300 |
commit | 9f4b7a156a40769a0383911ab38e9ed09f986507 (patch) | |
tree | afc00e6222941f8bd09d9fc483e3ca181c425d8c /Juick/ViewControllers/MessagesViewController.m | |
parent | 92451d2a01f928ebeaa5a6ec4634b7a7ed9a0edf (diff) |
fix reply updates
Diffstat (limited to 'Juick/ViewControllers/MessagesViewController.m')
-rw-r--r-- | Juick/ViewControllers/MessagesViewController.m | 81 |
1 files changed, 56 insertions, 25 deletions
diff --git a/Juick/ViewControllers/MessagesViewController.m b/Juick/ViewControllers/MessagesViewController.m index e8857a7..3e6b7c5 100644 --- a/Juick/ViewControllers/MessagesViewController.m +++ b/Juick/ViewControllers/MessagesViewController.m @@ -26,36 +26,52 @@ static NSString *CellIdentifier = @"MessageCell"; @property(nonatomic, assign) Boolean dataLoading; @property(nonatomic, strong) NSString *path; @property(nonatomic, strong) NSMutableDictionary *params; +@property(nonatomic, strong) dispatch_queue_t concurrent_queue; @end @implementation MessagesViewController +- (id)init { + if (self = [super init]) { + self.concurrent_queue = dispatch_queue_create("Queue", DISPATCH_QUEUE_SERIAL); + }; + return self; +} + - (void)loadFromPath:(NSString *)messagesPath withParams:(NSDictionary *)params withTitle:(NSString *)title { [self setPath:messagesPath]; [self setParams:[[NSMutableDictionary alloc] initWithDictionary:params]]; self.title = title; __weak MessagesViewController * weakSelf = self; - [self.messages removeAllObjects]; - [weakSelf refreshData]; + dispatch_sync(self.concurrent_queue, ^{ + [self.messages removeAllObjects]; + [weakSelf refreshData]; + }); } - (void) refreshData { - if ([self.path isEqualToString:[Message threadUrl]]) { - [self.messages removeAllObjects]; - } - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - self.dataLoading = YES; - [Message pullNextFromPath:self.path params:self.params callback:^(NSArray *next) { - [[NSOperationQueue mainQueue] addOperationWithBlock:^{ - [self.messages addObjectsFromArray:next]; - [self.tableView reloadData]; - [self.refreshControl endRefreshing]; - self.dataLoading = NO; - }]; - }]; - }); + self.dataLoading = YES; + [Message pullNextFromPath:self.path params:self.params callback:^(NSArray *next) { + NSArray *newMsgs = next; + if ([self.path isEqualToString:[Message threadUrl]]) { + NSUInteger lastRid = [((Message *)[self.messages lastObject]).rid unsignedIntegerValue]; + NSUInteger count = [next count] - 1; + NSUInteger oldCount = [self.messages count]; + if (oldCount > 0) { + if (lastRid && lastRid < count) { + newMsgs = [next subarrayWithRange:NSMakeRange(lastRid, count - lastRid)]; + } else { + return; + } + } + } + [self.messages addObjectsFromArray:newMsgs]; + [self.tableView reloadData]; + [self.refreshControl endRefreshing]; + self.dataLoading = NO; + }]; } - (UIStatusBarStyle)preferredStatusBarStyle @@ -119,12 +135,21 @@ static NSString *CellIdentifier = @"MessageCell"; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { - return _messages.count; + __block NSInteger count; + dispatch_sync(self.concurrent_queue, ^{ + count = self.messages.count; + }); + return count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; - Message *msg = [_messages objectAtIndex:indexPath.row]; + __block Message *msg; + dispatch_sync(self.concurrent_queue, ^{ + msg = [self.messages objectAtIndex:indexPath.row]; + }); + cell.layer.borderWidth = 6; + cell.layer.borderColor = [ColorScheme mainBackground].CGColor; [cell setMessage:msg]; if ([msg.attach length] > 0) { [cell.attach yy_setImageWithURL:[NSURL URLWithString:msg.attach] placeholder:[UIImage imageNamed:@"AttachPlaceholder"] options:YYWebImageOptionProgressiveBlur | YYWebImageOptionSetImageWithFadeAnimation completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) { @@ -146,7 +171,10 @@ static NSString *CellIdentifier = @"MessageCell"; - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if ([self.path isEqualToString:[Message threadUrl]]) return; - Message *msg = [_messages objectAtIndex:indexPath.row]; + __block Message *msg; + dispatch_sync(self.concurrent_queue, ^{ + msg = [self.messages objectAtIndex:indexPath.row]; + }); MessagesViewController *threadViewController = [[MessagesViewController alloc] init]; [threadViewController loadFromPath:[Message threadUrl] withParams:[NSDictionary dictionaryWithObjectsAndKeys:msg.mid, @"mid", nil] withTitle:@"Thread"]; [self.navigationController pushViewController:threadViewController animated:NO]; @@ -156,13 +184,16 @@ static NSString *CellIdentifier = @"MessageCell"; CGFloat actualPosition = scrollView_.contentOffset.y; CGFloat contentHeight = scrollView_.contentSize.height - scrollView_.contentSize.height / 2; if (actualPosition >= contentHeight && !self.dataLoading) { - NSNumber *lastMid = ((Message *)[self.messages lastObject]).mid; - if (![self.path isEqualToString:[Message threadUrl]]) { - [self.params setValue:lastMid forKey:@"before_mid"]; - } - [self refreshData]; + __block Message *lastMsg; + dispatch_sync(self.concurrent_queue, ^{ + lastMsg = [self.messages lastObject]; + NSNumber *lastMid = lastMsg.mid; + if (![self.path isEqualToString:[Message threadUrl]]) { + [self.params setValue:lastMid forKey:@"before_mid"]; + } + [self refreshData]; + }); } } - @end |