Skip to content

Commit

Permalink
feat(ios): encapsulate device info listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
okwasniewski committed Feb 4, 2025
1 parent 4ccb2f2 commit 48cb626
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ NS_ASSUME_NONNULL_BEGIN
(const facebook::react::ObjCTurboModule::InitParams &)params
* - (id<RCTTurboModule>)getModuleInstanceFromClass:(Class)moduleClass
*/
@interface RCTAppDelegate : RCTDefaultReactNativeFactoryDelegate <UIApplicationDelegate, UISceneDelegate>
@interface RCTAppDelegate : RCTDefaultReactNativeFactoryDelegate <UIApplicationDelegate>

/// The window object, used to render the UViewControllers
@property (nonatomic, strong, nonnull) UIWindow *window;
Expand Down
11 changes: 0 additions & 11 deletions packages/react-native/Libraries/AppDelegate/RCTAppDelegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,10 @@ - (void)loadReactNativeWindow:(NSDictionary *)launchOptions
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
UIViewController *rootViewController = [self createRootViewController];
[self setRootView:rootView toRootViewController:rootViewController];
_window.windowScene.delegate = self;
_window.rootViewController = rootViewController;
[_window makeKeyAndVisible];
}

#pragma mark - UISceneDelegate

- (void)windowScene:(UIWindowScene *)windowScene
didUpdateCoordinateSpace:(id<UICoordinateSpace>)previousCoordinateSpace
interfaceOrientation:(UIInterfaceOrientation)previousInterfaceOrientation
traitCollection:(UITraitCollection *)previousTraitCollection API_AVAILABLE(ios(13.0))
{
[[NSNotificationCenter defaultCenter] postNotificationName:RCTWindowFrameDidChangeNotification object:self];
}

- (RCTRootViewFactory *)rootViewFactory
{
return self.reactNativeFactory.rootViewFactory;
Expand Down
36 changes: 25 additions & 11 deletions packages/react-native/React/CoreModules/RCTDeviceInfo.mm
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#import <atomic>

#import "CoreModulesPlugins.h"
#import <UIKit/UIKit.h>

using namespace facebook::react;

Expand All @@ -32,10 +33,34 @@ @implementation RCTDeviceInfo {
std::atomic<BOOL> _invalidated;
}

static NSString *const kFrameKeyPath = @"frame";

@synthesize moduleRegistry = _moduleRegistry;

RCT_EXPORT_MODULE()

- (instancetype)init {
if (self = [super init]) {
UIWindow *mainWindow = RCTKeyWindow();
[mainWindow addObserver:self
forKeyPath:kFrameKeyPath
options:NSKeyValueObservingOptionNew
context:nil];
}
return self;
}

- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
if ([keyPath isEqualToString:kFrameKeyPath]) {
[self interfaceFrameDidChange];
[[NSNotificationCenter defaultCenter] postNotificationName:RCTWindowFrameDidChangeNotification object:self];
}
}


+ (BOOL)requiresMainQueueSetup
{
return YES;
Expand Down Expand Up @@ -65,15 +90,6 @@ - (void)initialize
name:RCTUserInterfaceStyleDidChangeNotification
object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(interfaceFrameDidChange)
name:RCTWindowFrameDidChangeNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(interfaceFrameDidChange)
name:UIApplicationDidBecomeActiveNotification
object:nil];

#if TARGET_OS_IOS

_currentInterfaceOrientation = RCTKeyWindow().windowScene.interfaceOrientation;
Expand Down Expand Up @@ -113,8 +129,6 @@ - (void)_cleanupObservers

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

[[NSNotificationCenter defaultCenter] removeObserver:self name:RCTWindowFrameDidChangeNotification object:nil];

[[NSNotificationCenter defaultCenter] removeObserver:self name:RCTBridgeWillInvalidateModulesNotification object:nil];

#if TARGET_OS_IOS
Expand Down

0 comments on commit 48cb626

Please sign in to comment.