Application Android et IOS pour l'amicale des élèves https://play.google.com/store/apps/details?id=fr.amicaleinsat.application
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.tsx 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Copyright (c) 2019 - 2020 Arnaud Vergnet.
  3. *
  4. * This file is part of Campus INSAT.
  5. *
  6. * Campus INSAT is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Campus INSAT is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Campus INSAT. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. import React, { useCallback, useEffect, useState } from 'react';
  20. import { Title, useTheme } from 'react-native-paper';
  21. import i18n from 'i18n-js';
  22. import { StyleSheet, View } from 'react-native';
  23. import { useNavigation } from '@react-navigation/native';
  24. import Autolink from 'react-native-autolink';
  25. import AlertDialog from '../../components/Dialogs/AlertDialog';
  26. import { dateToString, getTimeOnlyString } from '../../utils/Planning';
  27. import DateManager from '../../managers/DateManager';
  28. import type { PlanexGroupType } from './GroupSelectionScreen';
  29. import { MASCOT_STYLE } from '../../components/Mascot/Mascot';
  30. import MascotPopup from '../../components/Mascot/MascotPopup';
  31. import { getPrettierPlanexGroupName } from '../../utils/Utils';
  32. import GENERAL_STYLES from '../../constants/Styles';
  33. import PlanexWebview, {
  34. JS_LOADED_MESSAGE,
  35. } from '../../components/Screens/PlanexWebview';
  36. import PlanexBottomBar from '../../components/Animations/PlanexBottomBar';
  37. import {
  38. getPreferenceString,
  39. GeneralPreferenceKeys,
  40. PlanexPreferenceKeys,
  41. } from '../../utils/asyncStorage';
  42. import { usePlanexPreferences } from '../../context/preferencesContext';
  43. import BasicLoadingScreen from '../../components/Screens/BasicLoadingScreen';
  44. const styles = StyleSheet.create({
  45. container: {
  46. position: 'absolute',
  47. height: '100%',
  48. width: '100%',
  49. },
  50. popup: {
  51. borderWidth: 2,
  52. },
  53. });
  54. function PlanexScreen() {
  55. const navigation = useNavigation();
  56. const theme = useTheme();
  57. const { preferences } = usePlanexPreferences();
  58. const [dialogContent, setDialogContent] = useState<
  59. | undefined
  60. | {
  61. title: string | React.ReactElement;
  62. message: string | React.ReactElement;
  63. color: string;
  64. }
  65. >();
  66. const [injectJS, setInjectJS] = useState('');
  67. const [loading, setLoading] = useState(true);
  68. const getCurrentGroup: () => PlanexGroupType | undefined = useCallback(() => {
  69. let currentGroupString = getPreferenceString(
  70. PlanexPreferenceKeys.planexCurrentGroup,
  71. preferences
  72. );
  73. let group: PlanexGroupType;
  74. if (currentGroupString) {
  75. group = JSON.parse(currentGroupString);
  76. navigation.setOptions({
  77. title: getPrettierPlanexGroupName(group.name),
  78. });
  79. return group;
  80. }
  81. return undefined;
  82. }, [navigation, preferences]);
  83. const currentGroup = getCurrentGroup();
  84. /**
  85. * Gets the Webview, with an error view on top if no group is selected.
  86. *
  87. * @returns {*}
  88. */
  89. const getWebView = () => (
  90. <PlanexWebview
  91. currentGroup={currentGroup}
  92. injectJS={injectJS}
  93. onMessage={onMessage}
  94. />
  95. );
  96. /**
  97. * Callback used when the user clicks on the navigate to settings button.
  98. * This will hide the banner and open the SettingsScreen
  99. */
  100. const onGoToSettings = () => navigation.navigate('settings');
  101. /**
  102. * Sends a FullCalendar action to the web page inside the webview.
  103. *
  104. * @param action The action to perform, as described in the FullCalendar doc https://fullcalendar.io/docs/v3.
  105. * Or "setGroup" with the group id as data to set the selected group
  106. * @param data Data to pass to the action
  107. */
  108. const sendMessage = (action: string, data?: string) => {
  109. let command;
  110. if (action === 'setGroup') {
  111. command = `displayAde(${data})`;
  112. } else {
  113. command = `$('#calendar').fullCalendar('${action}', '${data}')`;
  114. }
  115. // String must resolve to true to prevent crash on iOS
  116. command += ';true;';
  117. // Change the injected
  118. if (command === injectJS) {
  119. command += ';true;';
  120. }
  121. setInjectJS(command);
  122. };
  123. /**
  124. * Shows a dialog when the user clicks on an event.
  125. *
  126. * @param event
  127. */
  128. const onMessage = (event: { nativeEvent: { data: string } }) => {
  129. if (event.nativeEvent.data === JS_LOADED_MESSAGE) {
  130. setLoading(false);
  131. } else {
  132. const data: {
  133. start: string;
  134. end: string;
  135. title: string;
  136. color: string;
  137. } = JSON.parse(event.nativeEvent.data);
  138. const startDate = dateToString(new Date(data.start), true);
  139. const endDate = dateToString(new Date(data.end), true);
  140. const startString = getTimeOnlyString(startDate);
  141. const endString = getTimeOnlyString(endDate);
  142. let msg = `${DateManager.getInstance().getTranslatedDate(startDate)}\n`;
  143. if (startString != null && endString != null) {
  144. msg += `${startString} - ${endString}`;
  145. }
  146. showDialog(data.title, msg, data.color);
  147. }
  148. };
  149. /**
  150. * Shows a simple dialog to the user.
  151. *
  152. * @param title The dialog's title
  153. * @param message The message to show
  154. */
  155. const showDialog = (title: string, message: string, color?: string) => {
  156. const finalColor = color ? color : theme.colors.surface;
  157. setDialogContent({
  158. title: (
  159. <Autolink
  160. text={title}
  161. hashtag={'facebook'}
  162. component={Title}
  163. truncate={32}
  164. email={true}
  165. url={true}
  166. phone={true}
  167. />
  168. ),
  169. message: message,
  170. color: finalColor,
  171. });
  172. };
  173. const hideDialog = () => setDialogContent(undefined);
  174. useEffect(() => {
  175. const group = getCurrentGroup();
  176. if (group) {
  177. sendMessage('setGroup', group.id.toString());
  178. navigation.setOptions({ title: getPrettierPlanexGroupName(group.name) });
  179. }
  180. // eslint-disable-next-line react-hooks/exhaustive-deps
  181. }, [getCurrentGroup, navigation]);
  182. const showMascot =
  183. getPreferenceString(
  184. GeneralPreferenceKeys.defaultStartScreen,
  185. preferences
  186. )?.toLowerCase() !== 'planex';
  187. return (
  188. <View style={GENERAL_STYLES.flex}>
  189. {/* Allow to draw webview bellow banner */}
  190. <View style={styles.container}>
  191. {theme.dark ? ( // Force component theme update by recreating it on theme change
  192. getWebView()
  193. ) : (
  194. <View style={GENERAL_STYLES.flex}>{getWebView()}</View>
  195. )}
  196. </View>
  197. {loading ? (
  198. <View style={styles.container}>
  199. <BasicLoadingScreen />
  200. </View>
  201. ) : null}
  202. {showMascot ? (
  203. <MascotPopup
  204. title={i18n.t('screens.planex.mascotDialog.title')}
  205. message={i18n.t('screens.planex.mascotDialog.message')}
  206. icon="emoticon-kiss"
  207. buttons={{
  208. action: {
  209. message: i18n.t('screens.planex.mascotDialog.ok'),
  210. icon: 'cog',
  211. onPress: onGoToSettings,
  212. },
  213. cancel: {
  214. message: i18n.t('screens.planex.mascotDialog.cancel'),
  215. icon: 'close',
  216. color: theme.colors.warning,
  217. },
  218. }}
  219. emotion={MASCOT_STYLE.INTELLO}
  220. />
  221. ) : null}
  222. <AlertDialog
  223. visible={dialogContent !== undefined}
  224. onDismiss={hideDialog}
  225. title={dialogContent ? dialogContent.title : ''}
  226. message={dialogContent ? dialogContent.message : ''}
  227. style={
  228. dialogContent
  229. ? { borderColor: dialogContent.color, ...styles.popup }
  230. : undefined
  231. }
  232. />
  233. <PlanexBottomBar
  234. onPress={sendMessage}
  235. seekAttention={currentGroup === undefined}
  236. />
  237. </View>
  238. );
  239. }
  240. export default PlanexScreen;