-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathApp.js
166 lines (146 loc) · 4.2 KB
/
App.js
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { Animated, Image, StyleSheet, View } from 'react-native';
import { Asset } from 'expo-asset';
import * as Font from 'expo-font';
import Constants from 'expo-constants';
import * as SplashScreen from 'expo-splash-screen';
import {
Entypo,
Feather,
FontAwesome,
Ionicons,
MaterialCommunityIcons,
MaterialIcons,
SimpleLineIcons,
} from '@expo/vector-icons';
import { Montserrat_500Medium } from '@expo-google-fonts/montserrat';
import * as Sentry from 'sentry-expo';
import RootContainer from './containers/rootContainer';
import SettingsManager from './utils/SettingsManager';
import DataManager from './utils/DataManager';
if (Constants.expoConfig.extra.sentryDSN) {
Sentry.init({
dsn: Constants.expoConfig.extra.sentryDSN,
enableInExpoDevelopment: false,
debug: false, // Sentry will try to print out useful debugging information if something goes wrong with sending an event. Set this to `false` in production.
});
} else {
console.warn('No Sentry URL provided.');
}
// Keep the splash screen visible while we fetch resources
SplashScreen.preventAutoHideAsync();
function AnimatedAppLoader({ children }) {
const [appIsReady, setAppIsReady] = useState(false);
const imageSrc = require('./assets/icons/splash.png');
useEffect(() => {
async function prepare() {
try {
await Font.loadAsync({ Montserrat_500Medium });
const imageAssets = cacheImages([require('./assets/icons/app.png')]);
const fontAssets = cacheFonts([
FontAwesome.font,
Feather.font,
Ionicons.font,
MaterialCommunityIcons.font,
MaterialIcons.font,
SimpleLineIcons.font,
Entypo.font,
]);
await DataManager.loadData();
await SettingsManager.loadSettings();
await Promise.all([...imageAssets, ...fontAssets]);
} catch (e) {
console.warn(e);
} finally {
// Tell the application to render
setAppIsReady(true);
}
}
prepare();
}, []);
const onLayoutRootView = useCallback(async () => {
if (appIsReady) {
// This tells the splash screen to hide immediately! If we call this after
// `setAppIsReady`, then we may see a blank screen while the app is
// loading its initial state and rendering its first pixels. So instead,
// we hide the splash screen once we know the root view has already
// performed layout.
setTimeout(() => SplashScreen.hideAsync());
}
}, [appIsReady]);
if (!appIsReady) {
return null;
}
return <AnimatedSplashScreen image={imageSrc}>{children}</AnimatedSplashScreen>;
}
function AnimatedSplashScreen({ children, image }) {
const animation = useMemo(() => new Animated.Value(1), []);
const [isAppReady, setAppReady] = useState(false);
const [isSplashAnimationComplete, setAnimationComplete] = useState(false);
useEffect(() => {
if (isAppReady) {
Animated.timing(animation, {
toValue: 0,
duration: 1000,
useNativeDriver: true,
}).start(() => setAnimationComplete(true));
}
}, [isAppReady]);
const onImageLoaded = useCallback(async () => {
try {
await SplashScreen.hideAsync();
} catch (e) {
console.log('err', e);
// handle errors
} finally {
setAppReady(true);
}
}, []);
return (
<View style={{ flex: 1 }}>
{isAppReady && children}
{!isSplashAnimationComplete && (
<Animated.View
pointerEvents="none"
style={[
StyleSheet.absoluteFill,
{
backgroundColor: Constants.manifest.splash.backgroundColor,
opacity: animation,
},
]}>
<Animated.Image
style={{
width: '100%',
height: '100%',
resizeMode: Constants.manifest.splash.resizeMode || 'contain',
}}
source={image}
onError={(e) => console.log(e.nativeEvent.error)}
onLoadEnd={onImageLoaded}
fadeDuration={0}
/>
</Animated.View>
)}
</View>
);
}
function cacheFonts(fonts) {
return fonts.map((font) => Font.loadAsync(font));
}
function cacheImages(images) {
return images.map((image) => {
if (typeof image === 'string') {
return Image.prefetch(image);
} else {
return Asset.fromModule(image).downloadAsync();
}
});
}
export default function App() {
return (
<AnimatedAppLoader>
<RootContainer />
</AnimatedAppLoader>
);
}