MOBILE-2428 update: Implement calendar update task

main
Dani Palou 2018-06-05 08:11:40 +02:00
parent eb36ee048c
commit b19a1f8fe1
3 changed files with 56 additions and 23 deletions

View File

@ -31,9 +31,9 @@ export class AddonCalendarProvider {
static DAYS_INTERVAL = 30;
static COMPONENT = 'AddonCalendarEvents';
static DEFAULT_NOTIFICATION_TIME_CHANGED = 'AddonCalendarDefaultNotificationTimeChangedEvent';
protected DEFAULT_NOTIFICATION_TIME_SETTING = 'mmaCalendarDefaultNotifTime';
static DEFAULT_NOTIFICATION_TIME_SETTING = 'mmaCalendarDefaultNotifTime';
static DEFAULT_NOTIFICATION_TIME = 60;
protected ROOT_CACHE_KEY = 'mmaCalendar:';
protected DEFAULT_NOTIFICATION_TIME = 60;
// Variables for database.
static EVENTS_TABLE = 'addon_calendar_events';
@ -136,6 +136,18 @@ export class AddonCalendarProvider {
this.sitesProvider.createTablesFromSchema(this.tablesSchema);
}
/**
* Get all calendar events from local Db.
*
* @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.
*/
getAllEventsFromLocalDb(siteId?: string): Promise<any[]> {
return this.sitesProvider.getSite(siteId).then((site) => {
return site.getDb().getAllRecords(AddonCalendarProvider.EVENTS_TABLE);
});
}
/**
* Get the configured default notification time.
*
@ -145,9 +157,9 @@ export class AddonCalendarProvider {
getDefaultNotificationTime(siteId?: string): Promise<number> {
siteId = siteId || this.sitesProvider.getCurrentSiteId();
const key = this.DEFAULT_NOTIFICATION_TIME_SETTING + '#' + siteId;
const key = AddonCalendarProvider.DEFAULT_NOTIFICATION_TIME_SETTING + '#' + siteId;
return this.configProvider.get(key, this.DEFAULT_NOTIFICATION_TIME);
return this.configProvider.get(key, AddonCalendarProvider.DEFAULT_NOTIFICATION_TIME);
}
/**
@ -496,11 +508,24 @@ export class AddonCalendarProvider {
setDefaultNotificationTime(time: number, siteId?: string): Promise<any[]> {
siteId = siteId || this.sitesProvider.getCurrentSiteId();
const key = this.DEFAULT_NOTIFICATION_TIME_SETTING + '#' + siteId;
const key = AddonCalendarProvider.DEFAULT_NOTIFICATION_TIME_SETTING + '#' + siteId;
return this.configProvider.set(key, time);
}
/**
* Store an event in local DB as it is.
*
* @param {any} event Event to store.
* @param {string} [siteId] ID of the site the event belongs to. If not defined, use current site.
* @return {Promise<any>} Promise resolved when stored.
*/
storeEventInLocalDb(event: any, siteId?: string): Promise<any> {
return this.sitesProvider.getSite(siteId).then((site) => {
return site.getDb().insertRecord(AddonCalendarProvider.EVENTS_TABLE, event);
});
}
/**
* Store events in local DB.
*

View File

@ -41,7 +41,6 @@ import { NavController, Nav } from 'ionic-angular';
templateUrl: 'core-split-view.html'
})
export class CoreSplitViewComponent implements OnInit {
// @todo Mix both panels header buttons
@ViewChild('detailNav') detailNav: Nav;
@Input() when?: string | boolean = 'md';

View File

@ -22,6 +22,7 @@ import { CoreLoggerProvider } from './logger';
import { CoreSitesProvider } from './sites';
import { CoreUtilsProvider } from './utils/utils';
import { CoreConfigConstants } from '../configconstants';
import { AddonCalendarProvider } from '@addon/calendar/providers/calendar';
import { SQLiteDB } from '@classes/sqlitedb';
/**
@ -323,7 +324,8 @@ export class CoreUpdateManagerProvider implements CoreInitHandler {
constructor(logger: CoreLoggerProvider, private configProvider: CoreConfigProvider, private sitesProvider: CoreSitesProvider,
private filepoolProvider: CoreFilepoolProvider, private notifProvider: CoreLocalNotificationsProvider,
private utils: CoreUtilsProvider, private appProvider: CoreAppProvider) {
private utils: CoreUtilsProvider, private appProvider: CoreAppProvider,
private calendarProvider: AddonCalendarProvider) {
this.logger = logger.getInstance('CoreUpdateManagerProvider');
}
@ -358,9 +360,8 @@ export class CoreUpdateManagerProvider implements CoreInitHandler {
promises.push(this.setSitesConfig());
}
if (versionCode >= 2018 && versionApplied < 2018 && versionApplied > 0) {
promises.push(this.adaptForumOfflineStores());
}
// In version 2018 we adapted the forum offline stores to match a new schema.
// However, due to the migration of data to SQLite we can no longer do that.
return Promise.all(promises).then(() => {
return this.configProvider.set(this.VERSION_APPLIED, versionCode);
@ -567,8 +568,27 @@ export class CoreUpdateManagerProvider implements CoreInitHandler {
return Promise.resolve();
}
// @todo: Implement it once Calendar addon is implemented.
return Promise.resolve();
return this.sitesProvider.getSitesIds().then((siteIds) => {
const promises = [];
siteIds.forEach((siteId) => {
// Get stored events.
promises.push(this.calendarProvider.getAllEventsFromLocalDb(siteId).then((events) => {
const eventPromises = [];
events.forEach((event) => {
if (event.notificationtime == AddonCalendarProvider.DEFAULT_NOTIFICATION_TIME) {
event.notificationtime = -1;
eventPromises.push(this.calendarProvider.storeEventInLocalDb(event, siteId));
}
});
return Promise.all(eventPromises);
}));
});
return Promise.all(promises);
});
}
/**
@ -626,15 +646,4 @@ export class CoreUpdateManagerProvider implements CoreInitHandler {
});
});
}
/**
* The data stored for offline discussions and posts changed its format. Adapt the entries already stored.
* Since it can be slow, we'll only block migrating the db of current site, the rest will be in background.
*
* @return {Promise<any>} Promise resolved when the db is migrated.
*/
protected adaptForumOfflineStores(): Promise<any> {
// @todo: Implement it once Forum addon is implemented.
return Promise.resolve();
}
}