-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.jsx
133 lines (119 loc) · 3.34 KB
/
App.jsx
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
import 'react-native-gesture-handler';
import AppLoading from 'expo-app-loading';
import { Asset } from 'expo-asset';
import Constants from 'expo-constants';
import * as SplashScreen from 'expo-splash-screen';
import { Animated, StyleSheet, View } from 'react-native';
import React from 'react';
import { useFonts } from 'expo-font';
import { AppearanceProvider } from 'react-native-appearance';
import Main from './src/Main';
SplashScreen.preventAutoHideAsync().catch(() => {
// reloading the app might trigger some race conditions, ignore them.
});
const customFonts = {
'Montserrat-Bold': require('./assets/fonts/Montserrat-Bold.ttf'),
'Montserrat-Medium': require('./assets/fonts/Montserrat-Medium.ttf'),
'Montserrat-Regular': require('./assets/fonts/Montserrat-Regular.ttf'),
'Montserrat-Thin': require('./assets/fonts/Montserrat-Thin.ttf'),
};
export default function App() {
return (
<AnimatedAppLoader image={require('./assets/splash.png')}>
<MainScreen />
</AnimatedAppLoader>
);
}
function MainScreen() {
return (
<AppearanceProvider>
<Main />
</AppearanceProvider>
);
}
function AnimatedAppLoader({ children, image }) {
const [isSplashReady, setSplashReady] = React.useState(false);
const [fontsLoaded] = useFonts(customFonts);
const startAsync = React.useMemo(
() => () => Asset.fromModule(image).downloadAsync(),
[image],
);
const onFinish = React.useMemo(() => setSplashReady(true), []);
if (!isSplashReady && !fontsLoaded) {
return (
<AppLoading
autoHideSplash={false}
startAsync={startAsync}
onError={console.error}
onFinish={onFinish}
/>
);
}
return (
<AnimatedSplashScreen image={image}>
{children}
</AnimatedSplashScreen>
);
}
function AnimatedSplashScreen({ children, image }) {
const animation = React.useMemo(() => new Animated.Value(1), []);
const [isAppReady, setAppReady] = React.useState(false);
const [
isSplashAnimationComplete,
setAnimationComplete,
] = React.useState(false);
React.useEffect(() => {
if (isAppReady) {
Animated.timing(animation, {
toValue: 0,
duration: 500,
useNativeDriver: true,
}).start(() => setAnimationComplete(true));
}
}, [isAppReady]);
const onImageLoaded = React.useMemo(() => async () => {
try {
await SplashScreen.hideAsync();
await Promise.all([]);
} catch (e) {
console.log(e);
} 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',
transform: [
{
scale: animation,
},
],
}}
source={image}
onLoadEnd={onImageLoaded}
fadeDuration={0}
/>
</Animated.View>
)}
</View>
);
}