Add the badge value for Home tab bar item.

Fix the missed discussion count
This commit is contained in:
Giom Foret
2017-04-18 15:55:51 +02:00
parent b110517ec6
commit f8141d5c27
7 changed files with 132 additions and 42 deletions
+77 -12
View File
@@ -22,6 +22,8 @@
#import "AppDelegate.h"
#import "MXRoom+Riot.h"
@interface MasterTabBarController ()
{
// Array of `MXSession` instances.
@@ -45,8 +47,8 @@
// Current alert (if any).
MXKAlert *currentAlert;
// Observer kMXKRoomDataSourceMetaDataChanged to keep updated the missed discussion count
id kMXKRoomDataSourceMetaDataChangedObserver;
// Observer kMXRoomSummaryDidChangeNotification to keep updated the missed discussion count
id mxRoomSummaryDidChangeObserver;
}
@end
@@ -73,7 +75,6 @@
for (UITabBarItem *tabBarItem in self.tabBar.items)
{
tabBarItem.imageInsets = UIEdgeInsetsMake(5, 0, -5, 0);
tabBarItem.badgeColor = kRiotColorPinkRed;
}
// Initialize here the data sources if a matrix session has been already set.
@@ -99,7 +100,7 @@
}
// Observe missed notifications
kMXKRoomDataSourceMetaDataChangedObserver = [[NSNotificationCenter defaultCenter] addObserverForName:kMXKRoomDataSourceMetaDataChanged object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notif) {
mxRoomSummaryDidChangeObserver = [[NSNotificationCenter defaultCenter] addObserverForName:kMXRoomSummaryDidChangeNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notif) {
[self refreshHomeTabBadge];
@@ -118,10 +119,10 @@
{
[super viewDidDisappear:animated];
if (kMXKRoomDataSourceMetaDataChangedObserver)
if (mxRoomSummaryDidChangeObserver)
{
[[NSNotificationCenter defaultCenter] removeObserver:kMXKRoomDataSourceMetaDataChangedObserver];
kMXKRoomDataSourceMetaDataChangedObserver = nil;
[[NSNotificationCenter defaultCenter] removeObserver:mxRoomSummaryDidChangeObserver];
mxRoomSummaryDidChangeObserver = nil;
}
}
@@ -146,10 +147,10 @@
authViewControllerObserver = nil;
}
if (kMXKRoomDataSourceMetaDataChangedObserver)
if (mxRoomSummaryDidChangeObserver)
{
[[NSNotificationCenter defaultCenter] removeObserver:kMXKRoomDataSourceMetaDataChangedObserver];
kMXKRoomDataSourceMetaDataChangedObserver = nil;
[[NSNotificationCenter defaultCenter] removeObserver:mxRoomSummaryDidChangeObserver];
mxRoomSummaryDidChangeObserver = nil;
}
}
@@ -216,6 +217,9 @@
if (!mxSessionArray)
{
mxSessionArray = [NSMutableArray array];
// Add matrix sessions observer on first added session
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onMatrixSessionStateDidChange:) name:kMXSessionStateDidChangeNotification object:nil];
}
[mxSessionArray addObject:mxSession];
}
@@ -227,6 +231,9 @@
// Check whether there are others sessions
if (!recentsDataSource.mxSessions.count)
{
// Remove matrix sessions observer
[[NSNotificationCenter defaultCenter] removeObserver:self name:kMXSessionStateDidChangeNotification object:nil];
[_homeViewController displayList:nil];
[_favouritesViewController displayList:nil];
[_peopleViewController displayList:nil];
@@ -239,6 +246,11 @@
[mxSessionArray removeObject:mxSession];
}
- (void)onMatrixSessionStateDidChange:(NSNotification *)notif
{
[self refreshHomeTabBadge];
}
- (void)showAuthenticationScreen
{
// Check whether an authentication screen is not already shown or preparing
@@ -357,6 +369,50 @@
}
}
- (NSUInteger)missedDiscussionsCount
{
NSUInteger roomCount = 0;
// Considering all the current sessions.
for (MXSession *session in mxSessionArray)
{
// Sum all the rooms with missed notifications.
for (MXRoomSummary *roomSummary in session.roomsSummaries)
{
NSUInteger notificationCount = roomSummary.notificationCount;
// Ignore the regular notification count if the room is in 'mentions only" mode at the Riot level.
if (roomSummary.room.isMentionsOnly)
{
// Only the highlighted missed messages must be considered here.
notificationCount = roomSummary.highlightCount;
}
if (notificationCount)
{
roomCount ++;
}
}
// Add the invites count
roomCount += [session invitedRooms].count;
}
return roomCount;
}
- (NSUInteger)missedHighlightDiscussionsCount
{
NSUInteger roomCount = 0;
for (MXSession *session in mxSessionArray)
{
roomCount += [session missedHighlightDiscussionsCount];
}
return roomCount;
}
#pragma mark -
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
@@ -523,7 +579,7 @@
- (void)refreshHomeTabBadge
{
NSUInteger count = [MXKRoomDataSourceManager missedDiscussionsCount];
NSUInteger count = [self missedDiscussionsCount];
if (count)
{
NSString *badgeValue;
@@ -531,7 +587,7 @@
if (count > 1000)
{
CGFloat value = count / 1000.0;
badgeValue = [NSString stringWithFormat:@"%.1f k", value];
badgeValue = [NSString stringWithFormat:NSLocalizedStringFromTable(@"large_badge_value_k_format", @"Vector", nil), value];
}
else
{
@@ -539,6 +595,15 @@
}
self.tabBar.items[TABBAR_HOME_INDEX].badgeValue = badgeValue;
if (self.missedHighlightDiscussionsCount)
{
self.tabBar.items[TABBAR_HOME_INDEX].badgeColor = kRiotColorPinkRed;
}
else
{
self.tabBar.items[TABBAR_HOME_INDEX].badgeColor = kRiotColorGreen;
}
}
else
{