MOBILE-3087 calendar: Fix 'lost' events when loading more events
When loading more events, it could happen that some events weren't displayed because the timestart was recalculated using the time the request was made. E.g. if I loaded the first events and, 2 minutes later, I loaded more events, there were 2 minutes where we didn't get events.main
parent
e740fe4205
commit
089c56b56b
|
@ -21,6 +21,7 @@ import { AddonCalendarHelperProvider } from '../../providers/helper';
|
||||||
import { AddonCalendarSyncProvider } from '../../providers/calendar-sync';
|
import { AddonCalendarSyncProvider } from '../../providers/calendar-sync';
|
||||||
import { CoreCoursesProvider } from '@core/courses/providers/courses';
|
import { CoreCoursesProvider } from '@core/courses/providers/courses';
|
||||||
import { CoreDomUtilsProvider } from '@providers/utils/dom';
|
import { CoreDomUtilsProvider } from '@providers/utils/dom';
|
||||||
|
import { CoreTimeUtilsProvider } from '@providers/utils/time';
|
||||||
import { CoreUtilsProvider } from '@providers/utils/utils';
|
import { CoreUtilsProvider } from '@providers/utils/utils';
|
||||||
import { CoreSitesProvider } from '@providers/sites';
|
import { CoreSitesProvider } from '@providers/sites';
|
||||||
import { CoreLocalNotificationsProvider } from '@providers/local-notifications';
|
import { CoreLocalNotificationsProvider } from '@providers/local-notifications';
|
||||||
|
@ -43,6 +44,7 @@ export class AddonCalendarListPage implements OnDestroy {
|
||||||
@ViewChild(Content) content: Content;
|
@ViewChild(Content) content: Content;
|
||||||
@ViewChild(CoreSplitViewComponent) splitviewCtrl: CoreSplitViewComponent;
|
@ViewChild(CoreSplitViewComponent) splitviewCtrl: CoreSplitViewComponent;
|
||||||
|
|
||||||
|
protected initialTime = 0;
|
||||||
protected daysLoaded = 0;
|
protected daysLoaded = 0;
|
||||||
protected emptyEventsTimes = 0; // Variable to identify consecutive calls returning 0 events.
|
protected emptyEventsTimes = 0; // Variable to identify consecutive calls returning 0 events.
|
||||||
protected categoriesRetrieved = false;
|
protected categoriesRetrieved = false;
|
||||||
|
@ -87,7 +89,7 @@ export class AddonCalendarListPage implements OnDestroy {
|
||||||
localNotificationsProvider: CoreLocalNotificationsProvider, private popoverCtrl: PopoverController,
|
localNotificationsProvider: CoreLocalNotificationsProvider, private popoverCtrl: PopoverController,
|
||||||
private eventsProvider: CoreEventsProvider, private navCtrl: NavController, private appProvider: CoreAppProvider,
|
private eventsProvider: CoreEventsProvider, private navCtrl: NavController, private appProvider: CoreAppProvider,
|
||||||
private calendarOffline: AddonCalendarOfflineProvider, private calendarSync: AddonCalendarSyncProvider,
|
private calendarOffline: AddonCalendarOfflineProvider, private calendarSync: AddonCalendarSyncProvider,
|
||||||
network: Network) {
|
network: Network, private timeUtils: CoreTimeUtilsProvider) {
|
||||||
|
|
||||||
this.siteHomeId = sitesProvider.getCurrentSite().getSiteHomeId();
|
this.siteHomeId = sitesProvider.getCurrentSite().getSiteHomeId();
|
||||||
this.notificationsEnabled = localNotificationsProvider.isAvailable();
|
this.notificationsEnabled = localNotificationsProvider.isAvailable();
|
||||||
|
@ -192,6 +194,7 @@ export class AddonCalendarListPage implements OnDestroy {
|
||||||
* @return {Promise<any>} Promise resolved when done.
|
* @return {Promise<any>} Promise resolved when done.
|
||||||
*/
|
*/
|
||||||
fetchData(refresh?: boolean, sync?: boolean, showErrors?: boolean): Promise<any> {
|
fetchData(refresh?: boolean, sync?: boolean, showErrors?: boolean): Promise<any> {
|
||||||
|
this.initialTime = this.timeUtils.timestamp();
|
||||||
this.daysLoaded = 0;
|
this.daysLoaded = 0;
|
||||||
this.emptyEventsTimes = 0;
|
this.emptyEventsTimes = 0;
|
||||||
this.isOnline = this.appProvider.isOnline();
|
this.isOnline = this.appProvider.isOnline();
|
||||||
|
@ -269,7 +272,9 @@ export class AddonCalendarListPage implements OnDestroy {
|
||||||
fetchEvents(refresh?: boolean): Promise<any> {
|
fetchEvents(refresh?: boolean): Promise<any> {
|
||||||
this.loadMoreError = false;
|
this.loadMoreError = false;
|
||||||
|
|
||||||
return this.calendarProvider.getEventsList(this.daysLoaded, AddonCalendarProvider.DAYS_INTERVAL).then((events) => {
|
return this.calendarProvider.getEventsList(this.initialTime, this.daysLoaded, AddonCalendarProvider.DAYS_INTERVAL)
|
||||||
|
.then((events) => {
|
||||||
|
|
||||||
this.daysLoaded += AddonCalendarProvider.DAYS_INTERVAL;
|
this.daysLoaded += AddonCalendarProvider.DAYS_INTERVAL;
|
||||||
if (events.length === 0) {
|
if (events.length === 0) {
|
||||||
this.emptyEventsTimes++;
|
this.emptyEventsTimes++;
|
||||||
|
|
|
@ -537,16 +537,20 @@ export class AddonCalendarProvider {
|
||||||
* Get the events in a certain period. The period is calculated like this:
|
* Get the events in a certain period. The period is calculated like this:
|
||||||
* start time: now + daysToStart
|
* start time: now + daysToStart
|
||||||
* end time: start time + daysInterval
|
* end time: start time + daysInterval
|
||||||
* E.g. using provider.getEventsList(30, 30) is going to get the events starting after 30 days from now
|
* E.g. using provider.getEventsList(undefined, 30, 30) is going to get the events starting after 30 days from now
|
||||||
* and ending before 60 days from now.
|
* and ending before 60 days from now.
|
||||||
*
|
*
|
||||||
* @param {number} [daysToStart=0] Number of days from now to start getting events.
|
* @param {number} [initialTime] Timestamp when the first fetch was done. If not defined, current time.
|
||||||
|
* @param {number} [daysToStart=0] Number of days from now to start getting events.
|
||||||
* @param {number} [daysInterval=30] Number of days between timestart and timeend.
|
* @param {number} [daysInterval=30] Number of days between timestart and timeend.
|
||||||
* @param {string} [siteId] Site to get the events from. If not defined, use current site.
|
* @param {string} [siteId] Site to get the events from. If not defined, use current site.
|
||||||
* @return {Promise<any[]>} Promise to be resolved when the participants are retrieved.
|
* @return {Promise<any[]>} Promise to be resolved when the participants are retrieved.
|
||||||
*/
|
*/
|
||||||
getEventsList(daysToStart: number = 0, daysInterval: number = AddonCalendarProvider.DAYS_INTERVAL, siteId?: string)
|
getEventsList(initialTime?: number, daysToStart: number = 0, daysInterval: number = AddonCalendarProvider.DAYS_INTERVAL,
|
||||||
: Promise<any[]> {
|
siteId?: string): Promise<any[]> {
|
||||||
|
|
||||||
|
initialTime = initialTime || this.timeUtils.timestamp();
|
||||||
|
|
||||||
return this.sitesProvider.getSite(siteId).then((site) => {
|
return this.sitesProvider.getSite(siteId).then((site) => {
|
||||||
siteId = site.getId();
|
siteId = site.getId();
|
||||||
const promises = [];
|
const promises = [];
|
||||||
|
@ -561,9 +565,8 @@ export class AddonCalendarProvider {
|
||||||
}));
|
}));
|
||||||
|
|
||||||
return Promise.all(promises).then(() => {
|
return Promise.all(promises).then(() => {
|
||||||
const now = this.timeUtils.timestamp(),
|
const start = initialTime + (CoreConstants.SECONDS_DAY * daysToStart),
|
||||||
start = now + (CoreConstants.SECONDS_DAY * daysToStart),
|
end = start + (CoreConstants.SECONDS_DAY * daysInterval) - 1,
|
||||||
end = start + (CoreConstants.SECONDS_DAY * daysInterval),
|
|
||||||
data = {
|
data = {
|
||||||
options: {
|
options: {
|
||||||
userevents: 1,
|
userevents: 1,
|
||||||
|
@ -733,7 +736,7 @@ export class AddonCalendarProvider {
|
||||||
return this.isDisabled(siteId).then((disabled) => {
|
return this.isDisabled(siteId).then((disabled) => {
|
||||||
if (!disabled) {
|
if (!disabled) {
|
||||||
// Get first events.
|
// Get first events.
|
||||||
return this.getEventsList(undefined, undefined, siteId).then((events) => {
|
return this.getEventsList(undefined, undefined, undefined, siteId).then((events) => {
|
||||||
return this.scheduleEventsNotifications(events, siteId);
|
return this.scheduleEventsNotifications(events, siteId);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue