From 5128880d6d6f3afa774c35eaca6e6d9fcda47199 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Wed, 9 Jan 2019 15:44:25 +0100 Subject: [PATCH] MOBILE-2795 notifications: Handle duplication event detection --- src/addon/calendar/providers/calendar.ts | 69 +++++++++++++++++------- src/providers/local-notifications.ts | 4 +- 2 files changed, 54 insertions(+), 19 deletions(-) diff --git a/src/addon/calendar/providers/calendar.ts b/src/addon/calendar/providers/calendar.ts index af29d4f7d..3aadd52e0 100644 --- a/src/addon/calendar/providers/calendar.ts +++ b/src/addon/calendar/providers/calendar.ts @@ -138,6 +138,37 @@ export class AddonCalendarProvider { this.sitesProvider.createTablesFromSchema(this.tablesSchema); } + /** + * Removes expired events from local DB. + * + * @param {string} [siteId] ID of the site the event belongs to. If not defined, use current site. + * @return {Promise} Promise resolved when done. + */ + cleanExpiredEvents(siteId?: string): Promise { + return this.sitesProvider.getSite(siteId).then((site) => { + let promise; + + // Cancel expired events notifications first. + if (this.localNotificationsProvider.isAvailable()) { + promise = site.getDb().getRecordsSelect(AddonCalendarProvider.EVENTS_TABLE, 'timestart < ?', + [this.timeUtils.timestamp()]).then((events) => { + events.forEach((event) => { + return this.localNotificationsProvider.cancel(event.id, AddonCalendarProvider.COMPONENT, site.getId()); + }); + }).catch(() => { + // Ignore errors. + }); + } else { + promise = Promise.resolve(); + } + + return promise.then(() => { + return site.getDb().deleteRecordsSelect(AddonCalendarProvider.EVENTS_TABLE, 'timestart < ?', + [this.timeUtils.timestamp()]); + }); + }); + } + /** * Get all calendar events from local Db. * @@ -442,27 +473,29 @@ export class AddonCalendarProvider { * @return {Promise} Promise resolved when all the notifications have been scheduled. */ scheduleAllSitesEventsNotifications(): Promise { - if (this.localNotificationsProvider.isAvailable()) { - return this.sitesProvider.getSitesIds().then((siteIds) => { - const promises = []; + const notificationsEnabled = this.localNotificationsProvider.isAvailable(); - siteIds.forEach((siteId) => { - // Check if calendar is disabled for the site. - promises.push(this.isDisabled(siteId).then((disabled) => { - if (!disabled) { - // Get first events. - return this.getEventsList(undefined, undefined, siteId).then((events) => { - return this.scheduleEventsNotifications(events, siteId); - }); - } - })); - }); + return this.sitesProvider.getSitesIds().then((siteIds) => { + const promises = []; - return Promise.all(promises); + siteIds.forEach((siteId) => { + promises.push(this.cleanExpiredEvents(siteId).then(() => { + if (notificationsEnabled) { + // Check if calendar is disabled for the site. + return this.isDisabled(siteId).then((disabled) => { + if (!disabled) { + // Get first events. + return this.getEventsList(undefined, undefined, siteId).then((events) => { + return this.scheduleEventsNotifications(events, siteId); + }); + } + }); + } + })); }); - } else { - return Promise.resolve([]); - } + + return Promise.all(promises); + }); } /** diff --git a/src/providers/local-notifications.ts b/src/providers/local-notifications.ts index 25e51da9e..8f6d66c60 100644 --- a/src/providers/local-notifications.ts +++ b/src/providers/local-notifications.ts @@ -489,7 +489,9 @@ export class CoreLocalNotificationsProvider { // Remove from triggered, since the notification could be in there with a different time. this.removeTriggered(notification.id); - this.localNotifications.schedule(notification); + this.localNotifications.cancel(notification.id).finally(() => { + this.localNotifications.schedule(notification); + }); }); } });