summaryrefslogtreecommitdiff
path: root/Juick/ViewControllers/DialogsViewController.m
blob: 4f898145050c4494e02c0886a6d52ba03683481e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//
//  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"

@interface DialogsViewController ()

@end

@implementation DialogsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController.visibleViewController setTitle:@"Chats"];
    [self.view setBackgroundColor:[ColorScheme mainBackground]];
    [self.tableView registerNib:[UINib nibWithNibName:@"ConversationCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"notificationCell"];
    self.chats = [NSMutableArray array];
    [[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];
        }
    }];
}

#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"]) {
        Chat *chat = [self.chats objectAtIndex:[self.tableView indexPathForSelectedRow].row];
        ChatViewController *vc = (ChatViewController *)segue.destinationViewController;
        [vc setUname:chat.uname];
    }
}


@end