From a319f4705795581ac3fddcdb221a1cfa4b54849e Mon Sep 17 00:00:00 2001 From: Giom Foret Date: Fri, 28 Apr 2017 14:06:58 +0200 Subject: [PATCH] Prepare UX rework: - Handle the missed conversation badges for each tabBar icon. --- Riot/Model/RoomList/RecentsDataSource.h | 30 +++++++ Riot/Model/RoomList/RecentsDataSource.m | 94 ++++++++++++++------ Riot/ViewController/MasterTabBarController.h | 5 ++ Riot/ViewController/MasterTabBarController.m | 81 +++++++---------- Riot/ViewController/RecentsViewController.m | 3 + 5 files changed, 139 insertions(+), 74 deletions(-) diff --git a/Riot/Model/RoomList/RecentsDataSource.h b/Riot/Model/RoomList/RecentsDataSource.h index a96956822..41b87a9fb 100644 --- a/Riot/Model/RoomList/RecentsDataSource.h +++ b/Riot/Model/RoomList/RecentsDataSource.h @@ -128,4 +128,34 @@ extern NSString *const kRecentsDataSourceTapOnDirectoryServerChange; */ - (void)moveRoomCell:(MXRoom*)room from:(NSIndexPath*)oldPath to:(NSIndexPath*)newPath success:(void (^)())moveSuccess failure:(void (^)(NSError *error))moveFailure; +/** + The current number of the favourite rooms with missed notifications. + */ +@property (nonatomic, readonly) NSUInteger missedFavouriteDiscussionsCount; + +/** + The current number of the favourite rooms with unread highlighted messages. + */ +@property (nonatomic, readonly) NSUInteger missedHighlightFavouriteDiscussionsCount; + +/** + The current number of the direct chats with missed notifications, including the invites. + */ +@property (nonatomic, readonly) NSUInteger missedDirectDiscussionsCount; + +/** + The current number of the direct chats with unread highlighted messages. + */ +@property (nonatomic, readonly) NSUInteger missedHighlightDirectDiscussionsCount; + +/** + The current number of the group chats with missed notifications, including the invites. + */ +@property (nonatomic, readonly) NSUInteger missedGroupDiscussionsCount; + +/** + The current number of the group chats with unread highlighted messages. + */ +@property (nonatomic, readonly) NSUInteger missedHighlightGroupDiscussionsCount; + @end diff --git a/Riot/Model/RoomList/RecentsDataSource.m b/Riot/Model/RoomList/RecentsDataSource.m index 0f75169b4..cbb4797fc 100644 --- a/Riot/Model/RoomList/RecentsDataSource.m +++ b/Riot/Model/RoomList/RecentsDataSource.m @@ -852,6 +852,10 @@ NSString *const kRecentsDataSourceTapOnDirectoryServerChange = @"kRecentsDataSou [conversationCellDataArray removeAllObjects]; [lowPriorityCellDataArray removeAllObjects]; + _missedFavouriteDiscussionsCount = _missedHighlightFavouriteDiscussionsCount = 0; + _missedDirectDiscussionsCount = _missedHighlightDirectDiscussionsCount = 0; + _missedGroupDiscussionsCount = _missedHighlightGroupDiscussionsCount = 0; + directorySection = favoritesSection = conversationSection = lowPrioritySection = invitesSection = -1; if (displayedRecentsDataSourceArray.count > 0) @@ -862,13 +866,13 @@ NSString *const kRecentsDataSourceTapOnDirectoryServerChange = @"kRecentsDataSou NSInteger count = recentsDataSource.numberOfCells; - if (_recentsDataSourceMode == RecentsDataSourceModeHome) + for (int index = 0; index < count; index++) { - for (int index = 0; index < count; index++) + id recentCellDataStoring = [recentsDataSource cellDataAtIndex:index]; + MXRoom* room = recentCellDataStoring.roomSummary.room; + + if (_recentsDataSourceMode == RecentsDataSourceModeHome) { - id recentCellDataStoring = [recentsDataSource cellDataAtIndex:index]; - MXRoom* room = recentCellDataStoring.roomSummary.room; - if (room.accountData.tags[kMXRoomTagFavourite]) { [favoriteCellDataArray addObject:recentCellDataStoring]; @@ -886,28 +890,16 @@ NSString *const kRecentsDataSourceTapOnDirectoryServerChange = @"kRecentsDataSou [conversationCellDataArray addObject:recentCellDataStoring]; } } - } - else if (_recentsDataSourceMode == RecentsDataSourceModeFavourites) - { - for (int index = 0; index < count; index++) + else if (_recentsDataSourceMode == RecentsDataSourceModeFavourites) { - id recentCellDataStoring = [recentsDataSource cellDataAtIndex:index]; - MXRoom* room = recentCellDataStoring.roomSummary.room; - // Keep only the favourites rooms. if (room.accountData.tags[kMXRoomTagFavourite]) { [favoriteCellDataArray addObject:recentCellDataStoring]; } } - } - else if (_recentsDataSourceMode == RecentsDataSourceModePeople) - { - for (int index = 0; index < count; index++) + else if (_recentsDataSourceMode == RecentsDataSourceModePeople) { - id recentCellDataStoring = [recentsDataSource cellDataAtIndex:index]; - MXRoom* room = recentCellDataStoring.roomSummary.room; - // Keep only the direct rooms. if (room.isDirect) { @@ -921,14 +913,8 @@ NSString *const kRecentsDataSourceTapOnDirectoryServerChange = @"kRecentsDataSou } } } - } - else if (_recentsDataSourceMode == RecentsDataSourceModeRooms) - { - for (int index = 0; index < count; index++) + else if (_recentsDataSourceMode == RecentsDataSourceModeRooms) { - id recentCellDataStoring = [recentsDataSource cellDataAtIndex:index]; - MXRoom* room = recentCellDataStoring.roomSummary.room; - // Consider only non direct rooms. if (!room.isDirect) { @@ -943,8 +929,62 @@ NSString *const kRecentsDataSourceTapOnDirectoryServerChange = @"kRecentsDataSou } } } + + // Update missed conversations counts + NSUInteger notificationCount = recentCellDataStoring.roomSummary.notificationCount; + + // Ignore the regular notification count if the room is in 'mentions only" mode at the Riot level. + if (room.isMentionsOnly) + { + // Only the highlighted missed messages must be considered here. + notificationCount = recentCellDataStoring.roomSummary.highlightCount; + } + + if (notificationCount) + { + if (room.accountData.tags[kMXRoomTagFavourite]) + { + _missedFavouriteDiscussionsCount ++; + + if (recentCellDataStoring.roomSummary.highlightCount) + { + _missedHighlightFavouriteDiscussionsCount ++; + } + } + + if (room.isDirect) + { + _missedDirectDiscussionsCount ++; + + if (recentCellDataStoring.roomSummary.highlightCount) + { + _missedHighlightDirectDiscussionsCount ++; + } + } + else + { + _missedGroupDiscussionsCount ++; + + if (recentCellDataStoring.roomSummary.highlightCount) + { + _missedHighlightGroupDiscussionsCount ++; + } + } + } + else if (room.state.membership == MXMembershipInvite) + { + if (room.isDirect) + { + _missedDirectDiscussionsCount ++; + } + else + { + _missedGroupDiscussionsCount ++; + } + } + } - + if (favoriteCellDataArray.count > 0) { // Sort them according to their tag order diff --git a/Riot/ViewController/MasterTabBarController.h b/Riot/ViewController/MasterTabBarController.h index 81fd214f8..348d12ebc 100644 --- a/Riot/ViewController/MasterTabBarController.h +++ b/Riot/ViewController/MasterTabBarController.h @@ -103,6 +103,11 @@ */ - (NSUInteger)missedHighlightDiscussionsCount; +/** + Refresh the missed conversations badges on tab bar icon + */ +- (void)refreshTabBarBadges; + // Reference to the current auth VC. It is not nil only when the auth screen is displayed. @property (nonatomic, readonly) AuthenticationViewController *authViewController; diff --git a/Riot/ViewController/MasterTabBarController.m b/Riot/ViewController/MasterTabBarController.m index b4eafa958..3dd6d5a03 100644 --- a/Riot/ViewController/MasterTabBarController.m +++ b/Riot/ViewController/MasterTabBarController.m @@ -46,9 +46,6 @@ // Current alert (if any). MXKAlert *currentAlert; - - // Observer kMXRoomSummaryDidChangeNotification to keep updated the missed discussion count - id mxRoomSummaryDidChangeObserver; } @property(nonatomic,getter=isHidden) BOOL hidden; @@ -109,13 +106,7 @@ [self promptUserBeforeUsingGoogleAnalytics]; } - // Observe missed notifications - mxRoomSummaryDidChangeObserver = [[NSNotificationCenter defaultCenter] addObserverForName:kMXRoomSummaryDidChangeNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notif) { - - [self refreshHomeTabBadge]; - - }]; - [self refreshHomeTabBadge]; + [self refreshTabBarBadges]; } if (unifiedSearchViewController) @@ -128,12 +119,6 @@ - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; - - if (mxRoomSummaryDidChangeObserver) - { - [[NSNotificationCenter defaultCenter] removeObserver:mxRoomSummaryDidChangeObserver]; - mxRoomSummaryDidChangeObserver = nil; - } } - (void)dealloc @@ -156,12 +141,6 @@ [[NSNotificationCenter defaultCenter] removeObserver:authViewControllerObserver]; authViewControllerObserver = nil; } - - if (mxRoomSummaryDidChangeObserver) - { - [[NSNotificationCenter defaultCenter] removeObserver:mxRoomSummaryDidChangeObserver]; - mxRoomSummaryDidChangeObserver = nil; - } } #pragma mark - @@ -258,7 +237,7 @@ - (void)onMatrixSessionStateDidChange:(NSNotification *)notif { - [self refreshHomeTabBadge]; + [self refreshTabBarBadges]; } - (void)showAuthenticationScreen @@ -597,44 +576,52 @@ #pragma mark - -- (void)refreshHomeTabBadge +- (void)refreshTabBarBadges +{ + [self setMissedDiscussionsCount:[self missedDiscussionsCount] onTabBarItem:TABBAR_HOME_INDEX withBadgeColor:(self.missedHighlightDiscussionsCount ? kRiotColorPinkRed : kRiotColorGreen)]; + + [self setMissedDiscussionsCount:recentsDataSource.missedFavouriteDiscussionsCount onTabBarItem:TABBAR_FAVOURITES_INDEX withBadgeColor:(recentsDataSource.missedHighlightFavouriteDiscussionsCount ? kRiotColorPinkRed : kRiotColorGreen)]; + [self setMissedDiscussionsCount:recentsDataSource.missedDirectDiscussionsCount onTabBarItem:TABBAR_PEOPLE_INDEX withBadgeColor:(recentsDataSource.missedHighlightDirectDiscussionsCount ? kRiotColorPinkRed : kRiotColorGreen)]; + [self setMissedDiscussionsCount:recentsDataSource.missedGroupDiscussionsCount onTabBarItem:TABBAR_ROOMS_INDEX withBadgeColor:(recentsDataSource.missedHighlightGroupDiscussionsCount ? kRiotColorPinkRed : kRiotColorGreen)]; +} + +- (void)setMissedDiscussionsCount:(NSUInteger)count onTabBarItem:(NSUInteger)index withBadgeColor:(UIColor*)badgeColor { - NSUInteger count = [self missedDiscussionsCount]; if (count) { - NSString *badgeValue; + NSString *badgeValue = [self tabBarBadgeStringValue:count]; - if (count > 1000) - { - CGFloat value = count / 1000.0; - badgeValue = [NSString stringWithFormat:NSLocalizedStringFromTable(@"large_badge_value_k_format", @"Vector", nil), value]; - } - else - { - badgeValue = [NSString stringWithFormat:@"%tu", count]; - } - - self.tabBar.items[TABBAR_HOME_INDEX].badgeValue = badgeValue; + self.tabBar.items[index].badgeValue = badgeValue; if ([UITabBarItem instancesRespondToSelector:@selector(setBadgeColor:)]) { - if (self.missedHighlightDiscussionsCount) - { - self.tabBar.items[TABBAR_HOME_INDEX].badgeColor = kRiotColorPinkRed; - } - else - { - self.tabBar.items[TABBAR_HOME_INDEX].badgeColor = kRiotColorGreen; - } + self.tabBar.items[index].badgeColor = badgeColor; } } else { - self.tabBar.items[TABBAR_HOME_INDEX].badgeValue = nil; + self.tabBar.items[index].badgeValue = nil; } } -#pragma mark - +- (NSString*)tabBarBadgeStringValue:(NSUInteger)count +{ + NSString *badgeValue; + + if (count > 1000) + { + CGFloat value = count / 1000.0; + badgeValue = [NSString stringWithFormat:NSLocalizedStringFromTable(@"large_badge_value_k_format", @"Vector", nil), value]; + } + else + { + badgeValue = [NSString stringWithFormat:@"%tu", count]; + } + + return badgeValue; +} + +#pragma mark - - (void)promptUserBeforeUsingGoogleAnalytics { diff --git a/Riot/ViewController/RecentsViewController.m b/Riot/ViewController/RecentsViewController.m index d262e9143..504fc1258 100644 --- a/Riot/ViewController/RecentsViewController.m +++ b/Riot/ViewController/RecentsViewController.m @@ -282,6 +282,9 @@ - (void)refreshRecentsTable { + // Refresh the tabBar icon badges + [[AppDelegate theDelegate].masterTabBarController refreshTabBarBadges]; + // do not refresh if there is a pending recent drag and drop if (movingCellPath) {