From f85a2c103ad66b5dc519a84c05b2e3e4905453b4 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Thu, 14 Jun 2018 09:59:34 +0200 Subject: [PATCH] MOBILE-2428 update: Migrate local notification components --- src/addon/calendar/calendar.module.ts | 3 ++ src/addon/messages/messages.module.ts | 4 ++ .../pushnotifications.module.ts | 3 ++ src/directives/link.ts | 4 +- src/providers/local-notifications.ts | 17 +++++++-- src/providers/update-manager.ts | 38 +++++++++++++++++++ 6 files changed, 64 insertions(+), 5 deletions(-) diff --git a/src/addon/calendar/calendar.module.ts b/src/addon/calendar/calendar.module.ts index 6269c08b5..f82454173 100644 --- a/src/addon/calendar/calendar.module.ts +++ b/src/addon/calendar/calendar.module.ts @@ -73,5 +73,8 @@ export class AddonCalendarModule { 'categoryid', 'groupid', 'userid', 'instance', 'modulename', 'timemodified', 'repeatid', 'visible', 'uuid', 'sequence', 'subscriptionid', 'notificationtime'] }); + + // Migrate the component name. + updateManager.registerLocalNotifComponentMigration('mmaCalendarComponent', AddonCalendarProvider.COMPONENT); } } diff --git a/src/addon/messages/messages.module.ts b/src/addon/messages/messages.module.ts index db6c558fa..90f4abff9 100644 --- a/src/addon/messages/messages.module.ts +++ b/src/addon/messages/messages.module.ts @@ -131,5 +131,9 @@ export class AddonMessagesModule { } ] }); + + // Migrate the component name. + updateManager.registerLocalNotifComponentMigration('mmaMessagesPushSimulation', + AddonMessagesProvider.PUSH_SIMULATION_COMPONENT); } } diff --git a/src/addon/pushnotifications/pushnotifications.module.ts b/src/addon/pushnotifications/pushnotifications.module.ts index 78b9966b6..257aae6eb 100644 --- a/src/addon/pushnotifications/pushnotifications.module.ts +++ b/src/addon/pushnotifications/pushnotifications.module.ts @@ -83,5 +83,8 @@ export class AddonPushNotificationsModule { } ] }); + + // Migrate the component name. + updateManager.registerLocalNotifComponentMigration('mmaPushNotifications', AddonPushNotificationsProvider.COMPONENT); } } diff --git a/src/directives/link.ts b/src/directives/link.ts index 7165f82f7..1b5700efb 100644 --- a/src/directives/link.ts +++ b/src/directives/link.ts @@ -90,8 +90,8 @@ export class CoreLinkDirective implements OnInit { href = href.substr(1); // In site links if (href.charAt(0) == '/') { - // @todo: Investigate how to achieve this behaviour. - // $location.url(href); + // @todo: This cannot be achieved with push/pop navigation, location.go() doesn't update the state, only the URL. + // In Ionic 4 the navigation will change, so maybe it can be done by then. } else { // Look for id or name. this.domUtils.scrollToElementBySelector(this.content, '#' + href + ', [name=\'' + href + '\']'); diff --git a/src/providers/local-notifications.ts b/src/providers/local-notifications.ts index 4d0541514..41d2d18ee 100644 --- a/src/providers/local-notifications.ts +++ b/src/providers/local-notifications.ts @@ -47,9 +47,6 @@ export interface CoreILocalNotification extends ILocalNotification { * * See https://angular.io/guide/dependency-injection for more info on providers * and Angular DI. - * - * @todo We might have to translate the old component name to the new one. - * Otherwise the unique ID of local notifications could change. */ @Injectable() export class CoreLocalNotificationsProvider { @@ -502,4 +499,18 @@ export class CoreLocalNotificationsProvider { return this.appDB.insertRecord(this.TRIGGERED_TABLE, entry); } + + /** + * Update a component name. + * + * @param {string} oldName The old name. + * @param {string} newName The new name. + * @return {Promise} Promise resolved when done. + */ + updateComponentName(oldName: string, newName: string): Promise { + const oldId = this.COMPONENTS_TABLE + '#' + oldName, + newId = this.COMPONENTS_TABLE + '#' + newName; + + return this.appDB.updateRecords(this.COMPONENTS_TABLE, {id: newId}, {id: oldId}); + } } diff --git a/src/providers/update-manager.ts b/src/providers/update-manager.ts index 350cb6104..744adcd5b 100644 --- a/src/providers/update-manager.ts +++ b/src/providers/update-manager.ts @@ -72,6 +72,7 @@ export class CoreUpdateManagerProvider implements CoreInitHandler { protected VERSION_APPLIED = 'version_applied'; protected logger; + protected localNotificationsComponentsMigrate: {[old: string]: string} = {}; /** * Tables to migrate from app DB ('MoodleMobile'). Include all the core ones to decrease the dependencies. @@ -343,6 +344,9 @@ export class CoreUpdateManagerProvider implements CoreInitHandler { if (!versionApplied) { // No version applied, either the app was just installed or it's being updated from Ionic 1. return this.migrateAllDBs().then(() => { + // Now that the DBs have been migrated, migrate the local notification components names. + return this.migrateLocalNotificationsComponents(); + }).then(() => { // DBs migrated, get the version applied again. return this.configProvider.get(this.VERSION_APPLIED, 0); }); @@ -411,6 +415,16 @@ export class CoreUpdateManagerProvider implements CoreInitHandler { this.siteDBTables.push(table); } + /** + * Register a migration of component name for local notifications. + * + * @param {string} oldName The old name. + * @param {string} newName The new name. + */ + registerLocalNotifComponentMigration(oldName: string, newName: string): void { + this.localNotificationsComponentsMigrate[oldName] = newName; + } + /** * Migrate all DBs and tables from the old format to SQLite. * @@ -556,6 +570,30 @@ export class CoreUpdateManagerProvider implements CoreInitHandler { }); } + /** + * Migrate local notifications components from the old nomenclature to the new one. + * + * @return {Promise} Promise resolved when done. + */ + protected migrateLocalNotificationsComponents(): Promise { + if (!this.notifProvider.isAvailable()) { + // Local notifications not available, nothing to do. + return Promise.resolve(); + } + + const promises = []; + + for (const oldName in this.localNotificationsComponentsMigrate) { + const newName = this.localNotificationsComponentsMigrate[oldName]; + + promises.push(this.notifProvider.updateComponentName(oldName, newName).catch((error) => { + this.logger.error('Error migrating local notif component from ' + oldName + ' to ' + newName + ': ', error); + })); + } + + return Promise.all(promises); + } + /** * Calendar default notification time is configurable from version 3.2.1, and a new option "Default" is added. * All events that were configured to use the fixed default time should now be configured to use "Default" option.