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

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