From 9f4b7a156a40769a0383911ab38e9ed09f986507 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 27 Mar 2017 01:22:24 +0300 Subject: fix reply updates --- Juick/ViewControllers/MessagesViewController.m | 81 ++++++++++++++++------- Juick/ViewControllers/RevealPanelViewController.m | 2 +- Juick/Views/MessageCell.m | 4 +- Juick/Views/MessageCell.xib | 21 ++++-- 4 files changed, 76 insertions(+), 32 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 diff --git a/Juick/ViewControllers/RevealPanelViewController.m b/Juick/ViewControllers/RevealPanelViewController.m index b74d556..c327ca6 100644 --- a/Juick/ViewControllers/RevealPanelViewController.m +++ b/Juick/ViewControllers/RevealPanelViewController.m @@ -46,7 +46,7 @@ static NSString *CellIdentifier = @"NavCell"; self.tableView.dataSource = self; [self.tableView setSeparatorInset:UIEdgeInsetsZero]; self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; - self.tableView.separatorColor =[UIColor darkGrayColor]; + self.tableView.separatorColor =[UIColor whiteColor]; [self.tableView registerNib:[UINib nibWithNibName:@"NavCell" bundle:nil] forCellReuseIdentifier:CellIdentifier]; [self.view addSubview:self.tableView]; self.signButton = [[UIButton alloc] init]; diff --git a/Juick/Views/MessageCell.m b/Juick/Views/MessageCell.m index 963707c..13f33a5 100644 --- a/Juick/Views/MessageCell.m +++ b/Juick/Views/MessageCell.m @@ -35,7 +35,9 @@ if ([msg.repliesBy length] > 0) { self.summary.text = [NSString stringWithFormat:@"%@ replies by %@", msg.repliesCount, msg.repliesBy]; } else { - self.summary.text = [NSString stringWithFormat:@"%@ replies", msg.repliesCount]; + if (msg.repliesCount > 0) { + self.summary.text = [NSString stringWithFormat:@"%@ replies", msg.repliesCount]; + } } } else { self.summary.text = nil; diff --git a/Juick/Views/MessageCell.xib b/Juick/Views/MessageCell.xib index c83b579..e555b60 100644 --- a/Juick/Views/MessageCell.xib +++ b/Juick/Views/MessageCell.xib @@ -1,5 +1,8 @@ - - + + + + + @@ -12,12 +15,14 @@ - + - + + + @@ -26,13 +31,16 @@ + - + + +