11#import " RNCSafeAreaProvider.h"
22
33#import < React/RCTBridge.h>
4+ #import < React/RCTEventDispatcher.h>
45#import < React/RCTUIManager.h>
6+ #import " RCTUIManagerObserverCoordinator.h"
7+ #import " RNCOnInsetsChangeEvent.h"
58#import " RNCSafeAreaUtils.h"
69
10+ @interface RNCSafeAreaProvider () <RCTUIManagerObserver>
11+
12+ @end
13+
714@implementation RNCSafeAreaProvider {
15+ id <RCTEventDispatcherProtocol> _eventDispatcher;
816 UIEdgeInsets _currentSafeAreaInsets;
917 CGRect _currentFrame;
1018 BOOL _initialInsetsSent;
1119}
1220
13- - (instancetype )init
21+ - (instancetype )initWithEventDispatcher : ( id <RCTEventDispatcherProtocol>) eventDispatcher
1422{
15- if ((self = [super init ])) {
23+ RCTAssertParam (eventDispatcher);
24+
25+ if ((self = [super initWithFrame: CGRectZero])) {
1626#if !TARGET_OS_TV && !TARGET_OS_OSX
27+
28+ _eventDispatcher = eventDispatcher;
29+
1730 [NSNotificationCenter .defaultCenter addObserver: self
1831 selector: @selector (invalidateSafeAreaInsets )
1932 name: UIKeyboardDidShowNotification
@@ -58,6 +71,7 @@ - (void)invalidateSafeAreaInsets
5871 safeAreaInsets = NSEdgeInsetsZero ;
5972 }
6073#endif
74+
6175 CGRect frame = [self convertRect: self .bounds toView: RNCParentViewController (self ).view];
6276
6377 if (_initialInsetsSent &&
@@ -69,27 +83,19 @@ - (void)invalidateSafeAreaInsets
6983 CGRectEqualToRect (frame, _currentFrame)) {
7084 return ;
7185 }
72-
7386 _initialInsetsSent = YES ;
7487 _currentSafeAreaInsets = safeAreaInsets;
7588 _currentFrame = frame;
7689
7790 [NSNotificationCenter .defaultCenter postNotificationName: RNCSafeAreaDidChange object: self userInfo: nil ];
7891
79- self.onInsetsChange (@{
80- @" insets" : @{
81- @" top" : @(safeAreaInsets.top ),
82- @" right" : @(safeAreaInsets.right ),
83- @" bottom" : @(safeAreaInsets.bottom ),
84- @" left" : @(safeAreaInsets.left ),
85- },
86- @" frame" : @{
87- @" x" : @(frame.origin .x ),
88- @" y" : @(frame.origin .y ),
89- @" width" : @(frame.size .width ),
90- @" height" : @(frame.size .height ),
91- },
92- });
92+ RNCOnInsetsChangeEvent *onInsetsChangeEvent = [[RNCOnInsetsChangeEvent alloc ] initWithEventName: @" onInsetsChange"
93+ reactTag: self .reactTag
94+ insets: safeAreaInsets
95+ frame: frame
96+ coalescingKey: 0 ];
97+
98+ [_eventDispatcher sendEvent: onInsetsChangeEvent];
9399}
94100
95101- (void )layoutSubviews
@@ -99,4 +105,12 @@ - (void)layoutSubviews
99105 [self invalidateSafeAreaInsets ];
100106}
101107
108+ RCT_NOT_IMPLEMENTED (-(instancetype )initWithFrame : (CGRect)frame)
109+ RCT_NOT_IMPLEMENTED(-(instancetype )initWithCoder : (NSCoder *)aDecoder)
110+
111+ - (void )dealloc
112+ {
113+ [_eventDispatcher.bridge.uiManager.observerCoordinator removeObserver: self ];
114+ }
115+
102116@end
0 commit comments