// // 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 "ConversationCell.h" #import "APIClient.h" #import "AppDelegate.h" @implementation DialogsViewController - (void)viewDidLoad { [super viewDidLoad]; [self.view setBackgroundColor:[UIColor colorNamed:@"Background"]]; [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)viewWillAppear:(BOOL)animated { self.navigationController.visibleViewController.title = self.title; NSIndexPath *tableSelection = [self.tableView indexPathForSelectedRow]; [self.tableView deselectRowAtIndexPath:tableSelection animated:NO]; } - (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.tableView.backgroundView = [UIView new]; } else { [self setEmptyMessageView:@"Sign in to view chats"]; [self.tableView reloadData]; } [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; if ([[AppDelegate shared].pushedUname length] > 0) { uname = [[AppDelegate shared].pushedUname copy]; [[AppDelegate shared] cleanupPushedData]; } else { Chat *chat = [self.chats objectAtIndex:[self.tableView indexPathForSelectedRow].row]; uname = chat.uname; } ChatViewController *vc = (ChatViewController *)segue.destinationViewController; [vc setUname:uname]; } } - (void) setEmptyMessageView:(NSString *)message { CGRect rect = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height); UILabel *messageLabel = [[UILabel alloc] initWithFrame:rect]; messageLabel.text = message; messageLabel.numberOfLines = 0; messageLabel.textAlignment = NSTextAlignmentCenter; messageLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleCallout]; messageLabel.textColor = [UIColor colorNamed:@"Muted"]; [messageLabel sizeToFit]; self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; [self.tableView setBackgroundView:messageLabel]; } @end