-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
108 lines (100 loc) · 3.04 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
import React, {useEffect, useState, useRef} from 'react';
import { StyleSheet, Animated, Text, SafeAreaView} from 'react-native';
import PlaceOrder from './src/screens/PlaceOrder';
import NetInfo from "@react-native-community/netinfo";
import LottieSplashScreen from "react-native-lottie-splash-screen";
import { moderateScale } from 'react-native-size-matters';
import colors from './src/common/colors';
import strings from './src/common/strings';
import ErrorBoundaryComponent from './src/common/ErrorBoundary/ErrorBoundaryComponent';
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: colors.background
},
banner: {
position: 'absolute',
top: 0,
left: 0,
right: 0,
height: moderateScale(50),
alignItems: 'center',
justifyContent: 'center',
marginBottom: moderateScale(20),
zIndex: 2, //to make the banner fixed at the top and the scroll view content go behind it when scrolled
},
bannerText: {
color: colors.defaultLight,
fontSize: moderateScale(16),
fontFamily: 'Poppins-SemiBold',
},
});
export default App = () => {
const [banner] = useState(new Animated.Value(0));
const [isOffline, setOfflineStatus] = useState(false);
const isInitialMount = useRef(true);
useEffect(() => {
const removeNetInfoSubscription = NetInfo.addEventListener((state) => {
const offline = !(state.isConnected && state.isInternetReachable);
setOfflineStatus(offline);
});
return () => removeNetInfoSubscription();
}, []);
// To hide Lottie splash screen
useEffect(() => {
LottieSplashScreen.hide();
}, []);
// To animate network banner
useEffect(() => {
if (isInitialMount?.current) {
isInitialMount.current = false;
if(isOffline) {
Animated.timing(banner, {
toValue: 1,
duration: 1000,
useNativeDriver: true,
}).start()
}
} else {
if(isOffline) {
Animated.timing(banner, {
toValue: 1,
duration: 1000,
useNativeDriver: true,
}).start()
} else {
Animated.timing(banner, {
toValue: 1,
duration: 1000,
useNativeDriver: true,
}).start(() => {
Animated.timing(banner, {
toValue: 0,
duration: 1000,
useNativeDriver: true,
}).start()
})
}
}
}, [isOffline]);
const bannerStyle = {
transform: [
{
translateY: banner.interpolate({
inputRange: [0, 1],
outputRange: [moderateScale(-50), 0],
}),
}
]
};
return (
<ErrorBoundaryComponent>
<SafeAreaView style = {styles.container}>
<Animated.View style={[styles.banner, bannerStyle, { backgroundColor: isOffline ? colors.networkBanner.offline : colors.networkBanner.online}]}>
<Text style={styles.bannerText}>{isOffline ? strings.App.offline : strings.App.online}</Text>
</Animated.View>
<PlaceOrder offline = {isOffline}/>
</SafeAreaView>
</ErrorBoundaryComponent>
);
};