Skip to content

Commit 95c1926

Browse files
ashwinbfacebook-github-bot
authored andcommitted
Introduce DeviceInfo as a new native module
Summary: The `UIManager` already has a lot of responsibilities and is deeply tied with React Native's view architecture. This diff separates out a `DeviceInfo` native module to provide information about screen dimensions and font scale, etc. Reviewed By: fkgozali Differential Revision: D4713834 fbshipit-source-id: f2ee93acf876a4221c29a8c731f5abeffbb97974
1 parent 238fd4a commit 95c1926

25 files changed

+345
-125
lines changed

Libraries/Utilities/DeviceInfo.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
* @providesModule DeviceInfo
10+
* @flow
11+
*/
12+
'use strict';
13+
14+
const DeviceInfo = require('NativeModules').DeviceInfo;
15+
16+
const invariant = require('invariant');
17+
18+
invariant(DeviceInfo, 'DeviceInfo native module is not installed correctly');
19+
20+
module.exports = DeviceInfo;

Libraries/Utilities/Dimensions.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
*/
1212
'use strict';
1313

14+
var DeviceInfo = require('DeviceInfo');
1415
var EventEmitter = require('EventEmitter');
1516
var Platform = require('Platform');
16-
var UIManager = require('UIManager');
1717
var RCTDeviceEventEmitter = require('RCTDeviceEventEmitter');
1818

1919
var invariant = require('fbjs/lib/invariant');
@@ -128,7 +128,7 @@ class Dimensions {
128128
}
129129
}
130130

131-
Dimensions.set(UIManager.Dimensions);
131+
Dimensions.set(DeviceInfo.Dimensions);
132132
RCTDeviceEventEmitter.addListener('didUpdateDimensions', function(update) {
133133
Dimensions.set(update);
134134
});

Libraries/react-native/react-native-implementation.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ const ReactNative = {
8585
get CameraRoll() { return require('CameraRoll'); },
8686
get Clipboard() { return require('Clipboard'); },
8787
get DatePickerAndroid() { return require('DatePickerAndroid'); },
88+
get DeviceInfo() { return require('DeviceInfo'); },
8889
get Dimensions() { return require('Dimensions'); },
8990
get Easing() { return require('Easing'); },
9091
get I18nManager() { return require('I18nManager'); },

React/Modules/RCTDeviceInfo.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
#import <UIKit/UIKit.h>
11+
12+
#import <React/RCTBridge.h>
13+
#import <React/RCTBridgeModule.h>
14+
15+
@interface RCTDeviceInfo : NSObject <RCTBridgeModule, RCTInvalidating>
16+
17+
@end

React/Modules/RCTDeviceInfo.m

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
#import "RCTDeviceInfo.h"
11+
12+
#import "RCTAccessibilityManager.h"
13+
#import "RCTAssert.h"
14+
#import "RCTEventDispatcher.h"
15+
#import "RCTUtils.h"
16+
17+
@implementation RCTDeviceInfo {
18+
#if !TARGET_OS_TV
19+
UIInterfaceOrientation _currentInterfaceOrientation;
20+
#endif
21+
}
22+
23+
@synthesize bridge = _bridge;
24+
25+
RCT_EXPORT_MODULE()
26+
27+
- (dispatch_queue_t)methodQueue
28+
{
29+
return dispatch_get_main_queue();
30+
}
31+
32+
- (void)setBridge:(RCTBridge *)bridge
33+
{
34+
_bridge = bridge;
35+
36+
[[NSNotificationCenter defaultCenter] addObserver:self
37+
selector:@selector(didReceiveNewContentSizeMultiplier)
38+
name:RCTAccessibilityManagerDidUpdateMultiplierNotification
39+
object:_bridge.accessibilityManager];
40+
#if !TARGET_OS_TV
41+
_currentInterfaceOrientation = [RCTSharedApplication() statusBarOrientation];
42+
43+
[[NSNotificationCenter defaultCenter] addObserver:self
44+
selector:@selector(interfaceOrientationDidChange)
45+
name:UIApplicationDidChangeStatusBarOrientationNotification
46+
object:nil];
47+
#endif
48+
}
49+
50+
static NSDictionary *RCTExportedDimensions(RCTBridge *bridge)
51+
{
52+
RCTAssertMainQueue();
53+
54+
// Don't use RCTScreenSize since it the interface orientation doesn't apply to it
55+
CGRect screenSize = [[UIScreen mainScreen] bounds];
56+
NSDictionary *dims = @{
57+
@"width": @(screenSize.size.width),
58+
@"height": @(screenSize.size.height),
59+
@"scale": @(RCTScreenScale()),
60+
@"fontScale": @(bridge.accessibilityManager.multiplier)
61+
};
62+
return @{
63+
@"window": dims,
64+
@"screen": dims
65+
};
66+
}
67+
68+
- (void)invalidate
69+
{
70+
dispatch_async(dispatch_get_main_queue(), ^{
71+
self->_bridge = nil;
72+
[[NSNotificationCenter defaultCenter] removeObserver:self];
73+
});
74+
}
75+
76+
- (NSDictionary<NSString *, id> *)constantsToExport
77+
{
78+
NSMutableDictionary<NSString *, NSDictionary *> *constants = [NSMutableDictionary new];
79+
constants[@"Dimensions"] = RCTExportedDimensions(_bridge);
80+
return constants;
81+
}
82+
83+
- (void)didReceiveNewContentSizeMultiplier
84+
{
85+
// Report the event across the bridge.
86+
#pragma clang diagnostic push
87+
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
88+
[_bridge.eventDispatcher sendDeviceEventWithName:@"didUpdateDimensions"
89+
body:RCTExportedDimensions(_bridge)];
90+
#pragma clang diagnostic pop
91+
}
92+
93+
94+
- (void)interfaceOrientationDidChange
95+
{
96+
#if !TARGET_OS_TV
97+
UIInterfaceOrientation nextOrientation = [RCTSharedApplication() statusBarOrientation];
98+
99+
// Update when we go from portrait to landscape, or landscape to portrait
100+
if ((UIInterfaceOrientationIsPortrait(_currentInterfaceOrientation) &&
101+
!UIInterfaceOrientationIsPortrait(nextOrientation)) ||
102+
(UIInterfaceOrientationIsLandscape(_currentInterfaceOrientation) &&
103+
!UIInterfaceOrientationIsLandscape(nextOrientation))) {
104+
#pragma clang diagnostic push
105+
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
106+
[_bridge.eventDispatcher sendDeviceEventWithName:@"didUpdateDimensions"
107+
body:RCTExportedDimensions(_bridge)];
108+
#pragma clang diagnostic pop
109+
}
110+
111+
_currentInterfaceOrientation = nextOrientation;
112+
#endif
113+
}
114+
115+
116+
@end

React/Modules/RCTUIManager.m

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,6 @@ @implementation RCTUIManager
225225
NSDictionary *_componentDataByName;
226226

227227
NSMutableSet<id<RCTComponent>> *_bridgeTransactionListeners;
228-
#if !TARGET_OS_TV
229-
UIInterfaceOrientation _currentInterfaceOrientation;
230-
#endif
231228
}
232229

233230
@synthesize bridge = _bridge;
@@ -239,8 +236,6 @@ - (void)didReceiveNewContentSizeMultiplier
239236
// Report the event across the bridge.
240237
#pragma clang diagnostic push
241238
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
242-
[_bridge.eventDispatcher sendDeviceEventWithName:@"didUpdateDimensions"
243-
body:RCTExportedDimensions(_bridge)];
244239
[_bridge.eventDispatcher sendDeviceEventWithName:@"didUpdateContentSizeMultiplier"
245240
body:@([_bridge.accessibilityManager multiplier])];
246241
#pragma clang diagnostic pop
@@ -252,28 +247,6 @@ - (void)didReceiveNewContentSizeMultiplier
252247
});
253248
}
254249

255-
- (void)interfaceOrientationDidChange
256-
{
257-
#if !TARGET_OS_TV
258-
UIInterfaceOrientation nextOrientation =
259-
[RCTSharedApplication() statusBarOrientation];
260-
261-
// Update when we go from portrait to landscape, or landscape to portrait
262-
if ((UIInterfaceOrientationIsPortrait(_currentInterfaceOrientation) &&
263-
!UIInterfaceOrientationIsPortrait(nextOrientation)) ||
264-
(UIInterfaceOrientationIsLandscape(_currentInterfaceOrientation) &&
265-
!UIInterfaceOrientationIsLandscape(nextOrientation))) {
266-
#pragma clang diagnostic push
267-
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
268-
[_bridge.eventDispatcher sendDeviceEventWithName:@"didUpdateDimensions"
269-
body:RCTExportedDimensions(_bridge)];
270-
#pragma clang diagnostic pop
271-
}
272-
273-
_currentInterfaceOrientation = nextOrientation;
274-
#endif
275-
}
276-
277250
- (void)invalidate
278251
{
279252
/**
@@ -351,13 +324,6 @@ - (void)setBridge:(RCTBridge *)bridge
351324
selector:@selector(didReceiveNewContentSizeMultiplier)
352325
name:RCTAccessibilityManagerDidUpdateMultiplierNotification
353326
object:_bridge.accessibilityManager];
354-
#if !TARGET_OS_TV
355-
[[NSNotificationCenter defaultCenter] addObserver:self
356-
selector:@selector(interfaceOrientationDidChange)
357-
name:UIApplicationDidChangeStatusBarOrientationNotification
358-
object:nil];
359-
#endif
360-
361327
[RCTAnimation initializeStatics];
362328
}
363329

@@ -1544,35 +1510,12 @@ static void RCTMeasureLayout(RCTShadowView *view,
15441510
constants[name] = moduleConstants;
15451511
}];
15461512

1547-
#if !TARGET_OS_TV
1548-
_currentInterfaceOrientation = [RCTSharedApplication() statusBarOrientation];
1549-
#endif
1550-
15511513
constants[@"customBubblingEventTypes"] = bubblingEvents;
15521514
constants[@"customDirectEventTypes"] = directEvents;
1553-
constants[@"Dimensions"] = RCTExportedDimensions(_bridge);
15541515

15551516
return constants;
15561517
}
15571518

1558-
static NSDictionary *RCTExportedDimensions(RCTBridge *bridge)
1559-
{
1560-
RCTAssertMainQueue();
1561-
1562-
// Don't use RCTScreenSize since it the interface orientation doesn't apply to it
1563-
CGRect screenSize = [[UIScreen mainScreen] bounds];
1564-
NSDictionary *dims = @{
1565-
@"width": @(screenSize.size.width),
1566-
@"height": @(screenSize.size.height),
1567-
@"scale": @(RCTScreenScale()),
1568-
@"fontScale": @(bridge.accessibilityManager.multiplier)
1569-
};
1570-
return @{
1571-
@"window": dims,
1572-
@"screen": dims
1573-
};
1574-
}
1575-
15761519
RCT_EXPORT_METHOD(configureNextLayoutAnimation:(NSDictionary *)config
15771520
withCallback:(RCTResponseSenderBlock)callback
15781521
errorCallback:(__unused RCTResponseSenderBlock)errorCallback)

React/React.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,10 @@
738738
B50558421E43E14000F71A00 /* RCTDevSettings.mm in Sources */ = {isa = PBXBuildFile; fileRef = B505583D1E43DFB900F71A00 /* RCTDevSettings.mm */; };
739739
B50558431E43E64600F71A00 /* RCTDevSettings.h in Headers */ = {isa = PBXBuildFile; fileRef = B505583C1E43DFB900F71A00 /* RCTDevSettings.h */; };
740740
B95154321D1B34B200FE7B80 /* RCTActivityIndicatorView.m in Sources */ = {isa = PBXBuildFile; fileRef = B95154311D1B34B200FE7B80 /* RCTActivityIndicatorView.m */; };
741+
CF85BC321E79EC6B00F1EF3B /* RCTDeviceInfo.h in Headers */ = {isa = PBXBuildFile; fileRef = CF85BC301E79EC6B00F1EF3B /* RCTDeviceInfo.h */; };
742+
CF85BC331E79EC6B00F1EF3B /* RCTDeviceInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = CF85BC311E79EC6B00F1EF3B /* RCTDeviceInfo.m */; };
743+
CF85BC341E79EC7A00F1EF3B /* RCTDeviceInfo.h in Headers */ = {isa = PBXBuildFile; fileRef = CF85BC301E79EC6B00F1EF3B /* RCTDeviceInfo.h */; };
744+
CF85BC351E79EC7D00F1EF3B /* RCTDeviceInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = CF85BC311E79EC6B00F1EF3B /* RCTDeviceInfo.m */; };
741745
E9B20B7B1B500126007A2DA7 /* RCTAccessibilityManager.m in Sources */ = {isa = PBXBuildFile; fileRef = E9B20B7A1B500126007A2DA7 /* RCTAccessibilityManager.m */; };
742746
/* End PBXBuildFile section */
743747

@@ -1381,6 +1385,8 @@
13811385
B505583D1E43DFB900F71A00 /* RCTDevSettings.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = RCTDevSettings.mm; sourceTree = "<group>"; };
13821386
B95154301D1B34B200FE7B80 /* RCTActivityIndicatorView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTActivityIndicatorView.h; sourceTree = "<group>"; };
13831387
B95154311D1B34B200FE7B80 /* RCTActivityIndicatorView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTActivityIndicatorView.m; sourceTree = "<group>"; };
1388+
CF85BC301E79EC6B00F1EF3B /* RCTDeviceInfo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTDeviceInfo.h; sourceTree = "<group>"; };
1389+
CF85BC311E79EC6B00F1EF3B /* RCTDeviceInfo.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTDeviceInfo.m; sourceTree = "<group>"; };
13841390
E3BBC8EB1ADE6F47001BBD81 /* RCTTextDecorationLineType.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RCTTextDecorationLineType.h; sourceTree = "<group>"; };
13851391
E9B20B791B500126007A2DA7 /* RCTAccessibilityManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTAccessibilityManager.h; sourceTree = "<group>"; };
13861392
E9B20B7A1B500126007A2DA7 /* RCTAccessibilityManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTAccessibilityManager.m; sourceTree = "<group>"; };
@@ -1456,6 +1462,8 @@
14561462
B505583B1E43DFB900F71A00 /* RCTDevMenu.m */,
14571463
B505583C1E43DFB900F71A00 /* RCTDevSettings.h */,
14581464
B505583D1E43DFB900F71A00 /* RCTDevSettings.mm */,
1465+
CF85BC301E79EC6B00F1EF3B /* RCTDeviceInfo.h */,
1466+
CF85BC311E79EC6B00F1EF3B /* RCTDeviceInfo.m */,
14591467
13D9FEE91CDCCECF00158BD7 /* RCTEventEmitter.h */,
14601468
13D9FEEA1CDCCECF00158BD7 /* RCTEventEmitter.m */,
14611469
13B07FE91A69327A00A75B9A /* RCTExceptionsManager.h */,
@@ -1869,6 +1877,7 @@
18691877
3D302F3E1DF828F800D6DDAE /* RCTKeyCommands.h in Headers */,
18701878
3D302F3F1DF828F800D6DDAE /* RCTLog.h in Headers */,
18711879
3D302F401DF828F800D6DDAE /* RCTModuleData.h in Headers */,
1880+
CF85BC341E79EC7A00F1EF3B /* RCTDeviceInfo.h in Headers */,
18721881
3D302F411DF828F800D6DDAE /* RCTModuleMethod.h in Headers */,
18731882
3D302F421DF828F800D6DDAE /* RCTMultipartDataTask.h in Headers */,
18741883
3D302F431DF828F800D6DDAE /* RCTMultipartStreamReader.h in Headers */,
@@ -2055,6 +2064,7 @@
20552064
A12E9E2A1E5DEB860029001B /* RCTReconnectingWebSocket.h in Headers */,
20562065
3D80DA311DF820620028D040 /* RCTJavaScriptLoader.h in Headers */,
20572066
3D80DA321DF820620028D040 /* RCTJSStackFrame.h in Headers */,
2067+
CF85BC321E79EC6B00F1EF3B /* RCTDeviceInfo.h in Headers */,
20582068
3D80DA331DF820620028D040 /* RCTKeyCommands.h in Headers */,
20592069
3D80DA341DF820620028D040 /* RCTLog.h in Headers */,
20602070
3D80DA351DF820620028D040 /* RCTModuleData.h in Headers */,
@@ -2465,6 +2475,7 @@
24652475
2D3B5EC21D9B093B00451313 /* RCTProfile.m in Sources */,
24662476
2D3B5ECB1D9B096200451313 /* RCTConvert+CoreLocation.m in Sources */,
24672477
A12E9E261E5DEB510029001B /* RCTPackagerClientResponder.m in Sources */,
2478+
CF85BC351E79EC7D00F1EF3B /* RCTDeviceInfo.m in Sources */,
24682479
2D3B5EEE1D9B09DA00451313 /* RCTView.m in Sources */,
24692480
594AD5D01E46D87500B07237 /* RCTScrollContentShadowView.m in Sources */,
24702481
2D3B5E981D9B089500451313 /* RCTConvert.m in Sources */,
@@ -2675,6 +2686,7 @@
26752686
945929C41DD62ADD00653A7D /* RCTConvert+Transform.m in Sources */,
26762687
13AB90C11B6FA36700713B4F /* RCTComponentData.m in Sources */,
26772688
13B0801B1A69489C00A75B9A /* RCTNavigatorManager.m in Sources */,
2689+
CF85BC331E79EC6B00F1EF3B /* RCTDeviceInfo.m in Sources */,
26782690
);
26792691
runOnlyForDeploymentPostprocessing = 0;
26802692
};

0 commit comments

Comments
 (0)