MOBILE-2620 messages: Contact requests count in main menu handler
This commit is contained in:
		
							parent
							
								
									1cefb4ae04
								
							
						
					
					
						commit
						d3459dc078
					
				| @ -43,6 +43,8 @@ export class AddonMessagesMainMenuHandler implements CoreMainMenuHandler, CoreCr | |||||||
|         loading: true |         loading: true | ||||||
|     }; |     }; | ||||||
| 
 | 
 | ||||||
|  |     protected updating = false; | ||||||
|  | 
 | ||||||
|     constructor(private messagesProvider: AddonMessagesProvider, private sitesProvider: CoreSitesProvider, |     constructor(private messagesProvider: AddonMessagesProvider, private sitesProvider: CoreSitesProvider, | ||||||
|             private eventsProvider: CoreEventsProvider, private appProvider: CoreAppProvider, |             private eventsProvider: CoreEventsProvider, private appProvider: CoreAppProvider, | ||||||
|             private localNotificationsProvider: CoreLocalNotificationsProvider, private textUtils: CoreTextUtilsProvider, |             private localNotificationsProvider: CoreLocalNotificationsProvider, private textUtils: CoreTextUtilsProvider, | ||||||
| @ -57,10 +59,15 @@ export class AddonMessagesMainMenuHandler implements CoreMainMenuHandler, CoreCr | |||||||
|             this.updateBadge(data.siteId); |             this.updateBadge(data.siteId); | ||||||
|         }); |         }); | ||||||
| 
 | 
 | ||||||
|  |         eventsProvider.on(AddonMessagesProvider.CONTACT_REQUESTS_COUNT_EVENT, (data) => { | ||||||
|  |             this.updateBadge(data.siteId, data.count); | ||||||
|  |         }); | ||||||
|  | 
 | ||||||
|         // Reset info on logout.
 |         // Reset info on logout.
 | ||||||
|         eventsProvider.on(CoreEventsProvider.LOGOUT, (data) => { |         eventsProvider.on(CoreEventsProvider.LOGOUT, (data) => { | ||||||
|             this.handler.badge = ''; |             this.handler.badge = ''; | ||||||
|             this.handler.loading = true; |             this.handler.loading = true; | ||||||
|  |             this.updating = false; | ||||||
|         }); |         }); | ||||||
| 
 | 
 | ||||||
|         // If a message push notification is received, refresh the count.
 |         // If a message push notification is received, refresh the count.
 | ||||||
| @ -103,23 +110,55 @@ export class AddonMessagesMainMenuHandler implements CoreMainMenuHandler, CoreCr | |||||||
|     /** |     /** | ||||||
|      * Triggers an update for the badge number and loading status. Mandatory if showBadge is enabled. |      * Triggers an update for the badge number and loading status. Mandatory if showBadge is enabled. | ||||||
|      * |      * | ||||||
|      * @param {string} siteId Site ID or current Site if undefined. |      * @param {string} [siteId] Site ID or current Site if undefined. | ||||||
|  |      * @param {number} [contactRequestsCount] Number of contact requests, if known. | ||||||
|      */ |      */ | ||||||
|     updateBadge(siteId?: string): void { |     updateBadge(siteId?: string, contactRequestsCount?: number): void { | ||||||
|         siteId = siteId || this.sitesProvider.getCurrentSiteId(); |         siteId = siteId || this.sitesProvider.getCurrentSiteId(); | ||||||
|         if (!siteId) { |         if (!siteId) { | ||||||
|             return; |             return; | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         this.messagesProvider.getUnreadConversationsCount(undefined, siteId).then((unread) => { |         if (this.updating) { | ||||||
|             // Leave badge enter if there is a 0+ or a 0.
 |             // An update is already in prgoress.
 | ||||||
|             this.handler.badge = parseInt(unread, 10) > 0 ? unread : ''; |             return; | ||||||
|             // Update badge.
 |         } | ||||||
|             this.pushNotificationsProvider.updateAddonCounter('AddonMessages', unread, siteId); | 
 | ||||||
|  |         this.updating = true; | ||||||
|  | 
 | ||||||
|  |         const promises = []; | ||||||
|  |         let unreadCount = 0; | ||||||
|  |         let unreadPlus = false; | ||||||
|  | 
 | ||||||
|  |         promises.push(this.messagesProvider.getUnreadConversationsCount(undefined, siteId).then((unread) => { | ||||||
|  |             unreadCount = parseInt(unread, 10); | ||||||
|  |             unreadPlus = (typeof unread === 'string' && unread.slice(-1) === '+'); | ||||||
|         }).catch(() => { |         }).catch(() => { | ||||||
|             this.handler.badge = ''; |             // Ignore error.
 | ||||||
|  |         })); | ||||||
|  | 
 | ||||||
|  |         // Get the number of contact requests in 3.6+ sites if needed.
 | ||||||
|  |         if (contactRequestsCount == null && this.messagesProvider.isGroupMessagingEnabled()) { | ||||||
|  |             promises.push(this.messagesProvider.getContactRequestsCount(siteId).then((count) => { | ||||||
|  |                 contactRequestsCount = count; | ||||||
|  |             }).catch(() => { | ||||||
|  |                 // Ignore errors
 | ||||||
|  |             })); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         Promise.all(promises).then(() => { | ||||||
|  |             const totalCount = unreadCount + (contactRequestsCount || 0); | ||||||
|  |             if (totalCount > 0) { | ||||||
|  |                 this.handler.badge = totalCount + (unreadPlus ? '+' : ''); | ||||||
|  |             } else { | ||||||
|  |                 this.handler.badge = ''; | ||||||
|  |             } | ||||||
|  | 
 | ||||||
|  |             // Update badge.
 | ||||||
|  |             this.pushNotificationsProvider.updateAddonCounter('AddonMessages', totalCount, siteId); | ||||||
|         }).finally(() => { |         }).finally(() => { | ||||||
|             this.handler.loading = false; |             this.handler.loading = false; | ||||||
|  |             this.updating = false; | ||||||
|         }); |         }); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user