From c5653a03fba64b7c35d7d458da0452492f7c4035 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Kwas=CC=81niewski?= Date: Fri, 28 Jun 2024 06:02:38 -0700 Subject: [PATCH] feat: add conditionals for iOS only code in `RCTDeviceInfo.mm` (#45176) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: Having React Native support every Apple platform is tough to achieve as it introduces many platform-specific ifdefs. On the other side, maintaining an OOT platform fork is already a demanding job, so to make it easier I propose adding ifdefs for iOS-specific code. Thanks to this change, OOT platforms can focus on their OS-specific features while the core is also adding iOS-specific features behind ifdefs. Fortunately, **most of the code on Apple platforms can be shared** and this PR aims to introduce better support for this and to minimize OOT fork's surface. In this example `RCTDeviceInfo.mm` has support for handling orientation changes and the availability of this feature across Apple OS looks as follows: | Platform | Support | | ------------- | ------------- | | macOS | ❌ | | tvOS | ❌ | | visionOS | ❌ | | iOS/iPadOS | ✅ | Here is a table from `TargetConditionals.h` header file which shows the coverage of `TARGET_OS_IOS` macro. (It supports both iOS and iPadOS) ![CleanShot 2024-06-26 at 11 51 47@2x](https://github.com/facebook/react-native/assets/52801365/5f7dc99a-72b8-46ce-87d4-896bdb017269) ## Changelog: [INTERNAL] [ADDED] - Conditionals for iOS only code in RCTDeviceInfo.mm Pull Request resolved: https://github.com/facebook/react-native/pull/45176 Test Plan: CI Green Reviewed By: christophpurrer Differential Revision: D59106103 Pulled By: cipolleschi fbshipit-source-id: 594a9d2451024baddfbc9cd3bc1ccfb8829fc31c --- Gemfile.lock | 2 +- .../React/CoreModules/RCTDeviceInfo.mm | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 9ec5569aaf6e63..7801444199f7db 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -90,7 +90,7 @@ PLATFORMS DEPENDENCIES activesupport (>= 6.1.7.5, < 7.1.0) - cocoapods (~> 1.13) + cocoapods (~> 1.13, != 1.15.1, != 1.15.0) RUBY VERSION ruby 3.2.0p0 diff --git a/packages/react-native/React/CoreModules/RCTDeviceInfo.mm b/packages/react-native/React/CoreModules/RCTDeviceInfo.mm index 245a392e854630..b72352f5453f3b 100644 --- a/packages/react-native/React/CoreModules/RCTDeviceInfo.mm +++ b/packages/react-native/React/CoreModules/RCTDeviceInfo.mm @@ -52,8 +52,6 @@ - (void)initialize name:RCTAccessibilityManagerDidUpdateMultiplierNotification object:[_moduleRegistry moduleForName:"AccessibilityManager"]]; - _currentInterfaceOrientation = RCTKeyWindow().windowScene.interfaceOrientation; - _currentInterfaceDimensions = [self _exportedDimensions]; [[NSNotificationCenter defaultCenter] addObserver:self @@ -70,10 +68,16 @@ - (void)initialize selector:@selector(interfaceFrameDidChange) name:RCTWindowFrameDidChangeNotification object:nil]; + +#if TARGET_OS_IOS + + _currentInterfaceOrientation = RCTKeyWindow().windowScene.interfaceOrientation; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(interfaceFrameDidChange) name:UIDeviceOrientationDidChangeNotification object:nil]; +#endif // TODO T175901725 - Registering the RCTDeviceInfo module to the notification is a short-term fix to unblock 0.73 // The actual behavior should be that the module is properly registered in the TurboModule/Bridge infrastructure @@ -109,7 +113,9 @@ - (void)_cleanupObservers [[NSNotificationCenter defaultCenter] removeObserver:self name:RCTBridgeWillInvalidateModulesNotification object:nil]; +#if TARGET_OS_IOS [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil]; +#endif } static BOOL RCTIsIPhoneNotched() @@ -117,12 +123,14 @@ static BOOL RCTIsIPhoneNotched() static BOOL isIPhoneNotched = NO; static dispatch_once_t onceToken; +#if TARGET_OS_IOS dispatch_once(&onceToken, ^{ RCTAssertMainQueue(); // 20pt is the top safeArea value in non-notched devices isIPhoneNotched = RCTSharedApplication().keyWindow.safeAreaInsets.top > 20; }); +#endif return isIPhoneNotched; } @@ -206,6 +214,7 @@ - (void)interfaceOrientationDidChange - (void)_interfaceOrientationDidChange { +#if TARGET_OS_IOS UIApplication *application = RCTSharedApplication(); UIInterfaceOrientation nextOrientation = RCTKeyWindow().windowScene.interfaceOrientation; @@ -235,6 +244,7 @@ - (void)_interfaceOrientationDidChange _isFullscreen = isRunningInFullScreen; #pragma clang diagnostic pop } +#endif } - (void)interfaceFrameDidChange