123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- /*
- * Copyright (c) 2019 - 2020 Arnaud Vergnet.
- *
- * This file is part of Campus INSAT.
- *
- * Campus INSAT is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * Campus INSAT is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Campus INSAT. If not, see <https://www.gnu.org/licenses/>.
- */
-
- import AsyncStorage from '@react-native-community/async-storage';
- import {SERVICES_KEY} from './ServicesManager';
-
- /**
- * Singleton 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.
- */
-
- export default class AsyncStorageManager {
- static instance: AsyncStorageManager | null = null;
-
- static PREFERENCES: {[key: string]: {key: string; default: string}} = {
- debugUnlocked: {
- key: 'debugUnlocked',
- default: '0',
- },
- showIntro: {
- key: 'showIntro',
- default: '1',
- },
- updateNumber: {
- key: 'updateNumber',
- default: '0',
- },
- proxiwashNotifications: {
- key: 'proxiwashNotifications',
- default: '5',
- },
- nightModeFollowSystem: {
- key: 'nightModeFollowSystem',
- default: '1',
- },
- nightMode: {
- key: 'nightMode',
- default: '1',
- },
- defaultStartScreen: {
- key: 'defaultStartScreen',
- default: 'home',
- },
- servicesShowMascot: {
- key: 'servicesShowMascot',
- default: '1',
- },
- proxiwashShowMascot: {
- key: 'proxiwashShowMascot',
- default: '1',
- },
- homeShowMascot: {
- key: 'homeShowMascot',
- default: '1',
- },
- eventsShowMascot: {
- key: 'eventsShowMascot',
- default: '1',
- },
- planexShowMascot: {
- key: 'planexShowMascot',
- default: '1',
- },
- loginShowMascot: {
- key: 'loginShowMascot',
- default: '1',
- },
- voteShowMascot: {
- key: 'voteShowMascot',
- default: '1',
- },
- equipmentShowMascot: {
- key: 'equipmentShowMascot',
- default: '1',
- },
- gameStartMascot: {
- key: 'gameStartMascot',
- default: '1',
- },
- proxiwashWatchedMachines: {
- key: 'proxiwashWatchedMachines',
- default: '[]',
- },
- showAprilFoolsStart: {
- key: 'showAprilFoolsStart',
- default: '1',
- },
- planexCurrentGroup: {
- key: 'planexCurrentGroup',
- default: '',
- },
- planexFavoriteGroups: {
- key: 'planexFavoriteGroups',
- default: '[]',
- },
- dashboardItems: {
- key: 'dashboardItems',
- default: JSON.stringify([
- SERVICES_KEY.EMAIL,
- SERVICES_KEY.WASHERS,
- SERVICES_KEY.PROXIMO,
- SERVICES_KEY.TUTOR_INSA,
- SERVICES_KEY.RU,
- ]),
- },
- gameScores: {
- key: 'gameScores',
- default: '[]',
- },
- selectedWash: {
- key: 'selectedWash',
- default: 'washinsa',
- },
- };
-
- private currentPreferences: {[key: string]: string};
-
- constructor() {
- this.currentPreferences = {};
- }
-
- /**
- * Get this class instance or create one if none is found
- * @returns {AsyncStorageManager}
- */
- static getInstance(): AsyncStorageManager {
- if (AsyncStorageManager.instance == null) {
- AsyncStorageManager.instance = new AsyncStorageManager();
- }
- return AsyncStorageManager.instance;
- }
-
- /**
- * Saves the value associated to the given key to preferences.
- *
- * @param key
- * @param value
- */
- static set(
- key: string,
- value: number | string | boolean | object | Array<any>,
- ) {
- AsyncStorageManager.getInstance().setPreference(key, value);
- }
-
- /**
- * Gets the string value of the given preference
- *
- * @param key
- * @returns {string}
- */
- static getString(key: string): string {
- const value = AsyncStorageManager.getInstance().getPreference(key);
- return value != null ? value : '';
- }
-
- /**
- * Gets the boolean value of the given preference
- *
- * @param key
- * @returns {boolean}
- */
- static getBool(key: string): boolean {
- const value = AsyncStorageManager.getString(key);
- return value === '1' || value === 'true';
- }
-
- /**
- * Gets the number value of the given preference
- *
- * @param key
- * @returns {number}
- */
- static getNumber(key: string): number {
- return parseFloat(AsyncStorageManager.getString(key));
- }
-
- /**
- * Gets the object value of the given preference
- *
- * @param key
- * @returns {{...}}
- */
- static getObject<T>(key: string): T {
- return JSON.parse(AsyncStorageManager.getString(key));
- }
-
- /**
- * Set preferences object current values from AsyncStorage.
- * This function should be called at the app's start.
- *
- * @return {Promise<void>}
- */
- async loadPreferences() {
- const prefKeys: Array<string> = [];
- // Get all available keys
- Object.keys(AsyncStorageManager.PREFERENCES).forEach((key: string) => {
- prefKeys.push(key);
- });
- // Get corresponding values
- const resultArray = await AsyncStorage.multiGet(prefKeys);
- // Save those values for later use
- resultArray.forEach((item: [string, string | null]) => {
- const key = item[0];
- let val = item[1];
- if (val === null) {
- val = AsyncStorageManager.PREFERENCES[key].default;
- }
- this.currentPreferences[key] = val;
- });
- }
-
- /**
- * Saves the value associated to the given key to preferences.
- * This updates the preferences object and saves it to AsyncStorage.
- *
- * @param key
- * @param value
- */
- setPreference(
- key: string,
- value: number | string | boolean | object | Array<any>,
- ) {
- if (AsyncStorageManager.PREFERENCES[key] != null) {
- 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): string | null {
- return this.currentPreferences[key];
- }
- }
|