2019-06-29 13:37:21 +02:00
|
|
|
// @flow
|
|
|
|
|
2019-08-06 13:28:11 +02:00
|
|
|
import * as React from 'react';
|
2020-04-20 12:28:25 +02:00
|
|
|
import {Platform, StatusBar, YellowBox} from 'react-native';
|
2020-04-05 23:56:43 +02:00
|
|
|
import LocaleManager from './src/managers/LocaleManager';
|
|
|
|
import AsyncStorageManager from "./src/managers/AsyncStorageManager";
|
2020-04-18 20:00:51 +02:00
|
|
|
import CustomIntroSlider from "./src/components/Overrides/CustomIntroSlider";
|
2020-04-20 12:28:25 +02:00
|
|
|
import {AppLoading} from 'expo';
|
2020-04-19 20:21:39 +02:00
|
|
|
import type {CustomTheme} from "./src/managers/ThemeManager";
|
2020-04-05 23:56:43 +02:00
|
|
|
import ThemeManager from './src/managers/ThemeManager';
|
2020-03-06 23:15:01 +01:00
|
|
|
import {NavigationContainer} from '@react-navigation/native';
|
2020-04-22 18:36:57 +02:00
|
|
|
import MainNavigator from './src/navigation/MainNavigator';
|
2020-04-05 23:56:43 +02:00
|
|
|
import {initExpoToken} from "./src/utils/Notifications";
|
2020-04-19 20:21:39 +02:00
|
|
|
import {Provider as PaperProvider} from 'react-native-paper';
|
2020-04-05 23:56:43 +02:00
|
|
|
import AprilFoolsManager from "./src/managers/AprilFoolsManager";
|
|
|
|
import Update from "./src/constants/Update";
|
|
|
|
import ConnectionManager from "./src/managers/ConnectionManager";
|
2020-04-08 12:10:06 +02:00
|
|
|
import URLHandler from "./src/utils/URLHandler";
|
2020-04-16 18:54:57 +02:00
|
|
|
import {setSafeBounceHeight} from "react-navigation-collapsible";
|
2020-04-20 19:27:23 +02:00
|
|
|
import {enableScreens} from 'react-native-screens';
|
|
|
|
|
|
|
|
// Native optimizations https://reactnavigation.org/docs/react-native-screens
|
|
|
|
enableScreens();
|
|
|
|
|
2019-06-29 13:37:21 +02:00
|
|
|
|
2020-04-17 21:14:39 +02:00
|
|
|
YellowBox.ignoreWarnings([ // collapsible headers cause this warning, just ignore as it is not an issue
|
|
|
|
'Non-serializable values were found in the navigation state',
|
|
|
|
]);
|
|
|
|
|
2019-06-29 13:37:21 +02:00
|
|
|
type Props = {};
|
|
|
|
|
|
|
|
type State = {
|
|
|
|
isLoading: boolean,
|
2019-08-05 17:18:48 +02:00
|
|
|
showIntro: boolean,
|
2019-09-16 00:10:14 +02:00
|
|
|
showUpdate: boolean,
|
2020-03-11 19:55:48 +01:00
|
|
|
showAprilFools: boolean,
|
2020-04-19 20:21:39 +02:00
|
|
|
currentTheme: CustomTheme | null,
|
2019-06-29 13:37:21 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export default class App extends React.Component<Props, State> {
|
2019-06-25 22:16:14 +02:00
|
|
|
|
2019-06-29 13:37:21 +02:00
|
|
|
state = {
|
|
|
|
isLoading: true,
|
2019-08-05 17:18:48 +02:00
|
|
|
showIntro: true,
|
2019-09-16 00:10:14 +02:00
|
|
|
showUpdate: true,
|
2020-03-11 19:55:48 +01:00
|
|
|
showAprilFools: false,
|
2019-06-29 13:37:21 +02:00
|
|
|
currentTheme: null,
|
|
|
|
};
|
2019-06-25 22:20:24 +02:00
|
|
|
|
2020-04-19 16:59:40 +02:00
|
|
|
navigatorRef: { current: null | NavigationContainer };
|
2020-04-08 00:47:38 +02:00
|
|
|
|
2020-04-19 16:59:40 +02:00
|
|
|
defaultHomeRoute: string | null;
|
|
|
|
defaultHomeData: { [key: string]: any }
|
2020-04-08 00:47:38 +02:00
|
|
|
|
2020-04-19 16:59:40 +02:00
|
|
|
createDrawerNavigator: () => React.Node;
|
2020-04-08 00:47:38 +02:00
|
|
|
|
2020-04-08 12:10:06 +02:00
|
|
|
urlHandler: URLHandler;
|
2020-04-19 16:59:40 +02:00
|
|
|
storageManager: AsyncStorageManager;
|
2020-04-08 12:10:06 +02:00
|
|
|
|
2020-03-05 21:48:37 +01:00
|
|
|
constructor() {
|
|
|
|
super();
|
2019-06-29 13:37:21 +02:00
|
|
|
LocaleManager.initTranslations();
|
2020-04-20 12:28:25 +02:00
|
|
|
// SplashScreen.preventAutoHide();
|
2020-04-08 00:47:38 +02:00
|
|
|
this.navigatorRef = React.createRef();
|
2020-04-19 16:59:40 +02:00
|
|
|
this.defaultHomeRoute = null;
|
|
|
|
this.defaultHomeData = {};
|
|
|
|
this.storageManager = AsyncStorageManager.getInstance();
|
2020-04-08 12:10:06 +02:00
|
|
|
this.urlHandler = new URLHandler(this.onInitialURLParsed, this.onDetectURL);
|
|
|
|
this.urlHandler.listen();
|
2020-04-18 16:55:08 +02:00
|
|
|
setSafeBounceHeight(Platform.OS === 'ios' ? 100 : 20);
|
2020-04-20 14:28:32 +02:00
|
|
|
this.loadAssetsAsync().then(() => {
|
|
|
|
this.onLoadFinished();
|
|
|
|
});
|
2020-04-08 00:47:38 +02:00
|
|
|
}
|
|
|
|
|
2020-04-19 16:59:40 +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: { route: string, data: { [key: string]: any } }) => {
|
|
|
|
this.defaultHomeRoute = parsedData.route;
|
|
|
|
this.defaultHomeData = parsedData.data;
|
2020-04-08 00:47:38 +02:00
|
|
|
};
|
|
|
|
|
2020-04-19 16:59:40 +02:00
|
|
|
/**
|
|
|
|
* 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: { route: string, data: { [key: string]: any } }) => {
|
2020-04-08 12:10:06 +02:00
|
|
|
// Navigate to nested navigator and pass data to the index screen
|
2020-04-19 16:59:40 +02:00
|
|
|
if (this.navigatorRef.current != null) {
|
|
|
|
this.navigatorRef.current.navigate('home', {
|
|
|
|
screen: 'index',
|
|
|
|
params: {nextScreen: parsedData.route, data: parsedData.data}
|
|
|
|
});
|
|
|
|
}
|
2020-04-08 00:47:38 +02:00
|
|
|
};
|
|
|
|
|
2019-06-29 13:37:21 +02:00
|
|
|
/**
|
2020-04-19 16:59:40 +02:00
|
|
|
* Updates the current theme
|
2019-06-29 13:37:21 +02:00
|
|
|
*/
|
2020-04-17 21:07:56 +02:00
|
|
|
onUpdateTheme = () => {
|
2019-08-20 20:00:34 +02:00
|
|
|
this.setState({
|
|
|
|
currentTheme: ThemeManager.getCurrentTheme()
|
|
|
|
});
|
2019-09-17 10:37:52 +02:00
|
|
|
this.setupStatusBar();
|
2020-04-17 21:07:56 +02:00
|
|
|
};
|
2019-08-20 20:00:34 +02:00
|
|
|
|
2020-04-19 16:59:40 +02:00
|
|
|
/**
|
|
|
|
* Updates status bar content color if on iOS only,
|
|
|
|
* as the android status bar is always set to black.
|
|
|
|
*/
|
2019-09-17 10:37:52 +02:00
|
|
|
setupStatusBar() {
|
2020-04-20 20:55:37 +02:00
|
|
|
if (ThemeManager.getNightMode()) {
|
|
|
|
StatusBar.setBarStyle('light-content', true);
|
|
|
|
} else {
|
|
|
|
StatusBar.setBarStyle('dark-content', true);
|
2019-09-17 10:37:52 +02:00
|
|
|
}
|
2020-04-20 20:55:37 +02:00
|
|
|
StatusBar.setBackgroundColor(ThemeManager.getCurrentTheme().colors.surface, true);
|
2019-09-17 10:37:52 +02:00
|
|
|
}
|
|
|
|
|
2019-08-20 20:00:34 +02:00
|
|
|
/**
|
2020-04-19 16:59:40 +02:00
|
|
|
* Callback when user ends the intro. Save in preferences to avoid showing back the introSlides
|
2019-08-20 20:00:34 +02:00
|
|
|
*/
|
2020-04-17 21:07:56 +02:00
|
|
|
onIntroDone = () => {
|
2019-09-16 00:10:14 +02:00
|
|
|
this.setState({
|
|
|
|
showIntro: false,
|
|
|
|
showUpdate: false,
|
2020-03-11 19:55:48 +01:00
|
|
|
showAprilFools: false,
|
2019-09-16 00:10:14 +02:00
|
|
|
});
|
2020-04-19 16:59:40 +02:00
|
|
|
this.storageManager.savePref(this.storageManager.preferences.showIntro.key, '0');
|
|
|
|
this.storageManager.savePref(this.storageManager.preferences.updateNumber.key, Update.number.toString());
|
|
|
|
this.storageManager.savePref(this.storageManager.preferences.showAprilFoolsStart.key, '0');
|
2020-04-17 21:07:56 +02:00
|
|
|
};
|
2019-08-20 20:00:34 +02:00
|
|
|
|
2020-04-19 16:59:40 +02:00
|
|
|
/**
|
|
|
|
* Loads every async data
|
|
|
|
*
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
2020-04-20 12:28:25 +02:00
|
|
|
loadAssetsAsync = async () => {
|
2020-04-19 16:59:40 +02:00
|
|
|
await this.storageManager.loadPreferences();
|
2020-03-30 15:28:08 +02:00
|
|
|
await initExpoToken();
|
2020-04-04 16:09:04 +02:00
|
|
|
try {
|
|
|
|
await ConnectionManager.getInstance().recoverLogin();
|
2020-04-08 00:47:38 +02:00
|
|
|
} catch (e) {
|
|
|
|
}
|
2019-08-20 20:00:34 +02:00
|
|
|
}
|
|
|
|
|
2020-04-19 16:59:40 +02:00
|
|
|
/**
|
|
|
|
* Async loading is done, finish processing startup data
|
|
|
|
*/
|
2019-08-20 20:00:34 +02:00
|
|
|
onLoadFinished() {
|
2019-08-06 13:28:11 +02:00
|
|
|
// Only show intro if this is the first time starting the app
|
2020-04-22 18:36:57 +02:00
|
|
|
this.createDrawerNavigator = () => <MainNavigator
|
2020-04-19 16:59:40 +02:00
|
|
|
defaultHomeRoute={this.defaultHomeRoute}
|
2020-04-20 12:28:25 +02:00
|
|
|
defaultHomeData={this.defaultHomeData}
|
|
|
|
/>;
|
2020-04-19 16:59:40 +02:00
|
|
|
ThemeManager.getInstance().setUpdateThemeCallback(this.onUpdateTheme);
|
2020-04-05 23:51:15 +02:00
|
|
|
// Status bar goes dark if set too fast on ios
|
|
|
|
if (Platform.OS === 'ios')
|
|
|
|
setTimeout(this.setupStatusBar, 1000);
|
|
|
|
else
|
|
|
|
this.setupStatusBar();
|
2020-04-19 16:59:40 +02:00
|
|
|
this.setState({
|
|
|
|
isLoading: false,
|
|
|
|
currentTheme: ThemeManager.getCurrentTheme(),
|
|
|
|
showIntro: this.storageManager.preferences.showIntro.current === '1',
|
|
|
|
showUpdate: this.storageManager.preferences.updateNumber.current !== Update.number.toString(),
|
|
|
|
showAprilFools: AprilFoolsManager.getInstance().isAprilFoolsEnabled() && this.storageManager.preferences.showAprilFoolsStart.current === '1',
|
|
|
|
});
|
2019-06-25 22:20:24 +02:00
|
|
|
}
|
|
|
|
|
2019-06-29 13:37:21 +02:00
|
|
|
/**
|
|
|
|
* Renders the app based on loading state
|
|
|
|
*/
|
2019-06-25 22:20:24 +02:00
|
|
|
render() {
|
|
|
|
if (this.state.isLoading) {
|
2020-04-20 12:28:25 +02:00
|
|
|
return <AppLoading/>;
|
2020-03-11 19:55:48 +01:00
|
|
|
} else if (this.state.showIntro || this.state.showUpdate || this.state.showAprilFools) {
|
2020-03-08 13:21:14 +01:00
|
|
|
return <CustomIntroSlider
|
|
|
|
onDone={this.onIntroDone}
|
|
|
|
isUpdate={this.state.showUpdate && !this.state.showIntro}
|
2020-03-11 19:55:48 +01:00
|
|
|
isAprilFools={this.state.showAprilFools && !this.state.showIntro}
|
2020-03-08 13:21:14 +01:00
|
|
|
/>;
|
2019-08-05 17:18:48 +02:00
|
|
|
} else {
|
|
|
|
return (
|
2020-03-06 23:15:01 +01:00
|
|
|
<PaperProvider theme={this.state.currentTheme}>
|
2020-04-20 12:28:25 +02:00
|
|
|
<NavigationContainer theme={this.state.currentTheme} ref={this.navigatorRef}>
|
2020-04-22 18:36:57 +02:00
|
|
|
<MainNavigator
|
2020-04-20 19:27:23 +02:00
|
|
|
defaultHomeRoute={this.defaultHomeRoute}
|
|
|
|
defaultHomeData={this.defaultHomeData}
|
|
|
|
/>
|
2020-04-20 12:28:25 +02:00
|
|
|
</NavigationContainer>
|
2020-03-06 23:15:01 +01:00
|
|
|
</PaperProvider>
|
2019-08-05 17:18:48 +02:00
|
|
|
);
|
|
|
|
}
|
2019-06-25 22:20:24 +02:00
|
|
|
}
|
|
|
|
}
|