|
1 | 1 | // An All Components Screen is a great way to dev and quick-test components |
2 | 2 | import React, {PropTypes} from 'react' |
3 | | -import { View, ScrollView, Text, Image } from 'react-native' |
| 3 | +import { View, ScrollView, Text, Image, NetInfo } from 'react-native' |
4 | 4 | import DeviceInfo from 'react-native-device-info' |
5 | 5 | import { Metrics, Images } from '../Themes' |
6 | 6 |
|
@@ -35,6 +35,56 @@ export default class DeviceInfoScreen extends React.Component { |
35 | 35 | static propTypes = { |
36 | 36 | } |
37 | 37 |
|
| 38 | + constructor (props) { |
| 39 | + super(props) |
| 40 | + |
| 41 | + this.state = { |
| 42 | + isConnected: false, |
| 43 | + connectionInfo: null, |
| 44 | + connectionInfoHistory: [] |
| 45 | + } |
| 46 | + |
| 47 | + this.setConnected = this.setConnected.bind(this) |
| 48 | + this.setConnectionInfo = this.setConnectionInfo.bind(this) |
| 49 | + this.updateConnectionInfoHistory = this.updateConnectionInfoHistory.bind(this) |
| 50 | + } |
| 51 | + |
| 52 | + componentDidMount () { |
| 53 | + NetInfo.isConnected.addEventListener('change', this.setConnected) |
| 54 | + NetInfo.isConnected.fetch().done(this.setConnected) |
| 55 | + NetInfo.addEventListener('change', this.setConnectionInfo) |
| 56 | + NetInfo.fetch().done(this.setConnectionInfo) |
| 57 | + NetInfo.addEventListener('change', this.updateConnectionInfoHistory) |
| 58 | + } |
| 59 | + |
| 60 | + componentWillUnmount () { |
| 61 | + NetInfo.isConnected.removeEventListener('change', this.setConnected) |
| 62 | + NetInfo.removeEventListener('change', this.setConnectionInfo) |
| 63 | + NetInfo.removeEventListener('change', this.updateConnectionInfoHistory) |
| 64 | + } |
| 65 | + |
| 66 | + setConnected (isConnected) { |
| 67 | + this.setState({isConnected}) |
| 68 | + } |
| 69 | + |
| 70 | + setConnectionInfo (connectionInfo) { |
| 71 | + this.setState({connectionInfo}) |
| 72 | + } |
| 73 | + |
| 74 | + updateConnectionInfoHistory (connectionInfo) { |
| 75 | + const connectionInfoHistory = this.state.connectionInfoHistory.slice() |
| 76 | + connectionInfoHistory.push(connectionInfo) |
| 77 | + this.setState({connectionInfoHistory}) |
| 78 | + } |
| 79 | + |
| 80 | + netInfo () { |
| 81 | + return ([ |
| 82 | + {title: 'Connection', info: (this.state.isConnected ? 'Online' : 'Offline')}, |
| 83 | + {title: 'Connection Info', info: this.state.connectionInfo}, |
| 84 | + {title: 'Connection Info History', info: JSON.stringify(this.state.connectionInfoHistory)} |
| 85 | + ]) |
| 86 | + } |
| 87 | + |
38 | 88 | renderCard (cardTitle, rowData) { |
39 | 89 | return ( |
40 | 90 | <View style={styles.cardContainer}> |
@@ -73,6 +123,7 @@ export default class DeviceInfoScreen extends React.Component { |
73 | 123 | {this.renderCard('Device Hardware', HARDWARE_DATA)} |
74 | 124 | {this.renderCard('Device OS', OS_DATA)} |
75 | 125 | {this.renderCard('App Info', APP_DATA)} |
| 126 | + {this.renderCard('Net Info', this.netInfo())} |
76 | 127 | </ScrollView> |
77 | 128 | </View> |
78 | 129 | ) |
|
0 commit comments