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

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