summaryrefslogtreecommitdiff
path: root/Juick/ViewControllers/DialogsViewController.m
blob: c2296dd15493cefe972942d28259b48254c065f8 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//
//  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.navigationController.visibleViewController.navigationItem.title = @"Chats";
    [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) 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].pushedThread = nil;
            [AppDelegate shared].pushedUname = nil;
        } 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