diff --git a/VideoCalls/NCDatabaseManager.h b/VideoCalls/NCDatabaseManager.h index 861e46c5..39a09e0d 100644 --- a/VideoCalls/NCDatabaseManager.h +++ b/VideoCalls/NCDatabaseManager.h @@ -22,6 +22,8 @@ NS_ASSUME_NONNULL_BEGIN @property NSString *deviceIdentifier; @property NSString *deviceSignature; @property NSString *userPublicKey; +@property NSInteger unreadBadgeNumber; +@property BOOL unreadNotification; @property BOOL active; @end @@ -60,6 +62,10 @@ NS_ASSUME_NONNULL_BEGIN - (NSString *)accountIdForUser:(NSString *)user inServer:(NSString *)server; - (void)createAccountForUser:(NSString *)user inServer:(NSString *)server; - (void)removeAccountWithAccountId:(NSString *)accountId; +- (void)increaseUnreadBadgeNumberForAccountId:(NSString *)accountId; +- (void)resetUnreadBadgeNumberForAccountId:(NSString *)accountId; +- (BOOL)shouldShowUnreadNotificationForInactiveAccounts; +- (void)removeUnreadNotificationForInactiveAccounts; - (ServerCapabilities *)serverCapabilitiesForAccountId:(NSString *)accountId; - (void)setServerCapabilities:(NSDictionary *)serverCapabilities forAccountId:(NSString *)accountId; diff --git a/VideoCalls/NCDatabaseManager.m b/VideoCalls/NCDatabaseManager.m index 7bd303e0..929311d0 100644 --- a/VideoCalls/NCDatabaseManager.m +++ b/VideoCalls/NCDatabaseManager.m @@ -54,7 +54,7 @@ RLMRealmConfiguration *configuration = [RLMRealmConfiguration defaultConfiguration]; NSURL *databaseURL = [[NSURL fileURLWithPath:path] URLByAppendingPathComponent:k_TalkDatabaseFileName]; configuration.fileURL = databaseURL; - configuration.schemaVersion = 1; + configuration.schemaVersion = 2; [RLMRealmConfiguration setDefaultConfiguration:configuration]; #ifdef DEBUG @@ -154,6 +154,47 @@ [realm commitWriteTransaction]; } +- (void)increaseUnreadBadgeNumberForAccountId:(NSString *)accountId +{ + RLMRealm *realm = [RLMRealm defaultRealm]; + [realm beginWriteTransaction]; + NSPredicate *query = [NSPredicate predicateWithFormat:@"accountId = %@", accountId]; + TalkAccount *account = [TalkAccount objectsWithPredicate:query].firstObject; + account.unreadBadgeNumber += 1; + account.unreadNotification = YES; + [realm commitWriteTransaction]; +} + +- (void)resetUnreadBadgeNumberForAccountId:(NSString *)accountId +{ + RLMRealm *realm = [RLMRealm defaultRealm]; + [realm beginWriteTransaction]; + NSPredicate *query = [NSPredicate predicateWithFormat:@"accountId = %@", accountId]; + TalkAccount *account = [TalkAccount objectsWithPredicate:query].firstObject; + account.unreadBadgeNumber = 0; + account.unreadNotification = NO; + [realm commitWriteTransaction]; +} + +- (BOOL)shouldShowUnreadNotificationForInactiveAccounts +{ + TalkAccount *accountToBeNotified = [TalkAccount objectsWhere:(@"unreadNotification = true")].firstObject; + if (accountToBeNotified) { + return YES; + } + return NO; +} + +- (void)removeUnreadNotificationForInactiveAccounts +{ + RLMRealm *realm = [RLMRealm defaultRealm]; + [realm beginWriteTransaction]; + for (TalkAccount *account in [TalkAccount allObjects]) { + account.unreadNotification = NO; + } + [realm commitWriteTransaction]; +} + #pragma mark - Server capabilities - (ServerCapabilities *)serverCapabilitiesForAccountId:(NSString *)accountId diff --git a/VideoCalls/NCNotificationController.m b/VideoCalls/NCNotificationController.m index 2d506861..c0d6bdaa 100644 --- a/VideoCalls/NCNotificationController.m +++ b/VideoCalls/NCNotificationController.m @@ -106,6 +106,11 @@ NSString * const NCLocalNotificationJoinChatNotification = @"NCLocalN [self updateAppIconBadgeNumber:1]; + NSString *activeAccountId = [[[NCDatabaseManager sharedInstance] activeAccount] accountId]; + if (![activeAccountId isEqualToString:pushNotification.accountId]) { + [[NCDatabaseManager sharedInstance] increaseUnreadBadgeNumberForAccountId:pushNotification.accountId]; + } + [[NSNotificationCenter defaultCenter] postNotificationName:NCNotificationControllerWillPresentNotification object:self userInfo:nil]; } diff --git a/VideoCalls/NCSettingsController.m b/VideoCalls/NCSettingsController.m index 07d0f791..faf1f59f 100644 --- a/VideoCalls/NCSettingsController.m +++ b/VideoCalls/NCSettingsController.m @@ -170,6 +170,7 @@ NSString * const NCUserProfileImageUpdatedNotification = @"NCUserProfileImageUpd { [[NCUserInterfaceController sharedInstance] presentConversationsList]; [[NCDatabaseManager sharedInstance] setActiveAccountWithAccountId:accountId]; + [[NCDatabaseManager sharedInstance] resetUnreadBadgeNumberForAccountId:accountId]; [[NSHTTPCookieStorage sharedHTTPCookieStorage] removeCookiesSinceDate:[NSDate dateWithTimeIntervalSince1970:0]]; [[NCConnectionController sharedInstance] checkAppState]; } diff --git a/VideoCalls/SettingsViewController.m b/VideoCalls/SettingsViewController.m index 5f52701e..0a679274 100644 --- a/VideoCalls/SettingsViewController.m +++ b/VideoCalls/SettingsViewController.m @@ -21,6 +21,7 @@ #import "UIImageView+AFNetworking.h" #import "UIImageView+Letters.h" #import "CCBKPasscode.h" +#import "RoundedNumberView.h" #import typedef enum SettingsSection { @@ -400,6 +401,13 @@ typedef enum AboutSection { NSString *accountServer = [account.server stringByReplacingOccurrencesOfString:[[NSURL URLWithString:account.server] scheme] withString:@""]; cell.accountServerLabel.text = [accountServer stringByReplacingOccurrencesOfString:@"://" withString:@""]; [cell.accountImageView setImage:[[NCAPIController sharedInstance] userProfileImageForAccount:account withSize:CGSizeMake(90, 90)]]; + cell.accessoryView = nil; + if (account.unreadBadgeNumber > 0) { + RoundedNumberView *badgeView = [[RoundedNumberView alloc] init]; + badgeView.important = YES; + badgeView.number = account.unreadBadgeNumber; + cell.accessoryView = badgeView; + } return cell; } else { cell = [tableView dequeueReusableCellWithIdentifier:addAccountCellIdentifier];