16e80583aSopenharmony_ci/** 26e80583aSopenharmony_ci * Copyright (c) 2024-2024 Huawei Device Co., Ltd. 36e80583aSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 46e80583aSopenharmony_ci * you may not use this file except in compliance with the License. 56e80583aSopenharmony_ci * You may obtain a copy of the License at 66e80583aSopenharmony_ci * 76e80583aSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 86e80583aSopenharmony_ci * 96e80583aSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 106e80583aSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 116e80583aSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 126e80583aSopenharmony_ci * See the License for the specific language governing permissions and 136e80583aSopenharmony_ci * limitations under the License. 146e80583aSopenharmony_ci */ 156e80583aSopenharmony_ci 166e80583aSopenharmony_ciimport DataPreferences from '@ohos.data.preferences'; 176e80583aSopenharmony_ciimport { Log } from '../utils/Log'; 186e80583aSopenharmony_ciimport { Context } from '@ohos.abilityAccessCtrl'; 196e80583aSopenharmony_ci 206e80583aSopenharmony_ci 216e80583aSopenharmony_ciconst TAG = 'PreferencesHelper'; 226e80583aSopenharmony_ci 236e80583aSopenharmony_ciexport class PreferencesHelper { 246e80583aSopenharmony_ci private static PREFERENCE_NAME = 'launcher_pref'; 256e80583aSopenharmony_ci private static sInstance:PreferencesHelper | undefined = undefined; 266e80583aSopenharmony_ci private preference:DataPreferences.Preferences; 276e80583aSopenharmony_ci 286e80583aSopenharmony_ci static getInstance() { 296e80583aSopenharmony_ci if (!PreferencesHelper.sInstance) { 306e80583aSopenharmony_ci PreferencesHelper.sInstance = new PreferencesHelper(); 316e80583aSopenharmony_ci } 326e80583aSopenharmony_ci return PreferencesHelper.sInstance; 336e80583aSopenharmony_ci } 346e80583aSopenharmony_ci 356e80583aSopenharmony_ci async initPreference(context: Context): Promise<void> { 366e80583aSopenharmony_ci if (this.preference) { 376e80583aSopenharmony_ci Log.showInfo(TAG, 'preference is inited'); 386e80583aSopenharmony_ci return; 396e80583aSopenharmony_ci } 406e80583aSopenharmony_ci try { 416e80583aSopenharmony_ci this.preference = await DataPreferences.getPreferences(context, PreferencesHelper.PREFERENCE_NAME); 426e80583aSopenharmony_ci } catch (err) { 436e80583aSopenharmony_ci Log.showError(TAG, `Failed to initPreference, Cause:${err.message || err?.code}`); 446e80583aSopenharmony_ci } 456e80583aSopenharmony_ci } 466e80583aSopenharmony_ci 476e80583aSopenharmony_ci async put(key:string, value):Promise<void>{ 486e80583aSopenharmony_ci try { 496e80583aSopenharmony_ci await this.preference?.put(key,value); 506e80583aSopenharmony_ci } catch (err) { 516e80583aSopenharmony_ci Log.showError(TAG, `Failed to put value, Cause:${err.message || err?.code}`); 526e80583aSopenharmony_ci } 536e80583aSopenharmony_ci await this.preference?.flush(); 546e80583aSopenharmony_ci } 556e80583aSopenharmony_ci 566e80583aSopenharmony_ci async get(key:string, defValue):Promise<DataPreferences.ValueType>{ 576e80583aSopenharmony_ci try { 586e80583aSopenharmony_ci let result = await this.preference?.get(key,defValue); 596e80583aSopenharmony_ci return result; 606e80583aSopenharmony_ci } catch (err) { 616e80583aSopenharmony_ci Log.showError(TAG, `Failed to get initPreferences, Cause:${err.message || err?.code}`); 626e80583aSopenharmony_ci } 636e80583aSopenharmony_ci return defValue; 646e80583aSopenharmony_ci } 656e80583aSopenharmony_ci} 66