2019-07-31 14:22:36 +02:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import {AsyncStorage} from "react-native";
|
|
|
|
|
|
|
|
/**
|
2019-08-06 13:28:11 +02:00
|
|
|
* Static class used to manage preferences.
|
|
|
|
* Preferences are fetched at the start of the app and saved in an instance object.
|
|
|
|
* This allows for a synchronous access to saved data.
|
2019-07-31 14:22:36 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
export default class AsyncStorageManager {
|
|
|
|
|
|
|
|
static instance: AsyncStorageManager | null = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get this class instance or create one if none is found
|
|
|
|
* @returns {ThemeManager}
|
|
|
|
*/
|
|
|
|
static getInstance(): AsyncStorageManager {
|
|
|
|
return AsyncStorageManager.instance === null ?
|
|
|
|
AsyncStorageManager.instance = new AsyncStorageManager() :
|
|
|
|
AsyncStorageManager.instance;
|
|
|
|
}
|
|
|
|
|
2019-08-06 13:28:11 +02:00
|
|
|
// Object storing preferences keys, default and current values for use in the app
|
2019-07-31 14:22:36 +02:00
|
|
|
preferences = {
|
2019-08-05 17:18:48 +02:00
|
|
|
showIntro: {
|
|
|
|
key: 'showIntro',
|
|
|
|
default: '1',
|
2019-08-06 13:28:11 +02:00
|
|
|
current: '',
|
2019-08-05 17:18:48 +02:00
|
|
|
},
|
2019-11-08 12:42:30 +01:00
|
|
|
showUpdate4: {
|
|
|
|
key: 'showUpdate4',
|
2019-09-16 00:10:14 +02:00
|
|
|
default: '1',
|
|
|
|
current: '',
|
|
|
|
},
|
2019-07-31 14:22:36 +02:00
|
|
|
proxiwashNotifications: {
|
|
|
|
key: 'proxiwashNotifications',
|
|
|
|
default: '5',
|
2019-08-06 13:28:11 +02:00
|
|
|
current: '',
|
2019-07-31 14:22:36 +02:00
|
|
|
},
|
2019-08-06 13:28:11 +02:00
|
|
|
proxiwashWatchedMachines: {
|
2019-07-31 14:22:36 +02:00
|
|
|
key: 'proxiwashWatchedMachines',
|
|
|
|
default: '[]',
|
2019-08-06 13:28:11 +02:00
|
|
|
current: '',
|
2019-07-31 14:22:36 +02:00
|
|
|
},
|
|
|
|
nightMode: {
|
|
|
|
key: 'nightMode',
|
2019-08-06 13:28:11 +02:00
|
|
|
default: '0',
|
|
|
|
current: '',
|
2019-08-13 12:20:03 +02:00
|
|
|
},
|
|
|
|
expoToken: {
|
|
|
|
key: 'expoToken',
|
|
|
|
default: '',
|
|
|
|
current: '',
|
2019-08-26 12:49:26 +02:00
|
|
|
},
|
|
|
|
debugUnlocked: {
|
|
|
|
key: 'debugUnlocked',
|
|
|
|
default: '0',
|
|
|
|
current: '',
|
2019-09-12 23:02:28 +02:00
|
|
|
},
|
|
|
|
defaultStartScreen: {
|
|
|
|
key: 'defaultStartScreen',
|
|
|
|
default: 'Home',
|
|
|
|
current: '',
|
2019-07-31 14:22:36 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-08-06 13:28:11 +02:00
|
|
|
/**
|
|
|
|
* Set preferences object current values from AsyncStorage.
|
|
|
|
* This function should be called at the app's start.
|
|
|
|
*
|
|
|
|
* @return {Promise<void>}
|
|
|
|
*/
|
2019-07-31 14:22:36 +02:00
|
|
|
async loadPreferences() {
|
|
|
|
let prefKeys = [];
|
|
|
|
// Get all available keys
|
|
|
|
for (let [key, value] of Object.entries(this.preferences)) {
|
|
|
|
prefKeys.push(value.key);
|
|
|
|
}
|
|
|
|
// Get corresponding values
|
2019-08-06 13:28:11 +02:00
|
|
|
let resultArray: Array<Array<string>> = await AsyncStorage.multiGet(prefKeys);
|
2019-07-31 14:22:36 +02:00
|
|
|
// Save those values for later use
|
|
|
|
for (let i = 0; i < resultArray.length; i++) {
|
2019-08-06 13:28:11 +02:00
|
|
|
let key: string = resultArray[i][0];
|
|
|
|
let val: string | null = resultArray[i][1];
|
2019-07-31 14:22:36 +02:00
|
|
|
if (val === null)
|
|
|
|
val = this.preferences[key].default;
|
|
|
|
this.preferences[key].current = val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-06 13:28:11 +02:00
|
|
|
/**
|
|
|
|
* Save the value associated to the given key to preferences.
|
|
|
|
* This updates the preferences object and saves it to AsynStorage.
|
|
|
|
*
|
|
|
|
* @param key
|
|
|
|
* @param val
|
|
|
|
*/
|
|
|
|
savePref(key: string, val: string) {
|
2019-07-31 14:22:36 +02:00
|
|
|
this.preferences[key].current = val;
|
|
|
|
AsyncStorage.setItem(key, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|