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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // @flow
  2. import {AsyncStorage} from "react-native";
  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. showIntro: {
  22. key: 'showIntro',
  23. default: '1',
  24. current: '',
  25. },
  26. updateNumber: {
  27. key: 'updateNumber',
  28. default: '0',
  29. current: '',
  30. },
  31. proxiwashNotifications: {
  32. key: 'proxiwashNotifications',
  33. default: '5',
  34. current: '',
  35. },
  36. nightModeFollowSystem: {
  37. key: 'nightModeFollowSystem',
  38. default: '1',
  39. current: '',
  40. },
  41. nightMode: {
  42. key: 'nightMode',
  43. default: '0',
  44. current: '',
  45. },
  46. expoToken: {
  47. key: 'expoToken',
  48. default: '',
  49. current: '',
  50. },
  51. debugUnlocked: {
  52. key: 'debugUnlocked',
  53. default: '0',
  54. current: '',
  55. },
  56. defaultStartScreen: {
  57. key: 'defaultStartScreen',
  58. default: 'home',
  59. current: '',
  60. },
  61. proxiwashShowBanner: {
  62. key: 'proxiwashShowBanner',
  63. default: '1',
  64. current: '',
  65. },
  66. proxiwashWatchedMachines: {
  67. key: 'proxiwashWatchedMachines',
  68. default: '[]',
  69. current: '',
  70. },
  71. planexShowBanner: {
  72. key: 'planexShowBanner',
  73. default: '1',
  74. current: '',
  75. },
  76. showAprilFoolsStart: {
  77. key: 'showAprilFoolsStart',
  78. default: '1',
  79. current: '',
  80. },
  81. planexCurrentGroup: {
  82. key: 'planexCurrentGroup',
  83. default: '',
  84. current: '',
  85. },
  86. planexFavoriteGroups: {
  87. key: 'planexFavoriteGroups',
  88. default: '[]',
  89. current: '',
  90. },
  91. };
  92. /**
  93. * Set preferences object current values from AsyncStorage.
  94. * This function should be called at the app's start.
  95. *
  96. * @return {Promise<void>}
  97. */
  98. async loadPreferences() {
  99. let prefKeys = [];
  100. // Get all available keys
  101. for (let [key, value] of Object.entries(this.preferences)) {
  102. //$FlowFixMe
  103. prefKeys.push(value.key);
  104. }
  105. // Get corresponding values
  106. let resultArray: Array<Array<string>> = await AsyncStorage.multiGet(prefKeys);
  107. // Save those values for later use
  108. for (let i = 0; i < resultArray.length; i++) {
  109. let key: string = resultArray[i][0];
  110. let val: string | null = resultArray[i][1];
  111. if (val === null)
  112. val = this.preferences[key].default;
  113. this.preferences[key].current = val;
  114. }
  115. }
  116. /**
  117. * Save the value associated to the given key to preferences.
  118. * This updates the preferences object and saves it to AsyncStorage.
  119. *
  120. * @param key
  121. * @param val
  122. */
  123. savePref(key: string, val: string) {
  124. this.preferences[key].current = val;
  125. AsyncStorage.setItem(key, val);
  126. }
  127. }