Skip to content

Commit

Permalink
DeviceInfo: Simplify RCTExportedDimensions's API
Browse files Browse the repository at this point in the history
Summary:
RCTExportedDimensions doesn't need access to the ModuleRegistry, or the bridge. It just uses those two things to get the fontScale.

We could make RCTExportedDimensions easier to understand, by making it do fewer things (i.e: computing the fontScale up front, and passing it into RCTExportedDimensions). Let's just do that.

Changelog: [Internal]

Reviewed By: cipolleschi

Differential Revision: D48237715

fbshipit-source-id: b3af648d88276846742d0e1192d33d180ee49dbb
  • Loading branch information
RSNara authored and facebook-github-bot committed Aug 20, 2023
1 parent 84e5934 commit adb9cbc
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions packages/react-native/React/CoreModules/RCTDeviceInfo.mm
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ @implementation RCTDeviceInfo {
BOOL _isFullscreen;
}

@synthesize bridge = _bridge;
@synthesize moduleRegistry = _moduleRegistry;

RCT_EXPORT_MODULE()
Expand Down Expand Up @@ -58,7 +57,7 @@ - (void)initialize
name:UIApplicationDidChangeStatusBarOrientationNotification
object:nil];

_currentInterfaceDimensions = RCTExportedDimensions(_moduleRegistry, _bridge);
_currentInterfaceDimensions = [self _exportedDimensions];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(interfaceOrientationDidChange)
Expand Down Expand Up @@ -86,17 +85,10 @@ static BOOL RCTIsIPhoneNotched()
return isIPhoneNotched;
}

static NSDictionary *RCTExportedDimensions(RCTModuleRegistry *moduleRegistry, RCTBridge *bridge)
static NSDictionary *RCTExportedDimensions(CGFloat fontScale)
{
RCTAssertMainQueue();
RCTDimensions dimensions;
if (moduleRegistry) {
RCTAccessibilityManager *accessibilityManager =
(RCTAccessibilityManager *)[moduleRegistry moduleForName:"AccessibilityManager"];
dimensions = RCTGetDimensions(accessibilityManager ? accessibilityManager.multiplier : 1.0);
} else {
RCTAssert(false, @"ModuleRegistry must be set to properly init dimensions. Bridge exists: %d", bridge != nil);
}
RCTDimensions dimensions = RCTGetDimensions(fontScale);
__typeof(dimensions.window) window = dimensions.window;
NSDictionary<NSString *, NSNumber *> *dimsWindow = @{
@"width" : @(window.width),
Expand All @@ -114,6 +106,15 @@ static BOOL RCTIsIPhoneNotched()
return @{@"window" : dimsWindow, @"screen" : dimsScreen};
}

- (NSDictionary *)_exportedDimensions
{
RCTAssert(_moduleRegistry, @"ModuleRegistry must be set to properly init dimensions");
RCTAccessibilityManager *accessibilityManager =
(RCTAccessibilityManager *)[_moduleRegistry moduleForName:"AccessibilityManager"];
CGFloat fontScale = accessibilityManager ? accessibilityManager.multiplier : 1.0;
return RCTExportedDimensions(fontScale);
}

- (NSDictionary<NSString *, id> *)constantsToExport
{
return [self getConstants];
Expand All @@ -122,11 +123,10 @@ static BOOL RCTIsIPhoneNotched()
- (NSDictionary<NSString *, id> *)getConstants
{
__block NSDictionary<NSString *, id> *constants;
RCTModuleRegistry *moduleRegistry = _moduleRegistry;
RCTBridge *bridge = _bridge;
__weak __typeof(self) weakSelf = self;
RCTUnsafeExecuteOnMainQueueSync(^{
constants = @{
@"Dimensions" : RCTExportedDimensions(moduleRegistry, bridge),
@"Dimensions" : [weakSelf _exportedDimensions],
// Note:
// This prop is deprecated and will be removed in a future release.
// Please use this only for a quick and temporary solution.
Expand All @@ -140,15 +140,14 @@ static BOOL RCTIsIPhoneNotched()

- (void)didReceiveNewContentSizeMultiplier
{
__weak __typeof(self) weakSelf = self;
RCTModuleRegistry *moduleRegistry = _moduleRegistry;
RCTBridge *bridge = _bridge;
RCTExecuteOnMainQueue(^{
// Report the event across the bridge.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[[moduleRegistry moduleForName:"EventDispatcher"]
sendDeviceEventWithName:@"didUpdateDimensions"
body:RCTExportedDimensions(moduleRegistry, bridge)];
[[moduleRegistry moduleForName:"EventDispatcher"] sendDeviceEventWithName:@"didUpdateDimensions"
body:[weakSelf _exportedDimensions]];
#pragma clang diagnostic pop
});
}
Expand Down Expand Up @@ -184,9 +183,8 @@ - (void)_interfaceOrientationDidChange
if ((isOrientationChanging || isResizingOrChangingToFullscreen) && RCTIsAppActive()) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[[_moduleRegistry moduleForName:"EventDispatcher"]
sendDeviceEventWithName:@"didUpdateDimensions"
body:RCTExportedDimensions(_moduleRegistry, _bridge)];
[[_moduleRegistry moduleForName:"EventDispatcher"] sendDeviceEventWithName:@"didUpdateDimensions"
body:[self _exportedDimensions]];
// We only want to track the current _currentInterfaceOrientation and _isFullscreen only
// when it happens and only when it is published.
_currentInterfaceOrientation = nextOrientation;
Expand All @@ -205,7 +203,7 @@ - (void)interfaceFrameDidChange

- (void)_interfaceFrameDidChange
{
NSDictionary *nextInterfaceDimensions = RCTExportedDimensions(_moduleRegistry, _bridge);
NSDictionary *nextInterfaceDimensions = [self _exportedDimensions];

// update and publish the even only when the app is in active state
if (!([nextInterfaceDimensions isEqual:_currentInterfaceDimensions]) && RCTIsAppActive()) {
Expand Down

0 comments on commit adb9cbc

Please sign in to comment.