// // DiscoverViewController.m // Juick // // Created by Vitaly Takmazov on 03/12/2017. // Copyright © 2017 com.juick. All rights reserved. // #import "DiscoverViewController.h" #import "ThreadViewController.h" #import "MessageCell.h" #import "APIClient.h" #import "AppDelegate.h" @interface DiscoverViewController () @end @implementation DiscoverViewController AppDelegate *appDelegate; -(void) viewDidLoad { [super viewDidLoad]; self.messagesDelegate = self; if ([User isAuthenticated]) { [User checkIsValid:^(BOOL success) { if (success) { if ([self.path length] == 0) { self.path = [APIClient feedUrl]; [self.navigationController.visibleViewController setTitle:@"My feed"]; } [self refreshData:NO]; } else { [User throwUnableToLogin:self]; } }]; } else { if ([self.path length] == 0) { self.path = [APIClient messagesUrl]; [self.navigationController.visibleViewController setTitle:@"Discover"]; } [self refreshData:NO]; } appDelegate = (AppDelegate *) [UIApplication sharedApplication].delegate; if (appDelegate.pushedThread != nil) { [self performSegueWithIdentifier:@"threadViewSegue" sender:self]; } } -(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSString * cellIdentifier = @"messageCell"; MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath]; [self performSegueWithIdentifier:@"threadViewSegue" sender:cell]; } -(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqual: @"threadViewSegue"]) { NSNumber *mid; BOOL scrollToEnd = NO; if (appDelegate.pushedThread != nil) { mid = [appDelegate.pushedThread copy]; appDelegate.pushedThread = nil; scrollToEnd = YES; } else { Message *msg = [self.messages objectAtIndex:[self.tableView indexPathForSelectedRow].row]; mid = msg.mid; } ThreadViewController *threadVC = (ThreadViewController *)segue.destinationViewController; [threadVC setPath:[APIClient threadUrl]]; [threadVC setParams:@{@"mid": mid }]; [threadVC refreshData:scrollToEnd]; } } - (IBAction)filterAction:(id)sender { if (![User isAuthenticated]) { [self performSegueWithIdentifier:@"loginSegue" sender:self]; return; } UIAlertController *filterAlert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; [filterAlert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]]; [filterAlert addAction:[UIAlertAction actionWithTitle:@"My feed" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [self.navigationController.visibleViewController setTitle:@"My feed"]; self.path = [APIClient feedUrl]; self.params = nil; [self.messages removeAllObjects]; [self.tableView reloadData]; [self refreshData:NO]; }]]; [filterAlert addAction:[UIAlertAction actionWithTitle:@"Discover" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [self.navigationController.visibleViewController setTitle:@"Discover"]; self.path = [APIClient messagesUrl]; self.params = nil; [self.messages removeAllObjects]; [self.tableView reloadData]; [self refreshData:NO]; }]]; [filterAlert addAction:[UIAlertAction actionWithTitle:@"Discussions" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [self.navigationController.visibleViewController setTitle:@"Discussions"]; self.path = [APIClient discussionsUrl]; self.params = nil; [self.messages removeAllObjects]; [self.tableView reloadData]; [self refreshData:NO]; }]]; [self presentViewController:filterAlert animated:YES completion:nil]; } -(void) loadMore { Message *lastMsg = [self.messages lastObject]; if (lastMsg != nil) { if ([self.path isEqualToString:[APIClient discussionsUrl]]) { NSDate *msgDate = [[APIClient sharedClient].dateFormatter dateFromString:lastMsg.timestamp]; self.params = [@{@"to" : [NSString stringWithFormat:@"%.0f", [msgDate timeIntervalSince1970] * 1000]} mutableCopy]; } else if (![self.path isEqualToString:[APIClient threadUrl]]) { NSNumber *lastMid = lastMsg.mid; self.params = [@{@"before_mid":lastMid} mutableCopy]; } [self refreshData:NO]; } } @end