2022-03-09 19:41:26 +00:00
|
|
|
import MMKVStorage, { create } from 'react-native-mmkv-storage';
|
2020-08-19 17:14:22 +00:00
|
|
|
|
|
|
|
const MMKV = new MMKVStorage.Loader()
|
|
|
|
// MODES.MULTI_PROCESS = ACCESSIBLE BY APP GROUP (iOS)
|
|
|
|
.setProcessingMode(MMKVStorage.MODES.MULTI_PROCESS)
|
|
|
|
.withEncryption()
|
|
|
|
.initialize();
|
|
|
|
|
2022-03-09 19:41:26 +00:00
|
|
|
export const useUserPreferences = create(MMKV);
|
|
|
|
|
2020-08-19 17:14:22 +00:00
|
|
|
class UserPreferences {
|
2022-01-11 14:23:43 +00:00
|
|
|
private mmkv: MMKVStorage.API;
|
2020-08-19 17:14:22 +00:00
|
|
|
constructor() {
|
|
|
|
this.mmkv = MMKV;
|
|
|
|
}
|
|
|
|
|
2022-03-09 19:41:26 +00:00
|
|
|
getString(key: string): string | null {
|
2020-08-19 17:14:22 +00:00
|
|
|
try {
|
2022-03-09 19:41:26 +00:00
|
|
|
return this.mmkv.getString(key) || null;
|
2020-08-19 17:14:22 +00:00
|
|
|
} catch {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-09 19:41:26 +00:00
|
|
|
setString(key: string, value: string): boolean | undefined {
|
|
|
|
return this.mmkv.setString(key, value);
|
2020-08-19 17:14:22 +00:00
|
|
|
}
|
|
|
|
|
2022-03-09 19:41:26 +00:00
|
|
|
getBool(key: string): boolean | null {
|
2020-08-19 17:14:22 +00:00
|
|
|
try {
|
2022-03-09 19:41:26 +00:00
|
|
|
return this.mmkv.getBool(key) || null;
|
2020-08-19 17:14:22 +00:00
|
|
|
} catch {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-09 19:41:26 +00:00
|
|
|
setBool(key: string, value: boolean): boolean | undefined {
|
|
|
|
return this.mmkv.setBool(key, value);
|
2020-08-19 17:14:22 +00:00
|
|
|
}
|
|
|
|
|
2022-03-09 19:41:26 +00:00
|
|
|
getMap(key: string): object | null {
|
2020-08-19 17:14:22 +00:00
|
|
|
try {
|
2022-03-09 19:41:26 +00:00
|
|
|
return this.mmkv.getMap(key) || null;
|
2020-08-19 17:14:22 +00:00
|
|
|
} catch {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-09 19:41:26 +00:00
|
|
|
setMap(key: string, value: object): boolean | undefined {
|
|
|
|
return this.mmkv.setMap(key, value);
|
2020-08-19 17:14:22 +00:00
|
|
|
}
|
|
|
|
|
2022-03-09 19:41:26 +00:00
|
|
|
removeItem(key: string): boolean | undefined {
|
2020-08-19 17:14:22 +00:00
|
|
|
return this.mmkv.removeItem(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const userPreferences = new UserPreferences();
|
|
|
|
export default userPreferences;
|