blob: cbf00245d4d19a14cb3b6b0c0bc2a3cc4d79a917 (
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
|
//
// 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.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];
}
}
@end
|