// // 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 "APIClient.h" #import "Message.h" #import "ColorScheme.h" #import "NewPostViewController.h" #import "LoginViewController.h" #import "ThreadViewController.h" #import "NSURL+PathParameters.h" @implementation MessagesViewController -(void) refreshData { [self refreshData:NO]; } -(void) refreshData:(BOOL)scrollToBottom { 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] + 1; NSUInteger count = [next count]; 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; if (scrollToBottom) { NSIndexPath *lastRow = [NSIndexPath indexPathForRow:self.messages.count - 1 inSection:0]; [self.tableView scrollToRowAtIndexPath:lastRow atScrollPosition:UITableViewScrollPositionBottom animated:NO]; } }]; } - (UIStatusBarStyle)preferredStatusBarStyle { return UIStatusBarStyleLightContent; } - (void)viewDidLoad { [super viewDidLoad]; self.dataLoading = NO; [self.view setBackgroundColor:[ColorScheme mainBackground]]; [self.tableView registerNib:[UINib nibWithNibName:@"MessageCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"messageCell"]; self.tableView.rowHeight = UITableViewAutomaticDimension; self.tableView.estimatedRowHeight = 500.0f; self.messages = [NSMutableArray array]; self.refreshControl = [UIRefreshControl new]; [self.refreshControl addTarget:self action:@selector(refreshData) forControlEvents:UIControlEventValueChanged]; } - (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)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]; } } @end