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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. import { setupNotifications } from './src/utils/Notifications';
  52. import { TabRoutes } from './src/navigation/TabNavigator';
  53. initLocales();
  54. setupNotifications();
  55. LogBox.ignoreLogs([
  56. 'Cannot update a component from inside the function body of a different component',
  57. '`new NativeEventEmitter()` was called with a non-null argument',
  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<any> };
  71. defaultData?: ParsedUrlDataType;
  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. loginToken: undefined,
  84. };
  85. this.navigatorRef = React.createRef();
  86. this.defaultData = 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.defaultData = parsedData;
  100. };
  101. /**
  102. * An url has been opened and parsed while the app was active.
  103. * Redirect the user to the screen according to parsed data.
  104. *
  105. * @param parsedData The data parsed from the url
  106. */
  107. onDetectURL = (parsedData: ParsedUrlDataType) => {
  108. // Navigate to nested navigator and pass data to the index screen
  109. const nav = this.navigatorRef.current;
  110. if (nav != null) {
  111. nav.navigate(TabRoutes.Home, {
  112. nextScreen: parsedData.route,
  113. data: parsedData.data,
  114. });
  115. }
  116. };
  117. /**
  118. * Async loading is done, finish processing startup data
  119. */
  120. onLoadFinished = (
  121. values: Array<
  122. | GeneralPreferencesType
  123. | PlanexPreferencesType
  124. | ProxiwashPreferencesType
  125. | MascotPreferencesType
  126. | string
  127. | undefined
  128. >
  129. ) => {
  130. const [general, planex, proxiwash, mascot, token] = 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. loginToken: token as string | undefined,
  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. retrieveLoginToken(),
  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. <LoginProvider initialToken={this.state.loginToken}>
  193. <MainApp
  194. ref={this.navigatorRef}
  195. defaultData={this.defaultData}
  196. />
  197. </LoginProvider>
  198. </MascotPreferencesProvider>
  199. </ProxiwashPreferencesProvider>
  200. </PlanexPreferencesProvider>
  201. </GeneralPreferencesProvider>
  202. );
  203. }
  204. }