From b19a1f8fe1551d151c326953d62f7b91b322e03f Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Tue, 5 Jun 2018 08:11:40 +0200 Subject: [PATCH] MOBILE-2428 update: Implement calendar update task --- src/addon/calendar/providers/calendar.ts | 35 ++++++++++++++++--- src/components/split-view/split-view.ts | 1 - src/providers/update-manager.ts | 43 ++++++++++++++---------- 3 files changed, 56 insertions(+), 23 deletions(-) diff --git a/src/addon/calendar/providers/calendar.ts b/src/addon/calendar/providers/calendar.ts index 226fde369..794818ff7 100644 --- a/src/addon/calendar/providers/calendar.ts +++ b/src/addon/calendar/providers/calendar.ts @@ -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} Promise resolved with all the events. + */ + getAllEventsFromLocalDb(siteId?: string): Promise { + 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 { 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 { 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} Promise resolved when stored. + */ + storeEventInLocalDb(event: any, siteId?: string): Promise { + return this.sitesProvider.getSite(siteId).then((site) => { + return site.getDb().insertRecord(AddonCalendarProvider.EVENTS_TABLE, event); + }); + } + /** * Store events in local DB. * diff --git a/src/components/split-view/split-view.ts b/src/components/split-view/split-view.ts index e585a0ee2..eb866c36a 100644 --- a/src/components/split-view/split-view.ts +++ b/src/components/split-view/split-view.ts @@ -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'; diff --git a/src/providers/update-manager.ts b/src/providers/update-manager.ts index 183d1b32b..350cb6104 100644 --- a/src/providers/update-manager.ts +++ b/src/providers/update-manager.ts @@ -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} Promise resolved when the db is migrated. - */ - protected adaptForumOfflineStores(): Promise { - // @todo: Implement it once Forum addon is implemented. - return Promise.resolve(); - } }