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.

App.tsx 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 React from 'react';
  20. import { LogBox, Platform } from 'react-native';
  21. import { setSafeBounceHeight } from 'react-navigation-collapsible';
  22. import SplashScreen from 'react-native-splash-screen';
  23. import ConnectionManager from './src/managers/ConnectionManager';
  24. import type { ParsedUrlDataType } from './src/utils/URLHandler';
  25. import URLHandler from './src/utils/URLHandler';
  26. import initLocales from './src/utils/Locales';
  27. import { NavigationContainerRef } from '@react-navigation/core';
  28. import {
  29. defaultMascotPreferences,
  30. defaultPlanexPreferences,
  31. defaultPreferences,
  32. defaultProxiwashPreferences,
  33. GeneralPreferenceKeys,
  34. GeneralPreferencesType,
  35. MascotPreferenceKeys,
  36. MascotPreferencesType,
  37. PlanexPreferenceKeys,
  38. PlanexPreferencesType,
  39. ProxiwashPreferenceKeys,
  40. ProxiwashPreferencesType,
  41. retrievePreferences,
  42. } from './src/utils/asyncStorage';
  43. import {
  44. GeneralPreferencesProvider,
  45. MascotPreferencesProvider,
  46. PlanexPreferencesProvider,
  47. ProxiwashPreferencesProvider,
  48. } from './src/components/providers/PreferencesProvider';
  49. import MainApp from './src/screens/MainApp';
  50. // Native optimizations https://reactnavigation.org/docs/react-native-screens
  51. // Crashes app when navigating away from webview on android 9+
  52. // enableScreens(true);
  53. LogBox.ignoreLogs([
  54. // collapsible headers cause this warning, just ignore as it is not an issue
  55. 'Non-serializable values were found in the navigation state',
  56. 'Cannot update a component from inside the function body of a different component',
  57. ]);
  58. // TODO move preferences in smaller contextes for improved performances
  59. type StateType = {
  60. isLoading: boolean;
  61. initialPreferences: {
  62. general: GeneralPreferencesType;
  63. planex: PlanexPreferencesType;
  64. proxiwash: ProxiwashPreferencesType;
  65. mascot: MascotPreferencesType;
  66. };
  67. };
  68. export default class App extends React.Component<{}, StateType> {
  69. navigatorRef: { current: null | NavigationContainerRef };
  70. defaultHomeRoute: string | undefined;
  71. defaultHomeData: { [key: string]: string } | undefined;
  72. urlHandler: URLHandler;
  73. constructor(props: {}) {
  74. super(props);
  75. this.state = {
  76. isLoading: true,
  77. initialPreferences: {
  78. general: defaultPreferences,
  79. planex: defaultPlanexPreferences,
  80. proxiwash: defaultProxiwashPreferences,
  81. mascot: defaultMascotPreferences,
  82. },
  83. };
  84. initLocales();
  85. this.navigatorRef = React.createRef();
  86. this.defaultHomeRoute = undefined;
  87. this.defaultHomeData = undefined;
  88. this.urlHandler = new URLHandler(this.onInitialURLParsed, this.onDetectURL);
  89. this.urlHandler.listen();
  90. setSafeBounceHeight(Platform.OS === 'ios' ? 100 : 20);
  91. this.loadAssetsAsync();
  92. }
  93. /**
  94. * The app has been started by an url, and it has been parsed.
  95. * Set a new default start route based on the data parsed.
  96. *
  97. * @param parsedData The data parsed from the url
  98. */
  99. onInitialURLParsed = (parsedData: ParsedUrlDataType) => {
  100. this.defaultHomeRoute = parsedData.route;
  101. this.defaultHomeData = parsedData.data;
  102. };
  103. /**
  104. * An url has been opened and parsed while the app was active.
  105. * Redirect the user to the screen according to parsed data.
  106. *
  107. * @param parsedData The data parsed from the url
  108. */
  109. onDetectURL = (parsedData: ParsedUrlDataType) => {
  110. // Navigate to nested navigator and pass data to the index screen
  111. const nav = this.navigatorRef.current;
  112. if (nav != null) {
  113. nav.navigate('home', {
  114. screen: 'index',
  115. params: { nextScreen: parsedData.route, data: parsedData.data },
  116. });
  117. }
  118. };
  119. /**
  120. * Async loading is done, finish processing startup data
  121. */
  122. onLoadFinished = (
  123. values: Array<
  124. | GeneralPreferencesType
  125. | PlanexPreferencesType
  126. | ProxiwashPreferencesType
  127. | MascotPreferencesType
  128. | void
  129. >
  130. ) => {
  131. const [general, planex, proxiwash, mascot] = values;
  132. this.setState({
  133. isLoading: false,
  134. initialPreferences: {
  135. general: general as GeneralPreferencesType,
  136. planex: planex as PlanexPreferencesType,
  137. proxiwash: proxiwash as ProxiwashPreferencesType,
  138. mascot: mascot as MascotPreferencesType,
  139. },
  140. });
  141. SplashScreen.hide();
  142. };
  143. /**
  144. * Loads every async data
  145. *
  146. * @returns {Promise<void>}
  147. */
  148. loadAssetsAsync() {
  149. Promise.all([
  150. retrievePreferences(
  151. Object.values(GeneralPreferenceKeys),
  152. defaultPreferences
  153. ),
  154. retrievePreferences(
  155. Object.values(PlanexPreferenceKeys),
  156. defaultPlanexPreferences
  157. ),
  158. retrievePreferences(
  159. Object.values(ProxiwashPreferenceKeys),
  160. defaultProxiwashPreferences
  161. ),
  162. retrievePreferences(
  163. Object.values(MascotPreferenceKeys),
  164. defaultMascotPreferences
  165. ),
  166. ConnectionManager.getInstance().recoverLogin(),
  167. ])
  168. .then(this.onLoadFinished)
  169. .catch(this.onLoadFinished);
  170. }
  171. /**
  172. * Renders the app based on loading state
  173. */
  174. render() {
  175. const { state } = this;
  176. if (state.isLoading) {
  177. return null;
  178. }
  179. return (
  180. <GeneralPreferencesProvider
  181. initialPreferences={this.state.initialPreferences.general}
  182. >
  183. <PlanexPreferencesProvider
  184. initialPreferences={this.state.initialPreferences.planex}
  185. >
  186. <ProxiwashPreferencesProvider
  187. initialPreferences={this.state.initialPreferences.proxiwash}
  188. >
  189. <MascotPreferencesProvider
  190. initialPreferences={this.state.initialPreferences.mascot}
  191. >
  192. <MainApp
  193. ref={this.navigatorRef}
  194. defaultHomeData={this.defaultHomeData}
  195. defaultHomeRoute={this.defaultHomeRoute}
  196. />
  197. </MascotPreferencesProvider>
  198. </ProxiwashPreferencesProvider>
  199. </PlanexPreferencesProvider>
  200. </GeneralPreferencesProvider>
  201. );
  202. }
  203. }