MOBILE-1927 calendar: Invalidate day when creating/editing an event
parent
184faa4a6d
commit
2b72b65ba5
|
@ -18,6 +18,7 @@ import { CoreSitesProvider } from '@providers/sites';
|
||||||
import { CoreCourseProvider } from '@core/course/providers/course';
|
import { CoreCourseProvider } from '@core/course/providers/course';
|
||||||
import { AddonCalendarProvider } from './calendar';
|
import { AddonCalendarProvider } from './calendar';
|
||||||
import { CoreConstants } from '@core/constants';
|
import { CoreConstants } from '@core/constants';
|
||||||
|
import { CoreUtilsProvider } from '@providers/utils/utils';
|
||||||
import * as moment from 'moment';
|
import * as moment from 'moment';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -38,7 +39,8 @@ export class AddonCalendarHelperProvider {
|
||||||
constructor(logger: CoreLoggerProvider,
|
constructor(logger: CoreLoggerProvider,
|
||||||
private courseProvider: CoreCourseProvider,
|
private courseProvider: CoreCourseProvider,
|
||||||
private sitesProvider: CoreSitesProvider,
|
private sitesProvider: CoreSitesProvider,
|
||||||
private calendarProvider: AddonCalendarProvider) {
|
private calendarProvider: AddonCalendarProvider,
|
||||||
|
private utils: CoreUtilsProvider) {
|
||||||
this.logger = logger.getInstance('AddonCalendarHelperProvider');
|
this.logger = logger.getInstance('AddonCalendarHelperProvider');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -286,57 +288,67 @@ export class AddonCalendarHelperProvider {
|
||||||
* @return {Promise<any>} REsolved when done.
|
* @return {Promise<any>} REsolved when done.
|
||||||
*/
|
*/
|
||||||
invalidateRepeatedEventsOnCalendar(event: any, repeated: number, siteId?: string): Promise<any> {
|
invalidateRepeatedEventsOnCalendar(event: any, repeated: number, siteId?: string): Promise<any> {
|
||||||
let invalidatePromise;
|
return this.sitesProvider.getSite(siteId).then((site) => {
|
||||||
const timestarts = [];
|
let invalidatePromise;
|
||||||
|
const timestarts = [];
|
||||||
|
|
||||||
if (repeated > 1) {
|
if (repeated > 1) {
|
||||||
if (event.repeatid) {
|
if (event.repeatid) {
|
||||||
// Being edited or deleted.
|
// Being edited or deleted.
|
||||||
invalidatePromise = this.calendarProvider.getLocalEventsByRepeatIdFromLocalDb(event.repeatid, siteId)
|
invalidatePromise = this.calendarProvider.getLocalEventsByRepeatIdFromLocalDb(event.repeatid, site.id)
|
||||||
.then((events) => {
|
.then((events) => {
|
||||||
return events.map((event) => {
|
return this.utils.allPromises(events.map((event) => {
|
||||||
timestarts.push(event.timestart);
|
timestarts.push(event.timestart);
|
||||||
|
|
||||||
return this.calendarProvider.invalidateEvent(event.id);
|
return this.calendarProvider.invalidateEvent(event.id);
|
||||||
|
}));
|
||||||
});
|
});
|
||||||
|
} else {
|
||||||
});
|
// Being added.
|
||||||
} else {
|
let time = event.timestart;
|
||||||
// Being added.
|
while (repeated > 0) {
|
||||||
let time = event.timestart;
|
timestarts.push(time);
|
||||||
while (repeated > 0) {
|
time += CoreConstants.SECONDS_DAY * 7;
|
||||||
timestarts.push(time);
|
repeated--;
|
||||||
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.
|
invalidatePromise = Promise.resolve();
|
||||||
lastMonth = day.month() + 1;
|
}
|
||||||
lastYear = day.year();
|
} else {
|
||||||
|
// Not repeated.
|
||||||
|
timestarts.push(event.timestart);
|
||||||
|
invalidatePromise = this.calendarProvider.invalidateEvent(event.id);
|
||||||
|
}
|
||||||
|
|
||||||
return this.calendarProvider.invalidateMonthlyEvents(lastYear, lastMonth, siteId);
|
return invalidatePromise.finally(() => {
|
||||||
})
|
let lastMonth, lastYear;
|
||||||
]);
|
|
||||||
|
return this.utils.allPromises([
|
||||||
|
this.calendarProvider.invalidateAllUpcomingEvents(),
|
||||||
|
|
||||||
|
// Invalidate months.
|
||||||
|
this.utils.allPromises(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, site.id);
|
||||||
|
})),
|
||||||
|
|
||||||
|
// Invalidate days.
|
||||||
|
this.utils.allPromises(timestarts.map((time) => {
|
||||||
|
const day = moment(new Date(time * 1000));
|
||||||
|
|
||||||
|
return this.calendarProvider.invalidateDayEvents(day.year(), day.month() + 1, day.date(), site.id);
|
||||||
|
})),
|
||||||
|
]);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue