-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
70 lines (60 loc) · 1.94 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
import React, { FC, useEffect, useState, PropsWithChildren } from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { Provider } from 'react-redux';
import Toast from 'react-native-toast-message';
import { StatusBar } from 'expo-status-bar';
import { useFonts } from 'expo-font';
import { preventAutoHideAsync, hideAsync } from 'expo-splash-screen';
import { ThemeProvider } from 'styled-components/native';
import Navigator from './src';
import { Loading, Error } from '@screens';
import { initDbDrafts } from '@utils';
import { THEME } from '@constants';
import { store } from '@store';
const ThemeContainer: FC<PropsWithChildren> = ({ children }) => (
<ThemeProvider theme={THEME}>{children}</ThemeProvider>
);
preventAutoHideAsync();
const App: FC = () => {
const [isSetupError, setIsSetupError] = useState(false);
const [databaseInitialized, setDatabaseInitialized] = useState(false);
const [fontsLoaded, fontsError] = useFonts({
'Roboto-Regular': require('./assets/fonts/Roboto/Roboto-Regular.ttf'),
});
const initDrafts = async () => {
try {
await initDbDrafts();
} catch (error) {
setIsSetupError(true);
console.error(error, 'App setup');
} finally {
setDatabaseInitialized(true); // anyway, db is initialized, so we show either app screen or error screen
}
};
useEffect(() => {
initDrafts();
}, []);
useEffect(() => {
if (fontsLoaded && databaseInitialized) hideAsync();
}, [fontsLoaded, databaseInitialized]);
if (fontsError || isSetupError)
return (
<ThemeContainer>
<Error />
</ThemeContainer>
);
return (
<>
<StatusBar style="dark" animated />
<Provider store={store}>
<NavigationContainer>
<ThemeContainer>
{fontsLoaded ? <Navigator /> : <Loading />}
</ThemeContainer>
</NavigationContainer>
</Provider>
<Toast />
</>
);
};
export default App;