summaryrefslogtreecommitdiff
path: root/Juick/ViewControllers/DialogsViewController.m
diff options
context:
space:
mode:
Diffstat (limited to 'Juick/ViewControllers/DialogsViewController.m')
-rw-r--r--Juick/ViewControllers/DialogsViewController.m64
1 files changed, 47 insertions, 17 deletions
diff --git a/Juick/ViewControllers/DialogsViewController.m b/Juick/ViewControllers/DialogsViewController.m
index 432cd53..cca5479 100644
--- a/Juick/ViewControllers/DialogsViewController.m
+++ b/Juick/ViewControllers/DialogsViewController.m
@@ -10,14 +10,25 @@
#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 refreshData];
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 {
@@ -29,23 +40,31 @@
- (void) refreshData {
self.chats = [NSMutableArray array];
[self.tableView reloadData];
- [[AppDelegate shared].api 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]];
+ 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.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];
- }];
+ 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: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
@@ -96,5 +115,16 @@
[self.tableView setBackgroundView:messageLabel];
}
+- (void)applicationActivated {
+ if (!self.initialDataLoaded) {
+ [self refreshData];
+ }
+}
+
+- (void)dealloc
+{
+ [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
+}
+
@end