Merge pull request #1993 from dpalou/MOBILE-3067

MOBILE-3067 notifications: Fix mark read when it shouldn't
main
Juan Leyva 2019-07-18 12:25:49 +02:00 committed by GitHub
commit 38ae11b8fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 39 additions and 9 deletions

View File

@ -41,8 +41,10 @@ export class AddonNotificationsListPage {
canMarkAllNotificationsAsRead = false;
loadingMarkAllNotificationsAsRead = false;
protected isCurrentView: boolean;
protected cronObserver: CoreEventObserver;
protected pushObserver: Subscription;
protected pendingRefresh = false;
constructor(navParams: NavParams, private domUtils: CoreDomUtilsProvider, private eventsProvider: CoreEventsProvider,
private sitesProvider: CoreSitesProvider, private textUtils: CoreTextUtilsProvider,
@ -55,17 +57,24 @@ export class AddonNotificationsListPage {
* View loaded.
*/
ionViewDidLoad(): void {
this.fetchNotifications().finally(() => {
this.notificationsLoaded = true;
});
this.fetchNotifications();
this.cronObserver = this.eventsProvider.on(AddonNotificationsProvider.READ_CRON_EVENT, () => this.refreshNotifications(),
this.sitesProvider.getCurrentSiteId());
this.cronObserver = this.eventsProvider.on(AddonNotificationsProvider.READ_CRON_EVENT, () => {
if (this.isCurrentView) {
this.notificationsLoaded = false;
this.refreshNotifications();
}
}, this.sitesProvider.getCurrentSiteId());
this.pushObserver = this.pushNotificationsDelegate.on('receive').subscribe((notification) => {
// New notification received. If it's from current site, refresh the data.
if (this.utils.isTrueOrOne(notification.notif) && this.sitesProvider.isCurrentSite(notification.site)) {
if (this.isCurrentView && this.utils.isTrueOrOne(notification.notif) &&
this.sitesProvider.isCurrentSite(notification.site)) {
this.notificationsLoaded = false;
this.refreshNotifications();
} else if (!this.isCurrentView) {
this.pendingRefresh = true;
}
});
}
@ -93,6 +102,8 @@ export class AddonNotificationsListPage {
}).catch((error) => {
this.domUtils.showErrorModalDefault(error, 'addon.notifications.errorgetnotifications', true);
this.loadMoreError = true; // Set to prevent infinite calls with infinite-loading.
}).finally(() => {
this.notificationsLoaded = true;
});
}
@ -110,9 +121,7 @@ export class AddonNotificationsListPage {
// All marked as read, refresh the list.
this.notificationsLoaded = false;
return this.refreshNotifications().finally(() => {
this.notificationsLoaded = true;
});
return this.refreshNotifications();
});
}
@ -198,6 +207,27 @@ export class AddonNotificationsListPage {
notification.mobiletext = this.textUtils.replaceNewLines(text, '<br>');
}
/**
* User entered the page.
*/
ionViewDidEnter(): void {
this.isCurrentView = true;
if (this.pendingRefresh) {
this.pendingRefresh = false;
this.notificationsLoaded = false;
this.refreshNotifications();
}
}
/**
* User left the page.
*/
ionViewDidLeave(): void {
this.isCurrentView = false;
}
/**
* Page destroyed.
*/