Merge pull request #2042 from crazyserver/MOBILE-3090

MOBILE-3090 calendar: Invalidate events when adding, editing or deleting
main
Juan Leyva 2019-08-06 10:49:13 +02:00 committed by GitHub
commit 0ac4ce4122
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 102 additions and 17 deletions

View File

@ -491,7 +491,13 @@ export class AddonCalendarEditEventPage implements OnInit, OnDestroy {
const modal = this.domUtils.showModalLoading('core.sending', true); const modal = this.domUtils.showModalLoading('core.sending', true);
this.calendarProvider.submitEvent(this.eventId, data).then((result) => { this.calendarProvider.submitEvent(this.eventId, data).then((result) => {
this.returnToList(result.event); const numberOfRepetitions = formData.repeat ? formData.repeats :
(data.repeateditall && this.event.othereventscount ? this.event.othereventscount + 1 : 1);
this.calendarHelper.invalidateRepeatedEventsOnCalendar(result.event, numberOfRepetitions).catch(() => {
// Ignore errors.
}).then(() => {
this.returnToList(result.event);
});
}).catch((error) => { }).catch((error) => {
this.domUtils.showErrorModalDefault(error, 'Error sending data.'); this.domUtils.showErrorModalDefault(error, 'Error sending data.');
}).finally(() => { }).finally(() => {

View File

@ -445,25 +445,28 @@ export class AddonCalendarEventPage implements OnDestroy {
const modal = this.domUtils.showModalLoading('core.sending', true); const modal = this.domUtils.showModalLoading('core.sending', true);
this.calendarProvider.deleteEvent(this.event.id, this.event.name, deleteAll).then((sent) => { this.calendarProvider.deleteEvent(this.event.id, this.event.name, deleteAll).then((sent) => {
this.calendarHelper.invalidateRepeatedEventsOnCalendar(this.event, deleteAll ? this.event.eventcount : 1)
.catch(() => {
// Ignore errors.
}).then(() => {
// Trigger an event.
this.eventsProvider.trigger(AddonCalendarProvider.DELETED_EVENT_EVENT, {
eventId: this.eventId,
sent: sent
}, this.sitesProvider.getCurrentSiteId());
// Trigger an event. if (sent) {
this.eventsProvider.trigger(AddonCalendarProvider.DELETED_EVENT_EVENT, { this.domUtils.showToast('addon.calendar.eventcalendareventdeleted', true, 3000, undefined, false);
eventId: this.eventId,
sent: sent
}, this.sitesProvider.getCurrentSiteId());
if (sent) { // Event deleted, close the view.
this.domUtils.showToast('addon.calendar.eventcalendareventdeleted', true, 3000, undefined, false); if (!this.svComponent || !this.svComponent.isOn()) {
this.navCtrl.pop();
// Event deleted, close the view. }
if (!this.svComponent || !this.svComponent.isOn()) { } else {
this.navCtrl.pop(); // Event deleted in offline, just mark it as deleted.
this.event.deleted = true;
} }
} else { });
// Event deleted in offline, just mark it as deleted.
this.event.deleted = true;
}
}).catch((error) => { }).catch((error) => {
this.domUtils.showErrorModalDefault(error, 'Error deleting event.'); this.domUtils.showErrorModalDefault(error, 'Error deleting event.');
}).finally(() => { }).finally(() => {

View File

@ -1146,6 +1146,19 @@ export class AddonCalendarProvider {
return this.getEventsListPrefixCacheKey() + daysToStart + ':' + daysInterval; return this.getEventsListPrefixCacheKey() + daysToStart + ':' + daysInterval;
} }
/**
* Get calendar events from local Db that have the same repeatid.
*
* @param {number} [repeatId] Repeat Id of the event.
* @param {string} [siteId] ID of the site the event belongs to. If not defined, use current site.
* @return {Promise<any[]>} Promise resolved with all the events.
*/
getLocalEventsByRepeatIdFromLocalDb(repeatId: number, siteId?: string): Promise<any[]> {
return this.sitesProvider.getSite(siteId).then((site) => {
return site.getDb().getRecords(AddonCalendarProvider.EVENTS_TABLE, {repeatid: repeatId});
});
}
/** /**
* Get monthly calendar events. * Get monthly calendar events.
* *

View File

@ -276,4 +276,67 @@ export class AddonCalendarHelperProvider {
// Show the event if it is from site home or if it matches the selected course. // Show the event if it is from site home or if it matches the selected course.
return event.courseid === this.sitesProvider.getSiteHomeId() || event.courseid == courseId; return event.courseid === this.sitesProvider.getSiteHomeId() || event.courseid == courseId;
} }
/**
* Invalidate all calls from calendar WS calls.
*
* @param {any} event Event that has been touched.
* @param {number} repeated Number of times the event is repeated.
* @param {string} [siteId] Site ID. If not defined, current site.
* @return {Promise<any>} REsolved when done.
*/
invalidateRepeatedEventsOnCalendar(event: any, repeated: number, siteId?: string): Promise<any> {
let invalidatePromise;
const timestarts = [];
if (repeated > 1) {
if (event.repeatid) {
// Being edited or deleted.
invalidatePromise = this.calendarProvider.getLocalEventsByRepeatIdFromLocalDb(event.repeatid, siteId)
.then((events) => {
return events.map((event) => {
timestarts.push(event.timestart);
return this.calendarProvider.invalidateEvent(event.id);
});
});
} else {
// Being added.
let time = event.timestart;
while (repeated > 0) {
timestarts.push(time);
time += CoreConstants.SECONDS_DAY * 7;
repeated--;
}
invalidatePromise = Promise.resolve();
}
} else {
// Not repeated.
timestarts.push(event.timestart);
invalidatePromise = this.calendarProvider.invalidateEvent(event.id);
}
return invalidatePromise.then(() => {
let lastMonth, lastYear;
return Promise.all([
this.calendarProvider.invalidateAllUpcomingEvents(),
timestarts.map((time) => {
const day = moment(new Date(time * 1000));
if (lastMonth && (lastMonth == day.month() + 1 && lastYear == day.year())) {
return Promise.resolve();
}
// Invalidate once.
lastMonth = day.month() + 1;
lastYear = day.year();
return this.calendarProvider.invalidateMonthlyEvents(lastYear, lastMonth, siteId);
})
]);
});
}
} }