-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppSplashAnimated.js
More file actions
102 lines (91 loc) · 2.66 KB
/
AppSplashAnimated.js
File metadata and controls
102 lines (91 loc) · 2.66 KB
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
import { AppLoading } from "expo"
import { Asset } from "expo-asset"
import Constants from "expo-constants"
import React from "react"
import { Animated, Button, StyleSheet, Text, View } from "react-native"
import * as SplashScreen from "expo-splash-screen"
// import * as Updates from "expo-updates";
import {DARK_GRAY} from './styles/colors'
// Instruct SplashScreen not to hide yet, we want to do this manually
SplashScreen.preventAutoHideAsync().catch(() => {});
export default function AppSplashAnimated({children}) {
return (
<AnimatedAppLoader image={require('./assets/splash-2.png')}>
{children}
</AnimatedAppLoader>
);
}
function AnimatedAppLoader({ children, image }) {
const [isSplashReady, setSplashReady] = React.useState(false);
const startAsync = React.useMemo(
// If you use a local image with require(...), use `Asset.fromModule`
() => () => Asset.fromModule(image).downloadAsync(),
[image]
);
const onFinish = React.useMemo(() => setSplashReady(true), []);
if (!isSplashReady) {
return (
<AppLoading
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: 400,
useNativeDriver: true,
}).start(() => setAnimationComplete(true));
}
}, [isAppReady]);
const onImageLoaded = React.useMemo(() => async () => {
SplashScreen.hideAsync();
try {
// Load stuff
await Promise.all([]);
} catch (e) {
// handle errors
} finally {
setAppReady(true);
}
});
return (
<View style={{ flex: 1 }}>
{isAppReady && children}
{!isSplashAnimationComplete && (
<Animated.View
pointerEvents="none"
style={[
StyleSheet.absoluteFill,
{
backgroundColor: DARK_GRAY,
opacity: animation,
},
]}
>
<Animated.Image
style={{
width: "100%",
height: "100%",
resizeMode: Constants.manifest.splash.resizeMode || "contain",
}}
source={image}
onLoadEnd={onImageLoaded}
fadeDuration={0}
/>
</Animated.View>
)}
</View>
);
}