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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. type StateType = {
  59. isLoading: boolean;
  60. initialPreferences: {
  61. general: GeneralPreferencesType;
  62. planex: PlanexPreferencesType;
  63. proxiwash: ProxiwashPreferencesType;
  64. mascot: MascotPreferencesType;
  65. };
  66. };
  67. export default class App extends React.Component<{}, StateType> {
  68. navigatorRef: { current: null | NavigationContainerRef };
  69. defaultHomeRoute: string | undefined;
  70. defaultHomeData: { [key: string]: string } | undefined;
  71. urlHandler: URLHandler;
  72. constructor(props: {}) {
  73. super(props);
  74. this.state = {
  75. isLoading: true,
  76. initialPreferences: {
  77. general: defaultPreferences,
  78. planex: defaultPlanexPreferences,
  79. proxiwash: defaultProxiwashPreferences,
  80. mascot: defaultMascotPreferences,
  81. },
  82. };
  83. initLocales();
  84. this.navigatorRef = React.createRef();
  85. this.defaultHomeRoute = undefined;
  86. this.defaultHomeData = undefined;
  87. this.urlHandler = new URLHandler(this.onInitialURLParsed, this.onDetectURL);
  88. this.urlHandler.listen();
  89. setSafeBounceHeight(Platform.OS === 'ios' ? 100 : 20);
  90. this.loadAssetsAsync();
  91. }
  92. /**
  93. * The app has been started by an url, and it has been parsed.
  94. * Set a new default start route based on the data parsed.
  95. *
  96. * @param parsedData The data parsed from the url
  97. */
  98. onInitialURLParsed = (parsedData: ParsedUrlDataType) => {
  99. this.defaultHomeRoute = parsedData.route;
  100. this.defaultHomeData = parsedData.data;
  101. };
  102. /**
  103. * An url has been opened and parsed while the app was active.
  104. * Redirect the user to the screen according to parsed data.
  105. *
  106. * @param parsedData The data parsed from the url
  107. */
  108. onDetectURL = (parsedData: ParsedUrlDataType) => {
  109. // Navigate to nested navigator and pass data to the index screen
  110. const nav = this.navigatorRef.current;
  111. if (nav != null) {
  112. nav.navigate('home', {
  113. screen: 'index',
  114. params: { nextScreen: parsedData.route, data: parsedData.data },
  115. });
  116. }
  117. };
  118. /**
  119. * Async loading is done, finish processing startup data
  120. */
  121. onLoadFinished = (
  122. values: Array<
  123. | GeneralPreferencesType
  124. | PlanexPreferencesType
  125. | ProxiwashPreferencesType
  126. | MascotPreferencesType
  127. | void
  128. >
  129. ) => {
  130. const [general, planex, proxiwash, mascot] = values;
  131. this.setState({
  132. isLoading: false,
  133. initialPreferences: {
  134. general: general as GeneralPreferencesType,
  135. planex: planex as PlanexPreferencesType,
  136. proxiwash: proxiwash as ProxiwashPreferencesType,
  137. mascot: mascot as MascotPreferencesType,
  138. },
  139. });
  140. SplashScreen.hide();
  141. };
  142. /**
  143. * Loads every async data
  144. *
  145. * @returns {Promise<void>}
  146. */
  147. loadAssetsAsync() {
  148. Promise.all([
  149. retrievePreferences(
  150. Object.values(GeneralPreferenceKeys),
  151. defaultPreferences
  152. ),
  153. retrievePreferences(
  154. Object.values(PlanexPreferenceKeys),
  155. defaultPlanexPreferences
  156. ),
  157. retrievePreferences(
  158. Object.values(ProxiwashPreferenceKeys),
  159. defaultProxiwashPreferences
  160. ),
  161. retrievePreferences(
  162. Object.values(MascotPreferenceKeys),
  163. defaultMascotPreferences
  164. ),
  165. ConnectionManager.getInstance().recoverLogin(),
  166. ])
  167. .then(this.onLoadFinished)
  168. .catch(this.onLoadFinished);
  169. }
  170. /**
  171. * Renders the app based on loading state
  172. */
  173. render() {
  174. const { state } = this;
  175. if (state.isLoading) {
  176. return null;
  177. }
  178. return (
  179. <GeneralPreferencesProvider
  180. initialPreferences={this.state.initialPreferences.general}
  181. >
  182. <PlanexPreferencesProvider
  183. initialPreferences={this.state.initialPreferences.planex}
  184. >
  185. <ProxiwashPreferencesProvider
  186. initialPreferences={this.state.initialPreferences.proxiwash}
  187. >
  188. <MascotPreferencesProvider
  189. initialPreferences={this.state.initialPreferences.mascot}
  190. >
  191. <MainApp
  192. ref={this.navigatorRef}
  193. defaultHomeData={this.defaultHomeData}
  194. defaultHomeRoute={this.defaultHomeRoute}
  195. />
  196. </MascotPreferencesProvider>
  197. </ProxiwashPreferencesProvider>
  198. </PlanexPreferencesProvider>
  199. </GeneralPreferencesProvider>
  200. );
  201. }
  202. }