// // 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 "ColorScheme.h" #import "NewPostViewController.h" #import "NSURL+PathParameters.h" static NSString *CellIdentifier = @"MessageCell"; static NSMutableDictionary *heightsCache; @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.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 performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO]; 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]]; } if (NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_6_1) { self.navigationController.navigationBar.tintColor = [ColorScheme navbarBackground]; } [self.tableView registerClass:[MessageCell class] forCellReuseIdentifier:CellIdentifier]; heightsCache = [[NSMutableDictionary alloc] init]; } - (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]; if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contentSizeCategoryChanged:) name:UIContentSizeCategoryDidChangeNotification object:nil]; } } - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) { [[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]; [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:^(NSInteger receivedSize, NSInteger 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 { Message *msg = [_messages objectAtIndex:indexPath.row]; 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 { /* if (self.dataLoading) { return [self tableView:tableView heightForRowAtIndexPath:indexPath]; }*/ return 500.0f; } - (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)splitViewController:(UISplitViewController *)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem { self.navigationItem.leftBarButtonItem = nil; } -(void)splitViewController:(UISplitViewController *)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)pc { self.navigationItem.leftBarButtonItem = barButtonItem; } @end