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 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 {StackNavigationProp} from "@react-navigation/stack";
  17. import {MASCOT_STYLE} from "../../components/Mascot/Mascot";
  18. import MascotPopup from "../../components/Mascot/MascotPopup";
  19. import AsyncStorageManager from "../../managers/AsyncStorageManager";
  20. LocaleConfig.locales['fr'] = {
  21. monthNames: ['Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre'],
  22. monthNamesShort: ['Janv.', 'Févr.', 'Mars', 'Avril', 'Mai', 'Juin', 'Juil.', 'Août', 'Sept.', 'Oct.', 'Nov.', 'Déc.'],
  23. dayNames: ['Dimanche', 'Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi'],
  24. dayNamesShort: ['Dim', 'Lun', 'Mar', 'Mer', 'Jeu', 'Ven', 'Sam'],
  25. today: 'Aujourd\'hui'
  26. };
  27. type Props = {
  28. navigation: StackNavigationProp,
  29. }
  30. type State = {
  31. refreshing: boolean,
  32. agendaItems: Object,
  33. calendarShowing: boolean,
  34. mascotDialogVisible: boolean,
  35. };
  36. const FETCH_URL = "https://www.amicale-insat.fr/api/event/list";
  37. const AGENDA_MONTH_SPAN = 3;
  38. /**
  39. * Class defining the app's planning screen
  40. */
  41. class PlanningScreen extends React.Component<Props, State> {
  42. agendaRef: Object;
  43. lastRefresh: Date;
  44. minTimeBetweenRefresh = 60;
  45. state = {
  46. refreshing: false,
  47. agendaItems: {},
  48. calendarShowing: false,
  49. mascotDialogVisible: AsyncStorageManager.getInstance().preferences.eventsShowBanner.current === "1"
  50. };
  51. currentDate = getDateOnlyString(getCurrentDateString());
  52. constructor(props: any) {
  53. super(props);
  54. if (i18n.currentLocale().startsWith("fr")) {
  55. LocaleConfig.defaultLocale = 'fr';
  56. }
  57. }
  58. /**
  59. * Captures focus and blur events to hook on android back button
  60. */
  61. componentDidMount() {
  62. this.onRefresh();
  63. this.props.navigation.addListener(
  64. 'focus',
  65. () =>
  66. BackHandler.addEventListener(
  67. 'hardwareBackPress',
  68. this.onBackButtonPressAndroid
  69. )
  70. );
  71. this.props.navigation.addListener(
  72. 'blur',
  73. () =>
  74. BackHandler.removeEventListener(
  75. 'hardwareBackPress',
  76. this.onBackButtonPressAndroid
  77. )
  78. );
  79. }
  80. /**
  81. * Overrides default android back button behaviour to close the calendar if it was open.
  82. *
  83. * @return {boolean}
  84. */
  85. onBackButtonPressAndroid = () => {
  86. if (this.state.calendarShowing) {
  87. this.agendaRef.chooseDay(this.agendaRef.state.selectedDay);
  88. return true;
  89. } else {
  90. return false;
  91. }
  92. };
  93. /**
  94. * Callback used when closing the banner.
  95. * This hides the banner and saves to preferences to prevent it from reopening
  96. */
  97. onHideMascotDialog = () => {
  98. this.setState({mascotDialogVisible: false});
  99. AsyncStorageManager.getInstance().savePref(
  100. AsyncStorageManager.getInstance().preferences.eventsShowBanner.key,
  101. '0'
  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, 'planning-information', {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 = () => <Divider/>;
  201. render() {
  202. return (
  203. <View style={{flex: 1}}>
  204. <CustomAgenda
  205. {...this.props}
  206. // the list of items that have to be displayed in agenda. If you want to render item as empty date
  207. // the value of date key kas to be an empty array []. If there exists no value for date key it is
  208. // considered that the date in question is not yet loaded
  209. items={this.state.agendaItems}
  210. // initially selected day
  211. selected={this.currentDate}
  212. // Minimum date that can be selected, dates before minDate will be grayed out. Default = undefined
  213. minDate={this.currentDate}
  214. // Max amount of months allowed to scroll to the past. Default = 50
  215. pastScrollRange={1}
  216. // Max amount of months allowed to scroll to the future. Default = 50
  217. futureScrollRange={AGENDA_MONTH_SPAN}
  218. // If provided, a standard RefreshControl will be added for "Pull to Refresh" functionality. Make sure to also set the refreshing prop correctly.
  219. onRefresh={this.onRefresh}
  220. // callback that fires when the calendar is opened or closed
  221. onCalendarToggled={this.onCalendarToggled}
  222. // Set this true while waiting for new data from a refresh
  223. refreshing={this.state.refreshing}
  224. renderItem={this.getRenderItem}
  225. renderEmptyDate={this.getRenderEmptyDate}
  226. rowHasChanged={this.rowHasChanged}
  227. // If firstDay=1 week starts from Monday. Note that dayNames and dayNamesShort should still start from Sunday.
  228. firstDay={1}
  229. // ref to this agenda in order to handle back button event
  230. onRef={this.onAgendaRef}
  231. />
  232. <MascotPopup
  233. visible={this.state.mascotDialogVisible}
  234. title={i18n.t("screens.planning.mascotDialog.title")}
  235. message={i18n.t("screens.planning.mascotDialog.message")}
  236. icon={"glass-cocktail"}
  237. buttons={{
  238. action: null,
  239. cancel: {
  240. message: i18n.t("screens.planning.mascotDialog.button"),
  241. icon: "check",
  242. onPress: this.onHideMascotDialog,
  243. }
  244. }}
  245. emotion={MASCOT_STYLE.HAPPY}
  246. />
  247. </View>
  248. );
  249. }
  250. }
  251. export default PlanningScreen;