mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-09 11:12:52 +00:00
refactor: clean up settings.provider
This commit is contained in:
@@ -104,10 +104,6 @@ export class SettingsProvider {
|
|||||||
* Order of the setting categories
|
* Order of the setting categories
|
||||||
*/
|
*/
|
||||||
categoriesOrder: string[];
|
categoriesOrder: string[];
|
||||||
/**
|
|
||||||
* Is provider initialized
|
|
||||||
*/
|
|
||||||
initialized = false;
|
|
||||||
/**
|
/**
|
||||||
* Settings actions observable
|
* Settings actions observable
|
||||||
*/
|
*/
|
||||||
@@ -213,9 +209,9 @@ export class SettingsProvider {
|
|||||||
* Add an Setting to the Cache if not exist and set undefined value to defaultValue
|
* Add an Setting to the Cache if not exist and set undefined value to defaultValue
|
||||||
* @param setting Setting with categories, defautlValue, name, input type and valid values
|
* @param setting Setting with categories, defautlValue, name, input type and valid values
|
||||||
*/
|
*/
|
||||||
private async addSetting(setting: SCSetting): Promise<void> {
|
private addSetting(setting: SCSetting): void {
|
||||||
if (!this.categoryExists(setting.categories[0])) {
|
if (!this.categoryExists(setting.categories[0])) {
|
||||||
await this.provideCategory(setting.categories[0]);
|
this.provideCategory(setting.categories[0]);
|
||||||
}
|
}
|
||||||
if (!this.settingExists(setting.categories[0], setting.name)) {
|
if (!this.settingExists(setting.categories[0], setting.name)) {
|
||||||
if (setting.value === undefined) {
|
if (setting.value === undefined) {
|
||||||
@@ -245,54 +241,11 @@ export class SettingsProvider {
|
|||||||
return settingValuesContainer;
|
return settingValuesContainer;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Initializes settings from config and stored values if exist
|
|
||||||
*/
|
|
||||||
private async initSettings(): Promise<void> {
|
|
||||||
try {
|
|
||||||
const settings: SCSetting[] = (await this.configProvider.getValue('settings')) as SCSetting[];
|
|
||||||
settings.forEach((setting) => this.addSetting(setting));
|
|
||||||
|
|
||||||
for (const category of Object.keys(this.settingsCache)) {
|
|
||||||
if (!this.categoriesOrder.includes(category)) {
|
|
||||||
this.categoriesOrder.push(category);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
this.settingsCache = {};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (await this.storage.has(STORAGE_KEY_SETTING_VALUES)) {
|
|
||||||
// get setting values from StorageProvider into settingsCache
|
|
||||||
const valuesContainer: SettingValuesContainer =
|
|
||||||
await this.storage.get<SettingValuesContainer>(STORAGE_KEY_SETTING_VALUES);
|
|
||||||
// iterate through keys of categories
|
|
||||||
for (const categoryKey of Object.keys(this.settingsCache)) {
|
|
||||||
// iterate through setting keys of category
|
|
||||||
for (const settingKey of Object.keys(this.settingsCache[categoryKey].settings)) {
|
|
||||||
// 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].value =
|
|
||||||
valuesContainer[categoryKey][settingKey];
|
|
||||||
} else {
|
|
||||||
this.settingsCache[categoryKey].settings[settingKey].value =
|
|
||||||
this.settingsCache[categoryKey].settings[settingKey].defaultValue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
await this.saveSettingValues();
|
|
||||||
}
|
|
||||||
this.initialized = true;
|
|
||||||
// publish provider initialised
|
|
||||||
this.settingsActionSource.next({type: 'stapps.settings.initialized'});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add category if not exists
|
* Add category if not exists
|
||||||
* @param category the category to provide
|
* @param category the category to provide
|
||||||
*/
|
*/
|
||||||
private async provideCategory(category: string): Promise<void> {
|
private provideCategory(category: string): void {
|
||||||
if (!this.categoryExists(category)) {
|
if (!this.categoryExists(category)) {
|
||||||
if (!this.categoriesOrder.includes(category)) {
|
if (!this.categoriesOrder.includes(category)) {
|
||||||
this.categoriesOrder.push(category);
|
this.categoriesOrder.push(category);
|
||||||
@@ -387,11 +340,42 @@ export class SettingsProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* initializes settingsProvider
|
* Initializes settings from config and stored values if exist
|
||||||
*/
|
*/
|
||||||
public async init(): Promise<void> {
|
public async init(): Promise<void> {
|
||||||
if (!this.initialized) {
|
try {
|
||||||
await this.initSettings();
|
const settings: SCSetting[] = (await this.configProvider.getValue('settings')) as SCSetting[];
|
||||||
|
settings.forEach((setting) => this.addSetting(setting));
|
||||||
|
|
||||||
|
for (const category of Object.keys(this.settingsCache)) {
|
||||||
|
if (!this.categoriesOrder.includes(category)) {
|
||||||
|
this.categoriesOrder.push(category);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
this.settingsCache = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (await this.storage.has(STORAGE_KEY_SETTING_VALUES)) {
|
||||||
|
// get setting values from StorageProvider into settingsCache
|
||||||
|
const valuesContainer: SettingValuesContainer =
|
||||||
|
await this.storage.get<SettingValuesContainer>(STORAGE_KEY_SETTING_VALUES);
|
||||||
|
// iterate through keys of categories
|
||||||
|
for (const categoryKey of Object.keys(this.settingsCache)) {
|
||||||
|
// iterate through setting keys of category
|
||||||
|
for (const settingKey of Object.keys(this.settingsCache[categoryKey].settings)) {
|
||||||
|
// 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].value =
|
||||||
|
valuesContainer[categoryKey][settingKey];
|
||||||
|
} else {
|
||||||
|
this.settingsCache[categoryKey].settings[settingKey].value =
|
||||||
|
this.settingsCache[categoryKey].settings[settingKey].defaultValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
await this.saveSettingValues();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -399,9 +383,8 @@ export class SettingsProvider {
|
|||||||
* Adds given setting and its category if not exist
|
* Adds given setting and its category if not exist
|
||||||
* @param setting the setting to add
|
* @param setting the setting to add
|
||||||
*/
|
*/
|
||||||
public async provideSetting(setting: SCSetting): Promise<void> {
|
public provideSetting(setting: SCSetting): void {
|
||||||
await this.init();
|
this.addSetting(setting);
|
||||||
await this.addSetting(setting);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -409,7 +392,7 @@ export class SettingsProvider {
|
|||||||
*/
|
*/
|
||||||
public async reset(): Promise<void> {
|
public async reset(): Promise<void> {
|
||||||
await this.storage.put(STORAGE_KEY_SETTING_VALUES, {});
|
await this.storage.put(STORAGE_KEY_SETTING_VALUES, {});
|
||||||
await this.initSettings();
|
await this.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user