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

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