Skip to content

Commit

Permalink
Reverted commit D3339945
Browse files Browse the repository at this point in the history
Summary: Updated networking and geolocation to use the new events system.

Reviewed By: javache

Differential Revision: D3339945

fbshipit-source-id: 01d307cf8a0aea3a404c87c6205132c42290abb1
  • Loading branch information
bestander authored and Facebook Github Bot 6 committed May 24, 2016
1 parent 8876eaa commit 2de0323
Show file tree
Hide file tree
Showing 27 changed files with 340 additions and 229 deletions.
32 changes: 3 additions & 29 deletions Examples/UIExplorer/AppStateExample.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
/**
* Copyright (c) 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* The examples provided by Facebook are for non-commercial testing and
* evaluation purposes only.
*
Expand All @@ -23,9 +16,9 @@
*/
'use strict';

const React = require('react');
const ReactNative = require('react-native');
const {
var React = require('react');
var ReactNative = require('react-native');
var {
AppState,
Text,
View
Expand All @@ -36,19 +29,13 @@ var AppStateSubscription = React.createClass({
return {
appState: AppState.currentState,
previousAppStates: [],
memoryWarnings: 0,
};
},
componentDidMount: function() {
AppState.addEventListener('change', this._handleAppStateChange);
AppState.addEventListener('memoryWarning', this._handleMemoryWarning);
},
componentWillUnmount: function() {
AppState.removeEventListener('change', this._handleAppStateChange);
AppState.removeEventListener('memoryWarning', this._handleMemoryWarning);
},
_handleMemoryWarning: function() {
this.setState({memoryWarnings: this.state.memoryWarnings + 1});
},
_handleAppStateChange: function(appState) {
var previousAppStates = this.state.previousAppStates.slice();
Expand All @@ -59,13 +46,6 @@ var AppStateSubscription = React.createClass({
});
},
render() {
if (this.props.showMemoryWarnings) {
return (
<View>
<Text>{this.state.memoryWarnings}</Text>
</View>
);
}
if (this.props.showCurrentOnly) {
return (
<View>
Expand Down Expand Up @@ -98,10 +78,4 @@ exports.examples = [
title: 'Previous states:',
render(): ReactElement { return <AppStateSubscription showCurrentOnly={false} />; }
},
{
platform: 'ios',
title: 'Memory Warnings',
description: 'In the IOS simulator, hit Shift+Command+M to simulate a memory warning.',
render(): ReactElement { return <AppStateSubscription showMemoryWarnings={true} />; }
},
];
99 changes: 99 additions & 0 deletions Examples/UIExplorer/AppStateIOSExample.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
* The examples provided by Facebook are for non-commercial testing and
* evaluation purposes only.
*
* Facebook reserves all rights not expressly granted.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* @providesModule AppStateIOSExample
* @flow
*/
'use strict';

var React = require('react');
var ReactNative = require('react-native');
var {
AppStateIOS,
Text,
View
} = ReactNative;

var AppStateSubscription = React.createClass({
getInitialState() {
return {
appState: AppStateIOS.currentState,
previousAppStates: [],
memoryWarnings: 0,
};
},
componentDidMount: function() {
AppStateIOS.addEventListener('change', this._handleAppStateChange);
AppStateIOS.addEventListener('memoryWarning', this._handleMemoryWarning);
},
componentWillUnmount: function() {
AppStateIOS.removeEventListener('change', this._handleAppStateChange);
AppStateIOS.removeEventListener('memoryWarning', this._handleMemoryWarning);
},
_handleMemoryWarning: function() {
this.setState({memoryWarnings: this.state.memoryWarnings + 1});
},
_handleAppStateChange: function(appState) {
var previousAppStates = this.state.previousAppStates.slice();
previousAppStates.push(this.state.appState);
this.setState({
appState,
previousAppStates,
});
},
render() {
if (this.props.showMemoryWarnings) {
return (
<View>
<Text>{this.state.memoryWarnings}</Text>
</View>
);
}
if (this.props.showCurrentOnly) {
return (
<View>
<Text>{this.state.appState}</Text>
</View>
);
}
return (
<View>
<Text>{JSON.stringify(this.state.previousAppStates)}</Text>
</View>
);
}
});

exports.title = 'AppStateIOS';
exports.description = 'iOS app background status';
exports.examples = [
{
title: 'AppStateIOS.currentState',
description: 'Can be null on app initialization',
render() { return <Text>{AppStateIOS.currentState}</Text>; }
},
{
title: 'Subscribed AppStateIOS:',
description: 'This changes according to the current state, so you can only ever see it rendered as "active"',
render(): ReactElement { return <AppStateSubscription showCurrentOnly={true} />; }
},
{
title: 'Previous states:',
render(): ReactElement { return <AppStateSubscription showCurrentOnly={false} />; }
},
{
title: 'Memory Warnings',
description: 'In the simulator, hit Shift+Command+M to simulate a memory warning.',
render(): ReactElement { return <AppStateSubscription showMemoryWarnings={true} />; }
},
];
4 changes: 4 additions & 0 deletions Examples/UIExplorer/UIExplorerList.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ const APIExamples: Array<UIExplorerExample> = [
key: 'AnExApp',
module: require('./AnimatedGratuitousApp/AnExApp'),
},
{
key: 'AppStateIOSExample',
module: require('./AppStateIOSExample'),
},
{
key: 'AppStateExample',
module: require('./AppStateExample'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ - (void)testLegacyEventsAreImmediatelyDispatched

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"

[_eventDispatcher sendDeviceEventWithName:_eventName body:_body];

#pragma clang diagnostic pop

[_bridge verify];
Expand Down
11 changes: 2 additions & 9 deletions Examples/UIExplorer/XHRExample.ios.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
/**
* Copyright (c) 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* The examples provided by Facebook are for non-commercial testing and
* evaluation purposes only.
*
Expand All @@ -28,7 +21,7 @@ var {
AlertIOS,
CameraRoll,
Image,
Linking,
LinkingIOS,
ProgressViewIOS,
StyleSheet,
Text,
Expand Down Expand Up @@ -222,7 +215,7 @@ class FormUploader extends React.Component {
return;
}
var url = xhr.responseText.slice(index).split('\n')[0];
Linking.openURL(url);
LinkingIOS.openURL(url);
};
var formdata = new FormData();
if (this.state.randomPhoto) {
Expand Down
17 changes: 8 additions & 9 deletions Libraries/Geolocation/Geolocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@
*/
'use strict';

const NativeEventEmitter = require('NativeEventEmitter');
const RCTLocationObserver = require('NativeModules').LocationObserver;
var RCTDeviceEventEmitter = require('RCTDeviceEventEmitter');
var RCTLocationObserver = require('NativeModules').LocationObserver;

const invariant = require('fbjs/lib/invariant');
const logError = require('logError');
const warning = require('fbjs/lib/warning');

const LocationEventEmitter = new NativeEventEmitter(RCTLocationObserver);
var invariant = require('fbjs/lib/invariant');
var logError = require('logError');
var warning = require('fbjs/lib/warning');

var subscriptions = [];

var updatesEnabled = false;

type GeoOptions = {
Expand Down Expand Up @@ -81,11 +80,11 @@ var Geolocation = {
}
var watchID = subscriptions.length;
subscriptions.push([
LocationEventEmitter.addListener(
RCTDeviceEventEmitter.addListener(
'geolocationDidChange',
success
),
error ? LocationEventEmitter.addListener(
error ? RCTDeviceEventEmitter.addListener(
'geolocationError',
error
) : null,
Expand Down
4 changes: 2 additions & 2 deletions Libraries/Geolocation/RCTLocationObserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
* of patent rights can be found in the PATENTS file in the same directory.
*/

#import "RCTEventEmitter.h"
#import "RCTBridgeModule.h"

@interface RCTLocationObserver : RCTEventEmitter
@interface RCTLocationObserver : NSObject<RCTBridgeModule>

@end
20 changes: 13 additions & 7 deletions Libraries/Geolocation/RCTLocationObserver.m
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ @implementation RCTLocationObserver

RCT_EXPORT_MODULE()

@synthesize bridge = _bridge;

#pragma mark - Lifecycle

- (void)dealloc
Expand All @@ -126,13 +128,9 @@ - (dispatch_queue_t)methodQueue
return dispatch_get_main_queue();
}

- (NSArray<NSString *> *)supportedEvents
{
return @[@"geolocationDidChange", @"geolocationError"];
}

#pragma mark - Private API


- (void)beginLocationUpdatesWithDesiredAccuracy:(CLLocationAccuracy)desiredAccuracy distanceFilter:(CLLocationDistance)distanceFilter
{
if (!_locationManager) {
Expand Down Expand Up @@ -281,7 +279,11 @@ - (void)locationManager:(CLLocationManager *)manager

// Send event
if (_observingLocation) {
[self sendEventWithName:@"geolocationDidChange" body:_lastLocationEvent];
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[_bridge.eventDispatcher sendDeviceEventWithName:@"geolocationDidChange"
body:_lastLocationEvent];
#pragma clang diagnostic pop
}

// Fire all queued callbacks
Expand Down Expand Up @@ -322,7 +324,11 @@ - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *

// Send event
if (_observingLocation) {
[self sendEventWithName:@"geolocationError" body:jsError];
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[_bridge.eventDispatcher sendDeviceEventWithName:@"geolocationError"
body:jsError];
#pragma clang diagnostic pop
}

// Fire all queued error callbacks
Expand Down
3 changes: 0 additions & 3 deletions Libraries/LinkingIOS/RCTLinkingManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,8 @@ + (BOOL)application:(UIApplication *)application

- (void)handleOpenURLNotification:(NSNotification *)notification
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[_bridge.eventDispatcher sendDeviceEventWithName:@"openURL"
body:notification.userInfo];
#pragma clang diagnostic pop
}

RCT_EXPORT_METHOD(openURL:(NSURL *)URL
Expand Down
6 changes: 2 additions & 4 deletions Libraries/Network/NetInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@
'use strict';

const Map = require('Map');
const NativeEventEmitter = require('NativeEventEmitter');
const NativeModules = require('NativeModules');
const Platform = require('Platform');
const RCTDeviceEventEmitter = require('RCTDeviceEventEmitter');
const RCTNetInfo = NativeModules.NetInfo;
const deprecatedCallback = require('deprecatedCallback');

const NetInfoEventEmitter = new NativeEventEmitter(RCTNetInfo);

const DEVICE_CONNECTIVITY_EVENT = 'networkStatusDidChange';

type ChangeEventName = $Enum<{
Expand Down Expand Up @@ -178,7 +176,7 @@ const NetInfo = {
eventName: ChangeEventName,
handler: Function
): {remove: () => void} {
const listener = NetInfoEventEmitter.addListener(
const listener = RCTDeviceEventEmitter.addListener(
DEVICE_CONNECTIVITY_EVENT,
(appStateData) => {
handler(appStateData.network_info);
Expand Down
6 changes: 3 additions & 3 deletions Libraries/Network/RCTNetInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

#import <SystemConfiguration/SystemConfiguration.h>

#import "RCTEventEmitter.h"
#import "RCTBridgeModule.h"

@interface RCTNetInfo : RCTEventEmitter
@interface RCTNetInfo : NSObject<RCTBridgeModule>

- (instancetype)initWithHost:(NSString *)host;
- (instancetype)initWithHost:(NSString *)host NS_DESIGNATED_INITIALIZER;

@end
Loading

0 comments on commit 2de0323

Please sign in to comment.