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 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. /**
  12. * Get this class instance or create one if none is found
  13. * @returns {AsyncStorageManager}
  14. */
  15. static getInstance(): AsyncStorageManager {
  16. return AsyncStorageManager.instance === null ?
  17. AsyncStorageManager.instance = new AsyncStorageManager() :
  18. AsyncStorageManager.instance;
  19. }
  20. // Object storing preferences keys, default and current values for use in the app
  21. preferences = {
  22. debugUnlocked: {
  23. key: 'debugUnlocked',
  24. default: '0',
  25. current: '',
  26. },
  27. showIntro: {
  28. key: 'showIntro',
  29. default: '1',
  30. current: '',
  31. },
  32. updateNumber: {
  33. key: 'updateNumber',
  34. default: '0',
  35. current: '',
  36. },
  37. proxiwashNotifications: {
  38. key: 'proxiwashNotifications',
  39. default: '5',
  40. current: '',
  41. },
  42. nightModeFollowSystem: {
  43. key: 'nightModeFollowSystem',
  44. default: '1',
  45. current: '',
  46. },
  47. nightMode: {
  48. key: 'nightMode',
  49. default: '1',
  50. current: '',
  51. },
  52. defaultStartScreen: {
  53. key: 'defaultStartScreen',
  54. default: 'home',
  55. current: '',
  56. },
  57. servicesShowBanner: {
  58. key: 'servicesShowBanner',
  59. default: '1',
  60. current: '',
  61. },
  62. proxiwashShowBanner: {
  63. key: 'proxiwashShowBanner',
  64. default: '1',
  65. current: '',
  66. },
  67. homeShowBanner: {
  68. key: 'homeShowBanner',
  69. default: '1',
  70. current: '',
  71. },
  72. eventsShowBanner: {
  73. key: 'eventsShowBanner',
  74. default: '1',
  75. current: '',
  76. },
  77. planexShowBanner: {
  78. key: 'planexShowBanner',
  79. default: '1',
  80. current: '',
  81. },
  82. loginShowBanner: {
  83. key: 'loginShowBanner',
  84. default: '1',
  85. current: '',
  86. },
  87. voteShowBanner: {
  88. key: 'voteShowBanner',
  89. default: '1',
  90. current: '',
  91. },
  92. equipmentShowBanner: {
  93. key: 'equipmentShowBanner',
  94. default: '1',
  95. current: '',
  96. },
  97. proxiwashWatchedMachines: {
  98. key: 'proxiwashWatchedMachines',
  99. default: '[]',
  100. current: '',
  101. },
  102. showAprilFoolsStart: {
  103. key: 'showAprilFoolsStart',
  104. default: '1',
  105. current: '',
  106. },
  107. planexCurrentGroup: {
  108. key: 'planexCurrentGroup',
  109. default: '',
  110. current: '',
  111. },
  112. planexFavoriteGroups: {
  113. key: 'planexFavoriteGroups',
  114. default: '[]',
  115. current: '',
  116. },
  117. dashboardItems: {
  118. key: 'dashboardItems',
  119. default: JSON.stringify([
  120. SERVICES_KEY.EMAIL,
  121. SERVICES_KEY.WASHERS,
  122. SERVICES_KEY.PROXIMO,
  123. SERVICES_KEY.TUTOR_INSA,
  124. SERVICES_KEY.RU,
  125. ]),
  126. current: '',
  127. },
  128. };
  129. /**
  130. * Set preferences object current values from AsyncStorage.
  131. * This function should be called at the app's start.
  132. *
  133. * @return {Promise<void>}
  134. */
  135. async loadPreferences() {
  136. let prefKeys = [];
  137. // Get all available keys
  138. for (let [key, value] of Object.entries(this.preferences)) {
  139. //$FlowFixMe
  140. prefKeys.push(value.key);
  141. }
  142. // Get corresponding values
  143. let resultArray: Array<Array<string>> = await AsyncStorage.multiGet(prefKeys);
  144. // Save those values for later use
  145. for (let i = 0; i < resultArray.length; i++) {
  146. let key: string = resultArray[i][0];
  147. let val: string | null = resultArray[i][1];
  148. if (val === null)
  149. val = this.preferences[key].default;
  150. this.preferences[key].current = val;
  151. }
  152. }
  153. /**
  154. * Save the value associated to the given key to preferences.
  155. * This updates the preferences object and saves it to AsyncStorage.
  156. *
  157. * @param key
  158. * @param val
  159. */
  160. savePref(key: string, val: string) {
  161. this.preferences[key].current = val;
  162. AsyncStorage.setItem(key, val);
  163. }
  164. }