MOBILE-3821 messages: Cache total badge count

main
Noel De Martin 2022-02-17 13:59:06 +01:00
parent bdd6e488b9
commit 84afd1ea4d
1 changed files with 18 additions and 7 deletions

View File

@ -54,18 +54,21 @@ export class AddonMessagesMainMenuHandlerService implements CoreMainMenuHandler,
protected unreadCount = 0;
protected contactRequestsCount = 0;
protected orMore = false;
protected badgeCount?: number;
constructor() {
CoreEvents.on(AddonMessagesProvider.UNREAD_CONVERSATION_COUNTS_EVENT, (data) => {
this.unreadCount = data.favourites + data.individual + data.group + data.self;
this.orMore = !!data.orMore;
this.updateBadge(data.siteId!);
data.siteId && this.updateBadge(data.siteId);
});
CoreEvents.on(AddonMessagesProvider.CONTACT_REQUESTS_COUNT_EVENT, (data) => {
this.contactRequestsCount = data.count;
this.updateBadge(data.siteId!);
data.siteId && this.updateBadge(data.siteId);
});
// Reset info on logout.
@ -123,27 +126,28 @@ export class AddonMessagesMainMenuHandlerService implements CoreMainMenuHandler,
* @return Resolve when done.
*/
async refreshBadge(siteId?: string, unreadOnly?: boolean): Promise<void> {
siteId = siteId || CoreSites.getCurrentSiteId();
if (!siteId) {
const badgeSiteId = siteId || CoreSites.getCurrentSiteId();
if (!badgeSiteId) {
return;
}
const promises: Promise<unknown>[] = [];
promises.push(AddonMessages.refreshUnreadConversationCounts(siteId).catch(() => {
promises.push(AddonMessages.refreshUnreadConversationCounts(badgeSiteId).catch(() => {
this.unreadCount = 0;
this.orMore = false;
}));
// Refresh the number of contact requests in 3.6+ sites.
if (!unreadOnly && AddonMessages.isGroupMessagingEnabled()) {
promises.push(AddonMessages.refreshContactRequestsCount(siteId).catch(() => {
promises.push(AddonMessages.refreshContactRequestsCount(badgeSiteId).catch(() => {
this.contactRequestsCount = 0;
}));
}
await Promise.all(promises).finally(() => {
this.updateBadge(siteId!);
this.updateBadge(badgeSiteId);
this.handler.loading = false;
});
}
@ -155,6 +159,13 @@ export class AddonMessagesMainMenuHandlerService implements CoreMainMenuHandler,
*/
updateBadge(siteId: string): void {
const totalCount = this.unreadCount + (this.contactRequestsCount || 0);
if (this.badgeCount === totalCount) {
return;
}
this.badgeCount = totalCount;
if (totalCount > 0) {
this.handler.badge = totalCount + (this.orMore ? '+' : '');
} else {