2020-08-19 17:14:22 +00:00
|
|
|
import MMKVStorage from 'react-native-mmkv-storage';
|
|
|
|
|
|
|
|
const MMKV = new MMKVStorage.Loader()
|
|
|
|
// MODES.MULTI_PROCESS = ACCESSIBLE BY APP GROUP (iOS)
|
|
|
|
.setProcessingMode(MMKVStorage.MODES.MULTI_PROCESS)
|
|
|
|
.withEncryption()
|
|
|
|
.initialize();
|
|
|
|
|
|
|
|
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-01-11 14:23:43 +00:00
|
|
|
async getStringAsync(key: string) {
|
2020-08-19 17:14:22 +00:00
|
|
|
try {
|
|
|
|
const value = await this.mmkv.getStringAsync(key);
|
|
|
|
return value;
|
|
|
|
} catch {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-11 14:23:43 +00:00
|
|
|
setStringAsync(key: string, value: string) {
|
2020-08-19 17:14:22 +00:00
|
|
|
return this.mmkv.setStringAsync(key, value);
|
|
|
|
}
|
|
|
|
|
2022-01-11 14:23:43 +00:00
|
|
|
async getBoolAsync(key: string) {
|
2020-08-19 17:14:22 +00:00
|
|
|
try {
|
|
|
|
const value = await this.mmkv.getBoolAsync(key);
|
|
|
|
return value;
|
|
|
|
} catch {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-11 14:23:43 +00:00
|
|
|
setBoolAsync(key: string, value: boolean) {
|
2020-08-19 17:14:22 +00:00
|
|
|
return this.mmkv.setBoolAsync(key, value);
|
|
|
|
}
|
|
|
|
|
2022-01-11 14:23:43 +00:00
|
|
|
async getMapAsync(key: string) {
|
2020-08-19 17:14:22 +00:00
|
|
|
try {
|
|
|
|
const value = await this.mmkv.getMapAsync(key);
|
|
|
|
return value;
|
|
|
|
} catch {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-11 14:23:43 +00:00
|
|
|
setMapAsync(key: string, value: object) {
|
2020-08-19 17:14:22 +00:00
|
|
|
return this.mmkv.setMapAsync(key, value);
|
|
|
|
}
|
|
|
|
|
2022-01-11 14:23:43 +00:00
|
|
|
removeItem(key: string) {
|
2020-08-19 17:14:22 +00:00
|
|
|
return this.mmkv.removeItem(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const userPreferences = new UserPreferences();
|
|
|
|
export default userPreferences;
|