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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // @flow
  2. import * as React from 'react';
  3. import {Platform, StatusBar, View, 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 type {CustomTheme} from "./src/managers/ThemeManager";
  8. import ThemeManager from './src/managers/ThemeManager';
  9. import {NavigationContainer} from '@react-navigation/native';
  10. import MainNavigator from './src/navigation/MainNavigator';
  11. import {initExpoToken} from "./src/utils/Notifications";
  12. import {Provider as PaperProvider} from 'react-native-paper';
  13. import AprilFoolsManager from "./src/managers/AprilFoolsManager";
  14. import Update from "./src/constants/Update";
  15. import ConnectionManager from "./src/managers/ConnectionManager";
  16. import URLHandler from "./src/utils/URLHandler";
  17. import {setSafeBounceHeight} from "react-navigation-collapsible";
  18. import {enableScreens} from 'react-native-screens';
  19. import SplashScreen from 'react-native-splash-screen'
  20. // Native optimizations https://reactnavigation.org/docs/react-native-screens
  21. enableScreens(true);
  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. this.navigatorRef = React.createRef();
  51. this.defaultHomeRoute = null;
  52. this.defaultHomeData = {};
  53. this.storageManager = AsyncStorageManager.getInstance();
  54. this.urlHandler = new URLHandler(this.onInitialURLParsed, this.onDetectURL);
  55. this.urlHandler.listen();
  56. setSafeBounceHeight(Platform.OS === 'ios' ? 100 : 20);
  57. this.loadAssetsAsync().then(() => {
  58. this.onLoadFinished();
  59. });
  60. console.log("USING HERMES:" + global.HermesInternal !== null);
  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. if (Platform.OS === "android")
  107. StatusBar.setBackgroundColor(ThemeManager.getCurrentTheme().colors.surface, true);
  108. }
  109. /**
  110. * Callback when user ends the intro. Save in preferences to avoid showing back the introSlides
  111. */
  112. onIntroDone = () => {
  113. this.setState({
  114. showIntro: false,
  115. showUpdate: false,
  116. showAprilFools: false,
  117. });
  118. this.storageManager.savePref(this.storageManager.preferences.showIntro.key, '0');
  119. this.storageManager.savePref(this.storageManager.preferences.updateNumber.key, Update.number.toString());
  120. this.storageManager.savePref(this.storageManager.preferences.showAprilFoolsStart.key, '0');
  121. };
  122. /**
  123. * Loads every async data
  124. *
  125. * @returns {Promise<void>}
  126. */
  127. loadAssetsAsync = async () => {
  128. await this.storageManager.loadPreferences();
  129. await initExpoToken();
  130. try {
  131. await ConnectionManager.getInstance().recoverLogin();
  132. } catch (e) {
  133. }
  134. }
  135. /**
  136. * Async loading is done, finish processing startup data
  137. */
  138. onLoadFinished() {
  139. // Only show intro if this is the first time starting the app
  140. this.createDrawerNavigator = () => <MainNavigator
  141. defaultHomeRoute={this.defaultHomeRoute}
  142. defaultHomeData={this.defaultHomeData}
  143. />;
  144. ThemeManager.getInstance().setUpdateThemeCallback(this.onUpdateTheme);
  145. // Status bar goes dark if set too fast on ios
  146. if (Platform.OS === 'ios')
  147. setTimeout(this.setupStatusBar, 1000);
  148. else
  149. this.setupStatusBar();
  150. this.setState({
  151. isLoading: false,
  152. currentTheme: ThemeManager.getCurrentTheme(),
  153. showIntro: this.storageManager.preferences.showIntro.current === '1',
  154. showUpdate: this.storageManager.preferences.updateNumber.current !== Update.number.toString(),
  155. showAprilFools: AprilFoolsManager.getInstance().isAprilFoolsEnabled() && this.storageManager.preferences.showAprilFoolsStart.current === '1',
  156. });
  157. console.log('hiding splash');
  158. SplashScreen.hide();
  159. }
  160. /**
  161. * Renders the app based on loading state
  162. */
  163. render() {
  164. if (this.state.isLoading) {
  165. return null;
  166. } else if (this.state.showIntro || this.state.showUpdate || this.state.showAprilFools) {
  167. return <CustomIntroSlider
  168. onDone={this.onIntroDone}
  169. isUpdate={this.state.showUpdate && !this.state.showIntro}
  170. isAprilFools={this.state.showAprilFools && !this.state.showIntro}
  171. />;
  172. } else {
  173. return (
  174. <PaperProvider theme={this.state.currentTheme}>
  175. <View style={{backgroundColor: ThemeManager.getCurrentTheme().colors.background, flex: 1}}>
  176. <NavigationContainer theme={this.state.currentTheme} ref={this.navigatorRef}>
  177. <MainNavigator
  178. defaultHomeRoute={this.defaultHomeRoute}
  179. defaultHomeData={this.defaultHomeData}
  180. />
  181. </NavigationContainer>
  182. </View>
  183. </PaperProvider>
  184. );
  185. }
  186. }
  187. }