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.js 5.9KB

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