From e3d5c95c87f39ad7f3748a3aa370b5183d400e56 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Fri, 27 Oct 2017 08:56:18 +0200 Subject: [PATCH] MOBILE-2261 core: Implement Config provider --- src/app/app.module.ts | 2 + src/providers/config.ts | 86 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 src/providers/config.ts diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 12655b0e0..0d832d81c 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -15,6 +15,7 @@ import { MyApp } from './app.component'; import { CoreLoggerProvider } from '../providers/logger'; import { CoreDbProvider } from '../providers/db'; import { CoreAppProvider } from '../providers/app'; +import { CoreConfigProvider } from '../providers/config'; // For translate loader. AoT requires an exported function for factories. export function createTranslateLoader(http: HttpClient) { @@ -50,6 +51,7 @@ export function createTranslateLoader(http: HttpClient) { CoreLoggerProvider, CoreDbProvider, CoreAppProvider, + CoreConfigProvider, ] }) export class AppModule {} diff --git a/src/providers/config.ts b/src/providers/config.ts new file mode 100644 index 000000000..a0672cf27 --- /dev/null +++ b/src/providers/config.ts @@ -0,0 +1,86 @@ +// (C) Copyright 2015 Martin Dougiamas +// +// 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 { CoreAppProvider } from './app'; +import { SQLiteDB } from '../classes/sqlitedb'; + +/** + * Factory to provide access to dynamic and permanent config and settings. + * It should not be abused into a temporary storage. + */ +@Injectable() +export class CoreConfigProvider { + appDB: SQLiteDB; + TABLE_NAME = 'core_config'; + tableSchema = { + name: this.TABLE_NAME, + columns: [ + { + name: 'name', + type: 'TEXT', + unique: true, + notNull: true + }, + { + name: 'value' + } + ] + }; + + constructor(appProvider: CoreAppProvider) { + this.appDB = appProvider.getDB(); + this.appDB.createTableFromSchema(this.tableSchema); + } + + /** + * Deletes an app setting. + * + * @param name The config name. + * @return Promise resolved when done. + */ + delete(name: string) : Promise { + return this.appDB.deleteRecords(this.TABLE_NAME, {name: name}); + } + + /** + * Get an app setting. + * + * @param name The config name. + * @param [defaultValue] Default value to use if the entry is not found. + * @return Resolves upon success along with the config data. Reject on failure. + */ + get(name: string, defaultValue?: any) : Promise { + return this.appDB.getRecord(this.TABLE_NAME, {name: name}).then((entry) => { + return entry.value; + }).catch((error) => { + if (typeof defaultValue != 'undefined') { + return defaultValue; + } else { + return Promise.reject(error); + } + }); + } + + /** + * Set an app setting. + * + * @param name The config name. + * @param value The config value. Can only store primitive values, not objects. + * @return Promise resolved when done. + */ + set(name: string, value: any) : Promise { + return this.appDB.insertOrUpdateRecord(this.TABLE_NAME, {name: name, value: value}, {name: name}); + } +}