2019-06-29 13:37:21 +02:00
|
|
|
// @flow
|
|
|
|
|
2019-08-06 13:28:11 +02:00
|
|
|
import * as React from 'react';
|
2020-07-14 11:19:02 +02:00
|
|
|
import {LogBox, Platform, SafeAreaView, StatusBar, View} 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-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-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-26 23:05:27 +02:00
|
|
|
import SplashScreen from 'react-native-splash-screen'
|
2020-05-20 11:45:15 +02:00
|
|
|
import {OverflowMenuProvider} from "react-navigation-header-buttons";
|
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
|
|
|
|
2019-06-29 13:37:21 +02:00
|
|
|
|
2020-07-14 11:19:02 +02:00
|
|
|
LogBox.ignoreLogs([ // collapsible headers cause this warning, just ignore as it is not an issue
|
2020-04-17 21:14:39 +02:00
|
|
|
'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-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
|
|
|
/**
|
2020-06-28 12:48:31 +02:00
|
|
|
* The app has been started by an url, and it has been parsed.
|
2020-04-19 16:59:40 +02:00
|
|
|
* 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-23 11:23:38 +02:00
|
|
|
if (Platform.OS === "android")
|
|
|
|
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-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',
|
|
|
|
});
|
2020-04-26 23:05:27 +02:00
|
|
|
SplashScreen.hide();
|
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-25 15:09:08 +02:00
|
|
|
return null;
|
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-05-20 11:45:15 +02:00
|
|
|
<OverflowMenuProvider>
|
|
|
|
<View style={{backgroundColor: ThemeManager.getCurrentTheme().colors.background, flex: 1}}>
|
2020-05-31 17:18:53 +02:00
|
|
|
<SafeAreaView style={{flex: 1}}>
|
2020-05-31 16:30:38 +02:00
|
|
|
<NavigationContainer theme={this.state.currentTheme} ref={this.navigatorRef}>
|
|
|
|
<MainNavigator
|
|
|
|
defaultHomeRoute={this.defaultHomeRoute}
|
|
|
|
defaultHomeData={this.defaultHomeData}
|
|
|
|
/>
|
|
|
|
</NavigationContainer>
|
2020-05-31 17:18:53 +02:00
|
|
|
</SafeAreaView>
|
2020-05-20 11:45:15 +02:00
|
|
|
</View>
|
|
|
|
</OverflowMenuProvider>
|
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
|
|
|
}
|
|
|
|
}
|