import dataPreferences from '@ohos.data.preferences' import { TextUtil } from './TextUtil' import {logger} from './Logger' let preference: dataPreferences.Preferences | null /** * 使用方式1:PreferenceUtil.writeBoolean(PreferenceKey.SHOW_PERMISSION_DESCRIPTION).then((userInfo: UserLoginDTO) => { Logger.d('userId:'+userInfo.userId) }) *使用方式2:let show = await PreferenceUtil.readBoolean(SPreferenceKey.HOW_PERMISSION_DESCRIPTION) * Logger.d('show:'+show) */ export class PreferenceUtil { private static readonly TAG: string = 'PreferenceUtil'; static readonly PREFERENCE_NAME: string = 'my_preference' static initPreference(preferences: dataPreferences.Preferences) { preference = preferences; } static async writeString(key: string, value?: string) { try { if (preference != null) { await preference.put(key, value) } } catch (e) { logger.error(PreferenceUtil.TAG, 'Failed to write value, cause:' + e) } if (preference != null) { await preference.flush() } } static async readString(key: string, defaultValue?: string) { let value: string = ""; try { if (preference != null) { value = await preference.get(key, defaultValue).then() } } catch (e) { logger.error(PreferenceUtil.TAG, 'Failed to read value, cause:' + e) } return value } static async writeArrayString(key: string, value?: Array<string>) { try { if (preference != null) { await preference.put(key, value) } } catch (e) { logger.error(PreferenceUtil.TAG, 'Failed to write value, cause:' + e) } if (preference != null) { await preference.flush() } } static async readArrayString(key: string, defaultValue?: Array<string>) { let value: Array<string>| null = null; try { if (preference != null) { value = await preference.get(key, defaultValue).then() } } catch (e) { logger.error(PreferenceUtil.TAG, 'Failed to read value, cause:' + e) } return value } static async writeNumber(key: string, value?: number) { try { if (preference != null) { await preference.put(key, value) } } catch (e) { logger.error(PreferenceUtil.TAG, 'Failed to write value, cause:' + e) } if (preference != null) { await preference.flush() } } static async readNumber(key: string, defaultValue?: number) { let value: number|undefined = undefined; try { if (preference != null) { value = await preference.get(key, defaultValue).then() } } catch (e) { logger.error(PreferenceUtil.TAG, 'Failed to read value, cause:' + e) } return value } static async writeArrayNumber(key: string, value?: Array<number>) { try { if (preference != null) { await preference.put(key, value) } } catch (e) { logger.error(PreferenceUtil.TAG, 'Failed to write value, cause:' + e) } if (preference != null) { await preference.flush() } } static async readArrayNumber(key: string, defaultValue?: Array<number>) { let value: Array<number> | null = null; try { if (preference != null) { value = await preference.get(key, defaultValue).then() } } catch (e) { logger.error(PreferenceUtil.TAG, 'Failed to read value, cause:' + e) } return value } static async writeBoolean(key: string, value?: boolean) { try { if (preference != null) { await preference.put(key, value) } } catch (e) { logger.error(PreferenceUtil.TAG, 'Failed to write value, cause:' + e) } if (preference != null) { await preference.flush() } } static async readBoolean(key: string, defaultValue?: boolean) { let value: boolean = false; try { if (preference != null) { value = await preference.get(key, defaultValue).then() } } catch (e) { logger.error(PreferenceUtil.TAG, 'Failed to read value, cause:' + e) } return value } static async writeArrayBoolean(key: string, value?: Array<boolean>) { try { if (preference != null) { await preference.put(key, value) } } catch (e) { logger.error(PreferenceUtil.TAG, 'Failed to write value, cause:' + e) } if (preference != null) { await preference.flush() } } static async readArrayBoolean(key: string, defaultValue?: Array<boolean>) { let value: Array<boolean> | null = null try { if (preference != null) { value = await preference.get(key, defaultValue).then() } } catch (e) { logger.error(PreferenceUtil.TAG, 'Failed to read value, cause:' + e) } return value } static async writeModel<T>(key: string, value?: T) { try { if (preference != null) { await preference.put(key, JSON.stringify(value)) } } catch (e) { logger.error(PreferenceUtil.TAG, 'Failed to write value, cause:' + e) } if (preference != null) { await preference.flush() } } static async readModel<T>(key: string) { let value: string = "" try { if (preference != null) { value = await preference.get(key, '').then() } } catch (e) { logger.error(PreferenceUtil.TAG, 'Failed to read value, cause:' + e) } if (TextUtil.isEmpty(value)) { return null } else { return JSON.parse(value) as T } } static async delete(key: string) { try { if (preference != null) { await preference.delete(key).then() } } catch (e) { logger.error(PreferenceUtil.TAG, 'Failed to delete, cause:' + e) } } static async clear() { try { if (preference != null) { await preference.clear().then() } } catch (e) { logger.error(PreferenceUtil.TAG, 'Failed to clear, cause:' + e) } } static async flush() { try { if (preference != null) { await preference.flush().then() } } catch (e) { logger.error(PreferenceUtil.TAG, 'Failed to flush, cause:' + e) } } }