Application Android et IOS pour l'amicale des élèves https://play.google.com/store/apps/details?id=fr.amicaleinsat.application
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.

asyncStorage.ts 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. import AsyncStorage from '@react-native-async-storage/async-storage';
  2. import { SERVICES_KEY } from './Services';
  3. export enum GeneralPreferenceKeys {
  4. debugUnlocked = 'debugUnlocked',
  5. showIntro = 'showIntro',
  6. updateNumber = 'updateNumber',
  7. nightModeFollowSystem = 'nightModeFollowSystem',
  8. nightMode = 'nightMode',
  9. defaultStartScreen = 'defaultStartScreen',
  10. showAprilFoolsStart = 'showAprilFoolsStart',
  11. dashboardItems = 'dashboardItems',
  12. gameScores = 'gameScores',
  13. }
  14. export enum PlanexPreferenceKeys {
  15. planexCurrentGroup = 'planexCurrentGroup',
  16. planexFavoriteGroups = 'planexFavoriteGroups',
  17. }
  18. export enum ProxiwashPreferenceKeys {
  19. proxiwashNotifications = 'proxiwashNotifications',
  20. proxiwashWatchedMachines = 'proxiwashWatchedMachines',
  21. selectedWash = 'selectedWash',
  22. }
  23. export enum MascotPreferenceKeys {
  24. servicesShowMascot = 'servicesShowMascot',
  25. proxiwashShowMascot = 'proxiwashShowMascot',
  26. homeShowMascot = 'homeShowMascot',
  27. eventsShowMascot = 'eventsShowMascot',
  28. planexShowMascot = 'planexShowMascot',
  29. loginShowMascot = 'loginShowMascot',
  30. voteShowMascot = 'voteShowMascot',
  31. equipmentShowMascot = 'equipmentShowMascot',
  32. gameShowMascot = 'gameShowMascot',
  33. }
  34. export const PreferenceKeys = {
  35. ...GeneralPreferenceKeys,
  36. ...PlanexPreferenceKeys,
  37. ...ProxiwashPreferenceKeys,
  38. ...MascotPreferenceKeys,
  39. };
  40. export type PreferenceKeys =
  41. | GeneralPreferenceKeys
  42. | PlanexPreferenceKeys
  43. | ProxiwashPreferenceKeys
  44. | MascotPreferenceKeys;
  45. export type PreferencesType = { [key in PreferenceKeys]: string };
  46. export type GeneralPreferencesType = { [key in GeneralPreferenceKeys]: string };
  47. export type PlanexPreferencesType = {
  48. [key in PlanexPreferenceKeys]: string;
  49. };
  50. export type ProxiwashPreferencesType = {
  51. [key in ProxiwashPreferenceKeys]: string;
  52. };
  53. export type MascotPreferencesType = { [key in MascotPreferenceKeys]: string };
  54. export const defaultPlanexPreferences: {
  55. [key in PlanexPreferenceKeys]: string;
  56. } = {
  57. [PlanexPreferenceKeys.planexCurrentGroup]: '',
  58. [PlanexPreferenceKeys.planexFavoriteGroups]: '[]',
  59. };
  60. export const defaultProxiwashPreferences: {
  61. [key in ProxiwashPreferenceKeys]: string;
  62. } = {
  63. [ProxiwashPreferenceKeys.proxiwashNotifications]: '5',
  64. [ProxiwashPreferenceKeys.proxiwashWatchedMachines]: '[]',
  65. [ProxiwashPreferenceKeys.selectedWash]: 'washinsa',
  66. };
  67. export const defaultMascotPreferences: {
  68. [key in MascotPreferenceKeys]: string;
  69. } = {
  70. [MascotPreferenceKeys.servicesShowMascot]: '1',
  71. [MascotPreferenceKeys.proxiwashShowMascot]: '1',
  72. [MascotPreferenceKeys.homeShowMascot]: '1',
  73. [MascotPreferenceKeys.eventsShowMascot]: '1',
  74. [MascotPreferenceKeys.planexShowMascot]: '1',
  75. [MascotPreferenceKeys.loginShowMascot]: '1',
  76. [MascotPreferenceKeys.voteShowMascot]: '1',
  77. [MascotPreferenceKeys.equipmentShowMascot]: '1',
  78. [MascotPreferenceKeys.gameShowMascot]: '1',
  79. };
  80. export const defaultPreferences: { [key in GeneralPreferenceKeys]: string } = {
  81. [GeneralPreferenceKeys.debugUnlocked]: '0',
  82. [GeneralPreferenceKeys.showIntro]: '1',
  83. [GeneralPreferenceKeys.updateNumber]: '0',
  84. [GeneralPreferenceKeys.nightModeFollowSystem]: '1',
  85. [GeneralPreferenceKeys.nightMode]: '1',
  86. [GeneralPreferenceKeys.defaultStartScreen]: 'home',
  87. [GeneralPreferenceKeys.showAprilFoolsStart]: '1',
  88. [GeneralPreferenceKeys.dashboardItems]: JSON.stringify([
  89. SERVICES_KEY.EMAIL,
  90. SERVICES_KEY.WASHERS,
  91. SERVICES_KEY.PROXIMO,
  92. SERVICES_KEY.TUTOR_INSA,
  93. SERVICES_KEY.RU,
  94. ]),
  95. [GeneralPreferenceKeys.gameScores]: '[]',
  96. };
  97. export function isValidGeneralPreferenceKey(
  98. key: string
  99. ): key is GeneralPreferenceKeys {
  100. return Object.values(GeneralPreferenceKeys).includes(
  101. key as GeneralPreferenceKeys
  102. );
  103. }
  104. export function isValidMascotPreferenceKey(
  105. key: string
  106. ): key is MascotPreferenceKeys {
  107. return Object.values(MascotPreferenceKeys).includes(
  108. key as MascotPreferenceKeys
  109. );
  110. }
  111. /**
  112. * Set preferences object current values from AsyncStorage.
  113. * This function should be called once on start.
  114. *
  115. * @return {Promise<PreferencesType>}
  116. */
  117. export function retrievePreferences<
  118. Keys extends PreferenceKeys,
  119. T extends Partial<PreferencesType>
  120. >(keys: Array<Keys>, defaults: T): Promise<T> {
  121. return new Promise((resolve: (preferences: T) => void) => {
  122. AsyncStorage.multiGet(keys)
  123. .then((result) => {
  124. const preferences = { ...defaults };
  125. result.forEach((item) => {
  126. let [key, value] = item;
  127. if (value !== null) {
  128. preferences[key as Keys] = value;
  129. }
  130. });
  131. resolve(preferences);
  132. })
  133. .catch(() => resolve(defaults));
  134. });
  135. }
  136. /**
  137. * Saves the value associated to the given key to preferences.
  138. * This updates the preferences object and saves it to AsyncStorage.
  139. *
  140. * @param key
  141. * @param value
  142. */
  143. export function setPreference<
  144. Keys extends PreferenceKeys,
  145. T extends Partial<PreferencesType>
  146. >(
  147. key: Keys,
  148. value: number | string | boolean | object | Array<any> | undefined,
  149. prevPreferences: T
  150. ): T {
  151. let convertedValue: string;
  152. if (typeof value === 'string') {
  153. convertedValue = value;
  154. } else if (typeof value === 'boolean' || typeof value === 'number') {
  155. convertedValue = value.toString();
  156. } else {
  157. convertedValue = JSON.stringify(value);
  158. }
  159. prevPreferences[key] = convertedValue;
  160. AsyncStorage.setItem(key, convertedValue)
  161. .then(undefined)
  162. .catch(() => console.debug('save error: ' + convertedValue));
  163. return prevPreferences;
  164. }
  165. /**
  166. * Gets the boolean value of the given preference
  167. *
  168. * @param key
  169. * @returns {boolean}
  170. */
  171. export function getPreferenceString<
  172. Keys extends PreferenceKeys,
  173. T extends Partial<PreferencesType>
  174. >(key: Keys, preferences: T): string | undefined {
  175. return preferences[key];
  176. }
  177. /**
  178. * Gets the boolean value of the given preference
  179. *
  180. * @param key
  181. * @returns {boolean}
  182. */
  183. export function getPreferenceBool<
  184. Keys extends PreferenceKeys,
  185. T extends Partial<PreferencesType>
  186. >(key: Keys, preferences: T): boolean | undefined {
  187. const value = preferences[key];
  188. return value ? value === '1' || value === 'true' : undefined;
  189. }
  190. /**
  191. * Gets the number value of the given preference
  192. *
  193. * @param key
  194. * @returns {number}
  195. */
  196. export function getPreferenceNumber<
  197. Keys extends PreferenceKeys,
  198. T extends Partial<PreferencesType>
  199. >(key: Keys, preferences: T): number | undefined {
  200. const value = preferences[key] as string | undefined;
  201. return value ? parseFloat(value) : undefined;
  202. }
  203. /**
  204. * Gets the object value of the given preference
  205. *
  206. * @param key
  207. * @returns {{...}}
  208. */
  209. export function getPreferenceObject<
  210. Keys extends PreferenceKeys,
  211. T extends Partial<PreferencesType>
  212. >(key: Keys, preferences: T): object | Array<any> | undefined {
  213. const value = preferences[key] as string | undefined;
  214. return value ? JSON.parse(value) : undefined;
  215. }