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

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