1
1
#import " RNCSafeAreaProvider.h"
2
2
3
3
#import < React/RCTBridge.h>
4
+ #import < React/RCTEventDispatcher.h>
4
5
#import < React/RCTUIManager.h>
6
+ #import " RCTUIManagerObserverCoordinator.h"
7
+ #import " RNCOnInsetsChangeEvent.h"
5
8
#import " RNCSafeAreaUtils.h"
6
9
10
+ @interface RNCSafeAreaProvider () <RCTUIManagerObserver>
11
+
12
+ @end
13
+
7
14
@implementation RNCSafeAreaProvider {
15
+ id <RCTEventDispatcherProtocol> _eventDispatcher;
8
16
UIEdgeInsets _currentSafeAreaInsets;
9
17
CGRect _currentFrame;
10
18
BOOL _initialInsetsSent;
11
19
}
12
20
13
- - (instancetype )init
21
+ - (instancetype )initWithEventDispatcher : ( id <RCTEventDispatcherProtocol>) eventDispatcher
14
22
{
15
- if ((self = [super init ])) {
23
+ RCTAssertParam (eventDispatcher);
24
+
25
+ if ((self = [super initWithFrame: CGRectZero ])) {
16
26
#if !TARGET_OS_TV && !TARGET_OS_OSX
27
+
28
+ _eventDispatcher = eventDispatcher;
29
+
17
30
[NSNotificationCenter .defaultCenter addObserver: self
18
31
selector: @selector (invalidateSafeAreaInsets )
19
32
name: UIKeyboardDidShowNotification
@@ -58,6 +71,7 @@ - (void)invalidateSafeAreaInsets
58
71
safeAreaInsets = NSEdgeInsetsZero ;
59
72
}
60
73
#endif
74
+
61
75
CGRect frame = [self convertRect: self .bounds toView: RNCParentViewController (self ).view];
62
76
63
77
if (_initialInsetsSent &&
@@ -69,27 +83,19 @@ - (void)invalidateSafeAreaInsets
69
83
CGRectEqualToRect (frame, _currentFrame)) {
70
84
return ;
71
85
}
72
-
73
86
_initialInsetsSent = YES ;
74
87
_currentSafeAreaInsets = safeAreaInsets;
75
88
_currentFrame = frame;
76
89
77
90
[NSNotificationCenter .defaultCenter postNotificationName: RNCSafeAreaDidChange object: self userInfo: nil ];
78
91
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];
93
99
}
94
100
95
101
- (void )layoutSubviews
@@ -99,4 +105,12 @@ - (void)layoutSubviews
99
105
[self invalidateSafeAreaInsets ];
100
106
}
101
107
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
+
102
116
@end
0 commit comments