From 57c8c45a4730fe7c980b8b7eadf94ca60b97db43 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 3 Feb 2014 16:51:47 +0400 Subject: Still fighting with scroll :( --- Juick/MessagesViewController.m | 94 +++++++++++++++++++++--------------------- 1 file changed, 47 insertions(+), 47 deletions(-) (limited to 'Juick/MessagesViewController.m') diff --git a/Juick/MessagesViewController.m b/Juick/MessagesViewController.m index e8e02e1..7e3f01a 100644 --- a/Juick/MessagesViewController.m +++ b/Juick/MessagesViewController.m @@ -21,6 +21,10 @@ static NSString *CellIdentifier = @"MessageCell"; +static NSMutableDictionary *heightsCache; + + + @interface MessagesViewController (); @property(nonatomic, strong) NSMutableArray *messages; @@ -37,20 +41,24 @@ static NSString *CellIdentifier = @"MessageCell"; [self setPath:messagesPath]; [self setParams:[[NSMutableDictionary alloc] initWithDictionary:params]]; self.title = title; - [self refreshData:nil]; -} - -- (void) refreshData:(UIRefreshControl *)refresh { - [self.messages removeAllObjects]; - [self.tableView reloadData]; + __weak MessagesViewController * weakSelf = self; + [self.tableView addPullToRefreshWithActionHandler:^{ + [weakSelf.messages removeAllObjects]; + [weakSelf refreshData]; + }]; + [self.tableView addInfiniteScrollingWithActionHandler:^{ + [weakSelf refreshData]; + }]; + [self.tableView triggerPullToRefresh]; +} + +- (void) refreshData { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ self.dataLoading = YES; [Message pullNextFromPath:self.path params:self.params callback:^(NSArray *next) { - [self.messages addObjectsFromArray:next]; - [self.tableView reloadData]; - [refresh endRefreshing]; - self.dataLoading = NO; - + [self.messages addObjectsFromArray:next]; + [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO]; + self.dataLoading = NO; }]; }); } @@ -60,43 +68,13 @@ static NSString *CellIdentifier = @"MessageCell"; return UIStatusBarStyleLightContent; } -- (void)scrollViewDidScroll:(UIScrollView *)scrollView { - if ([self.path isEqualToString:[Message threadUrl]]) { - return; - } - CGPoint pos = scrollView.contentOffset; - CGFloat contentHeight = scrollView.contentSize.height - scrollView.contentSize.height / 3; - if (pos.y >= contentHeight && !self.dataLoading) { - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - self.dataLoading = YES; - if (self.params == nil) { - self.params = [[NSMutableDictionary alloc] init]; - } - - Message * lastMessage = [self.messages lastObject]; - if (lastMessage != nil) { - [self.params setObject:lastMessage.MID forKey:@"before_mid"]; - } - [Message pullNextFromPath:self.path params:self.params callback:^(NSArray *next) { - [self.messages addObjectsFromArray:next]; - [self.tableView reloadData]; - self.dataLoading = NO; - - }]; - }); - - } -} - - (void)viewDidLoad { [super viewDidLoad]; self.dataLoading = NO; [self.view setBackgroundColor:[ColorScheme mainBackground]]; self.messages = [NSMutableArray array]; - UIRefreshControl *refresh = [[UIRefreshControl alloc] init]; - [refresh addTarget:self action:@selector(refreshData:) forControlEvents:UIControlEventValueChanged]; - self.refreshControl = refresh; + SWRevealViewController *revealController = [self revealViewController]; if (revealController) { // TODO: add to thread view too @@ -106,7 +84,8 @@ static NSString *CellIdentifier = @"MessageCell"; self.navigationController.navigationBar.tintColor = [ColorScheme navbarBackground]; } - [self.tableView registerClass:[MessageCell class] forCellReuseIdentifier:CellIdentifier]; + [self.tableView registerClass:[MessageCell class] forCellReuseIdentifier:CellIdentifier]; + heightsCache = [[NSMutableDictionary alloc] init]; } - (void) composePressed { @@ -164,16 +143,37 @@ static NSString *CellIdentifier = @"MessageCell"; [cell updateFonts]; Message *msg = [_messages objectAtIndex:indexPath.row]; [cell setMessage:msg]; + if ([msg.attach length] > 0) { + SDWebImageManager * manager = [SDWebImageManager sharedManager]; + [manager downloadWithURL:[NSURL URLWithString:msg.attach] options:SDWebImageContinueInBackground progress:^(NSUInteger receivedSize, long long expectedSize) { + // <#code#> + } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) { + [cell.attach setImage:[UIImage imageWithImage:image convertToWidth:300.0f]]; + [heightsCache setObject:[NSNumber numberWithFloat:[cell heightForCell]] forKey:[NSString stringWithFormat:@"%@#%@", msg.MID, msg.RID]]; + [tableView beginUpdates]; + [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone]; + [tableView endUpdates]; + + }]; + } else { + cell.attach.image = nil; + } + return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { - MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; - [cell updateFonts]; Message *msg = [_messages objectAtIndex:indexPath.row]; - [cell setMessage:msg]; - return [cell heightForCell]; + float height = [[heightsCache objectForKey:[NSString stringWithFormat:@"%@#%@", msg.MID, msg.RID]] floatValue]; + if (height > 0) { + return height; + } else { + MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; + [cell updateFonts]; + [cell setMessage:msg]; + return [cell heightForCell]; + } } - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath -- cgit v1.2.3