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 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // @flow
  2. import * as React from 'react';
  3. import {StatusBar, Platform} from 'react-native';
  4. import {Root, StyleProvider} from 'native-base';
  5. import {createAppContainerWithInitialRoute} from './navigation/AppNavigator';
  6. import ThemeManager from './utils/ThemeManager';
  7. import LocaleManager from './utils/LocaleManager';
  8. import * as Font from 'expo-font';
  9. import {clearThemeCache} from 'native-base-shoutem-theme';
  10. import AsyncStorageManager from "./utils/AsyncStorageManager";
  11. import CustomIntroSlider from "./components/CustomIntroSlider";
  12. import {AppLoading} from 'expo';
  13. import NotificationsManager from "./utils/NotificationsManager";
  14. type Props = {};
  15. type State = {
  16. isLoading: boolean,
  17. showIntro: boolean,
  18. showUpdate: boolean,
  19. currentTheme: ?Object,
  20. };
  21. export default class App extends React.Component<Props, State> {
  22. state = {
  23. isLoading: true,
  24. showIntro: true,
  25. showUpdate: true,
  26. currentTheme: null,
  27. };
  28. constructor(props: Object) {
  29. super(props);
  30. LocaleManager.initTranslations();
  31. }
  32. /**
  33. * Updates the theme and clears the cache to force reloading the app colors. Need to edit shoutem theme for ti to work
  34. */
  35. updateTheme() {
  36. this.setState({
  37. currentTheme: ThemeManager.getCurrentTheme()
  38. });
  39. this.setupStatusBar();
  40. clearThemeCache();
  41. }
  42. setupStatusBar() {
  43. if (Platform.OS === 'ios') {
  44. console.log(ThemeManager.getNightMode());
  45. if (ThemeManager.getNightMode()) {
  46. console.log('setting light mode');
  47. StatusBar.setBarStyle('light-content', true);
  48. } else {
  49. console.log('setting dark mode');
  50. StatusBar.setBarStyle('dark-content', true);
  51. }
  52. }
  53. }
  54. /**
  55. * Callback when user ends the intro. Save in preferences to avaoid showing back the introSlides
  56. */
  57. onIntroDone() {
  58. this.setState({
  59. showIntro: false,
  60. showUpdate: false,
  61. });
  62. AsyncStorageManager.getInstance().savePref(AsyncStorageManager.getInstance().preferences.showIntro.key, '0');
  63. AsyncStorageManager.getInstance().savePref(AsyncStorageManager.getInstance().preferences.showUpdate4.key, '0');
  64. }
  65. async loadAssetsAsync() {
  66. console.log('Starting loading assets...');
  67. // Wait for custom fonts to be loaded before showing the app
  68. await Font.loadAsync({
  69. 'Roboto': require('native-base/Fonts/Roboto.ttf'),
  70. 'Roboto_medium': require('native-base/Fonts/Roboto_medium.ttf'),
  71. 'material-community': require('native-base/Fonts/MaterialCommunityIcons.ttf'),
  72. });
  73. await AsyncStorageManager.getInstance().loadPreferences();
  74. ThemeManager.getInstance().setUpdateThemeCallback(() => this.updateTheme());
  75. await NotificationsManager.initExpoToken();
  76. // console.log(AsyncStorageManager.getInstance().preferences.expoToken.current);
  77. }
  78. onLoadFinished() {
  79. // Only show intro if this is the first time starting the app
  80. console.log('Finished loading');
  81. this.setState({
  82. isLoading: false,
  83. currentTheme: ThemeManager.getCurrentTheme(),
  84. showIntro: AsyncStorageManager.getInstance().preferences.showIntro.current === '1',
  85. showUpdate: AsyncStorageManager.getInstance().preferences.showUpdate4.current === '1'
  86. // showIntro: true
  87. });
  88. // Status bar goes dark if set too fast
  89. setTimeout(this.setupStatusBar,
  90. 1000
  91. )
  92. }
  93. /**
  94. * Renders the app based on loading state
  95. */
  96. render() {
  97. if (this.state.isLoading) {
  98. return (
  99. <AppLoading
  100. startAsync={() => this.loadAssetsAsync()}
  101. onFinish={() => this.onLoadFinished()}
  102. onError={console.warn}
  103. />
  104. );
  105. }
  106. if (this.state.showIntro || this.state.showUpdate) {
  107. return <CustomIntroSlider onDone={() => this.onIntroDone()}
  108. isUpdate={this.state.showUpdate && !this.state.showIntro}/>;
  109. } else {
  110. const AppNavigator = createAppContainerWithInitialRoute(AsyncStorageManager.getInstance().preferences.defaultStartScreen.current);
  111. return (
  112. <Root>
  113. <StyleProvider style={this.state.currentTheme}>
  114. <AppNavigator/>
  115. </StyleProvider>
  116. </Root>
  117. );
  118. }
  119. }
  120. }