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

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