// // 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 "Message.h" #import "ColorScheme.h" #import "NewPostViewController.h" #import "NSURL+PathParameters.h" static NSString *CellIdentifier = @"MessageCell"; @interface MessagesViewController (); @property(nonatomic, strong) NSMutableArray *messages; @property(nonatomic, assign) Boolean dataLoading; @property(nonatomic, strong) NSString *path; @property(nonatomic, strong) NSMutableDictionary *params; @end @implementation MessagesViewController - (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]; } - (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; }]; }]; }); } - (UIStatusBarStyle)preferredStatusBarStyle { return UIStatusBarStyleLightContent; } - (void)viewDidLoad { [super viewDidLoad]; self.dataLoading = NO; [self.view setBackgroundColor:[ColorScheme mainBackground]]; self.messages = [NSMutableArray array]; SWRevealViewController *revealController = [self revealViewController]; if (revealController) { // TODO: add to thread view too [self.view addGestureRecognizer:[revealController panGestureRecognizer]]; } [self.tableView registerNib:[UINib nibWithNibName:@"MessageCell" bundle:nil] forCellReuseIdentifier:CellIdentifier]; self.tableView.rowHeight = UITableViewAutomaticDimension; self.tableView.estimatedRowHeight = 500.0f; self.refreshControl = [UIRefreshControl new]; [self.refreshControl addTarget:self action:@selector(refreshData) forControlEvents:UIControlEventValueChanged];} - (void) composePressed { CATransition* transition = [CATransition animation]; transition.duration = 0.3; transition.type = kCATransitionFade; transition.subtype = kCATransitionFromTop; [self.navigationController.view.layer addAnimation:transition forKey:kCATransition]; [self.navigationController pushViewController:[[NewPostViewController alloc] init] animated:NO]; } - (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)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]; Message *msg = [_messages objectAtIndex:indexPath.row]; [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) { [[NSOperationQueue mainQueue] addOperationWithBlock:^{ [cell setNeedsUpdateConstraints]; [cell updateConstraintsIfNeeded]; [cell setNeedsLayout]; [cell layoutIfNeeded]; }]; }]; } else { cell.attach.image = nil; } return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if ([self.path isEqualToString:[Message threadUrl]]) return; Message *msg = [_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]; } -(void)scrollViewDidScroll:(UIScrollView *)scrollView_ { 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]; } } @end