// // MasterViewController.m // Juick // // Created by Vitaly Takmazov on 26.10.13. // Copyright (c) 2013 com.juick. All rights reserved. // #import "SWRevealViewController.h" #import "MessagesViewController.h" #import "MessageCell.h" #import "Message.h" #import "ColorsAndButtons.h" #import "NSURL+PathParameters.h" #import "UIImage+Helpers.h" static NSString *CellIdentifier = @"MessageCell"; @interface MessagesViewController (); @property(nonatomic, strong) NSMutableArray *messages; @property(nonatomic, assign) Boolean dataLoading; @property(nonatomic, strong) NSURL *url; @end @implementation MessagesViewController - (id)initWithURL:(NSURL *)messagesURL { self = [super init]; if (self) { self.url = messagesURL; } return self; } - (void) refreshData:(UIRefreshControl *)refresh { [self.messages removeAllObjects]; [self.tableView reloadData]; NSArray *next = [Message pullNextFromURL:self.url]; int64_t delayInSeconds = 1.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]; }); } - (UIStatusBarStyle)preferredStatusBarStyle { return UIStatusBarStyleLightContent; } - (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_DEFAULT, 0), ^{ self.dataLoading = YES; Message * lastMessage = [self.messages lastObject]; NSArray *next = [Message pullNextFromURL:[self.url URLByAppendingParameters:[NSDictionary dictionaryWithObjectsAndKeys:lastMessage.MID, @"before_mid", nil]]]; int64_t delayInSeconds = 1.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 = @"Discover"; [self.view setBackgroundColor:[ColorsAndButtons mainBackground]]; SWRevealViewController *revealController = [self revealViewController]; [self.navigationController.navigationBar addGestureRecognizer:revealController.panGestureRecognizer]; UIBarButtonItem *revealButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"reveal-icon.png"] style:UIBarButtonItemStyleBordered target:revealController action:@selector(revealToggle:)]; self.navigationItem.leftBarButtonItem = revealButtonItem; 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]; [UIImage loadFromURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://i.juick.com/as/%@.png", msg.userID]] callback:^(UIImage *image) { [cell.avatar setImage:image]; }]; cell.titleLabel.text = msg.user; cell.titleLabel.textColor = [ColorsAndButtons linkColor]; cell.bodyLabel.text = msg.text; [cell setBackgroundColor:[UIColor whiteColor]]; [cell setNeedsUpdateConstraints]; return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; [cell updateFonts]; Message *msg = [_messages objectAtIndex:indexPath.row]; [UIImage loadFromURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://i.juick.com/as/%@.png", msg.userID]] callback:^(UIImage *image) { [cell.avatar setImage:image]; }]; 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