// // MasterViewController.m // Juick // // Created by Vitaly Takmazov on 26.10.13. // Copyright (c) 2013 com.juick. All rights reserved. // #import "MasterViewController.h" #import "MessageCell.h" #import "Message.h" static NSString *CellIdentifier = @"MessageCell"; @interface MasterViewController (); @property(nonatomic, strong) NSMutableArray *messages; @property(nonatomic, assign) Boolean dataLoading; @end @implementation MasterViewController - (void) refreshData:(UIRefreshControl *)refresh { [self.messages removeAllObjects]; [self.tableView reloadData]; NSURL *url = [NSURL URLWithString:@"https://api.juick.com/messages"]; NSArray *next = [Message pullNextFromURL:url]; int64_t delayInSeconds = 2.0; dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (delayInSeconds * NSEC_PER_SEC)); dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ [self.tableView beginUpdates]; CGPoint offset = self.tableView.contentOffset; for (int i = 0; i < [next count]; i++) { [self.messages addObject:[next objectAtIndex:i]]; [self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.messages.count-1 inSection:0]] withRowAnimation:UITableViewRowAnimationNone]; } [self.tableView setContentOffset:offset animated:NO]; [self.tableView endUpdates]; [refresh endRefreshing]; }); } - (void)scrollViewDidScroll:(UIScrollView *)scrollView { 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_BACKGROUND, 0), ^{ self.dataLoading = YES; Message * lastMessage = [self.messages lastObject]; NSArray *next = [Message pullNextFromURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://api.juick.com/messages?before_mid=%@", lastMessage.MID]]]; int64_t delayInSeconds = 2.0; dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (delayInSeconds * NSEC_PER_SEC)); dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ [self.tableView beginUpdates]; for (int i = 0; i < [next count]; i++) { [self.messages addObject:[next objectAtIndex:i]]; [self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.messages.count-1 inSection:0]] withRowAnimation:UITableViewRowAnimationNone]; } [self.tableView setContentOffset:pos animated:NO]; [self.tableView endUpdates]; self.dataLoading = NO; }); }); } } - (void)viewDidLoad { [super viewDidLoad]; self.dataLoading = NO; self.title = @"Messages"; self.messages = [NSMutableArray array]; UIRefreshControl *refresh = [[UIRefreshControl alloc] init]; [refresh addTarget:self action:@selector(refreshData:) forControlEvents:UIControlEventValueChanged]; self.refreshControl = refresh; //UINib *cellNib = [UINib nibWithNibName:@"MessageCell" bundle:nil]; //[self.tableView registerNib:cellNib forCellReuseIdentifier:@"MessageCell"]; [self.tableView registerClass:[MessageCell class] forCellReuseIdentifier:CellIdentifier]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contentSizeCategoryChanged:) name:UIContentSizeCategoryDidChangeNotification object:nil]; } - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIContentSizeCategoryDidChangeNotification object:nil]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)contentSizeCategoryChanged:(NSNotification *)notification { [self.tableView reloadData]; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return _messages.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; [cell updateFonts]; Message *msg = [_messages objectAtIndex:indexPath.row]; [cell.avatar setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://i.juick.com/as/%@.png", msg.userID]]]]]; cell.titleLabel.text = msg.user; cell.bodyLabel.text = msg.text; [cell setNeedsUpdateConstraints]; return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; [cell updateFonts]; Message *msg = [_messages objectAtIndex:indexPath.row]; [cell.avatar setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://i.juick.com/as/%@.png", msg.userID]]]]]; cell.titleLabel.text = msg.user; cell.bodyLabel.text = msg.text; cell.bodyLabel.preferredMaxLayoutWidth = tableView.bounds.size.width - (kLabelHorizontalInsets * 2.0f); [cell setNeedsUpdateConstraints]; [cell updateConstraintsIfNeeded]; [cell.contentView setNeedsLayout]; [cell.contentView layoutIfNeeded]; CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height; return height; } - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath { return 100.0f; } @end