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.

preferencesContext.tsx 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { useNavigation } from '@react-navigation/core';
  2. import React, { useContext } from 'react';
  3. import { Appearance } from 'react-native-appearance';
  4. import {
  5. defaultPreferences,
  6. getPreferenceBool,
  7. getPreferenceObject,
  8. isValidPreferenceKey,
  9. PreferenceKeys,
  10. PreferencesType,
  11. } from '../utils/asyncStorage';
  12. import {
  13. getAmicaleServices,
  14. getINSAServices,
  15. getSpecialServices,
  16. getStudentServices,
  17. } from '../utils/Services';
  18. const colorScheme = Appearance.getColorScheme();
  19. export type PreferencesContextType = {
  20. preferences: PreferencesType;
  21. updatePreferences: (
  22. key: PreferenceKeys,
  23. value: number | string | boolean | object | Array<any>
  24. ) => void;
  25. resetPreferences: () => void;
  26. };
  27. export const PreferencesContext = React.createContext<PreferencesContextType>({
  28. preferences: defaultPreferences,
  29. updatePreferences: () => undefined,
  30. resetPreferences: () => undefined,
  31. });
  32. export function usePreferences() {
  33. return useContext(PreferencesContext);
  34. }
  35. export function useShouldShowMascot(route: string) {
  36. const { preferences, updatePreferences } = usePreferences();
  37. const key = route + 'ShowMascot';
  38. let shouldShow = false;
  39. if (isValidPreferenceKey(key)) {
  40. shouldShow = getPreferenceBool(key, preferences) !== false;
  41. }
  42. const setShouldShow = (show: boolean) => {
  43. if (isValidPreferenceKey(key)) {
  44. updatePreferences(key, show);
  45. } else {
  46. console.log('Invalid preference key: ' + key);
  47. }
  48. };
  49. return { shouldShow, setShouldShow };
  50. }
  51. export function useDarkTheme() {
  52. const { preferences } = usePreferences();
  53. return (
  54. (getPreferenceBool(PreferenceKeys.nightMode, preferences) !== false &&
  55. (getPreferenceBool(PreferenceKeys.nightModeFollowSystem, preferences) ===
  56. false ||
  57. colorScheme === 'no-preference')) ||
  58. (getPreferenceBool(PreferenceKeys.nightModeFollowSystem, preferences) !==
  59. false &&
  60. colorScheme === 'dark')
  61. );
  62. }
  63. export function useCurrentDashboard() {
  64. const { preferences, updatePreferences } = usePreferences();
  65. const navigation = useNavigation();
  66. const dashboardIdList = getPreferenceObject(
  67. PreferenceKeys.dashboardItems,
  68. preferences
  69. ) as Array<string>;
  70. const updateCurrentDashboard = (newList: Array<string>) => {
  71. updatePreferences(PreferenceKeys.dashboardItems, newList);
  72. };
  73. const allDatasets = [
  74. ...getAmicaleServices(navigation.navigate),
  75. ...getStudentServices(navigation.navigate),
  76. ...getINSAServices(navigation.navigate),
  77. ...getSpecialServices(navigation.navigate),
  78. ];
  79. return {
  80. currentDashboard: allDatasets.filter((item) =>
  81. dashboardIdList.includes(item.key)
  82. ),
  83. currentDashboardIdList: dashboardIdList,
  84. updateCurrentDashboard: updateCurrentDashboard,
  85. };
  86. }