Application Android et IOS pour l'amicale des élèves
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.js 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // @flow
  2. import * as React from 'react';
  3. import {Platform, StatusBar, YellowBox} from 'react-native';
  4. import LocaleManager from './src/managers/LocaleManager';
  5. import AsyncStorageManager from "./src/managers/AsyncStorageManager";
  6. import CustomIntroSlider from "./src/components/Overrides/CustomIntroSlider";
  7. import {AppLoading} from 'expo';
  8. import type {CustomTheme} from "./src/managers/ThemeManager";
  9. import ThemeManager from './src/managers/ThemeManager';
  10. import {NavigationContainer} from '@react-navigation/native';
  11. import MainNavigator from './src/navigation/MainNavigator';
  12. import {initExpoToken} from "./src/utils/Notifications";
  13. import {Provider as PaperProvider} from 'react-native-paper';
  14. import AprilFoolsManager from "./src/managers/AprilFoolsManager";
  15. import Update from "./src/constants/Update";
  16. import ConnectionManager from "./src/managers/ConnectionManager";
  17. import URLHandler from "./src/utils/URLHandler";
  18. import {setSafeBounceHeight} from "react-navigation-collapsible";
  19. import {enableScreens} from 'react-native-screens';
  20. // Native optimizations https://reactnavigation.org/docs/react-native-screens
  21. enableScreens();
  22. YellowBox.ignoreWarnings([ // collapsible headers cause this warning, just ignore as it is not an issue
  23. 'Non-serializable values were found in the navigation state',
  24. ]);
  25. type Props = {};
  26. type State = {
  27. isLoading: boolean,
  28. showIntro: boolean,
  29. showUpdate: boolean,
  30. showAprilFools: boolean,
  31. currentTheme: CustomTheme | null,
  32. };
  33. export default class App extends React.Component<Props, State> {
  34. state = {
  35. isLoading: true,
  36. showIntro: true,
  37. showUpdate: true,
  38. showAprilFools: false,
  39. currentTheme: null,
  40. };
  41. navigatorRef: { current: null | NavigationContainer };
  42. defaultHomeRoute: string | null;
  43. defaultHomeData: { [key: string]: any }
  44. createDrawerNavigator: () => React.Node;
  45. urlHandler: URLHandler;
  46. storageManager: AsyncStorageManager;
  47. constructor() {
  48. super();
  49. LocaleManager.initTranslations();
  50. // SplashScreen.preventAutoHide();
  51. this.navigatorRef = React.createRef();
  52. this.defaultHomeRoute = null;
  53. this.defaultHomeData = {};
  54. this.storageManager = AsyncStorageManager.getInstance();
  55. this.urlHandler = new URLHandler(this.onInitialURLParsed, this.onDetectURL);
  56. this.urlHandler.listen();
  57. setSafeBounceHeight(Platform.OS === 'ios' ? 100 : 20);
  58. this.loadAssetsAsync().then(() => {
  59. this.onLoadFinished();
  60. });
  61. }
  62. /**
  63. * THe app has been started by an url, and it has been parsed.
  64. * Set a new default start route based on the data parsed.
  65. *
  66. * @param parsedData The data parsed from the url
  67. */
  68. onInitialURLParsed = (parsedData: { route: string, data: { [key: string]: any } }) => {
  69. this.defaultHomeRoute = parsedData.route;
  70. this.defaultHomeData = parsedData.data;
  71. };
  72. /**
  73. * An url has been opened and parsed while the app was active.
  74. * Redirect the user to the screen according to parsed data.
  75. *
  76. * @param parsedData The data parsed from the url
  77. */
  78. onDetectURL = (parsedData: { route: string, data: { [key: string]: any } }) => {
  79. // Navigate to nested navigator and pass data to the index screen
  80. if (this.navigatorRef.current != null) {
  81. this.navigatorRef.current.navigate('home', {
  82. screen: 'index',
  83. params: {nextScreen: parsedData.route, data: parsedData.data}
  84. });
  85. }
  86. };
  87. /**
  88. * Updates the current theme
  89. */
  90. onUpdateTheme = () => {
  91. this.setState({
  92. currentTheme: ThemeManager.getCurrentTheme()
  93. });
  94. this.setupStatusBar();
  95. };
  96. /**
  97. * Updates status bar content color if on iOS only,
  98. * as the android status bar is always set to black.
  99. */
  100. setupStatusBar() {
  101. if (ThemeManager.getNightMode()) {
  102. StatusBar.setBarStyle('light-content', true);
  103. } else {
  104. StatusBar.setBarStyle('dark-content', true);
  105. }
  106. StatusBar.setBackgroundColor(ThemeManager.getCurrentTheme().colors.surface, true);
  107. }
  108. /**
  109. * Callback when user ends the intro. Save in preferences to avoid showing back the introSlides
  110. */
  111. onIntroDone = () => {
  112. this.setState({
  113. showIntro: false,
  114. showUpdate: false,
  115. showAprilFools: false,
  116. });
  117. this.storageManager.savePref(this.storageManager.preferences.showIntro.key, '0');
  118. this.storageManager.savePref(this.storageManager.preferences.updateNumber.key, Update.number.toString());
  119. this.storageManager.savePref(this.storageManager.preferences.showAprilFoolsStart.key, '0');
  120. };
  121. /**
  122. * Loads every async data
  123. *
  124. * @returns {Promise<void>}
  125. */
  126. loadAssetsAsync = async () => {
  127. await this.storageManager.loadPreferences();
  128. await initExpoToken();
  129. try {
  130. await ConnectionManager.getInstance().recoverLogin();
  131. } catch (e) {
  132. }
  133. }
  134. /**
  135. * Async loading is done, finish processing startup data
  136. */
  137. onLoadFinished() {
  138. // Only show intro if this is the first time starting the app
  139. this.createDrawerNavigator = () => <MainNavigator
  140. defaultHomeRoute={this.defaultHomeRoute}
  141. defaultHomeData={this.defaultHomeData}
  142. />;
  143. ThemeManager.getInstance().setUpdateThemeCallback(this.onUpdateTheme);
  144. // Status bar goes dark if set too fast on ios
  145. if (Platform.OS === 'ios')
  146. setTimeout(this.setupStatusBar, 1000);
  147. else
  148. this.setupStatusBar();
  149. this.setState({
  150. isLoading: false,
  151. currentTheme: ThemeManager.getCurrentTheme(),
  152. showIntro: this.storageManager.preferences.showIntro.current === '1',
  153. showUpdate: this.storageManager.preferences.updateNumber.current !== Update.number.toString(),
  154. showAprilFools: AprilFoolsManager.getInstance().isAprilFoolsEnabled() && this.storageManager.preferences.showAprilFoolsStart.current === '1',
  155. });
  156. }
  157. /**
  158. * Renders the app based on loading state
  159. */
  160. render() {
  161. if (this.state.isLoading) {
  162. return <AppLoading/>;
  163. } else if (this.state.showIntro || this.state.showUpdate || this.state.showAprilFools) {
  164. return <CustomIntroSlider
  165. onDone={this.onIntroDone}
  166. isUpdate={this.state.showUpdate && !this.state.showIntro}
  167. isAprilFools={this.state.showAprilFools && !this.state.showIntro}
  168. />;
  169. } else {
  170. return (
  171. <PaperProvider theme={this.state.currentTheme}>
  172. <NavigationContainer theme={this.state.currentTheme} ref={this.navigatorRef}>
  173. <MainNavigator
  174. defaultHomeRoute={this.defaultHomeRoute}
  175. defaultHomeData={this.defaultHomeData}
  176. />
  177. </NavigationContainer>
  178. </PaperProvider>
  179. );
  180. }
  181. }
  182. }