diff --git a/src/addon/calendar/pages/edit-event/edit-event.ts b/src/addon/calendar/pages/edit-event/edit-event.ts index 93f8b3499..f2808c627 100644 --- a/src/addon/calendar/pages/edit-event/edit-event.ts +++ b/src/addon/calendar/pages/edit-event/edit-event.ts @@ -491,7 +491,13 @@ export class AddonCalendarEditEventPage implements OnInit, OnDestroy { const modal = this.domUtils.showModalLoading('core.sending', true); 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) => { this.domUtils.showErrorModalDefault(error, 'Error sending data.'); }).finally(() => { diff --git a/src/addon/calendar/pages/event/event.ts b/src/addon/calendar/pages/event/event.ts index 5789fb574..2d4811227 100644 --- a/src/addon/calendar/pages/event/event.ts +++ b/src/addon/calendar/pages/event/event.ts @@ -445,25 +445,28 @@ export class AddonCalendarEventPage implements OnDestroy { const modal = this.domUtils.showModalLoading('core.sending', true); 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. - this.eventsProvider.trigger(AddonCalendarProvider.DELETED_EVENT_EVENT, { - eventId: this.eventId, - sent: sent - }, this.sitesProvider.getCurrentSiteId()); + if (sent) { + this.domUtils.showToast('addon.calendar.eventcalendareventdeleted', true, 3000, undefined, false); - if (sent) { - this.domUtils.showToast('addon.calendar.eventcalendareventdeleted', true, 3000, undefined, false); - - // Event deleted, close the view. - if (!this.svComponent || !this.svComponent.isOn()) { - this.navCtrl.pop(); + // Event deleted, close the view. + if (!this.svComponent || !this.svComponent.isOn()) { + this.navCtrl.pop(); + } + } else { + // 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) => { this.domUtils.showErrorModalDefault(error, 'Error deleting event.'); }).finally(() => { diff --git a/src/addon/calendar/providers/calendar.ts b/src/addon/calendar/providers/calendar.ts index 17a93ab0a..9ecc4effd 100644 --- a/src/addon/calendar/providers/calendar.ts +++ b/src/addon/calendar/providers/calendar.ts @@ -1146,6 +1146,19 @@ export class AddonCalendarProvider { 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} Promise resolved with all the events. + */ + getLocalEventsByRepeatIdFromLocalDb(repeatId: number, siteId?: string): Promise { + return this.sitesProvider.getSite(siteId).then((site) => { + return site.getDb().getRecords(AddonCalendarProvider.EVENTS_TABLE, {repeatid: repeatId}); + }); + } + /** * Get monthly calendar events. * diff --git a/src/addon/calendar/providers/helper.ts b/src/addon/calendar/providers/helper.ts index 575500c16..1a83ac4fe 100644 --- a/src/addon/calendar/providers/helper.ts +++ b/src/addon/calendar/providers/helper.ts @@ -276,4 +276,67 @@ export class AddonCalendarHelperProvider { // 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; } + + /** + * 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} REsolved when done. + */ + invalidateRepeatedEventsOnCalendar(event: any, repeated: number, siteId?: string): Promise { + 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); + }) + ]); + }); + } }