summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2019-03-24 13:05:27 +0300
committerGravatar Vitaly Takmazov2019-03-24 13:05:27 +0300
commit2e132e6bbffac6a50b088fb6414c6bfa2a7743ac (patch)
treede6dd3a22710b39b616ad7bf0ffbeae58f072417
parent212bdfdcdae73049385ff5134c550701995c9a71 (diff)
Update badge on discussions tab
-rw-r--r--Juick/AppDelegate.h2
-rw-r--r--Juick/AppDelegate.m14
-rw-r--r--Juick/Model/User.h1
3 files changed, 16 insertions, 1 deletions
diff --git a/Juick/AppDelegate.h b/Juick/AppDelegate.h
index 3316d3f..7b287e2 100644
--- a/Juick/AppDelegate.h
+++ b/Juick/AppDelegate.h
@@ -10,6 +10,8 @@
#import "ThreadViewController.h"
#import "User.h"
+extern NSString * const UserUpdatedNotificationName;
+
@interface AppDelegate : UIResponder <UIApplicationDelegate, UNUserNotificationCenterDelegate>
@property (strong, nonatomic) UIWindow *window;
diff --git a/Juick/AppDelegate.m b/Juick/AppDelegate.m
index 802bbab..d33a0fb 100644
--- a/Juick/AppDelegate.m
+++ b/Juick/AppDelegate.m
@@ -17,6 +17,8 @@
#import "NewPostViewController.h"
#import "FeedViewController.h"
+NSString * const UserUpdatedNotificationName = @"UserUpdated";
+
@interface AppDelegate()
-(void) parseNotificationPayload:(NSDictionary *)userInfo;
@end
@@ -108,7 +110,17 @@
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
if (userInfo[@"service"]) {
- application.applicationIconBadgeNumber = [userInfo[@"user"][@"unreadCount"] integerValue];
+ User *user = [User fromJSON:userInfo[@"user"]];
+ application.applicationIconBadgeNumber = user.unreadCount;
+ UITabBarController *main = (UITabBarController *)self.window.rootViewController;
+ UINavigationController *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 = @"";
+ }
+ [[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]) {
diff --git a/Juick/Model/User.h b/Juick/Model/User.h
index 4a4988a..cbf4e46 100644
--- a/Juick/Model/User.h
+++ b/Juick/Model/User.h
@@ -12,6 +12,7 @@
@property (nonatomic, strong) NSString *uname;
@property (nonatomic, strong) NSString *uid;
@property (nonatomic, strong) NSString *avatar;
+@property (nonatomic) NSInteger unreadCount;
+ (User *) fromJSON:(NSDictionary *)jsonData;