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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // @flow
  2. import * as React from 'react';
  3. import {LogBox, Platform, SafeAreaView, StatusBar, View} 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. LogBox.ignoreLogs([ // collapsible headers cause this warning, just ignore as it is not an issue
  23. 'Non-serializable values were found in the navigation state',
  24. 'Cannot update a component from inside the function body of a different component',
  25. ]);
  26. type Props = {};
  27. type State = {
  28. isLoading: boolean,
  29. showIntro: boolean,
  30. showUpdate: boolean,
  31. showAprilFools: boolean,
  32. currentTheme: CustomTheme | null,
  33. };
  34. export default class App extends React.Component<Props, State> {
  35. state = {
  36. isLoading: true,
  37. showIntro: true,
  38. showUpdate: true,
  39. showAprilFools: false,
  40. currentTheme: null,
  41. };
  42. navigatorRef: { current: null | NavigationContainer };
  43. defaultHomeRoute: string | null;
  44. defaultHomeData: { [key: string]: any }
  45. createDrawerNavigator: () => React.Node;
  46. urlHandler: URLHandler;
  47. storageManager: AsyncStorageManager;
  48. constructor() {
  49. super();
  50. LocaleManager.initTranslations();
  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. 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. 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. SplashScreen.hide();
  157. }
  158. /**
  159. * Renders the app based on loading state
  160. */
  161. render() {
  162. if (this.state.isLoading) {
  163. return null;
  164. } else if (this.state.showIntro || this.state.showUpdate || this.state.showAprilFools) {
  165. return <CustomIntroSlider
  166. onDone={this.onIntroDone}
  167. isUpdate={this.state.showUpdate && !this.state.showIntro}
  168. isAprilFools={this.state.showAprilFools && !this.state.showIntro}
  169. />;
  170. } else {
  171. return (
  172. <PaperProvider theme={this.state.currentTheme}>
  173. <OverflowMenuProvider>
  174. <View style={{backgroundColor: ThemeManager.getCurrentTheme().colors.background, flex: 1}}>
  175. <SafeAreaView style={{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. </SafeAreaView>
  183. </View>
  184. </OverflowMenuProvider>
  185. </PaperProvider>
  186. );
  187. }
  188. }
  189. }