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.

PlanningScreen.js 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // @flow
  2. import * as React from 'react';
  3. import {BackHandler, View} from 'react-native';
  4. import i18n from "i18n-js";
  5. import {LocaleConfig} from 'react-native-calendars';
  6. import {readData} from "../../utils/WebData";
  7. import type {eventObject} from "../../utils/Planning";
  8. import {
  9. generateEventAgenda,
  10. getCurrentDateString,
  11. getDateOnlyString,
  12. getFormattedEventTime,
  13. } from '../../utils/Planning';
  14. import {Avatar, Divider, List} from 'react-native-paper';
  15. import CustomAgenda from "../../components/CustomAgenda";
  16. LocaleConfig.locales['fr'] = {
  17. monthNames: ['Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre'],
  18. monthNamesShort: ['Janv.', 'Févr.', 'Mars', 'Avril', 'Mai', 'Juin', 'Juil.', 'Août', 'Sept.', 'Oct.', 'Nov.', 'Déc.'],
  19. dayNames: ['Dimanche', 'Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi'],
  20. dayNamesShort: ['Dim', 'Lun', 'Mar', 'Mer', 'Jeu', 'Ven', 'Sam'],
  21. today: 'Aujourd\'hui'
  22. };
  23. type Props = {
  24. navigation: Object,
  25. }
  26. type State = {
  27. refreshing: boolean,
  28. agendaItems: Object,
  29. calendarShowing: boolean,
  30. };
  31. const FETCH_URL = "https://www.amicale-insat.fr/api/event/list";
  32. const AGENDA_MONTH_SPAN = 3;
  33. /**
  34. * Class defining the app's planning screen
  35. */
  36. export default class PlanningScreen extends React.Component<Props, State> {
  37. agendaRef: Object;
  38. lastRefresh: Date;
  39. minTimeBetweenRefresh = 60;
  40. didFocusSubscription: Function;
  41. willBlurSubscription: Function;
  42. state = {
  43. refreshing: false,
  44. agendaItems: {},
  45. calendarShowing: false,
  46. };
  47. onRefresh: Function;
  48. onCalendarToggled: Function;
  49. getRenderItem: Function;
  50. getRenderEmptyDate: Function;
  51. onAgendaRef: Function;
  52. onCalendarToggled: Function;
  53. onBackButtonPressAndroid: Function;
  54. currentDate = getDateOnlyString(getCurrentDateString());
  55. constructor(props: any) {
  56. super(props);
  57. if (i18n.currentLocale().startsWith("fr")) {
  58. LocaleConfig.defaultLocale = 'fr';
  59. }
  60. // Create references for functions required in the render function
  61. this.onRefresh = this.onRefresh.bind(this);
  62. this.onCalendarToggled = this.onCalendarToggled.bind(this);
  63. this.getRenderItem = this.getRenderItem.bind(this);
  64. this.getRenderEmptyDate = this.getRenderEmptyDate.bind(this);
  65. this.onAgendaRef = this.onAgendaRef.bind(this);
  66. this.onCalendarToggled = this.onCalendarToggled.bind(this);
  67. this.onBackButtonPressAndroid = this.onBackButtonPressAndroid.bind(this);
  68. }
  69. /**
  70. * Captures focus and blur events to hook on android back button
  71. */
  72. componentDidMount() {
  73. this.onRefresh();
  74. this.didFocusSubscription = this.props.navigation.addListener(
  75. 'focus',
  76. () =>
  77. BackHandler.addEventListener(
  78. 'hardwareBackPress',
  79. this.onBackButtonPressAndroid
  80. )
  81. );
  82. this.willBlurSubscription = this.props.navigation.addListener(
  83. 'blur',
  84. () =>
  85. BackHandler.removeEventListener(
  86. 'hardwareBackPress',
  87. this.onBackButtonPressAndroid
  88. )
  89. );
  90. }
  91. /**
  92. * Overrides default android back button behaviour to close the calendar if it was open.
  93. *
  94. * @return {boolean}
  95. */
  96. onBackButtonPressAndroid() {
  97. if (this.state.calendarShowing) {
  98. this.agendaRef.chooseDay(this.agendaRef.state.selectedDay);
  99. return true;
  100. } else {
  101. return false;
  102. }
  103. };
  104. /**
  105. * Function used to check if a row has changed
  106. *
  107. * @param r1
  108. * @param r2
  109. * @return {boolean}
  110. */
  111. rowHasChanged(r1: Object, r2: Object) {
  112. return false;
  113. // if (r1 !== undefined && r2 !== undefined)
  114. // return r1.title !== r2.title;
  115. // else return !(r1 === undefined && r2 === undefined);
  116. }
  117. /**
  118. * Refreshes data and shows an animation while doing it
  119. */
  120. onRefresh = () => {
  121. let canRefresh;
  122. if (this.lastRefresh !== undefined)
  123. canRefresh = (new Date().getTime() - this.lastRefresh.getTime()) / 1000 > this.minTimeBetweenRefresh;
  124. else
  125. canRefresh = true;
  126. if (canRefresh) {
  127. this.setState({refreshing: true});
  128. readData(FETCH_URL)
  129. .then((fetchedData) => {
  130. this.setState({
  131. refreshing: false,
  132. agendaItems: generateEventAgenda(fetchedData, AGENDA_MONTH_SPAN)
  133. });
  134. this.lastRefresh = new Date();
  135. })
  136. .catch(() => {
  137. this.setState({
  138. refreshing: false,
  139. });
  140. });
  141. }
  142. };
  143. /**
  144. * Callback used when receiving the agenda ref
  145. *
  146. * @param ref
  147. */
  148. onAgendaRef(ref: Object) {
  149. this.agendaRef = ref;
  150. }
  151. /**
  152. * Callback used when a button is pressed to toggle the calendar
  153. *
  154. * @param isCalendarOpened True is the calendar is already open, false otherwise
  155. */
  156. onCalendarToggled(isCalendarOpened: boolean) {
  157. this.setState({calendarShowing: isCalendarOpened});
  158. }
  159. /**
  160. * Gets an event render item
  161. *
  162. * @param item The current event to render
  163. * @return {*}
  164. */
  165. getRenderItem(item: eventObject) {
  166. const onPress = this.props.navigation.navigate.bind(this, 'PlanningDisplayScreen', {data: item});
  167. if (item.logo !== null) {
  168. return (
  169. <View>
  170. <Divider/>
  171. <List.Item
  172. title={item.title}
  173. description={getFormattedEventTime(item["date_begin"], item["date_end"])}
  174. left={() => <Avatar.Image
  175. source={{uri: item.logo}}
  176. style={{backgroundColor: 'transparent'}}
  177. />}
  178. onPress={onPress}
  179. />
  180. </View>
  181. );
  182. } else {
  183. return (
  184. <View>
  185. <Divider/>
  186. <List.Item
  187. title={item.title}
  188. description={getFormattedEventTime(item["date_begin"], item["date_end"])}
  189. onPress={onPress}
  190. />
  191. </View>
  192. );
  193. }
  194. }
  195. /**
  196. * Gets an empty render item for an empty date
  197. *
  198. * @return {*}
  199. */
  200. getRenderEmptyDate() {
  201. return (
  202. <Divider/>
  203. );
  204. }
  205. render() {
  206. // console.log("rendering PlanningScreen");
  207. return (
  208. <CustomAgenda
  209. // the list of items that have to be displayed in agenda. If you want to render item as empty date
  210. // the value of date key kas to be an empty array []. If there exists no value for date key it is
  211. // considered that the date in question is not yet loaded
  212. items={this.state.agendaItems}
  213. // initially selected day
  214. selected={this.currentDate}
  215. // Minimum date that can be selected, dates before minDate will be grayed out. Default = undefined
  216. minDate={this.currentDate}
  217. // Max amount of months allowed to scroll to the past. Default = 50
  218. pastScrollRange={1}
  219. // Max amount of months allowed to scroll to the future. Default = 50
  220. futureScrollRange={AGENDA_MONTH_SPAN}
  221. // If provided, a standard RefreshControl will be added for "Pull to Refresh" functionality. Make sure to also set the refreshing prop correctly.
  222. onRefresh={this.onRefresh}
  223. // callback that fires when the calendar is opened or closed
  224. onCalendarToggled={this.onCalendarToggled}
  225. // Set this true while waiting for new data from a refresh
  226. refreshing={this.state.refreshing}
  227. renderItem={this.getRenderItem}
  228. renderEmptyDate={this.getRenderEmptyDate}
  229. rowHasChanged={this.rowHasChanged}
  230. // If firstDay=1 week starts from Monday. Note that dayNames and dayNamesShort should still start from Sunday.
  231. firstDay={1}
  232. // ref to this agenda in order to handle back button event
  233. onRef={this.onAgendaRef}
  234. />
  235. );
  236. }
  237. }