Application Android et IOS pour l'amicale des élèves
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AsyncStorageManager.ts 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Copyright (c) 2019 - 2020 Arnaud Vergnet.
  3. *
  4. * This file is part of Campus INSAT.
  5. *
  6. * Campus INSAT is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Campus INSAT is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Campus INSAT. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. import AsyncStorage from '@react-native-community/async-storage';
  20. import {SERVICES_KEY} from './ServicesManager';
  21. /**
  22. * Singleton used to manage preferences.
  23. * Preferences are fetched at the start of the app and saved in an instance object.
  24. * This allows for a synchronous access to saved data.
  25. */
  26. export default class AsyncStorageManager {
  27. static instance: AsyncStorageManager | null = null;
  28. static PREFERENCES: {[key: string]: {key: string; default: string}} = {
  29. debugUnlocked: {
  30. key: 'debugUnlocked',
  31. default: '0',
  32. },
  33. showIntro: {
  34. key: 'showIntro',
  35. default: '1',
  36. },
  37. updateNumber: {
  38. key: 'updateNumber',
  39. default: '0',
  40. },
  41. proxiwashNotifications: {
  42. key: 'proxiwashNotifications',
  43. default: '5',
  44. },
  45. nightModeFollowSystem: {
  46. key: 'nightModeFollowSystem',
  47. default: '1',
  48. },
  49. nightMode: {
  50. key: 'nightMode',
  51. default: '1',
  52. },
  53. defaultStartScreen: {
  54. key: 'defaultStartScreen',
  55. default: 'home',
  56. },
  57. servicesShowMascot: {
  58. key: 'servicesShowMascot',
  59. default: '1',
  60. },
  61. proxiwashShowMascot: {
  62. key: 'proxiwashShowMascot',
  63. default: '1',
  64. },
  65. homeShowMascot: {
  66. key: 'homeShowMascot',
  67. default: '1',
  68. },
  69. eventsShowMascot: {
  70. key: 'eventsShowMascot',
  71. default: '1',
  72. },
  73. planexShowMascot: {
  74. key: 'planexShowMascot',
  75. default: '1',
  76. },
  77. loginShowMascot: {
  78. key: 'loginShowMascot',
  79. default: '1',
  80. },
  81. voteShowMascot: {
  82. key: 'voteShowMascot',
  83. default: '1',
  84. },
  85. equipmentShowMascot: {
  86. key: 'equipmentShowMascot',
  87. default: '1',
  88. },
  89. gameStartMascot: {
  90. key: 'gameStartMascot',
  91. default: '1',
  92. },
  93. proxiwashWatchedMachines: {
  94. key: 'proxiwashWatchedMachines',
  95. default: '[]',
  96. },
  97. showAprilFoolsStart: {
  98. key: 'showAprilFoolsStart',
  99. default: '1',
  100. },
  101. planexCurrentGroup: {
  102. key: 'planexCurrentGroup',
  103. default: '',
  104. },
  105. planexFavoriteGroups: {
  106. key: 'planexFavoriteGroups',
  107. default: '[]',
  108. },
  109. dashboardItems: {
  110. key: 'dashboardItems',
  111. default: JSON.stringify([
  112. SERVICES_KEY.EMAIL,
  113. SERVICES_KEY.WASHERS,
  114. SERVICES_KEY.PROXIMO,
  115. SERVICES_KEY.TUTOR_INSA,
  116. SERVICES_KEY.RU,
  117. ]),
  118. },
  119. gameScores: {
  120. key: 'gameScores',
  121. default: '[]',
  122. },
  123. selectedWash: {
  124. key: 'selectedWash',
  125. default: 'washinsa',
  126. },
  127. };
  128. private currentPreferences: {[key: string]: string};
  129. constructor() {
  130. this.currentPreferences = {};
  131. }
  132. /**
  133. * Get this class instance or create one if none is found
  134. * @returns {AsyncStorageManager}
  135. */
  136. static getInstance(): AsyncStorageManager {
  137. if (AsyncStorageManager.instance == null) {
  138. AsyncStorageManager.instance = new AsyncStorageManager();
  139. }
  140. return AsyncStorageManager.instance;
  141. }
  142. /**
  143. * Saves the value associated to the given key to preferences.
  144. *
  145. * @param key
  146. * @param value
  147. */
  148. static set(
  149. key: string,
  150. value: number | string | boolean | object | Array<any>,
  151. ) {
  152. AsyncStorageManager.getInstance().setPreference(key, value);
  153. }
  154. /**
  155. * Gets the string value of the given preference
  156. *
  157. * @param key
  158. * @returns {string}
  159. */
  160. static getString(key: string): string {
  161. const value = AsyncStorageManager.getInstance().getPreference(key);
  162. return value != null ? value : '';
  163. }
  164. /**
  165. * Gets the boolean value of the given preference
  166. *
  167. * @param key
  168. * @returns {boolean}
  169. */
  170. static getBool(key: string): boolean {
  171. const value = AsyncStorageManager.getString(key);
  172. return value === '1' || value === 'true';
  173. }
  174. /**
  175. * Gets the number value of the given preference
  176. *
  177. * @param key
  178. * @returns {number}
  179. */
  180. static getNumber(key: string): number {
  181. return parseFloat(AsyncStorageManager.getString(key));
  182. }
  183. /**
  184. * Gets the object value of the given preference
  185. *
  186. * @param key
  187. * @returns {{...}}
  188. */
  189. static getObject<T>(key: string): T {
  190. return JSON.parse(AsyncStorageManager.getString(key));
  191. }
  192. /**
  193. * Set preferences object current values from AsyncStorage.
  194. * This function should be called at the app's start.
  195. *
  196. * @return {Promise<void>}
  197. */
  198. async loadPreferences() {
  199. const prefKeys: Array<string> = [];
  200. // Get all available keys
  201. Object.keys(AsyncStorageManager.PREFERENCES).forEach((key: string) => {
  202. prefKeys.push(key);
  203. });
  204. // Get corresponding values
  205. const resultArray = await AsyncStorage.multiGet(prefKeys);
  206. // Save those values for later use
  207. resultArray.forEach((item: [string, string | null]) => {
  208. const key = item[0];
  209. let val = item[1];
  210. if (val === null) {
  211. val = AsyncStorageManager.PREFERENCES[key].default;
  212. }
  213. this.currentPreferences[key] = val;
  214. });
  215. }
  216. /**
  217. * Saves the value associated to the given key to preferences.
  218. * This updates the preferences object and saves it to AsyncStorage.
  219. *
  220. * @param key
  221. * @param value
  222. */
  223. setPreference(
  224. key: string,
  225. value: number | string | boolean | object | Array<any>,
  226. ) {
  227. if (AsyncStorageManager.PREFERENCES[key] != null) {
  228. let convertedValue;
  229. if (typeof value === 'string') {
  230. convertedValue = value;
  231. } else if (typeof value === 'boolean' || typeof value === 'number') {
  232. convertedValue = value.toString();
  233. } else {
  234. convertedValue = JSON.stringify(value);
  235. }
  236. this.currentPreferences[key] = convertedValue;
  237. AsyncStorage.setItem(key, convertedValue);
  238. }
  239. }
  240. /**
  241. * Gets the value at the given key.
  242. * If the key is not available, returns null
  243. *
  244. * @param key
  245. * @returns {string|null}
  246. */
  247. getPreference(key: string): string | null {
  248. return this.currentPreferences[key];
  249. }
  250. }