From 7a72c35a20a18c19bf6ab883cb2c53a85bd4c5c0 Mon Sep 17 00:00:00 2001 From: Spencer Ahrens Date: Wed, 11 Dec 2019 10:02:12 -0800 Subject: [PATCH] emit Dimensions change enent when app goes split screen Summary: Fixes https://github.com/facebook/react-native/issues/26830 by removing version gating around `RCTUserInterfaceStyleDidChangeNotification` sent by `RCTRootView` and observing that notif for `Dimensions` changes. Also centralizes `RCTUserInterfaceStyleDidChangeNotification` constant definition in new `RCTConstants` file. Changelog: [iOS] [Fixed] - `Dimensions` module now updates on initial split screen Reviewed By: sammy-SC Differential Revision: D18931098 fbshipit-source-id: e9784be3f544f3b10360fbc2d6ad0324273b1a8f --- React/Base/RCTConstants.h | 11 +++++++++++ React/Base/RCTConstants.m | 11 +++++++++++ React/Base/RCTRootView.m | 17 ++++++----------- .../RCTSurfaceHostingView.mm | 19 ++++++------------- React/CoreModules/RCTAppearance.h | 2 -- React/CoreModules/RCTAppearance.mm | 3 ++- React/CoreModules/RCTDeviceInfo.mm | 13 ++++++++++--- 7 files changed, 46 insertions(+), 30 deletions(-) create mode 100644 React/Base/RCTConstants.h create mode 100644 React/Base/RCTConstants.m diff --git a/React/Base/RCTConstants.h b/React/Base/RCTConstants.h new file mode 100644 index 00000000000000..d7d6790d470e63 --- /dev/null +++ b/React/Base/RCTConstants.h @@ -0,0 +1,11 @@ +/* + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#import + +RCT_EXTERN NSString *const RCTUserInterfaceStyleDidChangeNotification; +RCT_EXTERN NSString *const RCTUserInterfaceStyleDidChangeNotificationTraitCollectionKey; diff --git a/React/Base/RCTConstants.m b/React/Base/RCTConstants.m new file mode 100644 index 00000000000000..832a3c8c735463 --- /dev/null +++ b/React/Base/RCTConstants.m @@ -0,0 +1,11 @@ +/* + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#import "RCTConstants.h" + +NSString *const RCTUserInterfaceStyleDidChangeNotification = @"RCTUserInterfaceStyleDidChangeNotification"; +NSString *const RCTUserInterfaceStyleDidChangeNotificationTraitCollectionKey = @"traitCollection"; diff --git a/React/Base/RCTRootView.m b/React/Base/RCTRootView.m index 5cfc343d0e1606..841f9fe73103f3 100644 --- a/React/Base/RCTRootView.m +++ b/React/Base/RCTRootView.m @@ -14,6 +14,7 @@ #import "RCTAssert.h" #import "RCTBridge.h" #import "RCTBridge+Private.h" +#import "RCTConstants.h" #import "RCTEventDispatcher.h" #import "RCTKeyCommands.h" #import "RCTLog.h" @@ -33,7 +34,6 @@ #endif NSString *const RCTContentDidAppearNotification = @"RCTContentDidAppearNotification"; -static NSString *const RCTUserInterfaceStyleDidChangeNotification = @"RCTUserInterfaceStyleDidChangeNotification"; @interface RCTUIManager (RCTRootView) @@ -367,21 +367,16 @@ - (void)contentViewInvalidated [self showLoadingView]; } -#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \ - __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0 - (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection { [super traitCollectionDidChange:previousTraitCollection]; - if (@available(iOS 13.0, *)) { - if ([previousTraitCollection hasDifferentColorAppearanceComparedToTraitCollection:self.traitCollection]) { - [[NSNotificationCenter defaultCenter] postNotificationName:RCTUserInterfaceStyleDidChangeNotification - object:self - userInfo:@{@"traitCollection": self.traitCollection}]; - } - } + [[NSNotificationCenter defaultCenter] postNotificationName:RCTUserInterfaceStyleDidChangeNotification + object:self + userInfo:@{ + RCTUserInterfaceStyleDidChangeNotificationTraitCollectionKey: self.traitCollection, + }]; } -#endif - (void)dealloc { diff --git a/React/Base/Surface/SurfaceHostingView/RCTSurfaceHostingView.mm b/React/Base/Surface/SurfaceHostingView/RCTSurfaceHostingView.mm index 83d808b5afdc12..4b131ce0f630b9 100644 --- a/React/Base/Surface/SurfaceHostingView/RCTSurfaceHostingView.mm +++ b/React/Base/Surface/SurfaceHostingView/RCTSurfaceHostingView.mm @@ -7,14 +7,13 @@ #import "RCTSurfaceHostingView.h" +#import "RCTConstants.h" #import "RCTDefines.h" #import "RCTSurface.h" #import "RCTSurfaceDelegate.h" #import "RCTSurfaceView.h" #import "RCTUtils.h" -static NSString *const RCTUserInterfaceStyleDidChangeNotification = @"RCTUserInterfaceStyleDidChangeNotification"; - @interface RCTSurfaceHostingView () @property (nonatomic, assign) BOOL isActivityIndicatorViewVisible; @@ -208,21 +207,15 @@ - (void)setActivityIndicatorViewFactory:(RCTSurfaceHostingViewActivityIndicatorV #pragma mark - UITraitCollection updates -#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \ - __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0 - (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection { [super traitCollectionDidChange:previousTraitCollection]; - - if (@available(iOS 13.0, *)) { - if ([previousTraitCollection hasDifferentColorAppearanceComparedToTraitCollection:self.traitCollection]) { - [[NSNotificationCenter defaultCenter] postNotificationName:RCTUserInterfaceStyleDidChangeNotification - object:self - userInfo:@{@"traitCollection": self.traitCollection}]; - } - } + [[NSNotificationCenter defaultCenter] postNotificationName:RCTUserInterfaceStyleDidChangeNotification + object:self + userInfo:@{ + RCTUserInterfaceStyleDidChangeNotificationTraitCollectionKey: self.traitCollection, + }]; } -#endif #pragma mark - Private stuff diff --git a/React/CoreModules/RCTAppearance.h b/React/CoreModules/RCTAppearance.h index 2644783e5d636b..085003aa1f5636 100644 --- a/React/CoreModules/RCTAppearance.h +++ b/React/CoreModules/RCTAppearance.h @@ -12,7 +12,5 @@ RCT_EXTERN void RCTEnableAppearancePreference(BOOL enabled); -NSString *const RCTUserInterfaceStyleDidChangeNotification = @"RCTUserInterfaceStyleDidChangeNotification"; - @interface RCTAppearance : RCTEventEmitter @end diff --git a/React/CoreModules/RCTAppearance.mm b/React/CoreModules/RCTAppearance.mm index 5b299380becc2f..c1c07eecf1d2f0 100644 --- a/React/CoreModules/RCTAppearance.mm +++ b/React/CoreModules/RCTAppearance.mm @@ -8,6 +8,7 @@ #import "RCTAppearance.h" #import +#import #import #import "CoreModulesPlugins.h" @@ -86,7 +87,7 @@ - (void)appearanceChanged:(NSNotification *)notification NSDictionary *userInfo = [notification userInfo]; UITraitCollection *traitCollection = nil; if (userInfo) { - traitCollection = userInfo[@"traitCollection"]; + traitCollection = userInfo[RCTUserInterfaceStyleDidChangeNotificationTraitCollectionKey]; } NSString *newColorScheme = RCTColorSchemePreference(traitCollection); if (![_currentColorScheme isEqualToString:newColorScheme]) { diff --git a/React/CoreModules/RCTDeviceInfo.mm b/React/CoreModules/RCTDeviceInfo.mm index 1f9a5ce2d0d4e2..182dafab5f7ddc 100644 --- a/React/CoreModules/RCTDeviceInfo.mm +++ b/React/CoreModules/RCTDeviceInfo.mm @@ -10,6 +10,7 @@ #import #import #import +#import #import #import #import @@ -61,9 +62,15 @@ - (void)setBridge:(RCTBridge *)bridge _currentInterfaceDimensions = RCTExportedDimensions(_bridge); [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(interfaceFrameDidChange) - name:UIApplicationDidBecomeActiveNotification - object:nil]; + selector:@selector(interfaceFrameDidChange) + name:UIApplicationDidBecomeActiveNotification + object:nil]; + + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(interfaceFrameDidChange) + name:RCTUserInterfaceStyleDidChangeNotification + object:nil]; + #endif }