2015-12-08 17:30:07 +00:00
|
|
|
/**
|
2017-05-06 03:50:47 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
2016-07-12 12:51:57 +00:00
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-07-12 12:51:57 +00:00
|
|
|
*
|
2018-05-11 20:32:37 +00:00
|
|
|
* @format
|
2015-12-08 17:30:07 +00:00
|
|
|
* @flow
|
|
|
|
*/
|
2018-05-11 20:32:37 +00:00
|
|
|
|
2015-12-08 17:30:07 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-04-09 03:36:40 +00:00
|
|
|
const React = require('react');
|
|
|
|
const ReactNative = require('react-native');
|
2018-05-11 20:32:37 +00:00
|
|
|
const {NetInfo, Text, View, TouchableWithoutFeedback} = ReactNative;
|
2015-12-08 17:30:07 +00:00
|
|
|
|
2017-08-18 01:36:54 +00:00
|
|
|
class ConnectionInfoSubscription extends React.Component<{}, $FlowFixMeState> {
|
2016-07-26 08:00:02 +00:00
|
|
|
state = {
|
|
|
|
connectionInfoHistory: [],
|
|
|
|
};
|
|
|
|
|
|
|
|
componentDidMount() {
|
2018-05-11 20:32:37 +00:00
|
|
|
NetInfo.addEventListener('change', this._handleConnectionInfoChange);
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2018-05-11 20:32:37 +00:00
|
|
|
NetInfo.removeEventListener('change', this._handleConnectionInfoChange);
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
|
|
|
|
2018-05-11 20:32:37 +00:00
|
|
|
_handleConnectionInfoChange = connectionInfo => {
|
2015-12-08 17:30:07 +00:00
|
|
|
const connectionInfoHistory = this.state.connectionInfoHistory.slice();
|
|
|
|
connectionInfoHistory.push(connectionInfo);
|
|
|
|
this.setState({
|
|
|
|
connectionInfoHistory,
|
|
|
|
});
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
|
|
|
|
2015-12-08 17:30:07 +00:00
|
|
|
render() {
|
|
|
|
return (
|
2018-05-11 20:32:37 +00:00
|
|
|
<View>
|
|
|
|
<Text>{JSON.stringify(this.state.connectionInfoHistory)}</Text>
|
|
|
|
</View>
|
2015-12-08 17:30:07 +00:00
|
|
|
);
|
|
|
|
}
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
2015-12-08 17:30:07 +00:00
|
|
|
|
2017-08-18 01:36:54 +00:00
|
|
|
class ConnectionInfoCurrent extends React.Component<{}, $FlowFixMeState> {
|
2016-07-26 08:00:02 +00:00
|
|
|
state = {
|
|
|
|
connectionInfo: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
componentDidMount() {
|
2018-05-11 20:32:37 +00:00
|
|
|
NetInfo.addEventListener('change', this._handleConnectionInfoChange);
|
|
|
|
NetInfo.fetch().done(connectionInfo => {
|
|
|
|
this.setState({connectionInfo});
|
|
|
|
});
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2018-05-11 20:32:37 +00:00
|
|
|
NetInfo.removeEventListener('change', this._handleConnectionInfoChange);
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
|
|
|
|
2018-05-11 20:32:37 +00:00
|
|
|
_handleConnectionInfoChange = connectionInfo => {
|
2015-12-08 17:30:07 +00:00
|
|
|
this.setState({
|
|
|
|
connectionInfo,
|
|
|
|
});
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
|
|
|
|
2015-12-08 17:30:07 +00:00
|
|
|
render() {
|
|
|
|
return (
|
2018-05-11 20:32:37 +00:00
|
|
|
<View>
|
|
|
|
<Text>{this.state.connectionInfo}</Text>
|
|
|
|
</View>
|
2015-12-08 17:30:07 +00:00
|
|
|
);
|
|
|
|
}
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
2015-12-08 17:30:07 +00:00
|
|
|
|
2017-08-18 01:36:54 +00:00
|
|
|
class IsConnected extends React.Component<{}, $FlowFixMeState> {
|
2016-07-26 08:00:02 +00:00
|
|
|
state = {
|
|
|
|
isConnected: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
componentDidMount() {
|
2015-12-08 17:30:07 +00:00
|
|
|
NetInfo.isConnected.addEventListener(
|
2018-05-11 20:32:37 +00:00
|
|
|
'change',
|
|
|
|
this._handleConnectivityChange,
|
2015-12-08 17:30:07 +00:00
|
|
|
);
|
2018-05-11 20:32:37 +00:00
|
|
|
NetInfo.isConnected.fetch().done(isConnected => {
|
|
|
|
this.setState({isConnected});
|
|
|
|
});
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2015-12-08 17:30:07 +00:00
|
|
|
NetInfo.isConnected.removeEventListener(
|
2018-05-11 20:32:37 +00:00
|
|
|
'change',
|
|
|
|
this._handleConnectivityChange,
|
2015-12-08 17:30:07 +00:00
|
|
|
);
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
|
|
|
|
2018-05-11 20:32:37 +00:00
|
|
|
_handleConnectivityChange = isConnected => {
|
2015-12-08 17:30:07 +00:00
|
|
|
this.setState({
|
|
|
|
isConnected,
|
|
|
|
});
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
|
|
|
|
2015-12-08 17:30:07 +00:00
|
|
|
render() {
|
|
|
|
return (
|
2018-05-11 20:32:37 +00:00
|
|
|
<View>
|
|
|
|
<Text>{this.state.isConnected ? 'Online' : 'Offline'}</Text>
|
|
|
|
</View>
|
2015-12-08 17:30:07 +00:00
|
|
|
);
|
|
|
|
}
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
2015-12-08 17:30:07 +00:00
|
|
|
|
2017-08-18 01:36:54 +00:00
|
|
|
class IsConnectionExpensive extends React.Component<{}, $FlowFixMeState> {
|
2016-07-26 08:00:02 +00:00
|
|
|
state = {
|
2018-05-11 20:32:37 +00:00
|
|
|
isConnectionExpensive: (null: ?boolean),
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
_checkIfExpensive = () => {
|
2018-05-11 20:32:37 +00:00
|
|
|
NetInfo.isConnectionExpensive().then(isConnectionExpensive => {
|
|
|
|
this.setState({isConnectionExpensive});
|
|
|
|
});
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
|
|
|
|
2015-12-08 17:30:07 +00:00
|
|
|
render() {
|
|
|
|
return (
|
2018-05-11 20:32:37 +00:00
|
|
|
<View>
|
|
|
|
<TouchableWithoutFeedback onPress={this._checkIfExpensive}>
|
|
|
|
<View>
|
|
|
|
<Text>
|
|
|
|
Click to see if connection is expensive:
|
|
|
|
{this.state.isConnectionExpensive === true
|
|
|
|
? 'Expensive'
|
|
|
|
: this.state.isConnectionExpensive === false
|
|
|
|
? 'Not expensive'
|
|
|
|
: 'Unknown'}
|
|
|
|
</Text>
|
|
|
|
</View>
|
|
|
|
</TouchableWithoutFeedback>
|
|
|
|
</View>
|
2015-12-08 17:30:07 +00:00
|
|
|
);
|
|
|
|
}
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
2015-12-08 17:30:07 +00:00
|
|
|
|
|
|
|
exports.title = 'NetInfo';
|
|
|
|
exports.description = 'Monitor network status';
|
|
|
|
exports.examples = [
|
|
|
|
{
|
|
|
|
title: 'NetInfo.isConnected',
|
|
|
|
description: 'Asynchronously load and observe connectivity',
|
2018-05-11 20:32:37 +00:00
|
|
|
render(): React.Element<any> {
|
|
|
|
return <IsConnected />;
|
|
|
|
},
|
2015-12-08 17:30:07 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'NetInfo.update',
|
|
|
|
description: 'Asynchronously load and observe connectionInfo',
|
2018-05-11 20:32:37 +00:00
|
|
|
render(): React.Element<any> {
|
|
|
|
return <ConnectionInfoCurrent />;
|
|
|
|
},
|
2015-12-08 17:30:07 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'NetInfo.updateHistory',
|
|
|
|
description: 'Observed updates to connectionInfo',
|
2018-05-11 20:32:37 +00:00
|
|
|
render(): React.Element<any> {
|
|
|
|
return <ConnectionInfoSubscription />;
|
|
|
|
},
|
2015-12-08 17:30:07 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
platform: 'android',
|
|
|
|
title: 'NetInfo.isConnectionExpensive (Android)',
|
2015-12-09 22:51:43 +00:00
|
|
|
description: 'Asynchronously check isConnectionExpensive',
|
2018-05-11 20:32:37 +00:00
|
|
|
render(): React.Element<any> {
|
|
|
|
return <IsConnectionExpensive />;
|
|
|
|
},
|
2015-12-08 17:30:07 +00:00
|
|
|
},
|
|
|
|
];
|