Skip to content

Commit 7dc85d0

Browse files
okwasniewskifacebook-github-bot
authored andcommittedFeb 13, 2025
feat(ios): encapsulate device info listeners (#49162)
Summary: This PR encapsulates device info listeners removing additional logic from RCTAppDelegate which is a prerequisite for #49078 Now we use KVO (Key Value Observation) to listen to window size changes making this module's logic encapsulated + allows to use it in brownfield scenarios. To keep backward compatibility RCTDeviceInfo still emits `RCTWindowFrameDidChangeNotification` ## Changelog: [IOS] [ADDED] - encapsulate device info listeners Pull Request resolved: #49162 Test Plan: Check if window size change listener is fired Reviewed By: sammy-SC Differential Revision: D69116660 Pulled By: cipolleschi fbshipit-source-id: b9a57c70826b10cd27d102337eb1e35da1b365c9
1 parent f9bffef commit 7dc85d0

File tree

3 files changed

+24
-18
lines changed

3 files changed

+24
-18
lines changed
 

‎packages/react-native/Libraries/AppDelegate/RCTAppDelegate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ NS_ASSUME_NONNULL_BEGIN
5858
(const facebook::react::ObjCTurboModule::InitParams &)params
5959
* - (id<RCTTurboModule>)getModuleInstanceFromClass:(Class)moduleClass
6060
*/
61-
@interface RCTAppDelegate : RCTDefaultReactNativeFactoryDelegate <UIApplicationDelegate, UISceneDelegate>
61+
@interface RCTAppDelegate : RCTDefaultReactNativeFactoryDelegate <UIApplicationDelegate>
6262

6363
/// The window object, used to render the UViewControllers
6464
@property (nonatomic, strong, nonnull) UIWindow *window;

‎packages/react-native/Libraries/AppDelegate/RCTAppDelegate.mm

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,10 @@ - (void)loadReactNativeWindow:(NSDictionary *)launchOptions
6262
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
6363
UIViewController *rootViewController = [self createRootViewController];
6464
[self setRootView:rootView toRootViewController:rootViewController];
65-
_window.windowScene.delegate = self;
6665
_window.rootViewController = rootViewController;
6766
[_window makeKeyAndVisible];
6867
}
6968

70-
#pragma mark - UISceneDelegate
71-
72-
- (void)windowScene:(UIWindowScene *)windowScene
73-
didUpdateCoordinateSpace:(id<UICoordinateSpace>)previousCoordinateSpace
74-
interfaceOrientation:(UIInterfaceOrientation)previousInterfaceOrientation
75-
traitCollection:(UITraitCollection *)previousTraitCollection API_AVAILABLE(ios(13.0))
76-
{
77-
[[NSNotificationCenter defaultCenter] postNotificationName:RCTWindowFrameDidChangeNotification object:self];
78-
}
79-
8069
- (RCTRootViewFactory *)rootViewFactory
8170
{
8271
return self.reactNativeFactory.rootViewFactory;

‎packages/react-native/React/CoreModules/RCTDeviceInfo.mm

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,31 @@ @implementation RCTDeviceInfo {
3232
std::atomic<BOOL> _invalidated;
3333
}
3434

35+
static NSString *const kFrameKeyPath = @"frame";
36+
3537
@synthesize moduleRegistry = _moduleRegistry;
3638

3739
RCT_EXPORT_MODULE()
3840

41+
- (instancetype)init
42+
{
43+
if (self = [super init]) {
44+
[RCTKeyWindow() addObserver:self forKeyPath:kFrameKeyPath options:NSKeyValueObservingOptionNew context:nil];
45+
}
46+
return self;
47+
}
48+
49+
- (void)observeValueForKeyPath:(NSString *)keyPath
50+
ofObject:(id)object
51+
change:(NSDictionary *)change
52+
context:(void *)context
53+
{
54+
if ([keyPath isEqualToString:kFrameKeyPath]) {
55+
[self interfaceFrameDidChange];
56+
[[NSNotificationCenter defaultCenter] postNotificationName:RCTWindowFrameDidChangeNotification object:self];
57+
}
58+
}
59+
3960
+ (BOOL)requiresMainQueueSetup
4061
{
4162
return YES;
@@ -65,10 +86,6 @@ - (void)initialize
6586
name:RCTUserInterfaceStyleDidChangeNotification
6687
object:nil];
6788

68-
[[NSNotificationCenter defaultCenter] addObserver:self
69-
selector:@selector(interfaceFrameDidChange)
70-
name:RCTWindowFrameDidChangeNotification
71-
object:nil];
7289
[[NSNotificationCenter defaultCenter] addObserver:self
7390
selector:@selector(interfaceFrameDidChange)
7491
name:UIApplicationDidBecomeActiveNotification
@@ -113,10 +130,10 @@ - (void)_cleanupObservers
113130

114131
[[NSNotificationCenter defaultCenter] removeObserver:self name:RCTUserInterfaceStyleDidChangeNotification object:nil];
115132

116-
[[NSNotificationCenter defaultCenter] removeObserver:self name:RCTWindowFrameDidChangeNotification object:nil];
117-
118133
[[NSNotificationCenter defaultCenter] removeObserver:self name:RCTBridgeWillInvalidateModulesNotification object:nil];
119134

135+
[RCTKeyWindow() removeObserver:self forKeyPath:kFrameKeyPath];
136+
120137
#if TARGET_OS_IOS
121138
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
122139
#endif

0 commit comments

Comments
 (0)