diff --git a/src/addon/notifications/lang/en.json b/src/addon/notifications/lang/en.json index b1582bf1a..3844a5525 100644 --- a/src/addon/notifications/lang/en.json +++ b/src/addon/notifications/lang/en.json @@ -1,5 +1,6 @@ { "errorgetnotifications": "Error getting notifications.", + "markallread": "Mark all as read", "notificationpreferences": "Notification preferences", "notifications": "Notifications", "playsound": "Play sound", diff --git a/src/addon/notifications/pages/list/list.html b/src/addon/notifications/pages/list/list.html index ea479a8c8..b08d112f3 100644 --- a/src/addon/notifications/pages/list/list.html +++ b/src/addon/notifications/pages/list/list.html @@ -8,6 +8,12 @@ +
+ +
diff --git a/src/addon/notifications/pages/list/list.ts b/src/addon/notifications/pages/list/list.ts index 8bcf66f5d..e9634d095 100644 --- a/src/addon/notifications/pages/list/list.ts +++ b/src/addon/notifications/pages/list/list.ts @@ -36,6 +36,7 @@ export class AddonNotificationsListPage { notifications = []; notificationsLoaded = false; canLoadMore = false; + canMarkAllNotificationsAsRead = false; protected readCount = 0; protected unreadCount = 0; @@ -82,7 +83,7 @@ export class AddonNotificationsListPage { const limit = AddonNotificationsProvider.LIST_LIMIT; return this.notificationsProvider.getUnreadNotifications(this.unreadCount, limit).then((unread) => { - let promise; + const promises = []; unread.forEach(this.formatText.bind(this)); @@ -93,7 +94,7 @@ export class AddonNotificationsListPage { if (unread.length < limit) { // Limit not reached. Get read notifications until reach the limit. const readLimit = limit - unread.length; - promise = this.notificationsProvider.getReadNotifications(this.readCount, readLimit).then((read) => { + promises.push(this.notificationsProvider.getReadNotifications(this.readCount, readLimit).then((read) => { read.forEach(this.formatText.bind(this)); this.readCount += read.length; if (refresh) { @@ -107,9 +108,8 @@ export class AddonNotificationsListPage { this.domUtils.showErrorModalDefault(error, 'addon.notifications.errorgetnotifications', true); this.canLoadMore = false; // Set to false to prevent infinite calls with infinite-loading. } - }); + })); } else { - promise = Promise.resolve(); if (refresh) { this.notifications = unread; } else { @@ -118,7 +118,16 @@ export class AddonNotificationsListPage { this.canLoadMore = true; } - return promise.then(() => { + // Check if mark all notifications as read is enabled and there are some to read. + if (this.notificationsProvider.isMarkAllNotificationsAsReadEnabled()) { + promises.push(this.notificationsProvider.getUnreadNotificationsCount().then((unread) => { + this.canMarkAllNotificationsAsRead = unread > 0 + })); + } else { + this.canMarkAllNotificationsAsRead = false; + } + + return Promise.all(promises).then(() => { // Mark retrieved notifications as read if they are not. this.markNotificationsAsRead(unread); }); @@ -128,6 +137,22 @@ export class AddonNotificationsListPage { }); } + /** + * Mark all notifications as read. + */ + markAllNotificationsAsRead(): void { + this.notificationsProvider.markAllNotificationsAsRead().then(() => { + const siteId = this.sitesProvider.getCurrentSiteId(); + this.eventsProvider.trigger(AddonNotificationsProvider.READ_CHANGED_EVENT, null, siteId); + + this.notificationsProvider.getUnreadNotificationsCount().then((unread) => { + this.canMarkAllNotificationsAsRead = unread > 0; + }); + }).catch(() => { + // Omit failure. + }); + } + /** * Mark notifications as read. * @@ -139,7 +164,9 @@ export class AddonNotificationsListPage { return this.notificationsProvider.markNotificationRead(notification.id); }); - Promise.all(promises).finally(() => { + Promise.all(promises).catch(() => { + // Ignore errors. + }).finally(() => { this.notificationsProvider.invalidateNotificationsList().finally(() => { const siteId = this.sitesProvider.getCurrentSiteId(); this.eventsProvider.trigger(AddonNotificationsProvider.READ_CHANGED_EVENT, null, siteId); diff --git a/src/addon/notifications/providers/notifications.ts b/src/addon/notifications/providers/notifications.ts index 34c3a1819..d7a1bde1b 100644 --- a/src/addon/notifications/providers/notifications.ts +++ b/src/addon/notifications/providers/notifications.ts @@ -234,6 +234,20 @@ export class AddonNotificationsProvider { }); } + /** + * Mark all message notification as read. + * + * @returns {Promise} Resolved when done. + * @since 3.2 + */ + markAllNotificationsAsRead(): Promise { + const params = { + useridto: this.sitesProvider.getCurrentSiteUserId() + }; + return this.sitesProvider.getCurrentSite().write('core_message_mark_all_notifications_as_read', params); + } + + /** * Mark message notification as read. * @@ -273,6 +287,16 @@ export class AddonNotificationsProvider { }); } + /** + * Returns whether or not we can mark all notifications as read. + * + * @return {boolean} True if enabled, false otherwise. + * @since 3.2 + */ + isMarkAllNotificationsAsReadEnabled(): boolean { + return this.sitesProvider.wsAvailableInCurrentSite('core_message_mark_all_notifications_as_read'); + } + /** * Returns whether or not we can count unread notifications precisely. *