-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
75 lines (65 loc) · 2.07 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
import React, { useContext, useState, useEffect } from 'react';
import { ActivityIndicator, View, StyleSheet } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import auth from '@react-native-firebase/auth';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { Provider, useDispatch } from 'react-redux';
import { store } from './src/store';
import { addSecretPhrase } from './src/store/slices/mainSlice';
import appTheme from './src/shared/appTheme';
import { AuthContext, AuthProvider } from './src/context/AuthProvider';
import { AuthNavigation, DrawerNavigation } from './src/navigation/NavigationStack';
import { COLORS } from './src/shared/constants';
const App = () => {
const { user, setUser } = useContext(AuthContext);
const [initializing, setInitializing] = useState<boolean>(true);
const [isAppLoading, setIsAppLoading] = useState<boolean>(true);
const dispatch = useDispatch();
useEffect(() => {
getSecretPhrase();
}, []);
useEffect(() => {
const subscriber = auth().onAuthStateChanged(onAuthStateChanged);
return subscriber;
}, []);
async function getSecretPhrase() {
const secretPhrase = await AsyncStorage.getItem('secretPhrase');
dispatch(addSecretPhrase(secretPhrase || ''));
setIsAppLoading(false);
}
function onAuthStateChanged(user: any) {
console.log('user:', user);
setUser(user);
if (initializing) setInitializing(false);
}
if (initializing) return null;
return (
<NavigationContainer theme={appTheme}>
{isAppLoading ?
(
<View style={styles.loaderContainer}>
<ActivityIndicator color={COLORS.darkblue} />
</View>
)
: user ? (
<DrawerNavigation />
) : (
<AuthNavigation />
)}
</NavigationContainer>
);
};
const styles = StyleSheet.create({
loaderContainer: {
flex: 1,
justifyContent: 'center',
alignItems: 'center'
}
});
export default () => (
<AuthProvider>
<Provider store={store}>
<App />
</Provider>
</AuthProvider>
);