// // DialogsViewController.m // Juick // // Created by Vitaly Takmazov on 04/03/2018. // Copyright © 2018 com.juick. All rights reserved. // #import "DialogsViewController.h" #import "ChatViewController.h" #import "ColorScheme.h" #import "ConversationCell.h" #import "APIClient.h" #import "AppDelegate.h" @implementation DialogsViewController - (void)viewDidLoad { [super viewDidLoad]; [self.tabBarItem setTitle:@"Chats"]; self.navigationController.visibleViewController.navigationItem.title = @"Chats"; [self.view setBackgroundColor:[ColorScheme mainBackground]]; [self.tableView registerNib:[UINib nibWithNibName:@"ConversationCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"notificationCell"]; [self refreshData]; self.refreshControl = [UIRefreshControl new]; [self.refreshControl addTarget:self action:@selector(refreshData) forControlEvents:UIControlEventValueChanged]; } - (void) refreshData { self.chats = [NSMutableArray array]; [self.tableView reloadData]; [[APIClient sharedClient] fetchChats:^(NSArray *groups, NSError *err) { if (err == nil) { [self.chats addObjectsFromArray:groups]; NSMutableArray *indexPaths = [NSMutableArray new]; for (NSUInteger index = 0; index < [groups count]; index++) { [indexPaths addObject:[NSIndexPath indexPathForRow:index inSection: 0]]; } [self.tableView beginUpdates]; [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:YES]; [self.tableView endUpdates]; } [self.refreshControl endRefreshing]; }]; } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.chats.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ConversationCell *cell = [tableView dequeueReusableCellWithIdentifier:@"notificationCell" forIndexPath:indexPath]; [cell configureWithChat:[self.chats objectAtIndex:indexPath.row]]; return cell; } -(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [self performSegueWithIdentifier:@"chatSegue" sender:self]; } -(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"chatSegue"]) { NSString *uname; AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; if ([appDelegate.pushedUname length] > 0) { uname = [appDelegate.pushedUname copy]; appDelegate.pushedThread = nil; appDelegate.pushedUname = nil; } else { Chat *chat = [self.chats objectAtIndex:[self.tableView indexPathForSelectedRow].row]; uname = chat.uname; } ChatViewController *vc = (ChatViewController *)segue.destinationViewController; [vc setUname:uname]; } } @end