harmony/commons/basic/src/main/ets/utils/PreferenceStore.ets
2025-07-21 16:23:36 +08:00

51 lines
1.4 KiB
Plaintext

import { preferences } from '@kit.ArkData'
class PreferenceStore {
KEY = 'gdxz_config'
store: preferences.Preferences | null = null
getStore() {
if (!this.store) {
this.store = preferences.getPreferencesSync(getContext(), { name: this.KEY })
}
return this.store
}
setItemString(keyword: string,value:string) {
this.getStore().putSync(keyword, value)
this.getStore().flush()
}
setItemNumber(keyword: string,value:number) {
this.getStore().putSync(keyword, value)
this.getStore().flush()
}
setItemBoolean(keyword: string,value:boolean) {
this.getStore().putSync(keyword, value)
this.getStore().flush()
}
delItem(keyword: string) {
this.getStore().deleteSync(keyword)
this.getStore().flush()
}
getItemString(keyword: string) {
return this.getStore().getSync(keyword,'') as string
}
getItemNumber(keyword: string) {
return this.getStore().getSync(keyword,0) as number
}
getItemBooleanT(keyword: string):boolean {
return this.getStore().getSync(keyword,true) as boolean
}
getItemBooleanF(keyword: string):boolean {
return this.getStore().getSync(keyword,false) as boolean
}
clear() {
this.getStore().clearSync()
}
getAll() {
const obj = this.getStore().getAllSync()
return Object.keys(obj)
}
}
export const preferenceStore = new PreferenceStore()