-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
104 lines (87 loc) · 3.02 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
import React from 'react';
import { AppLoading } from 'expo';
import { Asset } from 'expo-asset';
import { StatusBar } from 'expo-status-bar';
import AsyncStorage from '@react-native-community/async-storage';
import NetInfo from '@react-native-community/netinfo';
import Routes from './src/routes';
import NetworkStatus from './src/components/NetworkStatus';
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
connectedColor: 'green',
errorColor: 'red',
firstLaunch: false,
interval: null,
isReady: false,
networkAvailable: true,
showStatus: false,
};
}
async componentDidMount() {
NetInfo.addEventListener((state) => {
if (this.state.interval) clearTimeout(this.state.interval);
this.state.interval = setTimeout(() => {
this.setState({ networkAvailable: state.isConnected });
if (!state.isConnected) {
this.setState({ showStatus: true });
} else {
setTimeout(() => {
this.setState({ showStatus: false });
}, 3000);
}
}, 3000);
});
try {
const firstLaunch = await AsyncStorage.getItem('firstLaunch');
if (!firstLaunch) {
this.setState({ firstLaunch: true });
} else {
this.setState({ firstLaunch: false });
}
} catch (error) {
this.setState({ firstLaunch: true });
}
}
async cacheResourcesAsync() {
const images = [
require('./assets/favicon.png'),
require('./assets/icon.png'),
require('./assets/logo.png'),
require('./assets/splash.png'),
];
const cacheImages = images.map((image) => Asset.fromModule(image).downloadAsync());
return Promise.all(cacheImages);
}
render() {
const { firstLaunch, isReady, networkAvailable } = this.state;
if (!isReady) {
// Add an AppLoading in case it's needed later
return (
<AppLoading
startAsync={this.cacheResourcesAsync}
onFinish={() => this.setState({ isReady: true })}
/>
);
}
return (
<>
<StatusBar
style="light"
translucent
/>
<Routes
isFirstLaunch={firstLaunch}
networkAvailable={networkAvailable}
/>
{this.state.showStatus && (
<NetworkStatus
color={networkAvailable ? this.state.connectedColor : this.state.errorColor}
text={networkAvailable ? 'Connection established.' : 'No connection.'}
/>
)}
</>
);
}
}