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