2020-09-21 18:12:58 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019 - 2020 Arnaud Vergnet.
|
|
|
|
*
|
|
|
|
* This file is part of Campus INSAT.
|
|
|
|
*
|
|
|
|
* Campus INSAT is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Campus INSAT is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Campus INSAT. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2021-05-07 15:10:36 +02:00
|
|
|
import React from 'react';
|
|
|
|
import { LogBox, Platform, SafeAreaView, View } from 'react-native';
|
|
|
|
import { NavigationContainer } from '@react-navigation/native';
|
|
|
|
import { Provider as PaperProvider } from 'react-native-paper';
|
|
|
|
import { setSafeBounceHeight } from 'react-navigation-collapsible';
|
2020-08-01 20:59:59 +02:00
|
|
|
import SplashScreen from 'react-native-splash-screen';
|
2021-05-07 15:10:36 +02:00
|
|
|
import { OverflowMenuProvider } from 'react-navigation-header-buttons';
|
2020-08-01 20:59:59 +02:00
|
|
|
import AsyncStorageManager from './src/managers/AsyncStorageManager';
|
|
|
|
import CustomIntroSlider from './src/components/Overrides/CustomIntroSlider';
|
2020-04-05 23:56:43 +02:00
|
|
|
import ThemeManager from './src/managers/ThemeManager';
|
2020-04-22 18:36:57 +02:00
|
|
|
import MainNavigator from './src/navigation/MainNavigator';
|
2020-08-01 20:59:59 +02:00
|
|
|
import AprilFoolsManager from './src/managers/AprilFoolsManager';
|
|
|
|
import Update from './src/constants/Update';
|
|
|
|
import ConnectionManager from './src/managers/ConnectionManager';
|
2021-05-07 15:10:36 +02:00
|
|
|
import type { ParsedUrlDataType } from './src/utils/URLHandler';
|
2020-08-01 20:59:59 +02:00
|
|
|
import URLHandler from './src/utils/URLHandler';
|
2021-05-07 15:10:36 +02:00
|
|
|
import { setupStatusBar } from './src/utils/Utils';
|
2020-09-10 18:51:34 +02:00
|
|
|
import initLocales from './src/utils/Locales';
|
2021-05-07 15:10:36 +02:00
|
|
|
import { NavigationContainerRef } from '@react-navigation/core';
|
|
|
|
import GENERAL_STYLES from './src/constants/Styles';
|
2021-05-08 22:27:05 +02:00
|
|
|
import CollapsibleProvider from './src/components/providers/CollapsibleProvider';
|
2021-05-10 15:14:55 +02:00
|
|
|
import CacheProvider from './src/components/providers/CacheProvider';
|
2020-04-20 19:27:23 +02:00
|
|
|
|
|
|
|
// Native optimizations https://reactnavigation.org/docs/react-native-screens
|
2020-05-02 22:46:44 +02:00
|
|
|
// Crashes app when navigating away from webview on android 9+
|
|
|
|
// enableScreens(true);
|
2020-04-20 19:27:23 +02:00
|
|
|
|
2020-08-01 20:59:59 +02:00
|
|
|
LogBox.ignoreLogs([
|
|
|
|
// collapsible headers cause this warning, just ignore as it is not an issue
|
|
|
|
'Non-serializable values were found in the navigation state',
|
|
|
|
'Cannot update a component from inside the function body of a different component',
|
2020-04-17 21:14:39 +02:00
|
|
|
]);
|
|
|
|
|
2020-08-01 20:59:59 +02:00
|
|
|
type StateType = {
|
2020-09-21 21:44:07 +02:00
|
|
|
isLoading: boolean;
|
|
|
|
showIntro: boolean;
|
|
|
|
showUpdate: boolean;
|
|
|
|
showAprilFools: boolean;
|
|
|
|
currentTheme: ReactNativePaper.Theme | undefined;
|
2019-06-29 13:37:21 +02:00
|
|
|
};
|
|
|
|
|
2020-09-23 09:03:48 +02:00
|
|
|
export default class App extends React.Component<{}, StateType> {
|
2021-05-07 15:10:36 +02:00
|
|
|
navigatorRef: { current: null | NavigationContainerRef };
|
2020-04-08 00:47:38 +02:00
|
|
|
|
2020-08-01 20:59:59 +02:00
|
|
|
defaultHomeRoute: string | null;
|
2020-04-08 12:10:06 +02:00
|
|
|
|
2021-05-07 15:10:36 +02:00
|
|
|
defaultHomeData: { [key: string]: string };
|
2020-04-08 00:47:38 +02:00
|
|
|
|
2020-08-01 20:59:59 +02:00
|
|
|
urlHandler: URLHandler;
|
2020-04-08 00:47:38 +02:00
|
|
|
|
2020-09-23 09:03:48 +02:00
|
|
|
constructor(props: {}) {
|
|
|
|
super(props);
|
2020-08-01 20:59:59 +02:00
|
|
|
this.state = {
|
|
|
|
isLoading: true,
|
|
|
|
showIntro: true,
|
|
|
|
showUpdate: true,
|
|
|
|
showAprilFools: false,
|
2020-09-21 21:44:07 +02:00
|
|
|
currentTheme: undefined,
|
2020-04-17 21:07:56 +02:00
|
|
|
};
|
2020-09-10 18:51:34 +02:00
|
|
|
initLocales();
|
2020-08-01 20:59:59 +02:00
|
|
|
this.navigatorRef = React.createRef();
|
|
|
|
this.defaultHomeRoute = null;
|
|
|
|
this.defaultHomeData = {};
|
|
|
|
this.urlHandler = new URLHandler(this.onInitialURLParsed, this.onDetectURL);
|
|
|
|
this.urlHandler.listen();
|
|
|
|
setSafeBounceHeight(Platform.OS === 'ios' ? 100 : 20);
|
2020-10-07 10:42:08 +02:00
|
|
|
this.loadAssetsAsync();
|
2020-08-01 20:59:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The app has been started by an url, and it has been parsed.
|
|
|
|
* Set a new default start route based on the data parsed.
|
|
|
|
*
|
|
|
|
* @param parsedData The data parsed from the url
|
|
|
|
*/
|
|
|
|
onInitialURLParsed = (parsedData: ParsedUrlDataType) => {
|
|
|
|
this.defaultHomeRoute = parsedData.route;
|
|
|
|
this.defaultHomeData = parsedData.data;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An url has been opened and parsed while the app was active.
|
|
|
|
* Redirect the user to the screen according to parsed data.
|
|
|
|
*
|
|
|
|
* @param parsedData The data parsed from the url
|
|
|
|
*/
|
|
|
|
onDetectURL = (parsedData: ParsedUrlDataType) => {
|
|
|
|
// Navigate to nested navigator and pass data to the index screen
|
|
|
|
const nav = this.navigatorRef.current;
|
|
|
|
if (nav != null) {
|
|
|
|
nav.navigate('home', {
|
|
|
|
screen: 'index',
|
2021-05-07 15:10:36 +02:00
|
|
|
params: { nextScreen: parsedData.route, data: parsedData.data },
|
2020-08-01 20:59:59 +02:00
|
|
|
});
|
2019-09-17 10:37:52 +02:00
|
|
|
}
|
2020-08-01 20:59:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the current theme
|
|
|
|
*/
|
|
|
|
onUpdateTheme = () => {
|
|
|
|
this.setState({
|
|
|
|
currentTheme: ThemeManager.getCurrentTheme(),
|
|
|
|
});
|
|
|
|
setupStatusBar();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback when user ends the intro. Save in preferences to avoid showing back the introSlides
|
|
|
|
*/
|
|
|
|
onIntroDone = () => {
|
|
|
|
this.setState({
|
|
|
|
showIntro: false,
|
|
|
|
showUpdate: false,
|
|
|
|
showAprilFools: false,
|
|
|
|
});
|
|
|
|
AsyncStorageManager.set(
|
|
|
|
AsyncStorageManager.PREFERENCES.showIntro.key,
|
2021-05-07 15:10:36 +02:00
|
|
|
false
|
2020-08-01 20:59:59 +02:00
|
|
|
);
|
|
|
|
AsyncStorageManager.set(
|
|
|
|
AsyncStorageManager.PREFERENCES.updateNumber.key,
|
2021-05-07 15:10:36 +02:00
|
|
|
Update.number
|
2020-08-01 20:59:59 +02:00
|
|
|
);
|
|
|
|
AsyncStorageManager.set(
|
|
|
|
AsyncStorageManager.PREFERENCES.showAprilFoolsStart.key,
|
2021-05-07 15:10:36 +02:00
|
|
|
false
|
2020-08-01 20:59:59 +02:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Async loading is done, finish processing startup data
|
|
|
|
*/
|
2020-10-07 10:42:08 +02:00
|
|
|
onLoadFinished = () => {
|
2020-08-01 20:59:59 +02:00
|
|
|
// Only show intro if this is the first time starting the app
|
|
|
|
ThemeManager.getInstance().setUpdateThemeCallback(this.onUpdateTheme);
|
|
|
|
// Status bar goes dark if set too fast on ios
|
2020-09-21 21:44:07 +02:00
|
|
|
if (Platform.OS === 'ios') {
|
|
|
|
setTimeout(setupStatusBar, 1000);
|
|
|
|
} else {
|
|
|
|
setupStatusBar();
|
|
|
|
}
|
2020-08-01 20:59:59 +02:00
|
|
|
|
|
|
|
this.setState({
|
|
|
|
isLoading: false,
|
|
|
|
currentTheme: ThemeManager.getCurrentTheme(),
|
|
|
|
showIntro: AsyncStorageManager.getBool(
|
2021-05-07 15:10:36 +02:00
|
|
|
AsyncStorageManager.PREFERENCES.showIntro.key
|
2020-08-01 20:59:59 +02:00
|
|
|
),
|
|
|
|
showUpdate:
|
|
|
|
AsyncStorageManager.getNumber(
|
2021-05-07 15:10:36 +02:00
|
|
|
AsyncStorageManager.PREFERENCES.updateNumber.key
|
2020-08-01 20:59:59 +02:00
|
|
|
) !== Update.number,
|
|
|
|
showAprilFools:
|
|
|
|
AprilFoolsManager.getInstance().isAprilFoolsEnabled() &&
|
|
|
|
AsyncStorageManager.getBool(
|
2021-05-07 15:10:36 +02:00
|
|
|
AsyncStorageManager.PREFERENCES.showAprilFoolsStart.key
|
2020-08-01 20:59:59 +02:00
|
|
|
),
|
|
|
|
});
|
|
|
|
SplashScreen.hide();
|
2020-10-07 10:42:08 +02:00
|
|
|
};
|
2020-08-01 20:59:59 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads every async data
|
|
|
|
*
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
2020-10-07 10:42:08 +02:00
|
|
|
loadAssetsAsync() {
|
|
|
|
Promise.all([
|
|
|
|
AsyncStorageManager.getInstance().loadPreferences(),
|
|
|
|
ConnectionManager.getInstance().recoverLogin(),
|
|
|
|
])
|
|
|
|
.then(this.onLoadFinished)
|
|
|
|
.catch(this.onLoadFinished);
|
|
|
|
}
|
2020-08-01 20:59:59 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the app based on loading state
|
|
|
|
*/
|
2020-09-21 21:44:07 +02:00
|
|
|
render() {
|
2021-05-07 15:10:36 +02:00
|
|
|
const { state } = this;
|
2020-08-01 20:59:59 +02:00
|
|
|
if (state.isLoading) {
|
|
|
|
return null;
|
2019-08-20 20:00:34 +02:00
|
|
|
}
|
2020-08-01 20:59:59 +02:00
|
|
|
if (state.showIntro || state.showUpdate || state.showAprilFools) {
|
|
|
|
return (
|
|
|
|
<CustomIntroSlider
|
|
|
|
onDone={this.onIntroDone}
|
|
|
|
isUpdate={state.showUpdate && !state.showIntro}
|
|
|
|
isAprilFools={state.showAprilFools && !state.showIntro}
|
|
|
|
/>
|
|
|
|
);
|
2019-06-25 22:20:24 +02:00
|
|
|
}
|
2020-08-01 20:59:59 +02:00
|
|
|
return (
|
|
|
|
<PaperProvider theme={state.currentTheme}>
|
2021-05-08 22:27:05 +02:00
|
|
|
<CollapsibleProvider>
|
2021-05-10 15:14:55 +02:00
|
|
|
<CacheProvider>
|
|
|
|
<OverflowMenuProvider>
|
|
|
|
<View
|
|
|
|
style={{
|
|
|
|
backgroundColor: ThemeManager.getCurrentTheme().colors
|
|
|
|
.background,
|
|
|
|
...GENERAL_STYLES.flex,
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<SafeAreaView style={GENERAL_STYLES.flex}>
|
|
|
|
<NavigationContainer
|
|
|
|
theme={state.currentTheme}
|
|
|
|
ref={this.navigatorRef}
|
|
|
|
>
|
|
|
|
<MainNavigator
|
|
|
|
defaultHomeRoute={this.defaultHomeRoute}
|
|
|
|
defaultHomeData={this.defaultHomeData}
|
|
|
|
/>
|
|
|
|
</NavigationContainer>
|
|
|
|
</SafeAreaView>
|
|
|
|
</View>
|
|
|
|
</OverflowMenuProvider>
|
|
|
|
</CacheProvider>
|
2021-05-08 22:27:05 +02:00
|
|
|
</CollapsibleProvider>
|
2020-08-01 20:59:59 +02:00
|
|
|
</PaperProvider>
|
|
|
|
);
|
|
|
|
}
|
2019-06-25 22:20:24 +02:00
|
|
|
}
|