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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. planexShowBanner: {
  67. key: 'planexShowBanner',
  68. default: '1',
  69. current: '',
  70. },
  71. showAprilFoolsStart: {
  72. key: 'showAprilFoolsStart',
  73. default: '1',
  74. current: '',
  75. },
  76. };
  77. /**
  78. * Set preferences object current values from AsyncStorage.
  79. * This function should be called at the app's start.
  80. *
  81. * @return {Promise<void>}
  82. */
  83. async loadPreferences() {
  84. let prefKeys = [];
  85. // Get all available keys
  86. for (let [key, value] of Object.entries(this.preferences)) {
  87. //$FlowFixMe
  88. prefKeys.push(value.key);
  89. }
  90. // Get corresponding values
  91. let resultArray: Array<Array<string>> = await AsyncStorage.multiGet(prefKeys);
  92. // Save those values for later use
  93. for (let i = 0; i < resultArray.length; i++) {
  94. let key: string = resultArray[i][0];
  95. let val: string | null = resultArray[i][1];
  96. if (val === null)
  97. val = this.preferences[key].default;
  98. this.preferences[key].current = val;
  99. }
  100. }
  101. /**
  102. * Save the value associated to the given key to preferences.
  103. * This updates the preferences object and saves it to AsyncStorage.
  104. *
  105. * @param key
  106. * @param val
  107. */
  108. savePref(key: string, val: string) {
  109. this.preferences[key].current = val;
  110. AsyncStorage.setItem(key, val);
  111. }
  112. }