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

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