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.

ThemeManager.js 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // @flow
  2. import AsyncStorageManager from "./AsyncStorageManager";
  3. import {DarkTheme, DefaultTheme} from 'react-native-paper';
  4. import AprilFoolsManager from "./AprilFoolsManager";
  5. import {Appearance} from 'react-native-appearance';
  6. const colorScheme = Appearance.getColorScheme();
  7. /**
  8. * Singleton class used to manage themes
  9. */
  10. export default class ThemeManager {
  11. static instance: ThemeManager | null = null;
  12. updateThemeCallback: Function;
  13. constructor() {
  14. this.updateThemeCallback = null;
  15. }
  16. /**
  17. * Gets the light theme
  18. *
  19. * @return {Object} Object containing theme variables
  20. * */
  21. static getWhiteTheme(): Object {
  22. return {
  23. ...DefaultTheme,
  24. colors: {
  25. ...DefaultTheme.colors,
  26. primary: '#be1522',
  27. accent: '#be1522',
  28. tabIcon: "#929292",
  29. card: "rgb(255, 255, 255)",
  30. dividerBackground: '#e2e2e2',
  31. textDisabled: '#c1c1c1',
  32. icon: '#5d5d5d',
  33. subtitle: '#707070',
  34. success: "#5cb85c",
  35. warning: "#f0ad4e",
  36. danger: "#d9534f",
  37. // Calendar/Agenda
  38. agendaBackgroundColor: '#f3f3f4',
  39. agendaDayTextColor: '#636363',
  40. // PROXIWASH
  41. proxiwashFinishedColor: "#a5dc9d",
  42. proxiwashReadyColor: "transparent",
  43. proxiwashRunningColor: "#a0ceff",
  44. proxiwashRunningBgColor: "#c7e3ff",
  45. proxiwashBrokenColor: "#8e8e8e",
  46. proxiwashErrorColor: "rgba(204,7,0,0.31)#e35f57",
  47. // Screens
  48. planningColor: '#d9b10a',
  49. proximoColor: '#ec5904',
  50. proxiwashColor: '#1fa5ee',
  51. menuColor: '#e91314',
  52. tutorinsaColor: '#f93943',
  53. // Tetris
  54. tetrisBackground:'#e6e6e6',
  55. tetrisBorder:'#2f2f2f',
  56. tetrisScore:'#e2bd33',
  57. tetrisI : '#3cd9e6',
  58. tetrisO : '#ffdd00',
  59. tetrisT : '#a716e5',
  60. tetrisS : '#09c528',
  61. tetrisZ : '#ff0009',
  62. tetrisJ : '#2a67e3',
  63. tetrisL : '#da742d',
  64. },
  65. };
  66. }
  67. /**
  68. * Gets the dark theme
  69. *
  70. * @return {Object} Object containing theme variables
  71. * */
  72. static getDarkTheme(): Object {
  73. return {
  74. ...DarkTheme,
  75. colors: {
  76. ...DarkTheme.colors,
  77. primary: '#be1522',
  78. accent: '#be1522',
  79. tabBackground: "#181818",
  80. tabIcon: "#6d6d6d",
  81. card: "rgb(18, 18, 18)",
  82. dividerBackground: '#222222',
  83. textDisabled: '#5b5b5b',
  84. icon: '#b3b3b3',
  85. subtitle: '#aaaaaa',
  86. success: "#5cb85c",
  87. warning: "#f0ad4e",
  88. danger: "#d9534f",
  89. // Calendar/Agenda
  90. agendaBackgroundColor: '#171717',
  91. agendaDayTextColor: '#6d6d6d',
  92. // PROXIWASH
  93. proxiwashFinishedColor: "#31682c",
  94. proxiwashReadyColor: "transparent",
  95. proxiwashRunningColor: "#213c79",
  96. proxiwashRunningBgColor: "#1a2033",
  97. proxiwashBrokenColor: "#656565",
  98. proxiwashErrorColor: "#7e2e2f",
  99. // Screens
  100. planningColor: '#d99e09',
  101. proximoColor: '#ec5904',
  102. proxiwashColor: '#1fa5ee',
  103. menuColor: '#b81213',
  104. tutorinsaColor: '#f93943',
  105. // Tetris
  106. tetrisBackground:'#2c2c2c',
  107. tetrisBorder:'#1b1b1b',
  108. tetrisScore:'#e2d707',
  109. tetrisI : '#30b3be',
  110. tetrisO : '#c1a700',
  111. tetrisT : '#9114c7',
  112. tetrisS : '#08a121',
  113. tetrisZ : '#b50008',
  114. tetrisJ : '#0f37b9',
  115. tetrisL : '#b96226',
  116. },
  117. };
  118. }
  119. /**
  120. * Get this class instance or create one if none is found
  121. *
  122. * @returns {ThemeManager}
  123. */
  124. static getInstance(): ThemeManager {
  125. return ThemeManager.instance === null ?
  126. ThemeManager.instance = new ThemeManager() :
  127. ThemeManager.instance;
  128. }
  129. /**
  130. * Gets night mode status.
  131. * If Follow System Preferences is enabled, will first use system theme.
  132. * If disabled or not available, will use value stored din preferences
  133. *
  134. * @returns {boolean} Night mode state
  135. */
  136. static getNightMode(): boolean {
  137. return (AsyncStorageManager.getInstance().preferences.nightMode.current === '1' &&
  138. (AsyncStorageManager.getInstance().preferences.nightModeFollowSystem.current !== '1' ||
  139. colorScheme === 'no-preference')) ||
  140. (AsyncStorageManager.getInstance().preferences.nightModeFollowSystem.current === '1' && colorScheme === 'dark');
  141. }
  142. /**
  143. * Get the current theme based on night mode and events
  144. *
  145. * @returns {Object} The current theme
  146. */
  147. static getCurrentTheme(): Object {
  148. if (AprilFoolsManager.getInstance().isAprilFoolsEnabled())
  149. return AprilFoolsManager.getAprilFoolsTheme(ThemeManager.getWhiteTheme());
  150. else
  151. return ThemeManager.getBaseTheme()
  152. }
  153. /**
  154. * Get the theme based on night mode
  155. *
  156. * @return {Object} The theme
  157. */
  158. static getBaseTheme() {
  159. if (ThemeManager.getNightMode())
  160. return ThemeManager.getDarkTheme();
  161. else
  162. return ThemeManager.getWhiteTheme();
  163. }
  164. /**
  165. * Sets the function to be called when the theme is changed (allows for general reload of the app)
  166. *
  167. * @param callback Function to call after theme change
  168. */
  169. setUpdateThemeCallback(callback: ?Function) {
  170. this.updateThemeCallback = callback;
  171. }
  172. /**
  173. * Set night mode and save it to preferences
  174. *
  175. * @param isNightMode True to enable night mode, false to disable
  176. */
  177. setNightMode(isNightMode: boolean) {
  178. let nightModeKey = AsyncStorageManager.getInstance().preferences.nightMode.key;
  179. AsyncStorageManager.getInstance().savePref(nightModeKey, isNightMode ? '1' : '0');
  180. if (this.updateThemeCallback !== null)
  181. this.updateThemeCallback();
  182. }
  183. };