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. constructor() {
  48. super();
  49. LocaleManager.initTranslations();
  50. this.navigatorRef = React.createRef();
  51. this.defaultHomeRoute = null;
  52. this.defaultHomeData = {};
  53. this.urlHandler = new URLHandler(this.onInitialURLParsed, this.onDetectURL);
  54. this.urlHandler.listen();
  55. setSafeBounceHeight(Platform.OS === 'ios' ? 100 : 20);
  56. this.loadAssetsAsync().then(() => {
  57. this.onLoadFinished();
  58. });
  59. }
  60. /**
  61. * The app has been started by an url, and it has been parsed.
  62. * Set a new default start route based on the data parsed.
  63. *
  64. * @param parsedData The data parsed from the url
  65. */
  66. onInitialURLParsed = (parsedData: { route: string, data: { [key: string]: any } }) => {
  67. this.defaultHomeRoute = parsedData.route;
  68. this.defaultHomeData = parsedData.data;
  69. };
  70. /**
  71. * An url has been opened and parsed while the app was active.
  72. * Redirect the user to the screen according to parsed data.
  73. *
  74. * @param parsedData The data parsed from the url
  75. */
  76. onDetectURL = (parsedData: { route: string, data: { [key: string]: any } }) => {
  77. // Navigate to nested navigator and pass data to the index screen
  78. if (this.navigatorRef.current != null) {
  79. this.navigatorRef.current.navigate('home', {
  80. screen: 'index',
  81. params: {nextScreen: parsedData.route, data: parsedData.data}
  82. });
  83. }
  84. };
  85. /**
  86. * Updates the current theme
  87. */
  88. onUpdateTheme = () => {
  89. this.setState({
  90. currentTheme: ThemeManager.getCurrentTheme()
  91. });
  92. this.setupStatusBar();
  93. };
  94. /**
  95. * Updates status bar content color if on iOS only,
  96. * as the android status bar is always set to black.
  97. */
  98. setupStatusBar() {
  99. if (ThemeManager.getNightMode()) {
  100. StatusBar.setBarStyle('light-content', true);
  101. } else {
  102. StatusBar.setBarStyle('dark-content', true);
  103. }
  104. if (Platform.OS === "android")
  105. StatusBar.setBackgroundColor(ThemeManager.getCurrentTheme().colors.surface, true);
  106. }
  107. /**
  108. * Callback when user ends the intro. Save in preferences to avoid showing back the introSlides
  109. */
  110. onIntroDone = () => {
  111. this.setState({
  112. showIntro: false,
  113. showUpdate: false,
  114. showAprilFools: false,
  115. });
  116. AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.showIntro.key, false);
  117. AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.updateNumber.key, Update.number);
  118. AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.showAprilFoolsStart.key, false);
  119. };
  120. /**
  121. * Loads every async data
  122. *
  123. * @returns {Promise<void>}
  124. */
  125. loadAssetsAsync = async () => {
  126. await AsyncStorageManager.getInstance().loadPreferences();
  127. try {
  128. await ConnectionManager.getInstance().recoverLogin();
  129. } catch (e) {
  130. }
  131. }
  132. /**
  133. * Async loading is done, finish processing startup data
  134. */
  135. onLoadFinished() {
  136. // Only show intro if this is the first time starting the app
  137. this.createDrawerNavigator = () => <MainNavigator
  138. defaultHomeRoute={this.defaultHomeRoute}
  139. defaultHomeData={this.defaultHomeData}
  140. />;
  141. ThemeManager.getInstance().setUpdateThemeCallback(this.onUpdateTheme);
  142. // Status bar goes dark if set too fast on ios
  143. if (Platform.OS === 'ios')
  144. setTimeout(this.setupStatusBar, 1000);
  145. else
  146. this.setupStatusBar();
  147. this.setState({
  148. isLoading: false,
  149. currentTheme: ThemeManager.getCurrentTheme(),
  150. showIntro: AsyncStorageManager.getBool(AsyncStorageManager.PREFERENCES.showIntro.key),
  151. showUpdate: AsyncStorageManager.getNumber(AsyncStorageManager.PREFERENCES.updateNumber.key)
  152. !== Update.number,
  153. showAprilFools: AprilFoolsManager.getInstance().isAprilFoolsEnabled()
  154. && AsyncStorageManager.getBool(AsyncStorageManager.PREFERENCES.showAprilFoolsStart.key),
  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. }