Skip to content

Commit 7efae09

Browse files
authored
Merge pull request #272 from infinitered/feature/netinfo
Add NetInfo to DeviceInfoScreen
2 parents cf2b3cd + 9c81c83 commit 7efae09

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

ignite-base/App/Containers/DeviceInfoScreen.js

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// An All Components Screen is a great way to dev and quick-test components
22
import React, {PropTypes} from 'react'
3-
import { View, ScrollView, Text, Image } from 'react-native'
3+
import { View, ScrollView, Text, Image, NetInfo } from 'react-native'
44
import DeviceInfo from 'react-native-device-info'
55
import { Metrics, Images } from '../Themes'
66

@@ -35,6 +35,56 @@ export default class DeviceInfoScreen extends React.Component {
3535
static propTypes = {
3636
}
3737

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+
3888
renderCard (cardTitle, rowData) {
3989
return (
4090
<View style={styles.cardContainer}>
@@ -73,6 +123,7 @@ export default class DeviceInfoScreen extends React.Component {
73123
{this.renderCard('Device Hardware', HARDWARE_DATA)}
74124
{this.renderCard('Device OS', OS_DATA)}
75125
{this.renderCard('App Info', APP_DATA)}
126+
{this.renderCard('Net Info', this.netInfo())}
76127
</ScrollView>
77128
</View>
78129
)

ignite-base/android/app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
<uses-permission android:name="${applicationId}.permission.C2D_MESSAGE" />
88
<uses-permission android:name="android.permission.WAKE_LOCK" />
99
<uses-permission android:name="android.permission.VIBRATE" />
10-
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
10+
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
1111
<uses-permission android:name="android.permission.INTERNET" />
12-
<uses-permission android:name="android.permission.BLUETOOTH"/>
12+
<uses-permission android:name="android.permission.BLUETOOTH" />
13+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
1314

1415
<application
1516
android:name=".MainApplication"

0 commit comments

Comments
 (0)