2019-09-26 12:43:29 +02:00
|
|
|
// (C) Copyright 2015 Moodle Pty Ltd.
|
2017-11-21 09:56:16 +01:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { CoreConfigProvider } from './config';
|
|
|
|
import { CoreInitHandler, CoreInitDelegate } from './init';
|
|
|
|
import { CoreLoggerProvider } from './logger';
|
|
|
|
import { CoreConfigConstants } from '../configconstants';
|
2020-03-24 12:18:32 +01:00
|
|
|
import { makeSingleton } from '@singletons/core.singletons';
|
2017-11-21 09:56:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Factory to handle app updates. This factory shouldn't be used outside of core.
|
|
|
|
*
|
|
|
|
* This service handles processes that need to be run when updating the app, like migrate Ionic 1 database data to Ionic 3.
|
|
|
|
*/
|
|
|
|
@Injectable()
|
|
|
|
export class CoreUpdateManagerProvider implements CoreInitHandler {
|
|
|
|
// Data for init delegate.
|
2018-01-29 10:05:20 +01:00
|
|
|
name = 'CoreUpdateManager';
|
|
|
|
priority = CoreInitDelegate.MAX_RECOMMENDED_PRIORITY + 300;
|
|
|
|
blocking = true;
|
2017-11-21 09:56:16 +01:00
|
|
|
|
|
|
|
protected VERSION_APPLIED = 'version_applied';
|
|
|
|
protected logger;
|
|
|
|
|
2020-01-24 10:06:06 +01:00
|
|
|
constructor(logger: CoreLoggerProvider, private configProvider: CoreConfigProvider) {
|
2017-11-21 09:56:16 +01:00
|
|
|
this.logger = logger.getInstance('CoreUpdateManagerProvider');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the app has been updated and performs the needed processes.
|
|
|
|
* This function shouldn't be used outside of core.
|
|
|
|
*
|
2019-09-10 16:48:56 +02:00
|
|
|
* @return Promise resolved when the update process finishes.
|
2017-11-21 09:56:16 +01:00
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
load(): Promise<any> {
|
|
|
|
const promises = [],
|
2017-11-21 09:56:16 +01:00
|
|
|
versionCode = CoreConfigConstants.versioncode;
|
|
|
|
|
2020-01-24 10:05:27 +01:00
|
|
|
return this.configProvider.get(this.VERSION_APPLIED, 0).then((versionApplied: number) => {
|
2017-11-21 09:56:16 +01:00
|
|
|
|
2020-01-24 10:06:06 +01:00
|
|
|
// Put here the code to treat app updates.
|
2018-06-19 13:16:16 +02:00
|
|
|
|
2017-11-21 09:56:16 +01:00
|
|
|
return Promise.all(promises).then(() => {
|
|
|
|
return this.configProvider.set(this.VERSION_APPLIED, versionCode);
|
|
|
|
}).catch((error) => {
|
|
|
|
this.logger.error(`Error applying update from ${versionApplied} to ${versionCode}`, error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2020-03-24 12:18:32 +01:00
|
|
|
|
|
|
|
export class CoreUpdateManager extends makeSingleton<CoreUpdateManagerProvider>(CoreUpdateManagerProvider) {}
|