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.

PlanexScreen.js 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. // @flow
  2. import * as React from 'react';
  3. import type {CustomTheme} from "../../managers/ThemeManager";
  4. import ThemeManager from "../../managers/ThemeManager";
  5. import WebViewScreen from "../../components/Screens/WebViewScreen";
  6. import {Avatar, Banner, withTheme} from "react-native-paper";
  7. import i18n from "i18n-js";
  8. import {View} from "react-native";
  9. import AsyncStorageManager from "../../managers/AsyncStorageManager";
  10. import AlertDialog from "../../components/Dialogs/AlertDialog";
  11. import {withCollapsible} from "../../utils/withCollapsible";
  12. import {dateToString, getTimeOnlyString} from "../../utils/Planning";
  13. import DateManager from "../../managers/DateManager";
  14. import AnimatedBottomBar from "../../components/Animations/AnimatedBottomBar";
  15. import {CommonActions} from "@react-navigation/native";
  16. import ErrorView from "../../components/Screens/ErrorView";
  17. import {StackNavigationProp} from "@react-navigation/stack";
  18. import {Collapsible} from "react-navigation-collapsible";
  19. import type {group} from "./GroupSelectionScreen";
  20. type Props = {
  21. navigation: StackNavigationProp,
  22. route: { params: { group: group } },
  23. theme: CustomTheme,
  24. collapsibleStack: Collapsible,
  25. }
  26. type State = {
  27. bannerVisible: boolean,
  28. dialogVisible: boolean,
  29. dialogTitle: string,
  30. dialogMessage: string,
  31. currentGroup: group,
  32. }
  33. const PLANEX_URL = 'http://planex.insa-toulouse.fr/';
  34. // // JS + JQuery functions used to remove alpha from events. Copy paste in browser console for quick testing
  35. // // Remove alpha from given Jquery node
  36. // function removeAlpha(node) {
  37. // let bg = node.css("background-color");
  38. // if (bg.match("^rgba")) {
  39. // let a = bg.slice(5).split(',');
  40. // // Fix for tooltips with broken background
  41. // if (parseInt(a[0]) === parseInt(a[1]) && parseInt(a[1]) === parseInt(a[2]) && parseInt(a[2]) === 0) {
  42. // a[0] = a[1] = a[2] = '255';
  43. // }
  44. // let newBg ='rgb(' + a[0] + ',' + a[1] + ',' + a[2] + ')';
  45. // node.css("background-color", newBg);
  46. // }
  47. // }
  48. // // Observe for planning DOM changes
  49. // let observer = new MutationObserver(function(mutations) {
  50. // for (let i = 0; i < mutations.length; i++) {
  51. // if (mutations[i]['addedNodes'].length > 0 &&
  52. // ($(mutations[i]['addedNodes'][0]).hasClass("fc-event") || $(mutations[i]['addedNodes'][0]).hasClass("tooltiptopicevent")))
  53. // removeAlpha($(mutations[i]['addedNodes'][0]))
  54. // }
  55. // });
  56. // // observer.observe(document.querySelector(".fc-body"), {attributes: false, childList: true, characterData: false, subtree:true});
  57. // observer.observe(document.querySelector("body"), {attributes: false, childList: true, characterData: false, subtree:true});
  58. // // Run remove alpha a first time on whole planning. Useful when code injected after planning fully loaded.
  59. // $(".fc-event-container .fc-event").each(function(index) {
  60. // removeAlpha($(this));
  61. // });
  62. // Watch for changes in the calendar and call the remove alpha function to prevent invisible events
  63. const OBSERVE_MUTATIONS_INJECTED =
  64. 'function removeAlpha(node) {\n' +
  65. ' let bg = node.css("background-color");\n' +
  66. ' if (bg.match("^rgba")) {\n' +
  67. ' let a = bg.slice(5).split(\',\');\n' +
  68. ' // Fix for tooltips with broken background\n' +
  69. ' if (parseInt(a[0]) === parseInt(a[1]) && parseInt(a[1]) === parseInt(a[2]) && parseInt(a[2]) === 0) {\n' +
  70. ' a[0] = a[1] = a[2] = \'255\';\n' +
  71. ' }\n' +
  72. ' let newBg =\'rgb(\' + a[0] + \',\' + a[1] + \',\' + a[2] + \')\';\n' +
  73. ' node.css("background-color", newBg);\n' +
  74. ' }\n' +
  75. '}\n' +
  76. '// Observe for planning DOM changes\n' +
  77. 'let observer = new MutationObserver(function(mutations) {\n' +
  78. ' for (let i = 0; i < mutations.length; i++) {\n' +
  79. ' if (mutations[i][\'addedNodes\'].length > 0 &&\n' +
  80. ' ($(mutations[i][\'addedNodes\'][0]).hasClass("fc-event") || $(mutations[i][\'addedNodes\'][0]).hasClass("tooltiptopicevent")))\n' +
  81. ' removeAlpha($(mutations[i][\'addedNodes\'][0]))\n' +
  82. ' }\n' +
  83. '});\n' +
  84. '// observer.observe(document.querySelector(".fc-body"), {attributes: false, childList: true, characterData: false, subtree:true});\n' +
  85. 'observer.observe(document.querySelector("body"), {attributes: false, childList: true, characterData: false, subtree:true});\n' +
  86. '// Run remove alpha a first time on whole planning. Useful when code injected after planning fully loaded.\n' +
  87. '$(".fc-event-container .fc-event").each(function(index) {\n' +
  88. ' removeAlpha($(this));\n' +
  89. '});';
  90. // Overrides default settings to send a message to the webview when clicking on an event
  91. const FULL_CALENDAR_SETTINGS = `
  92. let calendar = $('#calendar').fullCalendar('getCalendar');
  93. calendar.option({
  94. eventClick: function (data, event, view) {
  95. let message = {
  96. title: data.title,
  97. color: data.color,
  98. start: data.start._d,
  99. end: data.end._d,
  100. };
  101. window.ReactNativeWebView.postMessage(JSON.stringify(message));
  102. }
  103. });`;
  104. const CUSTOM_CSS = "body>.container{padding-top:20px; padding-bottom: 50px}header,#entite,#groupe_visibility,#calendar .fc-left,#calendar .fc-right{display:none}#calendar .fc-agendaWeek-view .fc-content-skeleton .fc-title{font-size:.6rem}#calendar .fc-agendaWeek-view .fc-content-skeleton .fc-time{font-size:.5rem}#calendar .fc-month-view .fc-content-skeleton .fc-title{font-size:.6rem}#calendar .fc-month-view .fc-content-skeleton .fc-time{font-size:.7rem}.fc-axis{font-size:.8rem;width:15px!important}.fc-day-header{font-size:.8rem}.fc-unthemed td.fc-today{background:#be1522; opacity:0.4}";
  105. const CUSTOM_CSS_DARK = "body{background-color:#121212}.fc-unthemed .fc-content,.fc-unthemed .fc-divider,.fc-unthemed .fc-list-heading td,.fc-unthemed .fc-list-view,.fc-unthemed .fc-popover,.fc-unthemed .fc-row,.fc-unthemed tbody,.fc-unthemed td,.fc-unthemed th,.fc-unthemed thead{border-color:#222}.fc-toolbar .fc-center>*,h2,table{color:#fff}.fc-event-container{color:#121212}.fc-event-container .fc-bg{opacity:0.2;background-color:#000}.fc-unthemed td.fc-today{background:#be1522; opacity:0.4}";
  106. const INJECT_STYLE = `
  107. $('head').append('<style>` + CUSTOM_CSS + `</style>');
  108. `;
  109. /**
  110. * Class defining the app's Planex screen.
  111. * This screen uses a webview to render the page
  112. */
  113. class PlanexScreen extends React.Component<Props, State> {
  114. webScreenRef: { current: null | WebViewScreen };
  115. barRef: { current: null | AnimatedBottomBar };
  116. customInjectedJS: string;
  117. /**
  118. * Defines custom injected JavaScript to improve the page display on mobile
  119. */
  120. constructor(props) {
  121. super(props);
  122. this.webScreenRef = React.createRef();
  123. this.barRef = React.createRef();
  124. let currentGroup = AsyncStorageManager.getInstance().preferences.planexCurrentGroup.current;
  125. if (currentGroup === '')
  126. currentGroup = {name: "SELECT GROUP", id: -1, isFav: false};
  127. else {
  128. currentGroup = JSON.parse(currentGroup);
  129. props.navigation.setOptions({title: currentGroup.name})
  130. }
  131. this.state = {
  132. bannerVisible: false,
  133. dialogVisible: false,
  134. dialogTitle: "",
  135. dialogMessage: "",
  136. currentGroup: currentGroup,
  137. };
  138. this.generateInjectedJS(currentGroup.id);
  139. }
  140. /**
  141. * Register for events and show the banner after 2 seconds
  142. */
  143. componentDidMount() {
  144. this.props.navigation.addListener('focus', this.onScreenFocus);
  145. setTimeout(this.onBannerTimeout, 2000);
  146. }
  147. onBannerTimeout = () => {
  148. this.setState({
  149. bannerVisible:
  150. AsyncStorageManager.getInstance().preferences.planexShowBanner.current === '1' &&
  151. AsyncStorageManager.getInstance().preferences.defaultStartScreen.current !== 'Planex'
  152. })
  153. }
  154. /**
  155. * Callback used when closing the banner.
  156. * This hides the banner and saves to preferences to prevent it from reopening
  157. */
  158. onHideBanner = () => {
  159. this.setState({bannerVisible: false});
  160. AsyncStorageManager.getInstance().savePref(
  161. AsyncStorageManager.getInstance().preferences.planexShowBanner.key,
  162. '0'
  163. );
  164. };
  165. /**
  166. * Callback used when the user clicks on the navigate to settings button.
  167. * This will hide the banner and open the SettingsScreen
  168. */
  169. onGoToSettings = () => {
  170. this.onHideBanner();
  171. this.props.navigation.navigate('settings');
  172. };
  173. onScreenFocus = () => {
  174. this.handleNavigationParams();
  175. };
  176. /**
  177. * If navigations parameters contain a group, set it as selected
  178. */
  179. handleNavigationParams = () => {
  180. if (this.props.route.params != null) {
  181. if (this.props.route.params.group !== undefined && this.props.route.params.group !== null) {
  182. // reset params to prevent infinite loop
  183. this.selectNewGroup(this.props.route.params.group);
  184. this.props.navigation.dispatch(CommonActions.setParams({group: null}));
  185. }
  186. }
  187. };
  188. /**
  189. * Sends the webpage a message with the new group to select and save it to preferences
  190. *
  191. * @param group The group object selected
  192. */
  193. selectNewGroup(group: group) {
  194. this.sendMessage('setGroup', group.id);
  195. this.setState({currentGroup: group});
  196. AsyncStorageManager.getInstance().savePref(
  197. AsyncStorageManager.getInstance().preferences.planexCurrentGroup.key,
  198. JSON.stringify(group)
  199. );
  200. this.props.navigation.setOptions({title: group.name});
  201. this.generateInjectedJS(group.id);
  202. }
  203. /**
  204. * Generates custom JavaScript to be injected into the webpage
  205. *
  206. * @param groupID The current group selected
  207. */
  208. generateInjectedJS(groupID: number) {
  209. this.customInjectedJS = "$(document).ready(function() {"
  210. + OBSERVE_MUTATIONS_INJECTED
  211. + FULL_CALENDAR_SETTINGS
  212. + "displayAde(" + groupID + ");" // Reset Ade
  213. + (DateManager.isWeekend(new Date()) ? "calendar.next()" : "")
  214. + INJECT_STYLE;
  215. if (ThemeManager.getNightMode())
  216. this.customInjectedJS += "$('head').append('<style>" + CUSTOM_CSS_DARK + "</style>');";
  217. this.customInjectedJS += 'removeAlpha();});true;'; // Prevents crash on ios
  218. }
  219. /**
  220. * Only update the screen if the dark theme changed
  221. *
  222. * @param nextProps
  223. * @returns {boolean}
  224. */
  225. shouldComponentUpdate(nextProps: Props): boolean {
  226. if (nextProps.theme.dark !== this.props.theme.dark)
  227. this.generateInjectedJS(this.state.currentGroup.id);
  228. return true;
  229. }
  230. /**
  231. * Sends a FullCalendar action to the web page inside the webview.
  232. *
  233. * @param action The action to perform, as described in the FullCalendar doc https://fullcalendar.io/docs/v3.
  234. * Or "setGroup" with the group id as data to set the selected group
  235. * @param data Data to pass to the action
  236. */
  237. sendMessage = (action: string, data: any) => {
  238. let command;
  239. if (action === "setGroup")
  240. command = "displayAde(" + data + ")";
  241. else
  242. command = "$('#calendar').fullCalendar('" + action + "', '" + data + "')";
  243. if (this.webScreenRef.current != null)
  244. this.webScreenRef.current.injectJavaScript(command + ';true;'); // Injected javascript must end with true
  245. };
  246. /**
  247. * Shows a dialog when the user clicks on an event.
  248. *
  249. * @param event
  250. */
  251. onMessage = (event: { nativeEvent: { data: string } }) => {
  252. const data: { start: string, end: string, title: string, color: string } = JSON.parse(event.nativeEvent.data);
  253. const startDate = dateToString(new Date(data.start), true);
  254. const endDate = dateToString(new Date(data.end), true);
  255. const startString = getTimeOnlyString(startDate);
  256. const endString = getTimeOnlyString(endDate);
  257. let msg = DateManager.getInstance().getTranslatedDate(startDate) + "\n";
  258. if (startString != null && endString != null)
  259. msg += startString + ' - ' + endString;
  260. this.showDialog(data.title, msg)
  261. };
  262. /**
  263. * Shows a simple dialog to the user.
  264. *
  265. * @param title The dialog's title
  266. * @param message The message to show
  267. */
  268. showDialog = (title: string, message: string) => {
  269. this.setState({
  270. dialogVisible: true,
  271. dialogTitle: title,
  272. dialogMessage: message,
  273. });
  274. };
  275. /**
  276. * Hides the dialog
  277. */
  278. hideDialog = () => {
  279. this.setState({
  280. dialogVisible: false,
  281. });
  282. };
  283. /**
  284. * Binds the onScroll event to the control bar for automatic hiding based on scroll direction and speed
  285. *
  286. * @param event
  287. */
  288. onScroll = (event: SyntheticEvent<EventTarget>) => {
  289. if (this.barRef.current != null)
  290. this.barRef.current.onScroll(event);
  291. };
  292. /**
  293. * Gets the Webview, with an error view on top if no group is selected.
  294. *
  295. * @returns {*}
  296. */
  297. getWebView() {
  298. const showWebview = this.state.currentGroup.id !== -1;
  299. return (
  300. <View style={{height: '100%'}}>
  301. {!showWebview
  302. ? <ErrorView
  303. {...this.props}
  304. icon={'account-clock'}
  305. message={i18n.t("planexScreen.noGroupSelected")}
  306. showRetryButton={false}
  307. />
  308. : null}
  309. <WebViewScreen
  310. ref={this.webScreenRef}
  311. navigation={this.props.navigation}
  312. url={PLANEX_URL}
  313. customJS={this.customInjectedJS}
  314. onMessage={this.onMessage}
  315. onScroll={this.onScroll}
  316. showAdvancedControls={false}
  317. />
  318. </View>
  319. );
  320. }
  321. render() {
  322. const {containerPaddingTop} = this.props.collapsibleStack;
  323. return (
  324. <View
  325. style={{flex: 1}}
  326. >
  327. {/*Allow to draw webview bellow banner*/}
  328. <View style={{
  329. position: 'absolute',
  330. height: '100%',
  331. width: '100%',
  332. }}>
  333. {this.props.theme.dark // Force component theme update by recreating it on theme change
  334. ? this.getWebView()
  335. : <View style={{height: '100%'}}>{this.getWebView()}</View>}
  336. </View>
  337. <Banner
  338. style={{
  339. marginTop: containerPaddingTop,
  340. backgroundColor: this.props.theme.colors.surface
  341. }}
  342. visible={this.state.bannerVisible}
  343. actions={[
  344. {
  345. label: i18n.t('planexScreen.enableStartOK'),
  346. onPress: this.onGoToSettings,
  347. },
  348. {
  349. label: i18n.t('planexScreen.enableStartCancel'),
  350. onPress: this.onHideBanner,
  351. },
  352. ]}
  353. icon={() => <Avatar.Icon
  354. icon={'power'}
  355. size={40}
  356. />}
  357. >
  358. {i18n.t('planexScreen.enableStartScreen')}
  359. </Banner>
  360. <AlertDialog
  361. visible={this.state.dialogVisible}
  362. onDismiss={this.hideDialog}
  363. title={this.state.dialogTitle}
  364. message={this.state.dialogMessage}/>
  365. <AnimatedBottomBar
  366. {...this.props}
  367. ref={this.barRef}
  368. onPress={this.sendMessage}
  369. seekAttention={this.state.currentGroup.id === -1}
  370. />
  371. </View>
  372. );
  373. }
  374. }
  375. export default withCollapsible(withTheme(PlanexScreen));