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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 {Provider as PaperProvider} from 'react-native-paper';
  12. import AprilFoolsManager from "./src/managers/AprilFoolsManager";
  13. import Update from "./src/constants/Update";
  14. import ConnectionManager from "./src/managers/ConnectionManager";
  15. import URLHandler from "./src/utils/URLHandler";
  16. import {setSafeBounceHeight} from "react-navigation-collapsible";
  17. import SplashScreen from 'react-native-splash-screen'
  18. import {OverflowMenuProvider} from "react-navigation-header-buttons";
  19. // Native optimizations https://reactnavigation.org/docs/react-native-screens
  20. // Crashes app when navigating away from webview on android 9+
  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. }
  61. /**
  62. * THe app has been started by an url, and it has been parsed.
  63. * Set a new default start route based on the data parsed.
  64. *
  65. * @param parsedData The data parsed from the url
  66. */
  67. onInitialURLParsed = (parsedData: { route: string, data: { [key: string]: any } }) => {
  68. this.defaultHomeRoute = parsedData.route;
  69. this.defaultHomeData = parsedData.data;
  70. };
  71. /**
  72. * An url has been opened and parsed while the app was active.
  73. * Redirect the user to the screen according to parsed data.
  74. *
  75. * @param parsedData The data parsed from the url
  76. */
  77. onDetectURL = (parsedData: { route: string, data: { [key: string]: any } }) => {
  78. // Navigate to nested navigator and pass data to the index screen
  79. if (this.navigatorRef.current != null) {
  80. this.navigatorRef.current.navigate('home', {
  81. screen: 'index',
  82. params: {nextScreen: parsedData.route, data: parsedData.data}
  83. });
  84. }
  85. };
  86. /**
  87. * Updates the current theme
  88. */
  89. onUpdateTheme = () => {
  90. this.setState({
  91. currentTheme: ThemeManager.getCurrentTheme()
  92. });
  93. this.setupStatusBar();
  94. };
  95. /**
  96. * Updates status bar content color if on iOS only,
  97. * as the android status bar is always set to black.
  98. */
  99. setupStatusBar() {
  100. if (ThemeManager.getNightMode()) {
  101. StatusBar.setBarStyle('light-content', true);
  102. } else {
  103. StatusBar.setBarStyle('dark-content', true);
  104. }
  105. if (Platform.OS === "android")
  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. try {
  129. await ConnectionManager.getInstance().recoverLogin();
  130. } catch (e) {
  131. }
  132. }
  133. /**
  134. * Async loading is done, finish processing startup data
  135. */
  136. onLoadFinished() {
  137. // Only show intro if this is the first time starting the app
  138. this.createDrawerNavigator = () => <MainNavigator
  139. defaultHomeRoute={this.defaultHomeRoute}
  140. defaultHomeData={this.defaultHomeData}
  141. />;
  142. ThemeManager.getInstance().setUpdateThemeCallback(this.onUpdateTheme);
  143. // Status bar goes dark if set too fast on ios
  144. if (Platform.OS === 'ios')
  145. setTimeout(this.setupStatusBar, 1000);
  146. else
  147. this.setupStatusBar();
  148. this.setState({
  149. isLoading: false,
  150. currentTheme: ThemeManager.getCurrentTheme(),
  151. showIntro: this.storageManager.preferences.showIntro.current === '1',
  152. showUpdate: this.storageManager.preferences.updateNumber.current !== Update.number.toString(),
  153. showAprilFools: AprilFoolsManager.getInstance().isAprilFoolsEnabled() && this.storageManager.preferences.showAprilFoolsStart.current === '1',
  154. });
  155. SplashScreen.hide();
  156. }
  157. /**
  158. * Renders the app based on loading state
  159. */
  160. render() {
  161. if (this.state.isLoading) {
  162. return null;
  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. <OverflowMenuProvider>
  173. <View style={{backgroundColor: ThemeManager.getCurrentTheme().colors.background, flex: 1}}>
  174. <NavigationContainer theme={this.state.currentTheme} ref={this.navigatorRef}>
  175. <MainNavigator
  176. defaultHomeRoute={this.defaultHomeRoute}
  177. defaultHomeData={this.defaultHomeData}
  178. />
  179. </NavigationContainer>
  180. </View>
  181. </OverflowMenuProvider>
  182. </PaperProvider>
  183. );
  184. }
  185. }
  186. }