Improved async storage usage

This commit is contained in:
Arnaud Vergnet 2020-07-23 12:03:51 +02:00
parent 2b7e6b4541
commit 6254ce1814
19 changed files with 168 additions and 164 deletions

18
App.js
View file

@ -56,7 +56,6 @@ export default class App extends React.Component<Props, State> {
createDrawerNavigator: () => React.Node; createDrawerNavigator: () => React.Node;
urlHandler: URLHandler; urlHandler: URLHandler;
storageManager: AsyncStorageManager;
constructor() { constructor() {
super(); super();
@ -64,7 +63,6 @@ export default class App extends React.Component<Props, State> {
this.navigatorRef = React.createRef(); this.navigatorRef = React.createRef();
this.defaultHomeRoute = null; this.defaultHomeRoute = null;
this.defaultHomeData = {}; this.defaultHomeData = {};
this.storageManager = AsyncStorageManager.getInstance();
this.urlHandler = new URLHandler(this.onInitialURLParsed, this.onDetectURL); this.urlHandler = new URLHandler(this.onInitialURLParsed, this.onDetectURL);
this.urlHandler.listen(); this.urlHandler.listen();
setSafeBounceHeight(Platform.OS === 'ios' ? 100 : 20); setSafeBounceHeight(Platform.OS === 'ios' ? 100 : 20);
@ -133,9 +131,9 @@ export default class App extends React.Component<Props, State> {
showUpdate: false, showUpdate: false,
showAprilFools: false, showAprilFools: false,
}); });
this.storageManager.savePref(this.storageManager.preferences.showIntro.key, '0'); AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.showIntro.key, false);
this.storageManager.savePref(this.storageManager.preferences.updateNumber.key, Update.number.toString()); AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.updateNumber.key, Update.number);
this.storageManager.savePref(this.storageManager.preferences.showAprilFoolsStart.key, '0'); AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.showAprilFoolsStart.key, false);
}; };
/** /**
@ -144,7 +142,7 @@ export default class App extends React.Component<Props, State> {
* @returns {Promise<void>} * @returns {Promise<void>}
*/ */
loadAssetsAsync = async () => { loadAssetsAsync = async () => {
await this.storageManager.loadPreferences(); await AsyncStorageManager.getInstance().loadPreferences();
try { try {
await ConnectionManager.getInstance().recoverLogin(); await ConnectionManager.getInstance().recoverLogin();
} catch (e) { } catch (e) {
@ -169,9 +167,11 @@ export default class App extends React.Component<Props, State> {
this.setState({ this.setState({
isLoading: false, isLoading: false,
currentTheme: ThemeManager.getCurrentTheme(), currentTheme: ThemeManager.getCurrentTheme(),
showIntro: this.storageManager.preferences.showIntro.current === '1', showIntro: AsyncStorageManager.getBool(AsyncStorageManager.PREFERENCES.showIntro.key),
showUpdate: this.storageManager.preferences.updateNumber.current !== Update.number.toString(), showUpdate: AsyncStorageManager.getNumber(AsyncStorageManager.PREFERENCES.updateNumber.key)
showAprilFools: AprilFoolsManager.getInstance().isAprilFoolsEnabled() && this.storageManager.preferences.showAprilFoolsStart.current === '1', !== Update.number,
showAprilFools: AprilFoolsManager.getInstance().isAprilFoolsEnabled()
&& AsyncStorageManager.getBool(AsyncStorageManager.PREFERENCES.showAprilFoolsStart.key),
}); });
SplashScreen.hide(); SplashScreen.hide();
} }

View file

@ -13,117 +13,86 @@ export default class AsyncStorageManager {
static instance: AsyncStorageManager | null = null; static instance: AsyncStorageManager | null = null;
/** static PREFERENCES = {
* Get this class instance or create one if none is found
* @returns {AsyncStorageManager}
*/
static getInstance(): AsyncStorageManager {
return AsyncStorageManager.instance === null ?
AsyncStorageManager.instance = new AsyncStorageManager() :
AsyncStorageManager.instance;
}
// Object storing preferences keys, default and current values for use in the app
preferences = {
debugUnlocked: { debugUnlocked: {
key: 'debugUnlocked', key: 'debugUnlocked',
default: '0', default: '0',
current: '',
}, },
showIntro: { showIntro: {
key: 'showIntro', key: 'showIntro',
default: '1', default: '1',
current: '',
}, },
updateNumber: { updateNumber: {
key: 'updateNumber', key: 'updateNumber',
default: '0', default: '0',
current: '',
}, },
proxiwashNotifications: { proxiwashNotifications: {
key: 'proxiwashNotifications', key: 'proxiwashNotifications',
default: '5', default: '5',
current: '',
}, },
nightModeFollowSystem: { nightModeFollowSystem: {
key: 'nightModeFollowSystem', key: 'nightModeFollowSystem',
default: '1', default: '1',
current: '',
}, },
nightMode: { nightMode: {
key: 'nightMode', key: 'nightMode',
default: '1', default: '1',
current: '',
}, },
defaultStartScreen: { defaultStartScreen: {
key: 'defaultStartScreen', key: 'defaultStartScreen',
default: 'home', default: 'home',
current: '',
}, },
servicesShowBanner: { servicesShowBanner: {
key: 'servicesShowBanner', key: 'servicesShowBanner',
default: '1', default: '1',
current: '',
}, },
proxiwashShowBanner: { proxiwashShowBanner: {
key: 'proxiwashShowBanner', key: 'proxiwashShowBanner',
default: '1', default: '1',
current: '',
}, },
homeShowBanner: { homeShowBanner: {
key: 'homeShowBanner', key: 'homeShowBanner',
default: '1', default: '1',
current: '',
}, },
eventsShowBanner: { eventsShowBanner: {
key: 'eventsShowBanner', key: 'eventsShowBanner',
default: '1', default: '1',
current: '',
}, },
planexShowBanner: { planexShowBanner: {
key: 'planexShowBanner', key: 'planexShowBanner',
default: '1', default: '1',
current: '',
}, },
loginShowBanner: { loginShowBanner: {
key: 'loginShowBanner', key: 'loginShowBanner',
default: '1', default: '1',
current: '',
}, },
voteShowBanner: { voteShowBanner: {
key: 'voteShowBanner', key: 'voteShowBanner',
default: '1', default: '1',
current: '',
}, },
equipmentShowBanner: { equipmentShowBanner: {
key: 'equipmentShowBanner', key: 'equipmentShowBanner',
default: '1', default: '1',
current: '',
}, },
gameStartShowBanner: { gameStartShowBanner: {
key: 'gameStartShowBanner', key: 'gameStartShowBanner',
default: '1', default: '1',
current: '',
}, },
proxiwashWatchedMachines: { proxiwashWatchedMachines: {
key: 'proxiwashWatchedMachines', key: 'proxiwashWatchedMachines',
default: '[]', default: '[]',
current: '',
}, },
showAprilFoolsStart: { showAprilFoolsStart: {
key: 'showAprilFoolsStart', key: 'showAprilFoolsStart',
default: '1', default: '1',
current: '',
}, },
planexCurrentGroup: { planexCurrentGroup: {
key: 'planexCurrentGroup', key: 'planexCurrentGroup',
default: '', default: '',
current: '',
}, },
planexFavoriteGroups: { planexFavoriteGroups: {
key: 'planexFavoriteGroups', key: 'planexFavoriteGroups',
default: '[]', default: '[]',
current: '',
}, },
dashboardItems: { dashboardItems: {
key: 'dashboardItems', key: 'dashboardItems',
@ -134,14 +103,28 @@ export default class AsyncStorageManager {
SERVICES_KEY.TUTOR_INSA, SERVICES_KEY.TUTOR_INSA,
SERVICES_KEY.RU, SERVICES_KEY.RU,
]), ]),
current: '',
}, },
gameScores: { gameScores: {
key: 'gameScores', key: 'gameScores',
default: '[]', default: '[]',
current: '',
}, },
}; }
#currentPreferences: {[key: string]: string};
constructor() {
this.#currentPreferences = {};
}
/**
* Get this class instance or create one if none is found
* @returns {AsyncStorageManager}
*/
static getInstance(): AsyncStorageManager {
return AsyncStorageManager.instance === null ?
AsyncStorageManager.instance = new AsyncStorageManager() :
AsyncStorageManager.instance;
}
/** /**
* Set preferences object current values from AsyncStorage. * Set preferences object current values from AsyncStorage.
@ -152,9 +135,8 @@ export default class AsyncStorageManager {
async loadPreferences() { async loadPreferences() {
let prefKeys = []; let prefKeys = [];
// Get all available keys // Get all available keys
for (let [key, value] of Object.entries(this.preferences)) { for (let key in AsyncStorageManager.PREFERENCES) {
//$FlowFixMe prefKeys.push(key);
prefKeys.push(value.key);
} }
// Get corresponding values // Get corresponding values
let resultArray: Array<Array<string>> = await AsyncStorage.multiGet(prefKeys); let resultArray: Array<Array<string>> = await AsyncStorage.multiGet(prefKeys);
@ -163,21 +145,92 @@ export default class AsyncStorageManager {
let key: string = resultArray[i][0]; let key: string = resultArray[i][0];
let val: string | null = resultArray[i][1]; let val: string | null = resultArray[i][1];
if (val === null) if (val === null)
val = this.preferences[key].default; val = AsyncStorageManager.PREFERENCES[key].default;
this.preferences[key].current = val; this.#currentPreferences[key] = val;
} }
} }
/** /**
* Save the value associated to the given key to preferences. * Saves the value associated to the given key to preferences.
* This updates the preferences object and saves it to AsyncStorage. * This updates the preferences object and saves it to AsyncStorage.
* *
* @param key * @param key
* @param val * @param value
*/ */
savePref(key: string, val: string) { setPreference(key: string, value: any) {
this.preferences[key].current = val; if (AsyncStorageManager.PREFERENCES[key] != null) {
AsyncStorage.setItem(key, val); let convertedValue = "";
if (typeof value === "string")
convertedValue = value;
else if (typeof value === "boolean" || typeof value === "number")
convertedValue = value.toString();
else
convertedValue = JSON.stringify(value);
this.#currentPreferences[key] = convertedValue;
AsyncStorage.setItem(key, convertedValue);
}
}
/**
* Gets the value at the given key.
* If the key is not available, returns null
*
* @param key
* @returns {string|null}
*/
getPreference(key: string) {
return this.#currentPreferences[key];
}
/**
* aves the value associated to the given key to preferences.
*
* @param key
* @param value
*/
static set(key: string, value: any) {
AsyncStorageManager.getInstance().setPreference(key, value);
}
/**
* Gets the string value of the given preference
*
* @param key
* @returns {boolean}
*/
static getString(key: string) {
return AsyncStorageManager.getInstance().getPreference(key);
}
/**
* Gets the boolean value of the given preference
*
* @param key
* @returns {boolean}
*/
static getBool(key: string) {
const value = AsyncStorageManager.getString(key);
return value === "1" || value === "true";
}
/**
* Gets the number value of the given preference
*
* @param key
* @returns {boolean}
*/
static getNumber(key: string) {
return parseFloat(AsyncStorageManager.getString(key));
}
/**
* Gets the object value of the given preference
*
* @param key
* @returns {boolean}
*/
static getObject(key: string) {
return JSON.parse(AsyncStorageManager.getString(key));
} }
} }

View file

@ -7,14 +7,15 @@ import {getSublistWithIds} from "../utils/Utils";
import AsyncStorageManager from "./AsyncStorageManager"; import AsyncStorageManager from "./AsyncStorageManager";
export default class DashboardManager extends ServicesManager{ export default class DashboardManager extends ServicesManager {
constructor(nav: StackNavigationProp) { constructor(nav: StackNavigationProp) {
super(nav) super(nav)
} }
getCurrentDashboard(): Array<ServiceItem> { getCurrentDashboard(): Array<ServiceItem> {
const dashboardIdList = JSON.parse(AsyncStorageManager.getInstance().preferences.dashboardItems.current); const dashboardIdList = AsyncStorageManager
.getObject(AsyncStorageManager.PREFERENCES.dashboardItems.key);
const allDatasets = [ const allDatasets = [
...this.amicaleDataset, ...this.amicaleDataset,
...this.studentsDataset, ...this.studentsDataset,

View file

@ -228,10 +228,11 @@ export default class ThemeManager {
* @returns {boolean} Night mode state * @returns {boolean} Night mode state
*/ */
static getNightMode(): boolean { static getNightMode(): boolean {
return (AsyncStorageManager.getInstance().preferences.nightMode.current === '1' && return (AsyncStorageManager.getBool(AsyncStorageManager.PREFERENCES.nightMode.key) &&
(AsyncStorageManager.getInstance().preferences.nightModeFollowSystem.current !== '1' || (!AsyncStorageManager.getBool(AsyncStorageManager.PREFERENCES.nightModeFollowSystem.key)
colorScheme === 'no-preference')) || || colorScheme === 'no-preference')) ||
(AsyncStorageManager.getInstance().preferences.nightModeFollowSystem.current === '1' && colorScheme === 'dark'); (AsyncStorageManager.getBool(AsyncStorageManager.PREFERENCES.nightModeFollowSystem.key)
&& colorScheme === 'dark');
} }
/** /**
@ -273,8 +274,7 @@ export default class ThemeManager {
* @param isNightMode True to enable night mode, false to disable * @param isNightMode True to enable night mode, false to disable
*/ */
setNightMode(isNightMode: boolean) { setNightMode(isNightMode: boolean) {
let nightModeKey = AsyncStorageManager.getInstance().preferences.nightMode.key; AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.nightMode.key, isNightMode);
AsyncStorageManager.getInstance().savePref(nightModeKey, isNightMode ? '1' : '0');
if (this.updateThemeCallback != null) if (this.updateThemeCallback != null)
this.updateThemeCallback(); this.updateThemeCallback();
} }

View file

@ -202,7 +202,7 @@ export default class TabNavigator extends React.Component<Props> {
if (props.defaultHomeRoute != null) if (props.defaultHomeRoute != null)
this.defaultRoute = 'home'; this.defaultRoute = 'home';
else else
this.defaultRoute = AsyncStorageManager.getInstance().preferences.defaultStartScreen.current.toLowerCase(); this.defaultRoute = AsyncStorageManager.getString(AsyncStorageManager.PREFERENCES.defaultStartScreen.key).toLowerCase();
this.createHomeStackComponent = () => HomeStackComponent(props.defaultHomeRoute, props.defaultHomeData); this.createHomeStackComponent = () => HomeStackComponent(props.defaultHomeRoute, props.defaultHomeData);
} }

View file

@ -43,10 +43,11 @@ class DebugScreen extends React.Component<Props, State> {
constructor(props) { constructor(props) {
super(props); super(props);
this.modalInputValue = ""; this.modalInputValue = "";
let copy = {...AsyncStorageManager.getInstance().preferences};
let currentPreferences : Array<PreferenceItem> = []; let currentPreferences : Array<PreferenceItem> = [];
Object.values(copy).map((object: any) => { Object.values(AsyncStorageManager.PREFERENCES).map((object: any) => {
currentPreferences.push(object); let newObject: PreferenceItem = {...object};
newObject.current = AsyncStorageManager.getString(newObject.key);
currentPreferences.push(newObject);
}); });
this.state = { this.state = {
modalCurrentDisplayItem: {}, modalCurrentDisplayItem: {},
@ -139,7 +140,7 @@ class DebugScreen extends React.Component<Props, State> {
currentPreferences[this.findIndexOfKey(key)].current = value; currentPreferences[this.findIndexOfKey(key)].current = value;
return {currentPreferences}; return {currentPreferences};
}); });
AsyncStorageManager.getInstance().savePref(key, value); AsyncStorageManager.set(key, value);
this.modalRef.close(); this.modalRef.close();
} }

View file

@ -42,7 +42,7 @@ const LIST_ITEM_HEIGHT = 64;
class EquipmentListScreen extends React.Component<Props, State> { class EquipmentListScreen extends React.Component<Props, State> {
state = { state = {
mascotDialogVisible: AsyncStorageManager.getInstance().preferences.equipmentShowBanner.current === "1" mascotDialogVisible: AsyncStorageManager.getBool(AsyncStorageManager.PREFERENCES.equipmentShowBanner.key),
} }
data: Array<Device>; data: Array<Device>;
@ -146,10 +146,7 @@ class EquipmentListScreen extends React.Component<Props, State> {
}; };
hideMascotDialog = () => { hideMascotDialog = () => {
AsyncStorageManager.getInstance().savePref( AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.equipmentShowBanner.key, false);
AsyncStorageManager.getInstance().preferences.equipmentShowBanner.key,
'0'
);
this.setState({mascotDialogVisible: false}) this.setState({mascotDialogVisible: false})
}; };

View file

@ -48,7 +48,7 @@ class LoginScreen extends React.Component<Props, State> {
loading: false, loading: false,
dialogVisible: false, dialogVisible: false,
dialogError: 0, dialogError: 0,
mascotDialogVisible: AsyncStorageManager.getInstance().preferences.loginShowBanner.current === "1" mascotDialogVisible: AsyncStorageManager.getBool(AsyncStorageManager.PREFERENCES.loginShowBanner.key),
}; };
onEmailChange: (value: string) => null; onEmailChange: (value: string) => null;
@ -82,10 +82,7 @@ class LoginScreen extends React.Component<Props, State> {
} }
hideMascotDialog = () => { hideMascotDialog = () => {
AsyncStorageManager.getInstance().savePref( AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.loginShowBanner.key, false);
AsyncStorageManager.getInstance().preferences.loginShowBanner.key,
'0'
);
this.setState({mascotDialogVisible: false}) this.setState({mascotDialogVisible: false})
}; };
@ -111,11 +108,8 @@ class LoginScreen extends React.Component<Props, State> {
* Saves in user preferences to not show the login banner again. * Saves in user preferences to not show the login banner again.
*/ */
handleSuccess = () => { handleSuccess = () => {
// Do not show the login banner again // Do not show the home login banner again
AsyncStorageManager.getInstance().savePref( AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.homeShowBanner.key, false);
AsyncStorageManager.getInstance().preferences.homeShowBanner.key,
'0'
);
if (this.nextScreen == null) if (this.nextScreen == null)
this.props.navigation.goBack(); this.props.navigation.goBack();
else else

View file

@ -108,7 +108,7 @@ export default class VoteScreen extends React.Component<Props, State> {
state = { state = {
hasVoted: false, hasVoted: false,
mascotDialogVisible: AsyncStorageManager.getInstance().preferences.voteShowBanner.current === "1", mascotDialogVisible: AsyncStorageManager.getBool(AsyncStorageManager.PREFERENCES.voteShowBanner.key),
}; };
teams: Array<team>; teams: Array<team>;
@ -326,10 +326,7 @@ export default class VoteScreen extends React.Component<Props, State> {
}; };
hideMascotDialog = () => { hideMascotDialog = () => {
AsyncStorageManager.getInstance().savePref( AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.voteShowBanner.key, false);
AsyncStorageManager.getInstance().preferences.voteShowBanner.key,
'0'
);
this.setState({mascotDialogVisible: false}) this.setState({mascotDialogVisible: false})
}; };

View file

@ -46,13 +46,13 @@ class GameStartScreen extends React.Component<Props, State> {
isHighScore: boolean; isHighScore: boolean;
state = { state = {
mascotDialogVisible: AsyncStorageManager.getInstance().preferences.gameStartShowBanner.current === "1", mascotDialogVisible: AsyncStorageManager.getBool(AsyncStorageManager.PREFERENCES.gameStartShowBanner.key),
} }
constructor(props: Props) { constructor(props: Props) {
super(props); super(props);
this.gridManager = new GridManager(4, 4, props.theme); this.gridManager = new GridManager(4, 4, props.theme);
this.scores = JSON.parse(AsyncStorageManager.getInstance().preferences.gameScores.current); this.scores = AsyncStorageManager.getObject(AsyncStorageManager.PREFERENCES.gameScores.key);
this.scores.sort((a, b) => b - a); this.scores.sort((a, b) => b - a);
if (this.props.route.params != null) if (this.props.route.params != null)
this.recoverGameScore(); this.recoverGameScore();
@ -72,17 +72,11 @@ class GameStartScreen extends React.Component<Props, State> {
} }
if (this.scores.length > 3) if (this.scores.length > 3)
this.scores.splice(3, 1); this.scores.splice(3, 1);
AsyncStorageManager.getInstance().savePref( AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.gameScores.key, this.scores);
AsyncStorageManager.getInstance().preferences.gameScores.key,
JSON.stringify(this.scores)
);
} }
hideMascotDialog = () => { hideMascotDialog = () => {
AsyncStorageManager.getInstance().savePref( AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.gameStartShowBanner.key, false);
AsyncStorageManager.getInstance().preferences.gameStartShowBanner.key,
'0'
);
this.setState({mascotDialogVisible: false}) this.setState({mascotDialogVisible: false})
}; };

View file

@ -112,7 +112,8 @@ class HomeScreen extends React.Component<Props, State> {
}); });
this.state = { this.state = {
dialogVisible: false, dialogVisible: false,
mascotDialogVisible: AsyncStorageManager.getInstance().preferences.homeShowBanner.current === "1" mascotDialogVisible: AsyncStorageManager.getBool(
AsyncStorageManager.PREFERENCES.homeShowBanner.key)
&& !this.isLoggedIn, && !this.isLoggedIn,
} }
} }
@ -184,10 +185,7 @@ class HomeScreen extends React.Component<Props, State> {
}; };
hideMascotDialog = () => { hideMascotDialog = () => {
AsyncStorageManager.getInstance().savePref( AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.homeShowBanner.key, false);
AsyncStorageManager.getInstance().preferences.homeShowBanner.key,
'0'
);
this.setState({mascotDialogVisible: false}) this.setState({mascotDialogVisible: false})
}; };

View file

@ -38,7 +38,7 @@ class DashboardEditScreen extends React.Component<Props, State> {
constructor(props: Props) { constructor(props: Props) {
super(props); super(props);
let dashboardManager = new DashboardManager(this.props.navigation); let dashboardManager = new DashboardManager(this.props.navigation);
this.initialDashboardIdList = JSON.parse(AsyncStorageManager.getInstance().preferences.dashboardItems.current); this.initialDashboardIdList = AsyncStorageManager.getObject(AsyncStorageManager.PREFERENCES.dashboardItems.key);
this.initialDashboard = dashboardManager.getCurrentDashboard(); this.initialDashboard = dashboardManager.getCurrentDashboard();
this.state = { this.state = {
currentDashboard: [...this.initialDashboard], currentDashboard: [...this.initialDashboard],
@ -92,10 +92,7 @@ class DashboardEditScreen extends React.Component<Props, State> {
currentDashboard: currentDashboard, currentDashboard: currentDashboard,
currentDashboardIdList: currentDashboardIdList, currentDashboardIdList: currentDashboardIdList,
}); });
AsyncStorageManager.getInstance().savePref( AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.dashboardItems.key, currentDashboardIdList);
AsyncStorageManager.getInstance().preferences.dashboardItems.key,
JSON.stringify(currentDashboardIdList)
);
} }
undoDashboard = () => { undoDashboard = () => {
@ -103,10 +100,7 @@ class DashboardEditScreen extends React.Component<Props, State> {
currentDashboard: [...this.initialDashboard], currentDashboard: [...this.initialDashboard],
currentDashboardIdList: [...this.initialDashboardIdList] currentDashboardIdList: [...this.initialDashboardIdList]
}); });
AsyncStorageManager.getInstance().savePref( AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.dashboardItems.key, this.initialDashboardIdList);
AsyncStorageManager.getInstance().preferences.dashboardItems.key,
JSON.stringify(this.initialDashboardIdList)
);
} }
getListHeader() { getListHeader() {

View file

@ -37,18 +37,18 @@ class SettingsScreen extends React.Component<Props, State> {
*/ */
constructor() { constructor() {
super(); super();
let notifReminder = AsyncStorageManager.getInstance().preferences.proxiwashNotifications.current; let notifReminder = AsyncStorageManager.getString(AsyncStorageManager.PREFERENCES.proxiwashNotifications.key);
this.savedNotificationReminder = parseInt(notifReminder); this.savedNotificationReminder = parseInt(notifReminder);
if (isNaN(this.savedNotificationReminder)) if (isNaN(this.savedNotificationReminder))
this.savedNotificationReminder = 0; this.savedNotificationReminder = 0;
this.state = { this.state = {
nightMode: ThemeManager.getNightMode(), nightMode: ThemeManager.getNightMode(),
nightModeFollowSystem: AsyncStorageManager.getInstance().preferences.nightModeFollowSystem.current === '1' && nightModeFollowSystem: AsyncStorageManager.getBool(AsyncStorageManager.PREFERENCES.nightModeFollowSystem.key)
Appearance.getColorScheme() !== 'no-preference', && Appearance.getColorScheme() !== 'no-preference',
notificationReminderSelected: this.savedNotificationReminder, notificationReminderSelected: this.savedNotificationReminder,
startScreenPickerSelected: AsyncStorageManager.getInstance().preferences.defaultStartScreen.current, startScreenPickerSelected: AsyncStorageManager.getString(AsyncStorageManager.PREFERENCES.defaultStartScreen.key),
isDebugUnlocked: AsyncStorageManager.getInstance().preferences.debugUnlocked.current === '1' isDebugUnlocked: AsyncStorageManager.getBool(AsyncStorageManager.PREFERENCES.debugUnlocked.key)
}; };
} }
@ -57,8 +57,7 @@ class SettingsScreen extends React.Component<Props, State> {
*/ */
unlockDebugMode = () => { unlockDebugMode = () => {
this.setState({isDebugUnlocked: true}); this.setState({isDebugUnlocked: true});
let key = AsyncStorageManager.getInstance().preferences.debugUnlocked.key; AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.debugUnlocked.key, true);
AsyncStorageManager.getInstance().savePref(key, '1');
} }
/** /**
@ -67,9 +66,8 @@ class SettingsScreen extends React.Component<Props, State> {
* @param value The value to store * @param value The value to store
*/ */
onProxiwashNotifPickerValueChange = (value: number) => { onProxiwashNotifPickerValueChange = (value: number) => {
let key = AsyncStorageManager.getInstance().preferences.proxiwashNotifications.key; this.setState({notificationReminderSelected: value});
AsyncStorageManager.getInstance().savePref(key, value.toString()); AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.proxiwashNotifications.key, value);
this.setState({notificationReminderSelected: value})
}; };
/** /**
@ -79,11 +77,8 @@ class SettingsScreen extends React.Component<Props, State> {
*/ */
onStartScreenPickerValueChange = (value: string) => { onStartScreenPickerValueChange = (value: string) => {
if (value != null) { if (value != null) {
let key = AsyncStorageManager.getInstance().preferences.defaultStartScreen.key; this.setState({startScreenPickerSelected: value});
AsyncStorageManager.getInstance().savePref(key, value); AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.defaultStartScreen.key, value);
this.setState({
startScreenPickerSelected: value
});
} }
}; };
@ -139,8 +134,7 @@ class SettingsScreen extends React.Component<Props, State> {
onToggleNightModeFollowSystem = () => { onToggleNightModeFollowSystem = () => {
const value = !this.state.nightModeFollowSystem; const value = !this.state.nightModeFollowSystem;
this.setState({nightModeFollowSystem: value}); this.setState({nightModeFollowSystem: value});
let key = AsyncStorageManager.getInstance().preferences.nightModeFollowSystem.key; AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.nightModeFollowSystem.key, value);
AsyncStorageManager.getInstance().savePref(key, value ? '1' : '0');
if (value) { if (value) {
const nightMode = Appearance.getColorScheme() === 'dark'; const nightMode = Appearance.getColorScheme() === 'dark';
ThemeManager.getInstance().setNightMode(nightMode); ThemeManager.getInstance().setNightMode(nightMode);

View file

@ -53,7 +53,7 @@ class GroupSelectionScreen extends React.Component<Props, State> {
super(props); super(props);
this.state = { this.state = {
currentSearchString: '', currentSearchString: '',
favoriteGroups: JSON.parse(AsyncStorageManager.getInstance().preferences.planexFavoriteGroups.current), favoriteGroups: AsyncStorageManager.getObject(AsyncStorageManager.PREFERENCES.planexFavoriteGroups.key),
}; };
} }
@ -172,9 +172,7 @@ class GroupSelectionScreen extends React.Component<Props, State> {
else else
this.addGroupToFavorites(newFavorites, group); this.addGroupToFavorites(newFavorites, group);
this.setState({favoriteGroups: newFavorites}) this.setState({favoriteGroups: newFavorites})
AsyncStorageManager.getInstance().savePref( AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.planexFavoriteGroups.key, newFavorites);
AsyncStorageManager.getInstance().preferences.planexFavoriteGroups.key,
JSON.stringify(newFavorites));
} }
/** /**

View file

@ -135,7 +135,7 @@ class PlanexScreen extends React.Component<Props, State> {
this.webScreenRef = React.createRef(); this.webScreenRef = React.createRef();
this.barRef = React.createRef(); this.barRef = React.createRef();
let currentGroup = AsyncStorageManager.getInstance().preferences.planexCurrentGroup.current; let currentGroup = AsyncStorageManager.getString(AsyncStorageManager.PREFERENCES.planexCurrentGroup.key);
if (currentGroup === '') if (currentGroup === '')
currentGroup = {name: "SELECT GROUP", id: -1, isFav: false}; currentGroup = {name: "SELECT GROUP", id: -1, isFav: false};
else { else {
@ -144,8 +144,9 @@ class PlanexScreen extends React.Component<Props, State> {
} }
this.state = { this.state = {
mascotDialogVisible: mascotDialogVisible:
AsyncStorageManager.getInstance().preferences.planexShowBanner.current === '1' && AsyncStorageManager.getBool(AsyncStorageManager.PREFERENCES.planexShowBanner.key)
AsyncStorageManager.getInstance().preferences.defaultStartScreen.current !== 'Planex', && AsyncStorageManager.getString(AsyncStorageManager.PREFERENCES.defaultStartScreen.key)
.toLowerCase() !== 'planex',
dialogVisible: false, dialogVisible: false,
dialogTitle: "", dialogTitle: "",
dialogMessage: "", dialogMessage: "",
@ -167,10 +168,7 @@ class PlanexScreen extends React.Component<Props, State> {
*/ */
onMascotDialogCancel = () => { onMascotDialogCancel = () => {
this.setState({mascotDialogVisible: false}); this.setState({mascotDialogVisible: false});
AsyncStorageManager.getInstance().savePref( AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.planexShowBanner.key, false);
AsyncStorageManager.getInstance().preferences.planexShowBanner.key,
'0'
);
}; };
@ -208,10 +206,7 @@ class PlanexScreen extends React.Component<Props, State> {
selectNewGroup(group: group) { selectNewGroup(group: group) {
this.sendMessage('setGroup', group.id); this.sendMessage('setGroup', group.id);
this.setState({currentGroup: group}); this.setState({currentGroup: group});
AsyncStorageManager.getInstance().savePref( AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.planexCurrentGroup.key, group);
AsyncStorageManager.getInstance().preferences.planexCurrentGroup.key,
JSON.stringify(group)
);
this.props.navigation.setOptions({title: group.name}); this.props.navigation.setOptions({title: group.name});
this.generateInjectedJS(group.id); this.generateInjectedJS(group.id);
} }

View file

@ -56,7 +56,7 @@ class PlanningScreen extends React.Component<Props, State> {
refreshing: false, refreshing: false,
agendaItems: {}, agendaItems: {},
calendarShowing: false, calendarShowing: false,
mascotDialogVisible: AsyncStorageManager.getInstance().preferences.eventsShowBanner.current === "1" mascotDialogVisible: AsyncStorageManager.getBool(AsyncStorageManager.PREFERENCES.eventsShowBanner.key)
}; };
currentDate = getDateOnlyString(getCurrentDateString()); currentDate = getDateOnlyString(getCurrentDateString());
@ -111,10 +111,7 @@ class PlanningScreen extends React.Component<Props, State> {
*/ */
onHideMascotDialog = () => { onHideMascotDialog = () => {
this.setState({mascotDialogVisible: false}); this.setState({mascotDialogVisible: false});
AsyncStorageManager.getInstance().savePref( AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.eventsShowBanner.key, false);
AsyncStorageManager.getInstance().preferences.eventsShowBanner.key,
'0'
);
}; };
/** /**

View file

@ -66,8 +66,8 @@ class ProxiwashScreen extends React.Component<Props, State> {
state = { state = {
refreshing: false, refreshing: false,
modalCurrentDisplayItem: null, modalCurrentDisplayItem: null,
machinesWatched: JSON.parse(AsyncStorageManager.getInstance().preferences.proxiwashWatchedMachines.current), machinesWatched: AsyncStorageManager.getObject(AsyncStorageManager.PREFERENCES.proxiwashWatchedMachines.key),
mascotDialogVisible: AsyncStorageManager.getInstance().preferences.proxiwashShowBanner.current === "1", mascotDialogVisible: AsyncStorageManager.getBool(AsyncStorageManager.PREFERENCES.proxiwashShowBanner.key),
}; };
/** /**
@ -90,10 +90,7 @@ class ProxiwashScreen extends React.Component<Props, State> {
*/ */
onHideMascotDialog = () => { onHideMascotDialog = () => {
this.setState({mascotDialogVisible: false}); this.setState({mascotDialogVisible: false});
AsyncStorageManager.getInstance().savePref( AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.proxiwashShowBanner.key, false);
AsyncStorageManager.getInstance().preferences.proxiwashShowBanner.key,
'0'
);
}; };
/** /**
@ -185,10 +182,7 @@ class ProxiwashScreen extends React.Component<Props, State> {
saveNewWatchedList(list: Array<Machine>) { saveNewWatchedList(list: Array<Machine>) {
this.setState({machinesWatched: list}); this.setState({machinesWatched: list});
AsyncStorageManager.getInstance().savePref( AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.proxiwashWatchedMachines.key, list);
AsyncStorageManager.getInstance().preferences.proxiwashWatchedMachines.key,
JSON.stringify(list),
);
} }
/** /**

View file

@ -38,7 +38,7 @@ class ServicesScreen extends React.Component<Props, State> {
finalDataset: Array<listItem> finalDataset: Array<listItem>
state = { state = {
mascotDialogVisible: AsyncStorageManager.getInstance().preferences.servicesShowBanner.current === "1" mascotDialogVisible: AsyncStorageManager.getBool(AsyncStorageManager.PREFERENCES.servicesShowBanner.key),
} }
constructor(props) { constructor(props) {
@ -60,10 +60,7 @@ class ServicesScreen extends React.Component<Props, State> {
*/ */
onHideMascotDialog = () => { onHideMascotDialog = () => {
this.setState({mascotDialogVisible: false}); this.setState({mascotDialogVisible: false});
AsyncStorageManager.getInstance().savePref( AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.servicesShowBanner.key, false);
AsyncStorageManager.getInstance().preferences.servicesShowBanner.key,
'0'
);
}; };
getAboutButton = () => getAboutButton = () =>

View file

@ -43,7 +43,7 @@ export async function askPermissions() {
* @param date The date to trigger the notification at * @param date The date to trigger the notification at
*/ */
function createNotifications(machineID: string, date: Date) { function createNotifications(machineID: string, date: Date) {
let reminder = parseInt(AsyncStorageManager.getInstance().preferences.proxiwashNotifications.current); let reminder = AsyncStorageManager.getNumber(AsyncStorageManager.PREFERENCES.proxiwashNotifications.key);
if (!isNaN(reminder) && reminder > 0) { if (!isNaN(reminder) && reminder > 0) {
let id = reminderIdFactor * parseInt(machineID); let id = reminderIdFactor * parseInt(machineID);
let reminderDate = new Date(date); let reminderDate = new Date(date);