fix: update core and apply stricter tslint rules

This commit is contained in:
Michel Jonathan Schmitz
2019-07-10 12:38:29 +02:00
parent 03c317430a
commit 911492d064
67 changed files with 1291 additions and 507 deletions

View File

@@ -16,25 +16,29 @@ import {Injectable} from '@angular/core';
import {Geolocation} from '@ionic-native/geolocation/ngx';
import {
SCSetting,
SCSettingMultipleChoice,
SCSettingSingleChoice,
SCSettingValue,
SCSettingValues,
} from '@openstapps/core';
import {Logger} from '@openstapps/logger';
import * as deepMerge from 'deepmerge';
import {ConfigProvider} from '../config/config.provider';
import {StorageProvider} from '../storage/storage.provider';
export const STORAGE_KEY_SETTINGS = 'settings';
export const STORAGE_KEY_SETTINGS_SEPARATOR = '.';
export const STORAGE_KEY_SETTING_VALUES = STORAGE_KEY_SETTINGS + STORAGE_KEY_SETTINGS_SEPARATOR + 'values';
export const STORAGE_KEY_SETTING_VALUES = `${STORAGE_KEY_SETTINGS}${STORAGE_KEY_SETTINGS_SEPARATOR}values`;
/**
* Category structure of settings cache
*/
export interface CategoryWithSettings {
/**
* Category name
*/
category: string;
settings: { [key: string]: SCSetting };
/**
* Settings that belong in this category
*/
settings: { [key: string]: SCSetting; };
}
/**
@@ -60,36 +64,61 @@ export interface SettingValueContainer {
/**
* Provider for app settings
*
*/
@Injectable()
export class SettingsProvider {
/**
* Order of the setting categories
*/
categoriesOrder: string[];
/**
* Is provider initialized
*/
initialized = false;
logger = new Logger();
/**
* Cache for the imported settings
*/
settingsCache: SettingsCache;
/**
* Return true if all given values are valid to possible values in given settingInput
* @param settingInput
* @param values
* @param possibleValues Possible values
* @param enteredValues Entered value
*/
public static checkMultipleChoiceValue(settingInput: SCSettingMultipleChoice, values: SCSettingValue[]): boolean {
for (const value of values) {
if (!settingInput.values.includes(value)) {
public static checkMultipleChoiceValue(
possibleValues: SCSettingValues | undefined,
enteredValues: SCSettingValues,
): boolean {
if ( typeof possibleValues === 'undefined' ) {
return false;
}
for (const value of enteredValues) {
if (!possibleValues.includes(value)) {
return false;
}
}
return true;
}
/**
* Returns true if given value is valid to possible values in given settingInput
* @param settingInput
* @param value
* @param possibleValues Possible values
* @param enteredValue Entered value
*/
public static checkSingleChoiceValue(settingInput: SCSettingSingleChoice, value: SCSettingValue): boolean {
return settingInput.values !== undefined
&& settingInput.values.includes(value);
public static checkSingleChoiceValue(
possibleValues: SCSettingValues | undefined,
enteredValue: SCSettingValue,
): boolean {
if ( typeof possibleValues === 'undefined' ) {
return false;
}
return possibleValues !== undefined
&& (Array.isArray(possibleValues)
&& possibleValues.includes(enteredValue));
}
/**
@@ -97,19 +126,19 @@ export class SettingsProvider {
* @param setting setting to check value against
* @param value value to validate
*/
public static validateValue(setting: SCSetting, value: any): boolean {
let isValueValid: boolean = false;
switch (setting.input.inputType) {
public static validateValue(setting: SCSetting, value: SCSettingValue | SCSettingValues): boolean {
let isValueValid = false;
switch (setting.inputType) {
case 'number':
if (typeof value === 'number') {
isValueValid = true;
}
break;
case 'multipleChoice':
if (!value.isArray) {
case 'multiple choice':
if (!Array.isArray(value)) {
isValueValid = false;
} else {
isValueValid = SettingsProvider.checkMultipleChoiceValue(setting.input, value);
isValueValid = SettingsProvider.checkMultipleChoiceValue(setting.values, value);
}
break;
case 'password':
@@ -118,32 +147,43 @@ export class SettingsProvider {
isValueValid = true;
}
break;
case 'singleChoice':
isValueValid = SettingsProvider.checkSingleChoiceValue(setting.input, value);
case 'single choice':
if (Array.isArray(value)) {
isValueValid = false;
} else {
isValueValid = SettingsProvider.checkSingleChoiceValue(setting.values, value);
}
break;
default:
}
return isValueValid;
}
constructor(private storage: StorageProvider,
private configProvider: ConfigProvider,
private geoLocation: Geolocation) {
/**
*
* @param storage TODO
* @param configProvider TODO
* @param geoLocation TODO
*/
constructor(private readonly storage: StorageProvider,
private readonly configProvider: ConfigProvider,
private readonly geoLocation: Geolocation) {
this.categoriesOrder = [];
this.settingsCache = {};
}
/**
* Add an Setting to the Cache if not exist and set undefined value to defaultValue
* @param setting
* @param setting Setting with categories, defautlValue, name, input type and valid values
*/
private async addSetting(setting: SCSetting): Promise<void> {
if (!this.categoryExists(setting.categories[0])) {
await this.provideCategory(setting.categories[0]);
}
if (!this.settingExists(setting.categories[0], setting.name)) {
if (setting.input.value === undefined) {
setting.input.value = setting.input.defaultValue;
if (setting.value === undefined) {
setting.value = setting.defaultValue;
}
this.settingsCache[setting.categories[0]].settings[setting.name] = setting;
}
@@ -162,9 +202,10 @@ export class SettingsProvider {
settingValuesContainer[categoryKey] = {};
}
settingValuesContainer[categoryKey][settingKey] =
this.settingsCache[categoryKey].settings[settingKey].input.value;
this.settingsCache[categoryKey].settings[settingKey].value;
}
}
return settingValuesContainer;
}
@@ -196,11 +237,11 @@ export class SettingsProvider {
// if saved setting value exists set it, otherwise set to default value
if (typeof valuesContainer[categoryKey] !== 'undefined'
&& typeof valuesContainer[categoryKey][settingKey] !== 'undefined') {
this.settingsCache[categoryKey].settings[settingKey].input.value =
this.settingsCache[categoryKey].settings[settingKey].value =
valuesContainer[categoryKey][settingKey];
} else {
this.settingsCache[categoryKey].settings[settingKey].input.value =
this.settingsCache[categoryKey].settings[settingKey].input.defaultValue;
this.settingsCache[categoryKey].settings[settingKey].value =
this.settingsCache[categoryKey].settings[settingKey].defaultValue;
}
}
}
@@ -228,7 +269,7 @@ export class SettingsProvider {
/**
* Returns true if category exists
* @param category
* @param category Category key name
*/
public categoryExists(category: string): boolean {
return this.settingsCache[category] !== undefined;
@@ -256,6 +297,7 @@ export class SettingsProvider {
}
}
}
return true;
}
@@ -264,6 +306,7 @@ export class SettingsProvider {
*/
public async getCache(): Promise<SettingsCache> {
await this.init();
return JSON.parse(JSON.stringify(this.settingsCache));
}
@@ -281,14 +324,13 @@ export class SettingsProvider {
*
* @throws Exception if setting is not provided
*/
public async getSetting(category: string, name: string): Promise<any> {
public async getSetting(category: string, name: string): Promise<SCSetting> {
await this.init();
if (this.settingExists(category, name)) {
// return a copy of the settings
return JSON.parse(JSON.stringify(this.settingsCache[category].settings[name]));
} else {
throw new Error(`Setting "${name}" not provided`);
}
throw new Error(`Setting "${name}" not provided`);
}
/**
@@ -302,10 +344,9 @@ export class SettingsProvider {
await this.init();
if (this.settingExists(category, name)) {
// return a copy of the settings value
return JSON.parse(JSON.stringify(this.settingsCache[category].settings[name].input.value));
} else {
throw new Error(`Setting "${name}" not provided`);
return JSON.parse(JSON.stringify(this.settingsCache[category].settings[name].value));
}
throw new Error(`Setting "${name}" not provided`);
}
/**
@@ -340,7 +381,7 @@ export class SettingsProvider {
async resetDefault(): Promise<void> {
for (const catKey of Object.keys(this.settingsCache)) {
for (const settingKey of Object.keys(this.settingsCache[catKey].settings)) {
const settingInput = this.settingsCache[catKey].settings[settingKey].input;
const settingInput = this.settingsCache[catKey].settings[settingKey];
settingInput.value = settingInput.defaultValue;
}
}
@@ -373,24 +414,24 @@ export class SettingsProvider {
/**
* Sets a valid value of a setting and persists changes in storage
* @param category
* @param name
* @param value
* @param category Category key name
* @param name Setting key name
* @param value Value to be set
*
* @throws Exception if setting is not provided or value not valid to the settings inputType
*/
public async setSettingValue(category: string, name: string,
value: any): Promise<void> {
value: SCSettingValue | SCSettingValues): Promise<void> {
await this.init();
if (this.settingExists(category, name)) {
const setting: SCSetting = this.settingsCache[category].settings[name];
const isValueValid = SettingsProvider.validateValue(setting, value);
if (isValueValid) {
this.settingsCache[category].settings[name].input.value = value;
this.settingsCache[category].settings[name].value = value;
await this.saveSettingValues();
} else {
throw new Error(`Value "${value}" of type
${typeof value} is not valid for ${setting.input.inputType}`);
${typeof value} is not valid for ${setting.inputType}`);
}
} else {
throw new Error(`setting ${name} is not provided`);
@@ -399,8 +440,8 @@ export class SettingsProvider {
/**
* Returns true if setting in category exists
* @param category
* @param setting
* @param category Category key name
* @param setting Setting key name
*/
public settingExists(category: string, setting: string): boolean {
return this.categoryExists(category) && this.settingsCache[category].settings[setting] !== undefined;