|
@@ -1,210 +1,211 @@
|
1
|
1
|
// @flow
|
2
|
2
|
|
3
|
3
|
import * as React from 'react';
|
4
|
|
-import {LogBox, Platform, SafeAreaView, StatusBar, View} from 'react-native';
|
|
4
|
+import {LogBox, Platform, SafeAreaView, View} from 'react-native';
|
|
5
|
+import {NavigationContainer} from '@react-navigation/native';
|
|
6
|
+import {Provider as PaperProvider} from 'react-native-paper';
|
|
7
|
+import {setSafeBounceHeight} from 'react-navigation-collapsible';
|
|
8
|
+import SplashScreen from 'react-native-splash-screen';
|
|
9
|
+import {OverflowMenuProvider} from 'react-navigation-header-buttons';
|
5
|
10
|
import LocaleManager from './src/managers/LocaleManager';
|
6
|
|
-import AsyncStorageManager from "./src/managers/AsyncStorageManager";
|
7
|
|
-import CustomIntroSlider from "./src/components/Overrides/CustomIntroSlider";
|
8
|
|
-import type {CustomTheme} from "./src/managers/ThemeManager";
|
|
11
|
+import AsyncStorageManager from './src/managers/AsyncStorageManager';
|
|
12
|
+import CustomIntroSlider from './src/components/Overrides/CustomIntroSlider';
|
|
13
|
+import type {CustomTheme} from './src/managers/ThemeManager';
|
9
|
14
|
import ThemeManager from './src/managers/ThemeManager';
|
10
|
|
-import {NavigationContainer} from '@react-navigation/native';
|
11
|
15
|
import MainNavigator from './src/navigation/MainNavigator';
|
12
|
|
-import {Provider as PaperProvider} from 'react-native-paper';
|
13
|
|
-import AprilFoolsManager from "./src/managers/AprilFoolsManager";
|
14
|
|
-import Update from "./src/constants/Update";
|
15
|
|
-import ConnectionManager from "./src/managers/ConnectionManager";
|
16
|
|
-import URLHandler from "./src/utils/URLHandler";
|
17
|
|
-import {setSafeBounceHeight} from "react-navigation-collapsible";
|
18
|
|
-import SplashScreen from 'react-native-splash-screen'
|
19
|
|
-import {OverflowMenuProvider} from "react-navigation-header-buttons";
|
|
16
|
+import AprilFoolsManager from './src/managers/AprilFoolsManager';
|
|
17
|
+import Update from './src/constants/Update';
|
|
18
|
+import ConnectionManager from './src/managers/ConnectionManager';
|
|
19
|
+import type {ParsedUrlDataType} from './src/utils/URLHandler';
|
|
20
|
+import URLHandler from './src/utils/URLHandler';
|
|
21
|
+import {setupStatusBar} from './src/utils/Utils';
|
20
|
22
|
|
21
|
23
|
// Native optimizations https://reactnavigation.org/docs/react-native-screens
|
22
|
24
|
// Crashes app when navigating away from webview on android 9+
|
23
|
25
|
// enableScreens(true);
|
24
|
26
|
|
25
|
|
-
|
26
|
|
-LogBox.ignoreLogs([ // collapsible headers cause this warning, just ignore as it is not an issue
|
27
|
|
- 'Non-serializable values were found in the navigation state',
|
28
|
|
- 'Cannot update a component from inside the function body of a different component',
|
|
27
|
+LogBox.ignoreLogs([
|
|
28
|
+ // collapsible headers cause this warning, just ignore as it is not an issue
|
|
29
|
+ 'Non-serializable values were found in the navigation state',
|
|
30
|
+ 'Cannot update a component from inside the function body of a different component',
|
29
|
31
|
]);
|
30
|
32
|
|
31
|
|
-type Props = {};
|
32
|
|
-
|
33
|
|
-type State = {
|
34
|
|
- isLoading: boolean,
|
35
|
|
- showIntro: boolean,
|
36
|
|
- showUpdate: boolean,
|
37
|
|
- showAprilFools: boolean,
|
38
|
|
- currentTheme: CustomTheme | null,
|
|
33
|
+type StateType = {
|
|
34
|
+ isLoading: boolean,
|
|
35
|
+ showIntro: boolean,
|
|
36
|
+ showUpdate: boolean,
|
|
37
|
+ showAprilFools: boolean,
|
|
38
|
+ currentTheme: CustomTheme | null,
|
39
|
39
|
};
|
40
|
40
|
|
41
|
|
-export default class App extends React.Component<Props, State> {
|
42
|
|
-
|
43
|
|
- state = {
|
44
|
|
- isLoading: true,
|
45
|
|
- showIntro: true,
|
46
|
|
- showUpdate: true,
|
47
|
|
- showAprilFools: false,
|
48
|
|
- currentTheme: null,
|
49
|
|
- };
|
50
|
|
-
|
51
|
|
- navigatorRef: { current: null | NavigationContainer };
|
52
|
|
-
|
53
|
|
- defaultHomeRoute: string | null;
|
54
|
|
- defaultHomeData: { [key: string]: any }
|
55
|
|
-
|
56
|
|
- createDrawerNavigator: () => React.Node;
|
|
41
|
+export default class App extends React.Component<null, StateType> {
|
|
42
|
+ navigatorRef: {current: null | NavigationContainer};
|
57
|
43
|
|
58
|
|
- urlHandler: URLHandler;
|
|
44
|
+ defaultHomeRoute: string | null;
|
59
|
45
|
|
60
|
|
- constructor() {
|
61
|
|
- super();
|
62
|
|
- LocaleManager.initTranslations();
|
63
|
|
- this.navigatorRef = React.createRef();
|
64
|
|
- this.defaultHomeRoute = null;
|
65
|
|
- this.defaultHomeData = {};
|
66
|
|
- this.urlHandler = new URLHandler(this.onInitialURLParsed, this.onDetectURL);
|
67
|
|
- this.urlHandler.listen();
|
68
|
|
- setSafeBounceHeight(Platform.OS === 'ios' ? 100 : 20);
|
69
|
|
- this.loadAssetsAsync().then(() => {
|
70
|
|
- this.onLoadFinished();
|
71
|
|
- });
|
72
|
|
- }
|
73
|
|
-
|
74
|
|
- /**
|
75
|
|
- * The app has been started by an url, and it has been parsed.
|
76
|
|
- * Set a new default start route based on the data parsed.
|
77
|
|
- *
|
78
|
|
- * @param parsedData The data parsed from the url
|
79
|
|
- */
|
80
|
|
- onInitialURLParsed = (parsedData: { route: string, data: { [key: string]: any } }) => {
|
81
|
|
- this.defaultHomeRoute = parsedData.route;
|
82
|
|
- this.defaultHomeData = parsedData.data;
|
83
|
|
- };
|
|
46
|
+ defaultHomeData: {[key: string]: string};
|
84
|
47
|
|
85
|
|
- /**
|
86
|
|
- * An url has been opened and parsed while the app was active.
|
87
|
|
- * Redirect the user to the screen according to parsed data.
|
88
|
|
- *
|
89
|
|
- * @param parsedData The data parsed from the url
|
90
|
|
- */
|
91
|
|
- onDetectURL = (parsedData: { route: string, data: { [key: string]: any } }) => {
|
92
|
|
- // Navigate to nested navigator and pass data to the index screen
|
93
|
|
- if (this.navigatorRef.current != null) {
|
94
|
|
- this.navigatorRef.current.navigate('home', {
|
95
|
|
- screen: 'index',
|
96
|
|
- params: {nextScreen: parsedData.route, data: parsedData.data}
|
97
|
|
- });
|
98
|
|
- }
|
99
|
|
- };
|
|
48
|
+ urlHandler: URLHandler;
|
100
|
49
|
|
101
|
|
- /**
|
102
|
|
- * Updates the current theme
|
103
|
|
- */
|
104
|
|
- onUpdateTheme = () => {
|
105
|
|
- this.setState({
|
106
|
|
- currentTheme: ThemeManager.getCurrentTheme()
|
107
|
|
- });
|
108
|
|
- this.setupStatusBar();
|
|
50
|
+ constructor() {
|
|
51
|
+ super();
|
|
52
|
+ this.state = {
|
|
53
|
+ isLoading: true,
|
|
54
|
+ showIntro: true,
|
|
55
|
+ showUpdate: true,
|
|
56
|
+ showAprilFools: false,
|
|
57
|
+ currentTheme: null,
|
109
|
58
|
};
|
110
|
|
-
|
111
|
|
- /**
|
112
|
|
- * Updates status bar content color if on iOS only,
|
113
|
|
- * as the android status bar is always set to black.
|
114
|
|
- */
|
115
|
|
- setupStatusBar() {
|
116
|
|
- if (ThemeManager.getNightMode()) {
|
117
|
|
- StatusBar.setBarStyle('light-content', true);
|
118
|
|
- } else {
|
119
|
|
- StatusBar.setBarStyle('dark-content', true);
|
120
|
|
- }
|
121
|
|
- if (Platform.OS === "android")
|
122
|
|
- StatusBar.setBackgroundColor(ThemeManager.getCurrentTheme().colors.surface, true);
|
|
59
|
+ LocaleManager.initTranslations();
|
|
60
|
+ this.navigatorRef = React.createRef();
|
|
61
|
+ this.defaultHomeRoute = null;
|
|
62
|
+ this.defaultHomeData = {};
|
|
63
|
+ this.urlHandler = new URLHandler(this.onInitialURLParsed, this.onDetectURL);
|
|
64
|
+ this.urlHandler.listen();
|
|
65
|
+ setSafeBounceHeight(Platform.OS === 'ios' ? 100 : 20);
|
|
66
|
+ this.loadAssetsAsync().finally(() => {
|
|
67
|
+ this.onLoadFinished();
|
|
68
|
+ });
|
|
69
|
+ }
|
|
70
|
+
|
|
71
|
+ /**
|
|
72
|
+ * The app has been started by an url, and it has been parsed.
|
|
73
|
+ * Set a new default start route based on the data parsed.
|
|
74
|
+ *
|
|
75
|
+ * @param parsedData The data parsed from the url
|
|
76
|
+ */
|
|
77
|
+ onInitialURLParsed = (parsedData: ParsedUrlDataType) => {
|
|
78
|
+ this.defaultHomeRoute = parsedData.route;
|
|
79
|
+ this.defaultHomeData = parsedData.data;
|
|
80
|
+ };
|
|
81
|
+
|
|
82
|
+ /**
|
|
83
|
+ * An url has been opened and parsed while the app was active.
|
|
84
|
+ * Redirect the user to the screen according to parsed data.
|
|
85
|
+ *
|
|
86
|
+ * @param parsedData The data parsed from the url
|
|
87
|
+ */
|
|
88
|
+ onDetectURL = (parsedData: ParsedUrlDataType) => {
|
|
89
|
+ // Navigate to nested navigator and pass data to the index screen
|
|
90
|
+ const nav = this.navigatorRef.current;
|
|
91
|
+ if (nav != null) {
|
|
92
|
+ nav.navigate('home', {
|
|
93
|
+ screen: 'index',
|
|
94
|
+ params: {nextScreen: parsedData.route, data: parsedData.data},
|
|
95
|
+ });
|
123
|
96
|
}
|
124
|
|
-
|
125
|
|
- /**
|
126
|
|
- * Callback when user ends the intro. Save in preferences to avoid showing back the introSlides
|
127
|
|
- */
|
128
|
|
- onIntroDone = () => {
|
129
|
|
- this.setState({
|
130
|
|
- showIntro: false,
|
131
|
|
- showUpdate: false,
|
132
|
|
- showAprilFools: false,
|
133
|
|
- });
|
134
|
|
- AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.showIntro.key, false);
|
135
|
|
- AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.updateNumber.key, Update.number);
|
136
|
|
- AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.showAprilFoolsStart.key, false);
|
137
|
|
- };
|
138
|
|
-
|
139
|
|
- /**
|
140
|
|
- * Loads every async data
|
141
|
|
- *
|
142
|
|
- * @returns {Promise<void>}
|
143
|
|
- */
|
144
|
|
- loadAssetsAsync = async () => {
|
145
|
|
- await AsyncStorageManager.getInstance().loadPreferences();
|
146
|
|
- try {
|
147
|
|
- await ConnectionManager.getInstance().recoverLogin();
|
148
|
|
- } catch (e) {
|
149
|
|
- }
|
|
97
|
+ };
|
|
98
|
+
|
|
99
|
+ /**
|
|
100
|
+ * Updates the current theme
|
|
101
|
+ */
|
|
102
|
+ onUpdateTheme = () => {
|
|
103
|
+ this.setState({
|
|
104
|
+ currentTheme: ThemeManager.getCurrentTheme(),
|
|
105
|
+ });
|
|
106
|
+ setupStatusBar();
|
|
107
|
+ };
|
|
108
|
+
|
|
109
|
+ /**
|
|
110
|
+ * Callback when user ends the intro. Save in preferences to avoid showing back the introSlides
|
|
111
|
+ */
|
|
112
|
+ onIntroDone = () => {
|
|
113
|
+ this.setState({
|
|
114
|
+ showIntro: false,
|
|
115
|
+ showUpdate: false,
|
|
116
|
+ showAprilFools: false,
|
|
117
|
+ });
|
|
118
|
+ AsyncStorageManager.set(
|
|
119
|
+ AsyncStorageManager.PREFERENCES.showIntro.key,
|
|
120
|
+ false,
|
|
121
|
+ );
|
|
122
|
+ AsyncStorageManager.set(
|
|
123
|
+ AsyncStorageManager.PREFERENCES.updateNumber.key,
|
|
124
|
+ Update.number,
|
|
125
|
+ );
|
|
126
|
+ AsyncStorageManager.set(
|
|
127
|
+ AsyncStorageManager.PREFERENCES.showAprilFoolsStart.key,
|
|
128
|
+ false,
|
|
129
|
+ );
|
|
130
|
+ };
|
|
131
|
+
|
|
132
|
+ /**
|
|
133
|
+ * Async loading is done, finish processing startup data
|
|
134
|
+ */
|
|
135
|
+ onLoadFinished() {
|
|
136
|
+ // Only show intro if this is the first time starting the app
|
|
137
|
+ ThemeManager.getInstance().setUpdateThemeCallback(this.onUpdateTheme);
|
|
138
|
+ // Status bar goes dark if set too fast on ios
|
|
139
|
+ if (Platform.OS === 'ios') setTimeout(setupStatusBar, 1000);
|
|
140
|
+ else setupStatusBar();
|
|
141
|
+
|
|
142
|
+ this.setState({
|
|
143
|
+ isLoading: false,
|
|
144
|
+ currentTheme: ThemeManager.getCurrentTheme(),
|
|
145
|
+ showIntro: AsyncStorageManager.getBool(
|
|
146
|
+ AsyncStorageManager.PREFERENCES.showIntro.key,
|
|
147
|
+ ),
|
|
148
|
+ showUpdate:
|
|
149
|
+ AsyncStorageManager.getNumber(
|
|
150
|
+ AsyncStorageManager.PREFERENCES.updateNumber.key,
|
|
151
|
+ ) !== Update.number,
|
|
152
|
+ showAprilFools:
|
|
153
|
+ AprilFoolsManager.getInstance().isAprilFoolsEnabled() &&
|
|
154
|
+ AsyncStorageManager.getBool(
|
|
155
|
+ AsyncStorageManager.PREFERENCES.showAprilFoolsStart.key,
|
|
156
|
+ ),
|
|
157
|
+ });
|
|
158
|
+ SplashScreen.hide();
|
|
159
|
+ }
|
|
160
|
+
|
|
161
|
+ /**
|
|
162
|
+ * Loads every async data
|
|
163
|
+ *
|
|
164
|
+ * @returns {Promise<void>}
|
|
165
|
+ */
|
|
166
|
+ loadAssetsAsync = async () => {
|
|
167
|
+ await AsyncStorageManager.getInstance().loadPreferences();
|
|
168
|
+ await ConnectionManager.getInstance().recoverLogin();
|
|
169
|
+ };
|
|
170
|
+
|
|
171
|
+ /**
|
|
172
|
+ * Renders the app based on loading state
|
|
173
|
+ */
|
|
174
|
+ render(): React.Node {
|
|
175
|
+ const {state} = this;
|
|
176
|
+ if (state.isLoading) {
|
|
177
|
+ return null;
|
150
|
178
|
}
|
151
|
|
-
|
152
|
|
- /**
|
153
|
|
- * Async loading is done, finish processing startup data
|
154
|
|
- */
|
155
|
|
- onLoadFinished() {
|
156
|
|
- // Only show intro if this is the first time starting the app
|
157
|
|
- this.createDrawerNavigator = () => <MainNavigator
|
158
|
|
- defaultHomeRoute={this.defaultHomeRoute}
|
159
|
|
- defaultHomeData={this.defaultHomeData}
|
160
|
|
- />;
|
161
|
|
- ThemeManager.getInstance().setUpdateThemeCallback(this.onUpdateTheme);
|
162
|
|
- // Status bar goes dark if set too fast on ios
|
163
|
|
- if (Platform.OS === 'ios')
|
164
|
|
- setTimeout(this.setupStatusBar, 1000);
|
165
|
|
- else
|
166
|
|
- this.setupStatusBar();
|
167
|
|
- this.setState({
|
168
|
|
- isLoading: false,
|
169
|
|
- currentTheme: ThemeManager.getCurrentTheme(),
|
170
|
|
- showIntro: AsyncStorageManager.getBool(AsyncStorageManager.PREFERENCES.showIntro.key),
|
171
|
|
- showUpdate: AsyncStorageManager.getNumber(AsyncStorageManager.PREFERENCES.updateNumber.key)
|
172
|
|
- !== Update.number,
|
173
|
|
- showAprilFools: AprilFoolsManager.getInstance().isAprilFoolsEnabled()
|
174
|
|
- && AsyncStorageManager.getBool(AsyncStorageManager.PREFERENCES.showAprilFoolsStart.key),
|
175
|
|
- });
|
176
|
|
- SplashScreen.hide();
|
177
|
|
- }
|
178
|
|
-
|
179
|
|
- /**
|
180
|
|
- * Renders the app based on loading state
|
181
|
|
- */
|
182
|
|
- render() {
|
183
|
|
- if (this.state.isLoading) {
|
184
|
|
- return null;
|
185
|
|
- } else if (this.state.showIntro || this.state.showUpdate || this.state.showAprilFools) {
|
186
|
|
- return <CustomIntroSlider
|
187
|
|
- onDone={this.onIntroDone}
|
188
|
|
- isUpdate={this.state.showUpdate && !this.state.showIntro}
|
189
|
|
- isAprilFools={this.state.showAprilFools && !this.state.showIntro}
|
190
|
|
- />;
|
191
|
|
- } else {
|
192
|
|
- return (
|
193
|
|
- <PaperProvider theme={this.state.currentTheme}>
|
194
|
|
- <OverflowMenuProvider>
|
195
|
|
- <View style={{backgroundColor: ThemeManager.getCurrentTheme().colors.background, flex: 1}}>
|
196
|
|
- <SafeAreaView style={{flex: 1}}>
|
197
|
|
- <NavigationContainer theme={this.state.currentTheme} ref={this.navigatorRef}>
|
198
|
|
- <MainNavigator
|
199
|
|
- defaultHomeRoute={this.defaultHomeRoute}
|
200
|
|
- defaultHomeData={this.defaultHomeData}
|
201
|
|
- />
|
202
|
|
- </NavigationContainer>
|
203
|
|
- </SafeAreaView>
|
204
|
|
- </View>
|
205
|
|
- </OverflowMenuProvider>
|
206
|
|
- </PaperProvider>
|
207
|
|
- );
|
208
|
|
- }
|
|
179
|
+ if (state.showIntro || state.showUpdate || state.showAprilFools) {
|
|
180
|
+ return (
|
|
181
|
+ <CustomIntroSlider
|
|
182
|
+ onDone={this.onIntroDone}
|
|
183
|
+ isUpdate={state.showUpdate && !state.showIntro}
|
|
184
|
+ isAprilFools={state.showAprilFools && !state.showIntro}
|
|
185
|
+ />
|
|
186
|
+ );
|
209
|
187
|
}
|
|
188
|
+ return (
|
|
189
|
+ <PaperProvider theme={state.currentTheme}>
|
|
190
|
+ <OverflowMenuProvider>
|
|
191
|
+ <View
|
|
192
|
+ style={{
|
|
193
|
+ backgroundColor: ThemeManager.getCurrentTheme().colors.background,
|
|
194
|
+ flex: 1,
|
|
195
|
+ }}>
|
|
196
|
+ <SafeAreaView style={{flex: 1}}>
|
|
197
|
+ <NavigationContainer
|
|
198
|
+ theme={state.currentTheme}
|
|
199
|
+ ref={this.navigatorRef}>
|
|
200
|
+ <MainNavigator
|
|
201
|
+ defaultHomeRoute={this.defaultHomeRoute}
|
|
202
|
+ defaultHomeData={this.defaultHomeData}
|
|
203
|
+ />
|
|
204
|
+ </NavigationContainer>
|
|
205
|
+ </SafeAreaView>
|
|
206
|
+ </View>
|
|
207
|
+ </OverflowMenuProvider>
|
|
208
|
+ </PaperProvider>
|
|
209
|
+ );
|
|
210
|
+ }
|
210
|
211
|
}
|