Browse Source

Improved async storage usage

Arnaud Vergnet 1 year ago
parent
commit
6254ce1814

+ 9
- 9
App.js View File

@@ -56,7 +56,6 @@ export default class App extends React.Component<Props, State> {
56 56
     createDrawerNavigator: () => React.Node;
57 57
 
58 58
     urlHandler: URLHandler;
59
-    storageManager: AsyncStorageManager;
60 59
 
61 60
     constructor() {
62 61
         super();
@@ -64,7 +63,6 @@ export default class App extends React.Component<Props, State> {
64 63
         this.navigatorRef = React.createRef();
65 64
         this.defaultHomeRoute = null;
66 65
         this.defaultHomeData = {};
67
-        this.storageManager = AsyncStorageManager.getInstance();
68 66
         this.urlHandler = new URLHandler(this.onInitialURLParsed, this.onDetectURL);
69 67
         this.urlHandler.listen();
70 68
         setSafeBounceHeight(Platform.OS === 'ios' ? 100 : 20);
@@ -133,9 +131,9 @@ export default class App extends React.Component<Props, State> {
133 131
             showUpdate: false,
134 132
             showAprilFools: false,
135 133
         });
136
-        this.storageManager.savePref(this.storageManager.preferences.showIntro.key, '0');
137
-        this.storageManager.savePref(this.storageManager.preferences.updateNumber.key, Update.number.toString());
138
-        this.storageManager.savePref(this.storageManager.preferences.showAprilFoolsStart.key, '0');
134
+        AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.showIntro.key, false);
135
+        AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.updateNumber.key, Update.number);
136
+        AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.showAprilFoolsStart.key, false);
139 137
     };
140 138
 
141 139
     /**
@@ -144,7 +142,7 @@ export default class App extends React.Component<Props, State> {
144 142
      * @returns {Promise<void>}
145 143
      */
146 144
     loadAssetsAsync = async () => {
147
-        await this.storageManager.loadPreferences();
145
+        await AsyncStorageManager.getInstance().loadPreferences();
148 146
         try {
149 147
             await ConnectionManager.getInstance().recoverLogin();
150 148
         } catch (e) {
@@ -169,9 +167,11 @@ export default class App extends React.Component<Props, State> {
169 167
         this.setState({
170 168
             isLoading: false,
171 169
             currentTheme: ThemeManager.getCurrentTheme(),
172
-            showIntro: this.storageManager.preferences.showIntro.current === '1',
173
-            showUpdate: this.storageManager.preferences.updateNumber.current !== Update.number.toString(),
174
-            showAprilFools: AprilFoolsManager.getInstance().isAprilFoolsEnabled() && this.storageManager.preferences.showAprilFoolsStart.current === '1',
170
+            showIntro: AsyncStorageManager.getBool(AsyncStorageManager.PREFERENCES.showIntro.key),
171
+            showUpdate: AsyncStorageManager.getNumber(AsyncStorageManager.PREFERENCES.updateNumber.key)
172
+                !== Update.number,
173
+            showAprilFools: AprilFoolsManager.getInstance().isAprilFoolsEnabled()
174
+                && AsyncStorageManager.getBool(AsyncStorageManager.PREFERENCES.showAprilFoolsStart.key),
175 175
         });
176 176
         SplashScreen.hide();
177 177
     }

+ 98
- 45
src/managers/AsyncStorageManager.js View File

@@ -13,117 +13,86 @@ export default class AsyncStorageManager {
13 13
 
14 14
     static instance: AsyncStorageManager | null = null;
15 15
 
16
-    /**
17
-     * Get this class instance or create one if none is found
18
-     * @returns {AsyncStorageManager}
19
-     */
20
-    static getInstance(): AsyncStorageManager {
21
-        return AsyncStorageManager.instance === null ?
22
-            AsyncStorageManager.instance = new AsyncStorageManager() :
23
-            AsyncStorageManager.instance;
24
-    }
25
-
26
-    // Object storing preferences keys, default and current values for use in the app
27
-    preferences = {
16
+    static PREFERENCES = {
28 17
         debugUnlocked: {
29 18
             key: 'debugUnlocked',
30 19
             default: '0',
31
-            current: '',
32 20
         },
33 21
         showIntro: {
34 22
             key: 'showIntro',
35 23
             default: '1',
36
-            current: '',
37 24
         },
38 25
         updateNumber: {
39 26
             key: 'updateNumber',
40 27
             default: '0',
41
-            current: '',
42 28
         },
43 29
         proxiwashNotifications: {
44 30
             key: 'proxiwashNotifications',
45 31
             default: '5',
46
-            current: '',
47 32
         },
48 33
         nightModeFollowSystem: {
49 34
             key: 'nightModeFollowSystem',
50 35
             default: '1',
51
-            current: '',
52 36
         },
53 37
         nightMode: {
54 38
             key: 'nightMode',
55 39
             default: '1',
56
-            current: '',
57 40
         },
58 41
         defaultStartScreen: {
59 42
             key: 'defaultStartScreen',
60 43
             default: 'home',
61
-            current: '',
62 44
         },
63 45
         servicesShowBanner: {
64 46
             key: 'servicesShowBanner',
65 47
             default: '1',
66
-            current: '',
67 48
         },
68 49
         proxiwashShowBanner: {
69 50
             key: 'proxiwashShowBanner',
70 51
             default: '1',
71
-            current: '',
72 52
         },
73 53
         homeShowBanner: {
74 54
             key: 'homeShowBanner',
75 55
             default: '1',
76
-            current: '',
77 56
         },
78 57
         eventsShowBanner: {
79 58
             key: 'eventsShowBanner',
80 59
             default: '1',
81
-            current: '',
82 60
         },
83 61
         planexShowBanner: {
84 62
             key: 'planexShowBanner',
85 63
             default: '1',
86
-            current: '',
87 64
         },
88 65
         loginShowBanner: {
89 66
             key: 'loginShowBanner',
90 67
             default: '1',
91
-            current: '',
92 68
         },
93 69
         voteShowBanner: {
94 70
             key: 'voteShowBanner',
95 71
             default: '1',
96
-            current: '',
97 72
         },
98 73
         equipmentShowBanner: {
99 74
             key: 'equipmentShowBanner',
100 75
             default: '1',
101
-            current: '',
102 76
         },
103 77
         gameStartShowBanner: {
104 78
             key: 'gameStartShowBanner',
105 79
             default: '1',
106
-            current: '',
107 80
         },
108 81
         proxiwashWatchedMachines: {
109 82
             key: 'proxiwashWatchedMachines',
110 83
             default: '[]',
111
-            current: '',
112 84
         },
113 85
         showAprilFoolsStart: {
114 86
             key: 'showAprilFoolsStart',
115 87
             default: '1',
116
-            current: '',
117 88
         },
118 89
         planexCurrentGroup: {
119 90
             key: 'planexCurrentGroup',
120 91
             default: '',
121
-            current: '',
122 92
         },
123 93
         planexFavoriteGroups: {
124 94
             key: 'planexFavoriteGroups',
125 95
             default: '[]',
126
-            current: '',
127 96
         },
128 97
         dashboardItems: {
129 98
             key: 'dashboardItems',
@@ -134,14 +103,28 @@ export default class AsyncStorageManager {
134 103
                 SERVICES_KEY.TUTOR_INSA,
135 104
                 SERVICES_KEY.RU,
136 105
             ]),
137
-            current: '',
138 106
         },
139 107
         gameScores: {
140 108
             key: 'gameScores',
141 109
             default: '[]',
142
-            current: '',
143 110
         },
144
-    };
111
+    }
112
+
113
+    #currentPreferences: {[key: string]: string};
114
+
115
+    constructor() {
116
+        this.#currentPreferences = {};
117
+    }
118
+
119
+    /**
120
+     * Get this class instance or create one if none is found
121
+     * @returns {AsyncStorageManager}
122
+     */
123
+    static getInstance(): AsyncStorageManager {
124
+        return AsyncStorageManager.instance === null ?
125
+            AsyncStorageManager.instance = new AsyncStorageManager() :
126
+            AsyncStorageManager.instance;
127
+    }
145 128
 
146 129
     /**
147 130
      * Set preferences object current values from AsyncStorage.
@@ -152,9 +135,8 @@ export default class AsyncStorageManager {
152 135
     async loadPreferences() {
153 136
         let prefKeys = [];
154 137
         // Get all available keys
155
-        for (let [key, value] of Object.entries(this.preferences)) {
156
-            //$FlowFixMe
157
-            prefKeys.push(value.key);
138
+        for (let key in AsyncStorageManager.PREFERENCES) {
139
+            prefKeys.push(key);
158 140
         }
159 141
         // Get corresponding values
160 142
         let resultArray: Array<Array<string>> = await AsyncStorage.multiGet(prefKeys);
@@ -163,21 +145,92 @@ export default class AsyncStorageManager {
163 145
             let key: string = resultArray[i][0];
164 146
             let val: string | null = resultArray[i][1];
165 147
             if (val === null)
166
-                val = this.preferences[key].default;
167
-            this.preferences[key].current = val;
148
+                val = AsyncStorageManager.PREFERENCES[key].default;
149
+            this.#currentPreferences[key] = val;
168 150
         }
169 151
     }
170 152
 
171 153
     /**
172
-     * Save the value associated to the given key to preferences.
154
+     * Saves the value associated to the given key to preferences.
173 155
      * This updates the preferences object and saves it to AsyncStorage.
174 156
      *
175 157
      * @param key
176
-     * @param val
158
+     * @param value
159
+     */
160
+    setPreference(key: string, value: any) {
161
+        if (AsyncStorageManager.PREFERENCES[key] != null) {
162
+            let convertedValue = "";
163
+            if (typeof value === "string")
164
+                convertedValue = value;
165
+            else if (typeof value === "boolean" || typeof value === "number")
166
+                convertedValue = value.toString();
167
+            else
168
+                convertedValue = JSON.stringify(value);
169
+            this.#currentPreferences[key] = convertedValue;
170
+            AsyncStorage.setItem(key, convertedValue);
171
+        }
172
+    }
173
+
174
+    /**
175
+     * Gets the value at the given key.
176
+     * If the key is not available, returns null
177
+     *
178
+     * @param key
179
+     * @returns {string|null}
180
+     */
181
+    getPreference(key: string) {
182
+        return this.#currentPreferences[key];
183
+    }
184
+
185
+    /**
186
+     * aves the value associated to the given key to preferences.
187
+     *
188
+     * @param key
189
+     * @param value
190
+     */
191
+    static set(key: string, value: any) {
192
+        AsyncStorageManager.getInstance().setPreference(key, value);
193
+    }
194
+
195
+    /**
196
+     * Gets the string value of the given preference
197
+     *
198
+     * @param key
199
+     * @returns {boolean}
200
+     */
201
+    static getString(key: string) {
202
+        return AsyncStorageManager.getInstance().getPreference(key);
203
+    }
204
+
205
+    /**
206
+     * Gets the boolean value of the given preference
207
+     *
208
+     * @param key
209
+     * @returns {boolean}
210
+     */
211
+    static getBool(key: string) {
212
+        const value = AsyncStorageManager.getString(key);
213
+        return value === "1" || value === "true";
214
+    }
215
+
216
+    /**
217
+     * Gets the number value of the given preference
218
+     *
219
+     * @param key
220
+     * @returns {boolean}
221
+     */
222
+    static getNumber(key: string) {
223
+        return parseFloat(AsyncStorageManager.getString(key));
224
+    }
225
+
226
+    /**
227
+     * Gets the object value of the given preference
228
+     *
229
+     * @param key
230
+     * @returns {boolean}
177 231
      */
178
-    savePref(key: string, val: string) {
179
-        this.preferences[key].current = val;
180
-        AsyncStorage.setItem(key, val);
232
+    static getObject(key: string) {
233
+        return JSON.parse(AsyncStorageManager.getString(key));
181 234
     }
182 235
 
183 236
 }

+ 3
- 2
src/managers/DashboardManager.js View File

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

+ 6
- 6
src/managers/ThemeManager.js View File

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

+ 1
- 1
src/navigation/TabNavigator.js View File

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

+ 5
- 4
src/screens/About/DebugScreen.js View File

@@ -43,10 +43,11 @@ class DebugScreen extends React.Component<Props, State> {
43 43
     constructor(props) {
44 44
         super(props);
45 45
         this.modalInputValue = "";
46
-        let copy = {...AsyncStorageManager.getInstance().preferences};
47 46
         let currentPreferences : Array<PreferenceItem> = [];
48
-        Object.values(copy).map((object: any) => {
49
-            currentPreferences.push(object);
47
+        Object.values(AsyncStorageManager.PREFERENCES).map((object: any) => {
48
+            let newObject: PreferenceItem = {...object};
49
+            newObject.current = AsyncStorageManager.getString(newObject.key);
50
+            currentPreferences.push(newObject);
50 51
         });
51 52
         this.state = {
52 53
             modalCurrentDisplayItem: {},
@@ -139,7 +140,7 @@ class DebugScreen extends React.Component<Props, State> {
139 140
             currentPreferences[this.findIndexOfKey(key)].current = value;
140 141
             return {currentPreferences};
141 142
         });
142
-        AsyncStorageManager.getInstance().savePref(key, value);
143
+        AsyncStorageManager.set(key, value);
143 144
         this.modalRef.close();
144 145
     }
145 146
 

+ 2
- 5
src/screens/Amicale/Equipment/EquipmentListScreen.js View File

@@ -42,7 +42,7 @@ const LIST_ITEM_HEIGHT = 64;
42 42
 class EquipmentListScreen extends React.Component<Props, State> {
43 43
 
44 44
     state = {
45
-        mascotDialogVisible: AsyncStorageManager.getInstance().preferences.equipmentShowBanner.current === "1"
45
+        mascotDialogVisible: AsyncStorageManager.getBool(AsyncStorageManager.PREFERENCES.equipmentShowBanner.key),
46 46
     }
47 47
 
48 48
     data: Array<Device>;
@@ -146,10 +146,7 @@ class EquipmentListScreen extends React.Component<Props, State> {
146 146
     };
147 147
 
148 148
     hideMascotDialog = () => {
149
-        AsyncStorageManager.getInstance().savePref(
150
-            AsyncStorageManager.getInstance().preferences.equipmentShowBanner.key,
151
-            '0'
152
-        );
149
+        AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.equipmentShowBanner.key, false);
153 150
         this.setState({mascotDialogVisible: false})
154 151
     };
155 152
 

+ 4
- 10
src/screens/Amicale/LoginScreen.js View File

@@ -48,7 +48,7 @@ class LoginScreen extends React.Component<Props, State> {
48 48
         loading: false,
49 49
         dialogVisible: false,
50 50
         dialogError: 0,
51
-        mascotDialogVisible: AsyncStorageManager.getInstance().preferences.loginShowBanner.current === "1"
51
+        mascotDialogVisible: AsyncStorageManager.getBool(AsyncStorageManager.PREFERENCES.loginShowBanner.key),
52 52
     };
53 53
 
54 54
     onEmailChange: (value: string) => null;
@@ -82,10 +82,7 @@ class LoginScreen extends React.Component<Props, State> {
82 82
     }
83 83
 
84 84
     hideMascotDialog = () => {
85
-        AsyncStorageManager.getInstance().savePref(
86
-            AsyncStorageManager.getInstance().preferences.loginShowBanner.key,
87
-            '0'
88
-        );
85
+        AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.loginShowBanner.key, false);
89 86
         this.setState({mascotDialogVisible: false})
90 87
     };
91 88
 
@@ -111,11 +108,8 @@ class LoginScreen extends React.Component<Props, State> {
111 108
      * Saves in user preferences to not show the login banner again.
112 109
      */
113 110
     handleSuccess = () => {
114
-        // Do not show the login banner again
115
-        AsyncStorageManager.getInstance().savePref(
116
-            AsyncStorageManager.getInstance().preferences.homeShowBanner.key,
117
-            '0'
118
-        );
111
+        // Do not show the home login banner again
112
+        AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.homeShowBanner.key, false);
119 113
         if (this.nextScreen == null)
120 114
             this.props.navigation.goBack();
121 115
         else

+ 2
- 5
src/screens/Amicale/VoteScreen.js View File

@@ -108,7 +108,7 @@ export default class VoteScreen extends React.Component<Props, State> {
108 108
 
109 109
     state = {
110 110
         hasVoted: false,
111
-        mascotDialogVisible: AsyncStorageManager.getInstance().preferences.voteShowBanner.current === "1",
111
+        mascotDialogVisible: AsyncStorageManager.getBool(AsyncStorageManager.PREFERENCES.voteShowBanner.key),
112 112
     };
113 113
 
114 114
     teams: Array<team>;
@@ -326,10 +326,7 @@ export default class VoteScreen extends React.Component<Props, State> {
326 326
     };
327 327
 
328 328
     hideMascotDialog = () => {
329
-        AsyncStorageManager.getInstance().savePref(
330
-            AsyncStorageManager.getInstance().preferences.voteShowBanner.key,
331
-            '0'
332
-        );
329
+        AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.voteShowBanner.key, false);
333 330
         this.setState({mascotDialogVisible: false})
334 331
     };
335 332
 

+ 4
- 10
src/screens/Game/screens/GameStartScreen.js View File

@@ -46,13 +46,13 @@ class GameStartScreen extends React.Component<Props, State> {
46 46
     isHighScore: boolean;
47 47
 
48 48
     state = {
49
-        mascotDialogVisible: AsyncStorageManager.getInstance().preferences.gameStartShowBanner.current === "1",
49
+        mascotDialogVisible: AsyncStorageManager.getBool(AsyncStorageManager.PREFERENCES.gameStartShowBanner.key),
50 50
     }
51 51
 
52 52
     constructor(props: Props) {
53 53
         super(props);
54 54
         this.gridManager = new GridManager(4, 4, props.theme);
55
-        this.scores = JSON.parse(AsyncStorageManager.getInstance().preferences.gameScores.current);
55
+        this.scores = AsyncStorageManager.getObject(AsyncStorageManager.PREFERENCES.gameScores.key);
56 56
         this.scores.sort((a, b) => b - a);
57 57
         if (this.props.route.params != null)
58 58
             this.recoverGameScore();
@@ -72,17 +72,11 @@ class GameStartScreen extends React.Component<Props, State> {
72 72
         }
73 73
         if (this.scores.length > 3)
74 74
             this.scores.splice(3, 1);
75
-        AsyncStorageManager.getInstance().savePref(
76
-            AsyncStorageManager.getInstance().preferences.gameScores.key,
77
-            JSON.stringify(this.scores)
78
-        );
75
+        AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.gameScores.key, this.scores);
79 76
     }
80 77
 
81 78
     hideMascotDialog = () => {
82
-        AsyncStorageManager.getInstance().savePref(
83
-            AsyncStorageManager.getInstance().preferences.gameStartShowBanner.key,
84
-            '0'
85
-        );
79
+        AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.gameStartShowBanner.key, false);
86 80
         this.setState({mascotDialogVisible: false})
87 81
     };
88 82
 

+ 3
- 5
src/screens/Home/HomeScreen.js View File

@@ -112,7 +112,8 @@ class HomeScreen extends React.Component<Props, State> {
112 112
         });
113 113
         this.state = {
114 114
             dialogVisible: false,
115
-            mascotDialogVisible: AsyncStorageManager.getInstance().preferences.homeShowBanner.current === "1"
115
+            mascotDialogVisible: AsyncStorageManager.getBool(
116
+                AsyncStorageManager.PREFERENCES.homeShowBanner.key)
116 117
                 && !this.isLoggedIn,
117 118
         }
118 119
     }
@@ -184,10 +185,7 @@ class HomeScreen extends React.Component<Props, State> {
184 185
     };
185 186
 
186 187
     hideMascotDialog = () => {
187
-        AsyncStorageManager.getInstance().savePref(
188
-            AsyncStorageManager.getInstance().preferences.homeShowBanner.key,
189
-            '0'
190
-        );
188
+        AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.homeShowBanner.key, false);
191 189
         this.setState({mascotDialogVisible: false})
192 190
     };
193 191
 

+ 3
- 9
src/screens/Other/Settings/DashboardEditScreen.js View File

@@ -38,7 +38,7 @@ class DashboardEditScreen extends React.Component<Props, State> {
38 38
     constructor(props: Props) {
39 39
         super(props);
40 40
         let dashboardManager = new DashboardManager(this.props.navigation);
41
-        this.initialDashboardIdList = JSON.parse(AsyncStorageManager.getInstance().preferences.dashboardItems.current);
41
+        this.initialDashboardIdList = AsyncStorageManager.getObject(AsyncStorageManager.PREFERENCES.dashboardItems.key);
42 42
         this.initialDashboard = dashboardManager.getCurrentDashboard();
43 43
         this.state = {
44 44
             currentDashboard: [...this.initialDashboard],
@@ -92,10 +92,7 @@ class DashboardEditScreen extends React.Component<Props, State> {
92 92
             currentDashboard: currentDashboard,
93 93
             currentDashboardIdList: currentDashboardIdList,
94 94
         });
95
-        AsyncStorageManager.getInstance().savePref(
96
-            AsyncStorageManager.getInstance().preferences.dashboardItems.key,
97
-            JSON.stringify(currentDashboardIdList)
98
-        );
95
+        AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.dashboardItems.key, currentDashboardIdList);
99 96
     }
100 97
 
101 98
     undoDashboard = () => {
@@ -103,10 +100,7 @@ class DashboardEditScreen extends React.Component<Props, State> {
103 100
             currentDashboard: [...this.initialDashboard],
104 101
             currentDashboardIdList: [...this.initialDashboardIdList]
105 102
         });
106
-        AsyncStorageManager.getInstance().savePref(
107
-            AsyncStorageManager.getInstance().preferences.dashboardItems.key,
108
-            JSON.stringify(this.initialDashboardIdList)
109
-        );
103
+        AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.dashboardItems.key, this.initialDashboardIdList);
110 104
     }
111 105
 
112 106
     getListHeader() {

+ 11
- 17
src/screens/Other/Settings/SettingsScreen.js View File

@@ -37,18 +37,18 @@ class SettingsScreen extends React.Component<Props, State> {
37 37
      */
38 38
     constructor() {
39 39
         super();
40
-        let notifReminder = AsyncStorageManager.getInstance().preferences.proxiwashNotifications.current;
40
+        let notifReminder = AsyncStorageManager.getString(AsyncStorageManager.PREFERENCES.proxiwashNotifications.key);
41 41
         this.savedNotificationReminder = parseInt(notifReminder);
42 42
         if (isNaN(this.savedNotificationReminder))
43 43
             this.savedNotificationReminder = 0;
44 44
 
45 45
         this.state = {
46 46
             nightMode: ThemeManager.getNightMode(),
47
-            nightModeFollowSystem: AsyncStorageManager.getInstance().preferences.nightModeFollowSystem.current === '1' &&
48
-                Appearance.getColorScheme() !== 'no-preference',
47
+            nightModeFollowSystem: AsyncStorageManager.getBool(AsyncStorageManager.PREFERENCES.nightModeFollowSystem.key)
48
+                && Appearance.getColorScheme() !== 'no-preference',
49 49
             notificationReminderSelected: this.savedNotificationReminder,
50
-            startScreenPickerSelected: AsyncStorageManager.getInstance().preferences.defaultStartScreen.current,
51
-            isDebugUnlocked: AsyncStorageManager.getInstance().preferences.debugUnlocked.current === '1'
50
+            startScreenPickerSelected: AsyncStorageManager.getString(AsyncStorageManager.PREFERENCES.defaultStartScreen.key),
51
+            isDebugUnlocked: AsyncStorageManager.getBool(AsyncStorageManager.PREFERENCES.debugUnlocked.key)
52 52
         };
53 53
     }
54 54
 
@@ -57,8 +57,7 @@ class SettingsScreen extends React.Component<Props, State> {
57 57
      */
58 58
     unlockDebugMode = () => {
59 59
         this.setState({isDebugUnlocked: true});
60
-        let key = AsyncStorageManager.getInstance().preferences.debugUnlocked.key;
61
-        AsyncStorageManager.getInstance().savePref(key, '1');
60
+        AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.debugUnlocked.key, true);
62 61
     }
63 62
 
64 63
     /**
@@ -67,9 +66,8 @@ class SettingsScreen extends React.Component<Props, State> {
67 66
      * @param value The value to store
68 67
      */
69 68
     onProxiwashNotifPickerValueChange = (value: number) => {
70
-        let key = AsyncStorageManager.getInstance().preferences.proxiwashNotifications.key;
71
-        AsyncStorageManager.getInstance().savePref(key, value.toString());
72
-        this.setState({notificationReminderSelected: value})
69
+        this.setState({notificationReminderSelected: value});
70
+        AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.proxiwashNotifications.key, value);
73 71
     };
74 72
 
75 73
     /**
@@ -79,11 +77,8 @@ class SettingsScreen extends React.Component<Props, State> {
79 77
      */
80 78
     onStartScreenPickerValueChange = (value: string) => {
81 79
         if (value != null) {
82
-            let key = AsyncStorageManager.getInstance().preferences.defaultStartScreen.key;
83
-            AsyncStorageManager.getInstance().savePref(key, value);
84
-            this.setState({
85
-                startScreenPickerSelected: value
86
-            });
80
+            this.setState({startScreenPickerSelected: value});
81
+            AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.defaultStartScreen.key, value);
87 82
         }
88 83
     };
89 84
 
@@ -139,8 +134,7 @@ class SettingsScreen extends React.Component<Props, State> {
139 134
     onToggleNightModeFollowSystem = () => {
140 135
         const value = !this.state.nightModeFollowSystem;
141 136
         this.setState({nightModeFollowSystem: value});
142
-        let key = AsyncStorageManager.getInstance().preferences.nightModeFollowSystem.key;
143
-        AsyncStorageManager.getInstance().savePref(key, value ? '1' : '0');
137
+        AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.nightModeFollowSystem.key, value);
144 138
         if (value) {
145 139
             const nightMode = Appearance.getColorScheme() === 'dark';
146 140
             ThemeManager.getInstance().setNightMode(nightMode);

+ 2
- 4
src/screens/Planex/GroupSelectionScreen.js View File

@@ -53,7 +53,7 @@ class GroupSelectionScreen extends React.Component<Props, State> {
53 53
         super(props);
54 54
         this.state = {
55 55
             currentSearchString: '',
56
-            favoriteGroups: JSON.parse(AsyncStorageManager.getInstance().preferences.planexFavoriteGroups.current),
56
+            favoriteGroups: AsyncStorageManager.getObject(AsyncStorageManager.PREFERENCES.planexFavoriteGroups.key),
57 57
         };
58 58
     }
59 59
 
@@ -172,9 +172,7 @@ class GroupSelectionScreen extends React.Component<Props, State> {
172 172
         else
173 173
             this.addGroupToFavorites(newFavorites, group);
174 174
         this.setState({favoriteGroups: newFavorites})
175
-        AsyncStorageManager.getInstance().savePref(
176
-            AsyncStorageManager.getInstance().preferences.planexFavoriteGroups.key,
177
-            JSON.stringify(newFavorites));
175
+        AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.planexFavoriteGroups.key, newFavorites);
178 176
     }
179 177
 
180 178
     /**

+ 6
- 11
src/screens/Planex/PlanexScreen.js View File

@@ -135,7 +135,7 @@ class PlanexScreen extends React.Component<Props, State> {
135 135
         this.webScreenRef = React.createRef();
136 136
         this.barRef = React.createRef();
137 137
 
138
-        let currentGroup = AsyncStorageManager.getInstance().preferences.planexCurrentGroup.current;
138
+        let currentGroup = AsyncStorageManager.getString(AsyncStorageManager.PREFERENCES.planexCurrentGroup.key);
139 139
         if (currentGroup === '')
140 140
             currentGroup = {name: "SELECT GROUP", id: -1, isFav: false};
141 141
         else {
@@ -144,8 +144,9 @@ class PlanexScreen extends React.Component<Props, State> {
144 144
         }
145 145
         this.state = {
146 146
             mascotDialogVisible:
147
-                AsyncStorageManager.getInstance().preferences.planexShowBanner.current === '1' &&
148
-                AsyncStorageManager.getInstance().preferences.defaultStartScreen.current !== 'Planex',
147
+                AsyncStorageManager.getBool(AsyncStorageManager.PREFERENCES.planexShowBanner.key)
148
+                && AsyncStorageManager.getString(AsyncStorageManager.PREFERENCES.defaultStartScreen.key)
149
+                    .toLowerCase() !== 'planex',
149 150
             dialogVisible: false,
150 151
             dialogTitle: "",
151 152
             dialogMessage: "",
@@ -167,10 +168,7 @@ class PlanexScreen extends React.Component<Props, State> {
167 168
      */
168 169
     onMascotDialogCancel = () => {
169 170
         this.setState({mascotDialogVisible: false});
170
-        AsyncStorageManager.getInstance().savePref(
171
-            AsyncStorageManager.getInstance().preferences.planexShowBanner.key,
172
-            '0'
173
-        );
171
+        AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.planexShowBanner.key, false);
174 172
     };
175 173
 
176 174
 
@@ -208,10 +206,7 @@ class PlanexScreen extends React.Component<Props, State> {
208 206
     selectNewGroup(group: group) {
209 207
         this.sendMessage('setGroup', group.id);
210 208
         this.setState({currentGroup: group});
211
-        AsyncStorageManager.getInstance().savePref(
212
-            AsyncStorageManager.getInstance().preferences.planexCurrentGroup.key,
213
-            JSON.stringify(group)
214
-        );
209
+        AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.planexCurrentGroup.key, group);
215 210
         this.props.navigation.setOptions({title: group.name});
216 211
         this.generateInjectedJS(group.id);
217 212
     }

+ 2
- 5
src/screens/Planning/PlanningScreen.js View File

@@ -56,7 +56,7 @@ class PlanningScreen extends React.Component<Props, State> {
56 56
         refreshing: false,
57 57
         agendaItems: {},
58 58
         calendarShowing: false,
59
-        mascotDialogVisible: AsyncStorageManager.getInstance().preferences.eventsShowBanner.current === "1"
59
+        mascotDialogVisible: AsyncStorageManager.getBool(AsyncStorageManager.PREFERENCES.eventsShowBanner.key)
60 60
     };
61 61
 
62 62
     currentDate = getDateOnlyString(getCurrentDateString());
@@ -111,10 +111,7 @@ class PlanningScreen extends React.Component<Props, State> {
111 111
      */
112 112
     onHideMascotDialog = () => {
113 113
         this.setState({mascotDialogVisible: false});
114
-        AsyncStorageManager.getInstance().savePref(
115
-            AsyncStorageManager.getInstance().preferences.eventsShowBanner.key,
116
-            '0'
117
-        );
114
+        AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.eventsShowBanner.key, false);
118 115
     };
119 116
 
120 117
     /**

+ 4
- 10
src/screens/Proxiwash/ProxiwashScreen.js View File

@@ -66,8 +66,8 @@ class ProxiwashScreen extends React.Component<Props, State> {
66 66
     state = {
67 67
         refreshing: false,
68 68
         modalCurrentDisplayItem: null,
69
-        machinesWatched: JSON.parse(AsyncStorageManager.getInstance().preferences.proxiwashWatchedMachines.current),
70
-        mascotDialogVisible: AsyncStorageManager.getInstance().preferences.proxiwashShowBanner.current === "1",
69
+        machinesWatched: AsyncStorageManager.getObject(AsyncStorageManager.PREFERENCES.proxiwashWatchedMachines.key),
70
+        mascotDialogVisible: AsyncStorageManager.getBool(AsyncStorageManager.PREFERENCES.proxiwashShowBanner.key),
71 71
     };
72 72
 
73 73
     /**
@@ -90,10 +90,7 @@ class ProxiwashScreen extends React.Component<Props, State> {
90 90
      */
91 91
     onHideMascotDialog = () => {
92 92
         this.setState({mascotDialogVisible: false});
93
-        AsyncStorageManager.getInstance().savePref(
94
-            AsyncStorageManager.getInstance().preferences.proxiwashShowBanner.key,
95
-            '0'
96
-        );
93
+        AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.proxiwashShowBanner.key, false);
97 94
     };
98 95
 
99 96
     /**
@@ -185,10 +182,7 @@ class ProxiwashScreen extends React.Component<Props, State> {
185 182
 
186 183
     saveNewWatchedList(list: Array<Machine>) {
187 184
         this.setState({machinesWatched: list});
188
-        AsyncStorageManager.getInstance().savePref(
189
-            AsyncStorageManager.getInstance().preferences.proxiwashWatchedMachines.key,
190
-            JSON.stringify(list),
191
-        );
185
+        AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.proxiwashWatchedMachines.key, list);
192 186
     }
193 187
 
194 188
     /**

+ 2
- 5
src/screens/Services/ServicesScreen.js View File

@@ -38,7 +38,7 @@ class ServicesScreen extends React.Component<Props, State> {
38 38
     finalDataset: Array<listItem>
39 39
 
40 40
     state = {
41
-        mascotDialogVisible: AsyncStorageManager.getInstance().preferences.servicesShowBanner.current === "1"
41
+        mascotDialogVisible: AsyncStorageManager.getBool(AsyncStorageManager.PREFERENCES.servicesShowBanner.key),
42 42
     }
43 43
 
44 44
     constructor(props) {
@@ -60,10 +60,7 @@ class ServicesScreen extends React.Component<Props, State> {
60 60
      */
61 61
     onHideMascotDialog = () => {
62 62
         this.setState({mascotDialogVisible: false});
63
-        AsyncStorageManager.getInstance().savePref(
64
-            AsyncStorageManager.getInstance().preferences.servicesShowBanner.key,
65
-            '0'
66
-        );
63
+        AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.servicesShowBanner.key, false);
67 64
     };
68 65
 
69 66
     getAboutButton = () =>

+ 1
- 1
src/utils/Notifications.js View File

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

Loading…
Cancel
Save