@@ -40,12 +40,10 @@ @implementation RCTKeyboardAvoidingViewComponentView {
4040 // The window guide never updates, and the guide of this view moves on resize.
4141 __weak UIView *_guideOwnerView;
4242 BOOL _isUpdateScheduled;
43-
4443 CGFloat _minTrackingHeight;
4544 CGSize _guideOwnerSize;
4645 CGFloat _guideOwnerBottomSafeArea;
4746 BOOL _isTrackingViewLaidOut;
48-
4947 CGFloat _keyboardInset;
5048 // `height` shrinks this view, so measure it while the keyboard doesn't overlap.
5149 CGFloat _restingHeight;
@@ -80,20 +78,24 @@ - (void)didMoveToWindow
8078 _trackingView = nil ;
8179
8280 UIWindow *window = self.window ;
81+
8382 if (window == nil ) {
8483 return ;
8584 }
8685
8786 UIView *guideOwnerView = window.rootViewController .view ;
87+
8888 for (UIResponder *responder = self.nextResponder ; responder != nil ; responder = responder.nextResponder ) {
8989 if ([responder isKindOfClass: [UIViewController class ]]) {
9090 guideOwnerView = ((UIViewController *)responder).view ;
9191 break ;
9292 }
9393 }
94+
9495 if (guideOwnerView == nil ) {
9596 return ;
9697 }
98+
9799 _guideOwnerView = guideOwnerView;
98100 _guideOwnerSize = CGSizeZero;
99101 _isTrackingViewLaidOut = NO ;
@@ -107,14 +109,18 @@ - (void)didMoveToWindow
107109 _trackingView = [RCTKeyboardTrackingView new ];
108110 _trackingView.translatesAutoresizingMaskIntoConstraints = NO ;
109111 __weak __typeof (self) weakSelf = self;
112+
110113 _trackingView.onLayout = ^{
111114 __typeof (self) strongSelf = weakSelf;
115+
112116 if (strongSelf == nil ) {
113117 return ;
114118 }
119+
115120 strongSelf->_isTrackingViewLaidOut = YES ;
116121 [strongSelf _updateKeyboardInset ];
117122 };
123+
118124 [_trackingContainerView addSubview: _trackingView];
119125
120126 [NSLayoutConstraint activateConstraints: @[
@@ -133,13 +139,15 @@ - (void)didMoveToWindow
133139- (void )_keyboardWillChangeFrame : (NSNotification *)notification
134140{
135141 UIView *trackingContainerView = _trackingContainerView;
142+
136143 if (trackingContainerView == nil ) {
137144 return ;
138145 }
139146
140147 NSTimeInterval duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue ];
141148 UIViewAnimationCurve curve =
142149 (UIViewAnimationCurve)[notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue ];
150+
143151 [UIView animateWithDuration: duration
144152 delay: 0
145153 options: (UIViewAnimationOptions)curve << 16 | UIViewAnimationOptionBeginFromCurrentState
@@ -153,17 +161,21 @@ - (CGFloat)_hiddenKeyboardTrackingHeight
153161{
154162 // Capped at the bottom safe area, for when the keyboard was never hidden.
155163 UIView *guideOwnerView = _guideOwnerView;
164+
156165 if (guideOwnerView == nil ) {
157166 return 0 ;
158167 }
168+
159169 CGFloat bottomSafeArea =
160170 MAX (CGRectGetMaxY (guideOwnerView.bounds ) - CGRectGetMaxY (guideOwnerView.safeAreaLayoutGuide .layoutFrame ), 0 );
171+
161172 // The hidden guide follows the bottom safe area, which can change without a resize.
162173 if (!CGSizeEqualToSize (guideOwnerView.bounds .size , _guideOwnerSize) || bottomSafeArea != _guideOwnerBottomSafeArea) {
163174 _guideOwnerSize = guideOwnerView.bounds .size ;
164175 _guideOwnerBottomSafeArea = bottomSafeArea;
165176 _minTrackingHeight = CGFLOAT_MAX ;
166177 }
178+
167179 _minTrackingHeight = MIN (_minTrackingHeight, _trackingView.bounds .size .height );
168180 return MIN (bottomSafeArea, _minTrackingHeight);
169181}
@@ -173,13 +185,17 @@ - (void)_scheduleUpdate
173185 if (_isUpdateScheduled) {
174186 return ;
175187 }
188+
176189 _isUpdateScheduled = YES ;
177190 __weak __typeof (self) weakSelf = self;
191+
178192 dispatch_async (dispatch_get_main_queue (), ^{
179193 __typeof (self) strongSelf = weakSelf;
194+
180195 if (strongSelf == nil ) {
181196 return ;
182197 }
198+
183199 strongSelf->_isUpdateScheduled = NO ;
184200 [strongSelf _updateKeyboardInset ];
185201 });
@@ -199,6 +215,7 @@ - (void)_updateKeyboardInset
199215
200216 CGFloat inset = 0 ;
201217 BOOL isKeyboardVisible = _trackingView.bounds .size .height > [self _hiddenKeyboardTrackingHeight ];
218+
202219 if (props.enabled && isKeyboardVisible) {
203220 CGFloat keyboardY = CGRectGetMinY (_trackingView.frame ) - props.keyboardVerticalOffset ;
204221 inset = MAX (_layoutMetrics.frame .origin .y + _restingHeight - keyboardY, 0 );
@@ -207,9 +224,10 @@ - (void)_updateKeyboardInset
207224 if (inset == _keyboardInset) {
208225 return ;
209226 }
210- _keyboardInset = inset;
211227
228+ _keyboardInset = inset;
212229 CGFloat maxHeight = inset > 0 ? MAX (_restingHeight - inset, 0 ) : INFINITY ;
230+
213231 // A `folly::dynamic` payload, which C++ Native Animated can read.
214232 _eventEmitter->dispatchUniqueEvent (
215233 " keyboardInsetChange" , folly::dynamic::object (" inset" , inset)(" maxHeight" , maxHeight));
0 commit comments