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.

PlanningScreen.tsx 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 * as React from 'react';
  20. import { BackHandler, StyleSheet, View } from 'react-native';
  21. import i18n from 'i18n-js';
  22. import { Agenda, LocaleConfig } from 'react-native-calendars';
  23. import { Avatar, Divider, List } from 'react-native-paper';
  24. import { StackNavigationProp } from '@react-navigation/stack';
  25. import { readData } from '../../utils/WebData';
  26. import {
  27. generateEventAgenda,
  28. getCurrentDateString,
  29. getDateOnlyString,
  30. getTimeOnlyString,
  31. PlanningEventType,
  32. } from '../../utils/Planning';
  33. import CustomAgenda from '../../components/Overrides/CustomAgenda';
  34. import { MASCOT_STYLE } from '../../components/Mascot/Mascot';
  35. import MascotPopup from '../../components/Mascot/MascotPopup';
  36. import GENERAL_STYLES from '../../constants/Styles';
  37. import Urls from '../../constants/Urls';
  38. LocaleConfig.locales.fr = {
  39. monthNames: [
  40. 'Janvier',
  41. 'Février',
  42. 'Mars',
  43. 'Avril',
  44. 'Mai',
  45. 'Juin',
  46. 'Juillet',
  47. 'Août',
  48. 'Septembre',
  49. 'Octobre',
  50. 'Novembre',
  51. 'Décembre',
  52. ],
  53. monthNamesShort: [
  54. 'Janv.',
  55. 'Févr.',
  56. 'Mars',
  57. 'Avril',
  58. 'Mai',
  59. 'Juin',
  60. 'Juil.',
  61. 'Août',
  62. 'Sept.',
  63. 'Oct.',
  64. 'Nov.',
  65. 'Déc.',
  66. ],
  67. dayNames: [
  68. 'Dimanche',
  69. 'Lundi',
  70. 'Mardi',
  71. 'Mercredi',
  72. 'Jeudi',
  73. 'Vendredi',
  74. 'Samedi',
  75. ],
  76. dayNamesShort: ['Dim', 'Lun', 'Mar', 'Mer', 'Jeu', 'Ven', 'Sam'],
  77. today: "Aujourd'hui",
  78. };
  79. type PropsType = {
  80. navigation: StackNavigationProp<any>;
  81. };
  82. type StateType = {
  83. refreshing: boolean;
  84. agendaItems: { [key: string]: Array<PlanningEventType> };
  85. calendarShowing: boolean;
  86. };
  87. const AGENDA_MONTH_SPAN = 3;
  88. const styles = StyleSheet.create({
  89. icon: {
  90. backgroundColor: 'transparent',
  91. },
  92. });
  93. /**
  94. * Class defining the app's planning screen
  95. */
  96. class PlanningScreen extends React.Component<PropsType, StateType> {
  97. agendaRef: null | Agenda<any>;
  98. lastRefresh: Date | null;
  99. minTimeBetweenRefresh = 60;
  100. currentDate: string | null;
  101. constructor(props: PropsType) {
  102. super(props);
  103. if (i18n.currentLocale().startsWith('fr')) {
  104. LocaleConfig.defaultLocale = 'fr';
  105. }
  106. this.agendaRef = null;
  107. this.currentDate = getDateOnlyString(getCurrentDateString());
  108. this.lastRefresh = null;
  109. this.state = {
  110. refreshing: false,
  111. agendaItems: {},
  112. calendarShowing: false,
  113. };
  114. }
  115. /**
  116. * Captures focus and blur events to hook on android back button
  117. */
  118. componentDidMount() {
  119. const { navigation } = this.props;
  120. this.onRefresh();
  121. navigation.addListener('focus', () => {
  122. BackHandler.addEventListener(
  123. 'hardwareBackPress',
  124. this.onBackButtonPressAndroid
  125. );
  126. });
  127. navigation.addListener('blur', () => {
  128. BackHandler.removeEventListener(
  129. 'hardwareBackPress',
  130. this.onBackButtonPressAndroid
  131. );
  132. });
  133. }
  134. /**
  135. * Overrides default android back button behaviour to close the calendar if it was open.
  136. *
  137. * @return {boolean}
  138. */
  139. onBackButtonPressAndroid = (): boolean => {
  140. const { calendarShowing } = this.state;
  141. if (calendarShowing && this.agendaRef != null) {
  142. // @ts-ignore
  143. this.agendaRef.chooseDay(this.agendaRef.state.selectedDay);
  144. return true;
  145. }
  146. return false;
  147. };
  148. /**
  149. * Refreshes data and shows an animation while doing it
  150. */
  151. onRefresh = () => {
  152. let canRefresh;
  153. if (this.lastRefresh) {
  154. canRefresh =
  155. (new Date().getTime() - this.lastRefresh.getTime()) / 1000 >
  156. this.minTimeBetweenRefresh;
  157. } else {
  158. canRefresh = true;
  159. }
  160. if (canRefresh) {
  161. this.setState({ refreshing: true });
  162. readData<Array<PlanningEventType>>(Urls.amicale.events)
  163. .then((fetchedData) => {
  164. this.setState({
  165. refreshing: false,
  166. agendaItems: generateEventAgenda(fetchedData, AGENDA_MONTH_SPAN),
  167. });
  168. this.lastRefresh = new Date();
  169. })
  170. .catch(() => {
  171. this.setState({
  172. refreshing: false,
  173. });
  174. });
  175. }
  176. };
  177. /**
  178. * Callback used when receiving the agenda ref
  179. *
  180. * @param ref
  181. */
  182. onAgendaRef = (ref: Agenda<any>) => {
  183. this.agendaRef = ref;
  184. };
  185. /**
  186. * Callback used when a button is pressed to toggle the calendar
  187. *
  188. * @param isCalendarOpened True is the calendar is already open, false otherwise
  189. */
  190. onCalendarToggled = (isCalendarOpened: boolean) => {
  191. this.setState({ calendarShowing: isCalendarOpened });
  192. };
  193. /**
  194. * Gets an event render item
  195. *
  196. * @param item The current event to render
  197. * @return {*}
  198. */
  199. getRenderItem = (item: PlanningEventType) => {
  200. const { navigation } = this.props;
  201. const onPress = () => {
  202. navigation.navigate('planning-information', {
  203. data: item,
  204. });
  205. };
  206. const logo = item.logo;
  207. if (logo) {
  208. return (
  209. <View>
  210. <Divider />
  211. <List.Item
  212. title={item.title}
  213. description={getTimeOnlyString(item.date_begin)}
  214. left={() => (
  215. <Avatar.Image source={{ uri: logo }} style={styles.icon} />
  216. )}
  217. onPress={onPress}
  218. />
  219. </View>
  220. );
  221. }
  222. return (
  223. <View>
  224. <Divider />
  225. <List.Item
  226. title={item.title}
  227. description={getTimeOnlyString(item.date_begin)}
  228. onPress={onPress}
  229. />
  230. </View>
  231. );
  232. };
  233. /**
  234. * Gets an empty render item for an empty date
  235. *
  236. * @return {*}
  237. */
  238. getRenderEmptyDate = () => <Divider />;
  239. render() {
  240. const { state, props } = this;
  241. return (
  242. <View style={GENERAL_STYLES.flex}>
  243. <CustomAgenda
  244. {...props}
  245. // the list of items that have to be displayed in agenda. If you want to render item as empty date
  246. // the value of date key kas to be an empty array []. If there exists no value for date key it is
  247. // considered that the date in question is not yet loaded
  248. items={state.agendaItems}
  249. // initially selected day
  250. selected={this.currentDate ? this.currentDate : undefined}
  251. // Minimum date that can be selected, dates before minDate will be grayed out. Default = undefined
  252. minDate={this.currentDate ? this.currentDate : undefined}
  253. // Max amount of months allowed to scroll to the past. Default = 50
  254. pastScrollRange={1}
  255. // Max amount of months allowed to scroll to the future. Default = 50
  256. futureScrollRange={AGENDA_MONTH_SPAN}
  257. // If provided, a standard RefreshControl will be added for "Pull to Refresh" functionality. Make sure to also set the refreshing prop correctly.
  258. onRefresh={this.onRefresh}
  259. // callback that fires when the calendar is opened or closed
  260. onCalendarToggled={this.onCalendarToggled}
  261. // Set this true while waiting for new data from a refresh
  262. refreshing={state.refreshing}
  263. renderItem={this.getRenderItem}
  264. renderEmptyDate={this.getRenderEmptyDate}
  265. // If firstDay=1 week starts from Monday. Note that dayNames and dayNamesShort should still start from Sunday.
  266. firstDay={1}
  267. // ref to this agenda in order to handle back button event
  268. onRef={this.onAgendaRef}
  269. rowHasChanged={(r1: PlanningEventType, r2: PlanningEventType) =>
  270. r1.id !== r2.id
  271. }
  272. />
  273. <MascotPopup
  274. title={i18n.t('screens.planning.mascotDialog.title')}
  275. message={i18n.t('screens.planning.mascotDialog.message')}
  276. icon="party-popper"
  277. buttons={{
  278. cancel: {
  279. message: i18n.t('screens.planning.mascotDialog.button'),
  280. icon: 'check',
  281. },
  282. }}
  283. emotion={MASCOT_STYLE.HAPPY}
  284. />
  285. </View>
  286. );
  287. }
  288. }
  289. export default PlanningScreen;