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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. servicesShowBanner: {
  41. key: 'servicesShowBanner',
  42. default: '1',
  43. },
  44. proxiwashShowBanner: {
  45. key: 'proxiwashShowBanner',
  46. default: '1',
  47. },
  48. homeShowBanner: {
  49. key: 'homeShowBanner',
  50. default: '1',
  51. },
  52. eventsShowBanner: {
  53. key: 'eventsShowBanner',
  54. default: '1',
  55. },
  56. planexShowBanner: {
  57. key: 'planexShowBanner',
  58. default: '1',
  59. },
  60. loginShowBanner: {
  61. key: 'loginShowBanner',
  62. default: '1',
  63. },
  64. voteShowBanner: {
  65. key: 'voteShowBanner',
  66. default: '1',
  67. },
  68. equipmentShowBanner: {
  69. key: 'equipmentShowBanner',
  70. default: '1',
  71. },
  72. gameStartShowBanner: {
  73. key: 'gameStartShowBanner',
  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. };
  107. #currentPreferences: {[key: string]: string};
  108. constructor() {
  109. this.#currentPreferences = {};
  110. }
  111. /**
  112. * Get this class instance or create one if none is found
  113. * @returns {AsyncStorageManager}
  114. */
  115. static getInstance(): AsyncStorageManager {
  116. if (AsyncStorageManager.instance == null)
  117. AsyncStorageManager.instance = new AsyncStorageManager();
  118. return AsyncStorageManager.instance;
  119. }
  120. /**
  121. * Saves the value associated to the given key to preferences.
  122. *
  123. * @param key
  124. * @param value
  125. */
  126. static set(
  127. key: string,
  128. // eslint-disable-next-line flowtype/no-weak-types
  129. value: number | string | boolean | {...} | Array<any>,
  130. ) {
  131. AsyncStorageManager.getInstance().setPreference(key, value);
  132. }
  133. /**
  134. * Gets the string value of the given preference
  135. *
  136. * @param key
  137. * @returns {string}
  138. */
  139. static getString(key: string): string {
  140. const value = AsyncStorageManager.getInstance().getPreference(key);
  141. return value != null ? value : '';
  142. }
  143. /**
  144. * Gets the boolean value of the given preference
  145. *
  146. * @param key
  147. * @returns {boolean}
  148. */
  149. static getBool(key: string): boolean {
  150. const value = AsyncStorageManager.getString(key);
  151. return value === '1' || value === 'true';
  152. }
  153. /**
  154. * Gets the number value of the given preference
  155. *
  156. * @param key
  157. * @returns {number}
  158. */
  159. static getNumber(key: string): number {
  160. return parseFloat(AsyncStorageManager.getString(key));
  161. }
  162. /**
  163. * Gets the object value of the given preference
  164. *
  165. * @param key
  166. * @returns {{...}}
  167. */
  168. // eslint-disable-next-line flowtype/no-weak-types
  169. static getObject(key: string): any {
  170. return JSON.parse(AsyncStorageManager.getString(key));
  171. }
  172. /**
  173. * Set preferences object current values from AsyncStorage.
  174. * This function should be called at the app's start.
  175. *
  176. * @return {Promise<void>}
  177. */
  178. async loadPreferences() {
  179. const prefKeys = [];
  180. // Get all available keys
  181. Object.keys(AsyncStorageManager.PREFERENCES).forEach((key: string) => {
  182. prefKeys.push(key);
  183. });
  184. // Get corresponding values
  185. const resultArray = await AsyncStorage.multiGet(prefKeys);
  186. // Save those values for later use
  187. resultArray.forEach((item: [string, string | null]) => {
  188. const key = item[0];
  189. let val = item[1];
  190. if (val === null) val = AsyncStorageManager.PREFERENCES[key].default;
  191. this.#currentPreferences[key] = val;
  192. });
  193. }
  194. /**
  195. * Saves the value associated to the given key to preferences.
  196. * This updates the preferences object and saves it to AsyncStorage.
  197. *
  198. * @param key
  199. * @param value
  200. */
  201. setPreference(
  202. key: string,
  203. // eslint-disable-next-line flowtype/no-weak-types
  204. value: number | string | boolean | {...} | Array<any>,
  205. ) {
  206. if (AsyncStorageManager.PREFERENCES[key] != null) {
  207. let convertedValue;
  208. if (typeof value === 'string') convertedValue = value;
  209. else if (typeof value === 'boolean' || typeof value === 'number')
  210. convertedValue = value.toString();
  211. else convertedValue = JSON.stringify(value);
  212. this.#currentPreferences[key] = convertedValue;
  213. AsyncStorage.setItem(key, convertedValue);
  214. }
  215. }
  216. /**
  217. * Gets the value at the given key.
  218. * If the key is not available, returns null
  219. *
  220. * @param key
  221. * @returns {string|null}
  222. */
  223. getPreference(key: string): string | null {
  224. return this.#currentPreferences[key];
  225. }
  226. }