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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // @flow
  2. import AsyncStorage from '@react-native-community/async-storage';
  3. /**
  4. * Singleton used to manage preferences.
  5. * Preferences are fetched at the start of the app and saved in an instance object.
  6. * This allows for a synchronous access to saved data.
  7. */
  8. export default class AsyncStorageManager {
  9. static instance: AsyncStorageManager | null = null;
  10. /**
  11. * Get this class instance or create one if none is found
  12. * @returns {AsyncStorageManager}
  13. */
  14. static getInstance(): AsyncStorageManager {
  15. return AsyncStorageManager.instance === null ?
  16. AsyncStorageManager.instance = new AsyncStorageManager() :
  17. AsyncStorageManager.instance;
  18. }
  19. // Object storing preferences keys, default and current values for use in the app
  20. preferences = {
  21. debugUnlocked: {
  22. key: 'debugUnlocked',
  23. default: '0',
  24. current: '',
  25. },
  26. showIntro: {
  27. key: 'showIntro',
  28. default: '1',
  29. current: '',
  30. },
  31. updateNumber: {
  32. key: 'updateNumber',
  33. default: '0',
  34. current: '',
  35. },
  36. proxiwashNotifications: {
  37. key: 'proxiwashNotifications',
  38. default: '5',
  39. current: '',
  40. },
  41. nightModeFollowSystem: {
  42. key: 'nightModeFollowSystem',
  43. default: '1',
  44. current: '',
  45. },
  46. nightMode: {
  47. key: 'nightMode',
  48. default: '1',
  49. current: '',
  50. },
  51. defaultStartScreen: {
  52. key: 'defaultStartScreen',
  53. default: 'home',
  54. current: '',
  55. },
  56. servicesShowBanner: {
  57. key: 'servicesShowBanner',
  58. default: '1',
  59. current: '',
  60. },
  61. proxiwashShowBanner: {
  62. key: 'proxiwashShowBanner',
  63. default: '1',
  64. current: '',
  65. },
  66. homeShowBanner: {
  67. key: 'homeShowBanner',
  68. default: '1',
  69. current: '',
  70. },
  71. eventsShowBanner: {
  72. key: 'eventsShowBanner',
  73. default: '1',
  74. current: '',
  75. },
  76. planexShowBanner: {
  77. key: 'planexShowBanner',
  78. default: '1',
  79. current: '',
  80. },
  81. loginShowBanner: {
  82. key: 'loginShowBanner',
  83. default: '1',
  84. current: '',
  85. },
  86. proxiwashWatchedMachines: {
  87. key: 'proxiwashWatchedMachines',
  88. default: '[]',
  89. current: '',
  90. },
  91. showAprilFoolsStart: {
  92. key: 'showAprilFoolsStart',
  93. default: '1',
  94. current: '',
  95. },
  96. planexCurrentGroup: {
  97. key: 'planexCurrentGroup',
  98. default: '',
  99. current: '',
  100. },
  101. planexFavoriteGroups: {
  102. key: 'planexFavoriteGroups',
  103. default: '[]',
  104. current: '',
  105. },
  106. };
  107. /**
  108. * Set preferences object current values from AsyncStorage.
  109. * This function should be called at the app's start.
  110. *
  111. * @return {Promise<void>}
  112. */
  113. async loadPreferences() {
  114. let prefKeys = [];
  115. // Get all available keys
  116. for (let [key, value] of Object.entries(this.preferences)) {
  117. //$FlowFixMe
  118. prefKeys.push(value.key);
  119. }
  120. // Get corresponding values
  121. let resultArray: Array<Array<string>> = await AsyncStorage.multiGet(prefKeys);
  122. // Save those values for later use
  123. for (let i = 0; i < resultArray.length; i++) {
  124. let key: string = resultArray[i][0];
  125. let val: string | null = resultArray[i][1];
  126. if (val === null)
  127. val = this.preferences[key].default;
  128. this.preferences[key].current = val;
  129. }
  130. }
  131. /**
  132. * Save the value associated to the given key to preferences.
  133. * This updates the preferences object and saves it to AsyncStorage.
  134. *
  135. * @param key
  136. * @param val
  137. */
  138. savePref(key: string, val: string) {
  139. this.preferences[key].current = val;
  140. AsyncStorage.setItem(key, val);
  141. }
  142. }