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.9KB

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