MOBILE-2569 notifications: Add mark all as read button

main
Pau Ferrer Ocaña 2018-08-29 18:11:03 +02:00
parent b72cea5823
commit 59273eb9c1
4 changed files with 64 additions and 6 deletions

View File

@ -1,5 +1,6 @@
{ {
"errorgetnotifications": "Error getting notifications.", "errorgetnotifications": "Error getting notifications.",
"markallread": "Mark all as read",
"notificationpreferences": "Notification preferences", "notificationpreferences": "Notification preferences",
"notifications": "Notifications", "notifications": "Notifications",
"playsound": "Play sound", "playsound": "Play sound",

View File

@ -8,6 +8,12 @@
<ion-refresher-content pullingText="{{ 'core.pulltorefresh' | translate }}"></ion-refresher-content> <ion-refresher-content pullingText="{{ 'core.pulltorefresh' | translate }}"></ion-refresher-content>
</ion-refresher> </ion-refresher>
<core-loading [hideUntil]="notificationsLoaded"> <core-loading [hideUntil]="notificationsLoaded">
<div padding *ngIf="canMarkAllNotificationsAsRead">
<button ion-button block (click)="markAllNotificationsAsRead()" color="light" icon-start>
<core-icon name="fa-check"></core-icon>
{{ 'addon.notifications.markallread' | translate }}
</button>
</div>
<ion-card *ngFor="let notification of notifications"> <ion-card *ngFor="let notification of notifications">
<ion-item> <ion-item>
<ion-avatar item-start core-user-link [userId]="notification.useridfrom" [courseId]="notification.courseid"> <ion-avatar item-start core-user-link [userId]="notification.useridfrom" [courseId]="notification.courseid">

View File

@ -36,6 +36,7 @@ export class AddonNotificationsListPage {
notifications = []; notifications = [];
notificationsLoaded = false; notificationsLoaded = false;
canLoadMore = false; canLoadMore = false;
canMarkAllNotificationsAsRead = false;
protected readCount = 0; protected readCount = 0;
protected unreadCount = 0; protected unreadCount = 0;
@ -82,7 +83,7 @@ export class AddonNotificationsListPage {
const limit = AddonNotificationsProvider.LIST_LIMIT; const limit = AddonNotificationsProvider.LIST_LIMIT;
return this.notificationsProvider.getUnreadNotifications(this.unreadCount, limit).then((unread) => { return this.notificationsProvider.getUnreadNotifications(this.unreadCount, limit).then((unread) => {
let promise; const promises = [];
unread.forEach(this.formatText.bind(this)); unread.forEach(this.formatText.bind(this));
@ -93,7 +94,7 @@ export class AddonNotificationsListPage {
if (unread.length < limit) { if (unread.length < limit) {
// Limit not reached. Get read notifications until reach the limit. // Limit not reached. Get read notifications until reach the limit.
const readLimit = limit - unread.length; 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)); read.forEach(this.formatText.bind(this));
this.readCount += read.length; this.readCount += read.length;
if (refresh) { if (refresh) {
@ -107,9 +108,8 @@ export class AddonNotificationsListPage {
this.domUtils.showErrorModalDefault(error, 'addon.notifications.errorgetnotifications', true); this.domUtils.showErrorModalDefault(error, 'addon.notifications.errorgetnotifications', true);
this.canLoadMore = false; // Set to false to prevent infinite calls with infinite-loading. this.canLoadMore = false; // Set to false to prevent infinite calls with infinite-loading.
} }
}); }));
} else { } else {
promise = Promise.resolve();
if (refresh) { if (refresh) {
this.notifications = unread; this.notifications = unread;
} else { } else {
@ -118,7 +118,16 @@ export class AddonNotificationsListPage {
this.canLoadMore = true; 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. // Mark retrieved notifications as read if they are not.
this.markNotificationsAsRead(unread); 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. * Mark notifications as read.
* *
@ -139,7 +164,9 @@ export class AddonNotificationsListPage {
return this.notificationsProvider.markNotificationRead(notification.id); return this.notificationsProvider.markNotificationRead(notification.id);
}); });
Promise.all(promises).finally(() => { Promise.all(promises).catch(() => {
// Ignore errors.
}).finally(() => {
this.notificationsProvider.invalidateNotificationsList().finally(() => { this.notificationsProvider.invalidateNotificationsList().finally(() => {
const siteId = this.sitesProvider.getCurrentSiteId(); const siteId = this.sitesProvider.getCurrentSiteId();
this.eventsProvider.trigger(AddonNotificationsProvider.READ_CHANGED_EVENT, null, siteId); this.eventsProvider.trigger(AddonNotificationsProvider.READ_CHANGED_EVENT, null, siteId);

View File

@ -234,6 +234,20 @@ export class AddonNotificationsProvider {
}); });
} }
/**
* Mark all message notification as read.
*
* @returns {Promise<any>} Resolved when done.
* @since 3.2
*/
markAllNotificationsAsRead(): Promise<any> {
const params = {
useridto: this.sitesProvider.getCurrentSiteUserId()
};
return this.sitesProvider.getCurrentSite().write('core_message_mark_all_notifications_as_read', params);
}
/** /**
* Mark message notification as read. * 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. * Returns whether or not we can count unread notifications precisely.
* *