-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.tsx
92 lines (86 loc) · 2.79 KB
/
App.tsx
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
import React, {useEffect, createContext, useState} from 'react';
import SplashScreen from 'react-native-splash-screen';
import {NavigationContainer} from '@react-navigation/native';
import BottomNav from './src/navigation/BottomNav';
import PreloginStack from './src/navigation/simple/Preloginstack';
import {Provider} from 'react-native-paper';
import NetInfo from '@react-native-community/netinfo';
import {Toast} from './src/utils/Toast.util';
import NetworkErrorScreen from './src/screens/NetworKErrorScreen';
export const LoginContext = createContext();
const App = () => {
const [isLogin, setIsLogin] = useState(false);
const [token, setToken] = useState(null);
const [loading, setLoading] = useState(true);
const [farmId, setFarmId] = useState('');
const [reload, setReload] = useState(false);
const [homereload, setHomeReload] = useState(false);
const [networkErrorScreen, setNetworkErrorScreen] = useState(false);
const [netWorkToastTrigger, setNetworkToastTrigger] = useState({
error: false,
success: false,
});
const [farmlatlng, setfarmlatlng] = useState({
latitude: 0,
longitude: 0,
altitude: 0,
});
useEffect(() => {
if (netWorkToastTrigger.success) {
Toast('You are online :)');
}
if (netWorkToastTrigger.error) {
Toast('You are offline :(');
}
setInterval(() => {
NetInfo.fetch().then(state => {
// console.log('Connection type', state.type);
// console.log('Is connected?', state.isConnected);
if (state.isConnected) {
setNetworkErrorScreen(false);
setNetworkToastTrigger({error: false, success: true});
} else {
setNetworkErrorScreen(true);
setNetworkToastTrigger({error: true, success: false});
}
});
}, 10000);
}, [setNetworkErrorScreen]);
useEffect(() => {
const hideSplashScreen = () => {
SplashScreen.hide();
};
const timeout = setTimeout(hideSplashScreen, 1000);
return () => clearTimeout(timeout);
}, []);
if (networkErrorScreen) {
return <NetworkErrorScreen />;
} else {
return (
<LoginContext.Provider
value={{
isLogin: isLogin,
setIsLogin: setIsLogin,
token: token,
setToken: setToken,
loading: loading,
setLoading: setLoading,
farmId: farmId,
setFarmId: setFarmId,
farmlatlng: farmlatlng,
setfarmlatlng: setfarmlatlng,
reload: reload,
setReload: setReload,
homereload: homereload,
setHomeReload: setHomeReload,
}}>
<NavigationContainer>
<Provider>
{isLogin && token ? <BottomNav /> : <PreloginStack />}
</Provider>
</NavigationContainer>
</LoginContext.Provider>
);
}
};
export default App;