-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
148 lines (130 loc) · 3.59 KB
/
App.js
File metadata and controls
148 lines (130 loc) · 3.59 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
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
import { GestureHandlerRootView } from 'react-native-gesture-handler';
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 RootContainer from './src/shared/navigation/rootContainer';
import { SettingsManager } from './src/shared/services/AppCore'
import { DataManager } from './src/shared/services/DataService';
import { SafeAreaProvider } from 'react-native-safe-area-context';
SplashScreen.preventAutoHideAsync();
function AnimatedAppLoader({ children }) {
const [appIsReady, setAppIsReady] = useState(false);
const imageSrc = require('./assets/icons/icon.png');
useEffect(() => {
async function prepare() {
try {
await Font.loadAsync({ Montserrat_500Medium });
const imageAssets = cacheImages([require('./assets/icons/logo.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 {
setAppIsReady(true);
}
}
prepare();
}, []);
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);
} finally {
setAppReady(true);
}
}, []);
const splashConfig = Constants.expoConfig?.splash || {};
return (
<View style={{ flex: 1 }}>
{isAppReady && children}
{!isSplashAnimationComplete && (
<Animated.View
pointerEvents="none"
style={[
StyleSheet.absoluteFill,
{
backgroundColor: splashConfig.backgroundColor || '#ffffff',
opacity: animation,
},
]}>
<Animated.Image
style={{
width: '100%',
height: '100%',
resizeMode: splashConfig.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 (
<SafeAreaProvider>
<GestureHandlerRootView style={{ flex: 1 }}>
<AnimatedAppLoader>
<RootContainer />
</AnimatedAppLoader>
</GestureHandlerRootView>
</SafeAreaProvider>
);
}