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

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