MOBILE-2795 notifications: Handle duplication event detection

main
Pau Ferrer Ocaña 2019-01-09 15:44:25 +01:00
parent cb4f6c196a
commit 5128880d6d
2 changed files with 54 additions and 19 deletions

View File

@ -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<void>} Promise resolved when done.
*/
cleanExpiredEvents(siteId?: string): Promise<void> {
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<any[]> {
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);
});
}
/**

View File

@ -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);
});
});
}
});