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

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