1/** 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import FileUtils from '../utils/FileUtils'; 17 18/** 19 * Layout configuration base class, 20 * which defines the interfaces that all configuration objects need to implement. 21 */ 22export abstract class ILayoutConfig { 23 24 private static readonly COMMON_FEATURE_NAME = 'featureCommon'; 25 26 protected constructor() { 27 } 28 29 /** 30 * Initialize the configuration, 31 * each configuration must recreate this function. 32 */ 33 abstract initConfig(): void; 34 35 /** 36 * The current configuration level, 37 * each configuration must reset this function. 38 */ 39 abstract getConfigLevel(): string; 40 41 /** 42 * The current configuration type, 43 * each configuration must recreate this function. 44 */ 45 abstract getConfigType(): number; 46 47 /** 48 * The current configuration name, 49 * each configuration must recreate this function. 50 */ 51 abstract getConfigName(): string; 52 53 /** 54 * The JSON string corresponding to the current configuration. 55 */ 56 protected abstract getPersistConfigJson(): string; 57 58 /** 59 * load configuration. 60 */ 61 protected loadPersistConfig(): any { 62 let defaultConfig = this.getPersistConfigJson(); 63 return JSON.parse(defaultConfig); 64 } 65 66 /** 67 * load configuration from file. 68 */ 69 loadPersistConfigFromFile(): any { 70 const configFromFile = FileUtils.readStringFromFile(this.getConfigFileAbsPath()); 71 return configFromFile; 72 } 73 74 /** 75 * Check if the current property already exists. 76 */ 77 isConfigExist(): boolean { 78 return FileUtils.isExist(this.getConfigFileAbsPath()); 79 } 80 81 /** 82 * Force reload of configuration values. 83 */ 84 forceReloadConfig(): void { 85 this.initConfig(); 86 } 87 88 /** 89 * Persistent configuration values. 90 */ 91 persistConfig(): void { 92 FileUtils.writeStringToFile(this.getPersistConfigJson(), this.getConfigFileAbsPath()); 93 } 94 95 /** 96 * delete configuration value. 97 */ 98 deleteConfig(): void { 99 FileUtils.deleteConfigFile(this.getConfigFileAbsPath()); 100 } 101 102 /** 103 * Get the current configuration name. 104 */ 105 getFeatureName(): string { 106 return ILayoutConfig.COMMON_FEATURE_NAME; 107 } 108 109 /** 110 * Get the absolute path of the current configuration file. 111 */ 112 getConfigFileAbsPath(): string { 113 let filesDir = globalThis.desktopContext.filesDir + '/' 114 return filesDir + this.getConfigName() + '.json'; 115 } 116}