summaryrefslogtreecommitdiff
path: root/Juick
diff options
context:
space:
mode:
Diffstat (limited to 'Juick')
-rw-r--r--Juick/AppDelegate.m15
-rw-r--r--Juick/ViewControllers/DialogsViewController.m64
-rw-r--r--Juick/ViewControllers/JuickNavigationController.m7
-rw-r--r--Juick/ViewControllers/MessagesViewController.m127
4 files changed, 134 insertions, 79 deletions
diff --git a/Juick/AppDelegate.m b/Juick/AppDelegate.m
index bfde8cf..aac90c7 100644
--- a/Juick/AppDelegate.m
+++ b/Juick/AppDelegate.m
@@ -16,8 +16,6 @@
#import "FeedViewController.h"
#import "NSData+Hex.h"
-NSString * const UserUpdatedNotificationName = @"UserUpdated";
-
@interface AppDelegate()
-(void) parseNotificationPayload:(NSDictionary *)userInfo;
@end
@@ -119,24 +117,17 @@ NSString * const UserUpdatedNotificationName = @"UserUpdated";
if (userInfo[@"service"]) {
User *user = [User fromJSON:userInfo[@"user"]];
application.applicationIconBadgeNumber = user.unreadCount;
- UITabBarController *main = (UITabBarController *)[self navigator];
- UIViewController *discussions = [main.viewControllers objectAtIndex:1];
- if (user.unreadCount > 0) {
- [discussions tabBarItem].badgeColor = [UIColor colorNamed:@"Funny"];
- [discussions tabBarItem].badgeValue = [NSString stringWithFormat:@"%ld", user.unreadCount];
- } else {
- [discussions tabBarItem].badgeValue = nil;
- }
- [[NSNotificationCenter defaultCenter] postNotificationName:UserUpdatedNotificationName object:user];
[[UNUserNotificationCenter currentNotificationCenter] getDeliveredNotificationsWithCompletionHandler:^(NSArray<UNNotification *> * _Nonnull notifications) {
for (UNNotification* notification in notifications) {
if ([notification.request.content.userInfo[@"mid"] integerValue] == [userInfo[@"mid"] integerValue]) {
[[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers:@[notification.request.identifier]];
}
}
+ completionHandler(UIBackgroundFetchResultNewData);
}];
+ } else {
+ completionHandler(UIBackgroundFetchResultNoData);
}
- completionHandler(UIBackgroundFetchResultNewData);
}
-(void) parseNotificationPayload:(NSDictionary *)userInfo {
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
diff --git a/Juick/ViewControllers/JuickNavigationController.m b/Juick/ViewControllers/JuickNavigationController.m
index f535c82..d79206c 100644
--- a/Juick/ViewControllers/JuickNavigationController.m
+++ b/Juick/ViewControllers/JuickNavigationController.m
@@ -32,6 +32,13 @@
} else {
avatarUrl = user.avatar;
}
+ UIViewController *discussions = [self.viewControllers objectAtIndex:1];
+ if (user.unreadCount > 0) {
+ [discussions tabBarItem].badgeColor = [UIColor colorNamed:@"Funny"];
+ [discussions tabBarItem].badgeValue = [NSString stringWithFormat:@"%ld", user.unreadCount];
+ } else {
+ [discussions tabBarItem].badgeValue = nil;
+ }
[[AppDelegate shared].api fetchImageWithURL:[NSURL URLWithString:avatarUrl] callback:^(NSData *data) {
self.avatarButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.avatarButton addTarget:self action:@selector(showLoginForm:) forControlEvents:UIControlEventTouchUpInside];
diff --git a/Juick/ViewControllers/MessagesViewController.m b/Juick/ViewControllers/MessagesViewController.m
index d3bd8aa..7814d1e 100644
--- a/Juick/ViewControllers/MessagesViewController.m
+++ b/Juick/ViewControllers/MessagesViewController.m
@@ -19,66 +19,82 @@
NSString* const messageCellIdentifier = @"messageCell";
+
+@interface MessagesViewController()
+
+@property(nonatomic, assign) Boolean initialDataLoaded;
+
+-(void) applicationActivated;
+
+@end
+
@implementation MessagesViewController
- (void)viewWillAppear:(BOOL)animated {
+ [super viewWillAppear:animated];
self.navigationController.visibleViewController.title = self.title;
}
-(void) refreshData {
- if ([self.path length] > 0) {
- self.dataLoading = YES;
- if (self.messages.count == 0) {
- [self.tableView reloadData];
- }
- [[AppDelegate shared].api pullNextFromPath:self.path params:self.params callback:^(NSArray *next, NSError *err) {
- if (err) {
- [User throwUnableToLogin:self error:err path:self.path params:self.params];
- return;
+ UIApplicationState state = [UIApplication sharedApplication].applicationState;
+ if (state == UIApplicationStateActive && self.dataLoading == NO) {
+ if ([self.path length] > 0) {
+ self.dataLoading = YES;
+ if (self.messages.count == 0) {
+ [self.tableView reloadData];
}
- NSArray *newMsgs = next;
- if ([self isAtTop:self.params]) {
- if (![self.path isEqualToString:[API threadUrl]]) {
- [self.messages removeAllObjects];
- [self.tableView reloadData];
+ [[AppDelegate shared].api pullNextFromPath:self.path params:self.params callback:^(NSArray *next, NSError *err) {
+ if (!self.initialDataLoaded) {
+ self.initialDataLoaded = YES;
}
- }
- NSUInteger oldCount = [self.messages count];
- if ([self.path isEqualToString:[API threadUrl]]) {
- NSUInteger lastRid = [((Message *)[self.messages lastObject]).rid unsignedIntegerValue] + 1;
- NSUInteger count = [next count];
- if (oldCount > 0) {
- if (lastRid && lastRid < count) {
- newMsgs = [next subarrayWithRange:NSMakeRange(lastRid, count - lastRid)];
- } else {
- [self.refreshControl endRefreshing];
- return;
+ self.dataLoading = NO;
+ if (err) {
+ [User throwUnableToLogin:self error:err path:self.path params:self.params];
+ return;
+ }
+ NSArray *newMsgs = next;
+ if ([self isAtTop:self.params]) {
+ if (![self.path isEqualToString:[API threadUrl]]) {
+ [self.messages removeAllObjects];
+ [self.tableView reloadData];
}
}
- }
- self.dataLoading = NO;
- if (self.messages.count == 0) {
- [self.tableView reloadData];
- }
- [self.messages addObjectsFromArray:newMsgs];
- NSMutableArray *indexPaths = [NSMutableArray new];
- for (NSUInteger index = oldCount; index <= oldCount + [newMsgs count] - 1; index++) {
- [indexPaths addObject:[NSIndexPath indexPathForRow:index inSection: 0]];
- }
- [self.tableView beginUpdates];
- [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:NO];
- [self.tableView endUpdates];
- [self.refreshControl endRefreshing];
- if (self.shouldScrollToUnreadOnRefresh) {
- NSInteger messagesCount = [self.messages count];
- if (messagesCount > 0) {
- NSInteger itemToScroll = [self.firstUnread integerValue] ? : self.messages.count - 1;
- NSIndexPath *lastRow = [NSIndexPath indexPathForRow:MIN(itemToScroll, messagesCount - 1) inSection:0];
- [self.tableView scrollToRowAtIndexPath:lastRow atScrollPosition:UITableViewScrollPositionBottom animated:NO];
+ NSUInteger oldCount = [self.messages count];
+ if ([self.path isEqualToString:[API threadUrl]]) {
+ NSUInteger lastRid = [((Message *)[self.messages lastObject]).rid unsignedIntegerValue] + 1;
+ NSUInteger count = [next count];
+ if (oldCount > 0) {
+ if (lastRid && lastRid < count) {
+ newMsgs = [next subarrayWithRange:NSMakeRange(lastRid, count - lastRid)];
+ } else {
+ [self.refreshControl endRefreshing];
+ return;
+ }
+ }
}
- self.firstUnread = nil;
- }
- }];
+ if (self.messages.count == 0) {
+ [self.tableView reloadData];
+ }
+ [self.messages addObjectsFromArray:newMsgs];
+ NSMutableArray *indexPaths = [NSMutableArray new];
+ for (NSUInteger index = oldCount; index <= oldCount + [newMsgs count] - 1; index++) {
+ [indexPaths addObject:[NSIndexPath indexPathForRow:index inSection: 0]];
+ }
+ [self.tableView beginUpdates];
+ [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:NO];
+ [self.tableView endUpdates];
+ [self.refreshControl endRefreshing];
+ if (self.shouldScrollToUnreadOnRefresh) {
+ NSInteger messagesCount = [self.messages count];
+ if (messagesCount > 0) {
+ NSInteger itemToScroll = [self.firstUnread integerValue] ? : self.messages.count - 1;
+ NSIndexPath *lastRow = [NSIndexPath indexPathForRow:MIN(itemToScroll, messagesCount - 1) inSection:0];
+ [self.tableView scrollToRowAtIndexPath:lastRow atScrollPosition:UITableViewScrollPositionBottom animated:NO];
+ }
+ self.firstUnread = nil;
+ }
+ }];
+ }
}
}
@@ -94,7 +110,6 @@ NSString* const messageCellIdentifier = @"messageCell";
- (void)viewDidLoad
{
[super viewDidLoad];
- self.dataLoading = YES;
[self.view setBackgroundColor:[UIColor colorNamed:@"Background"]];
[self.tableView registerNib:[UINib nibWithNibName:@"MessageCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:messageCellIdentifier];
[self.tableView registerNib:[UINib nibWithNibName:@"ContentLoadingCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"contentLoadingCell"];
@@ -105,8 +120,9 @@ NSString* const messageCellIdentifier = @"messageCell";
}
self.refreshControl = [UIRefreshControl new];
[self.refreshControl addTarget:self action:@selector(refreshData) forControlEvents:UIControlEventValueChanged];
- [self refreshData];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(newMessage:) name:NewMessageNotificationName object:nil];
+ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationActivated) name:UIApplicationDidBecomeActiveNotification object:nil];
+ [self refreshData];
}
- (void) composePressed {
@@ -170,4 +186,15 @@ NSString* const messageCellIdentifier = @"messageCell";
[self viewThreadForMessage:msg mid:msg.mid scrollTo:0];
}
+- (void)applicationActivated {
+ if (!self.initialDataLoaded) {
+ [self refreshData];
+ }
+}
+
+- (void)dealloc
+{
+ [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
+}
+
@end