diff --git a/src/addons/messages/services/handlers/mainmenu.ts b/src/addons/messages/services/handlers/mainmenu.ts index ea20c90d6..5df64592f 100644 --- a/src/addons/messages/services/handlers/mainmenu.ts +++ b/src/addons/messages/services/handlers/mainmenu.ts @@ -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 { - siteId = siteId || CoreSites.getCurrentSiteId(); - if (!siteId) { + const badgeSiteId = siteId || CoreSites.getCurrentSiteId(); + + if (!badgeSiteId) { return; } const promises: Promise[] = []; - 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 {