This repository has been archived by the owner on Jun 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
134 lines (110 loc) · 3.98 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import React, { FunctionComponent, useEffect, useRef, useState } from 'react';
import { AppState, AppStateStatus, Platform, UIManager, YellowBox } from 'react-native';
import { Provider } from 'react-redux';
import { momentWithDeviceLocale, NavigationService } from './src/utilities';
import { AppContainer } from './src/navigators/AppContainer';
import configureStore from './src/store/configureStore';
import { PersistGate } from 'redux-persist/integration/react';
import { setCurrentDateTimestamp, setUser } from './src/store/settings/actions';
import { configureNotifications } from './src/utilities/configureNotifications';
import SplashScreen from 'react-native-splash-screen';
import { setStatusBarStyle } from './src/utilities/statusBar';
import { enableScreens } from 'react-native-screens';
import PushNotificationIOS from '@react-native-community/push-notification-ios';
import Storybook from './storybook';
import { User } from './src/store/settings/types';
YellowBox.ignoreWarnings([
'Warning:',
'VirtualizedLists should never be nested',
'Story with id',
'`-[RCTRootView cancelTouches]`',
'Require cycle:',
'Starting Storybook v5.3.0',
]);
if (Platform.OS === 'android' && UIManager.setLayoutAnimationEnabledExperimental) {
UIManager.setLayoutAnimationEnabledExperimental(true);
}
export default Storybook;
enableScreens();
const { store, persistor } = configureStore();
configureNotifications();
const App: FunctionComponent = () => {
const appState = useRef(AppState.currentState);
const [dayChangeTimeoutId, setDayChangeTimeoutId] = useState();
const [loaded, setLoaded] = useState(false);
// function onAuthStateChanged(user: FirebaseAuthTypes.User | null) {
// let currentUser: User | undefined;
//
// if (user != null) {
// currentUser = {
// email: user.email as string,
// };
// }
//
// store.dispatch(setUser(currentUser));
// }
// useEffect(() => {
// return auth().onAuthStateChanged(onAuthStateChanged); // Unsubscribe on unmount.
// }, []);
useEffect(() => {
updateCurrentDateAndSetTimeoutForTheNextDay();
}, []);
useEffect(() => {
AppState.addEventListener('change', handleAppStateChange);
return () => AppState.removeEventListener('change', handleAppStateChange);
}, []);
useEffect(() => {
if (!loaded) return;
setStatusBarStyle('dark');
SplashScreen.hide();
const state = store.getState();
const { id, startTimestamp } = state.goals.trackedGoal;
const { hasOnboarded } = state.settings;
if (id != null && startTimestamp != null) {
NavigationService.navigate('TrackGoal', {});
}
if (!hasOnboarded) {
NavigationService.navigate('Onboarding', {});
}
}, [loaded]);
const updateCurrentDateAndSetTimeoutForTheNextDay = () => {
const currentTimestamp = Date.now();
store.dispatch(setCurrentDateTimestamp(currentTimestamp));
if (Platform.OS === 'ios') {
clearTimeout(dayChangeTimeoutId);
const msUntilTomorrow = +momentWithDeviceLocale(currentTimestamp)
.add(1, 'day')
.startOf('day')
.subtract(currentTimestamp);
setDayChangeTimeoutId(
setTimeout(() => {
updateCurrentDateAndSetTimeoutForTheNextDay();
}, msUntilTomorrow),
);
}
};
const handleAppStateChange = (nextAppState: AppStateStatus) => {
if (
appState.current.match(/inactive|background/) &&
nextAppState === 'active'
) {
updateCurrentDateAndSetTimeoutForTheNextDay();
if (Platform.OS === 'ios') {
PushNotificationIOS.removeAllDeliveredNotifications();
}
}
appState.current = nextAppState;
};
const handleFirstLoad = () => {
setLoaded(true);
};
return (
<Provider store={store}>
<PersistGate persistor={persistor}>
<AppContainer ref={navigatorRef => NavigationService.setTopLevelNavigator(navigatorRef)}
onNavigationStateChange={handleFirstLoad} />
</PersistGate>
</Provider>
);
};
// export default App;