summaryrefslogtreecommitdiff
path: root/Juick/ViewControllers/DialogsViewController.m
blob: 5f2dca9ab60918c9c5a79c35cbb8a40b1a13bb3c (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//
//  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"

@interface DialogsViewController()

@property(nonatomic, assign) Boolean dataLoading;

@property(nonatomic, assign) Boolean initialDataLoaded;

-(void) applicationActivated;

@end

@implementation DialogsViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor colorNamed:@"Background"]];
    [self.tableView registerNib:[UINib nibWithNibName:@"ConversationCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"notificationCell"];
    self.refreshControl = [UIRefreshControl new];
    [self.refreshControl addTarget:self action:@selector(refreshData) forControlEvents:UIControlEventValueChanged];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationActivated) name:UIApplicationDidBecomeActiveNotification object:nil];
    [self refreshData];
}

- (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];
    UIApplicationState state = [UIApplication sharedApplication].applicationState;
    if (state == UIApplicationStateActive && self.dataLoading == NO) {
        self.dataLoading = YES;
        [[AppDelegate shared].api fetchChats:^(NSArray *groups, NSError *err) {
            if (!self.initialDataLoaded) {
                self.initialDataLoaded = YES;
            }
            self.dataLoading = NO;
            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:NO];
                [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];
}

- (void)applicationActivated {
    if (!self.initialDataLoaded) {
        [self refreshData];
    }
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
}


@end