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.js 8.6KB

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