MOBILE-2428 update: Migrate local notification components

main
Dani Palou 2018-06-14 09:59:34 +02:00
parent 3d2e056a79
commit f85a2c103a
6 changed files with 64 additions and 5 deletions

View File

@ -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);
}
}

View File

@ -131,5 +131,9 @@ export class AddonMessagesModule {
}
]
});
// Migrate the component name.
updateManager.registerLocalNotifComponentMigration('mmaMessagesPushSimulation',
AddonMessagesProvider.PUSH_SIMULATION_COMPONENT);
}
}

View File

@ -83,5 +83,8 @@ export class AddonPushNotificationsModule {
}
]
});
// Migrate the component name.
updateManager.registerLocalNotifComponentMigration('mmaPushNotifications', AddonPushNotificationsProvider.COMPONENT);
}
}

View File

@ -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 + '\']');

View File

@ -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<any>} Promise resolved when done.
*/
updateComponentName(oldName: string, newName: string): Promise<any> {
const oldId = this.COMPONENTS_TABLE + '#' + oldName,
newId = this.COMPONENTS_TABLE + '#' + newName;
return this.appDB.updateRecords(this.COMPONENTS_TABLE, {id: newId}, {id: oldId});
}
}

View File

@ -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<any>} Promise resolved when done.
*/
protected migrateLocalNotificationsComponents(): Promise<any> {
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.