diff --git a/node_modules/react-native-keyboard-tracking-view/lib/KeyboardTrackingViewManager.m b/node_modules/react-native-keyboard-tracking-view/lib/KeyboardTrackingViewManager.m index 1333a10..e83b01e 100644 --- a/node_modules/react-native-keyboard-tracking-view/lib/KeyboardTrackingViewManager.m +++ b/node_modules/react-native-keyboard-tracking-view/lib/KeyboardTrackingViewManager.m @@ -23,7 +23,7 @@ NSUInteger const kInputViewKey = 101010; NSUInteger const kMaxDeferedInitializeAccessoryViews = 15; NSInteger const kTrackingViewNotFoundErrorCode = 1; -NSInteger const kBottomViewHeight = 100; +NSInteger const kBottomViewHeight = 34; typedef NS_ENUM(NSUInteger, KeyboardTrackingScrollBehavior) { KeyboardTrackingScrollBehaviorNone, @@ -40,6 +40,7 @@ typedef NS_ENUM(NSUInteger, KeyboardTrackingScrollBehavior) { CGFloat _bottomViewHeight; } +@property (nonatomic) NSMutableDictionary* rctScrollViewsArray; @property (nonatomic, strong) UIScrollView *scrollViewToManage; @property (nonatomic) BOOL scrollIsInverted; @property (nonatomic) BOOL revealKeyboardInteractive; @@ -53,6 +54,13 @@ typedef NS_ENUM(NSUInteger, KeyboardTrackingScrollBehavior) { @property (nonatomic) BOOL scrollToFocusedInput; @property (nonatomic) BOOL allowHitsOutsideBounds; +@property (nonatomic) BOOL normalList; +@property (nonatomic) NSString* scrollViewNativeID; +@property (nonatomic) CGFloat initialOffsetY; +@property (nonatomic) BOOL initialOffsetIsSet; +@property (nonatomic, strong) UIView *accessoriesContainer; +@property (nonatomic) NSString* accessoriesContainerID; + @end @interface KeyboardTrackingView () @@ -70,12 +78,17 @@ typedef NS_ENUM(NSUInteger, KeyboardTrackingScrollBehavior) { [self addObserver:self forKeyPath:@"bounds" options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew context:NULL]; _inputViewsMap = [NSMapTable weakToWeakObjectsMapTable]; _deferedInitializeAccessoryViewsCount = 0; + _rctScrollViewsArray = [[NSMutableDictionary alloc] init]; _observingInputAccessoryView = [ObservingInputAccessoryView new]; _observingInputAccessoryView.delegate = self; + _initialOffsetY = 0; + _initialOffsetIsSet = NO; + _manageScrollView = YES; _allowHitsOutsideBounds = NO; + _requiresSameParentToManageScrollView = YES; _bottomViewHeight = kBottomViewHeight; @@ -134,7 +147,7 @@ typedef NS_ENUM(NSUInteger, KeyboardTrackingScrollBehavior) { UIView* subview; for (UIView* view in webview.scrollView.subviews) { - if([[view.class description] hasPrefix:@"UIWeb"]) + if([[view.class description] hasPrefix:@"WKWeb"]) { subview = view; } @@ -167,33 +180,32 @@ typedef NS_ENUM(NSUInteger, KeyboardTrackingScrollBehavior) { - (void)initializeAccessoryViewsAndHandleInsets { NSArray* allSubviews = [self getBreadthFirstSubviewsForView:[self getRootView]]; - NSMutableArray* rctScrollViewsArray = [NSMutableArray array]; for (UIView* subview in allSubviews) { + if(subview.nativeID) { + NSLog(@"self.accessoriesContainerID %@ %@", self.accessoriesContainerID, subview.nativeID); + } + + if (subview.nativeID && [subview.nativeID isEqualToString:self.accessoriesContainerID]) { + NSLog(@"SuperView ID: %@", subview.nativeID); + _accessoriesContainer = subview; + } + if(_manageScrollView) { if(_scrollViewToManage == nil) { - if(_requiresSameParentToManageScrollView && [subview isKindOfClass:[RCTScrollView class]] && subview.superview == self.superview) + if([subview isKindOfClass:[RCTScrollView class]]) { - _scrollViewToManage = ((RCTScrollView*)subview).scrollView; - } - else if(!_requiresSameParentToManageScrollView && [subview isKindOfClass:[UIScrollView class]]) - { - _scrollViewToManage = (UIScrollView*)subview; - } + RCTScrollView *scrollView = (RCTScrollView*)subview; - if(_scrollViewToManage != nil) - { - _scrollIsInverted = CGAffineTransformEqualToTransform(_scrollViewToManage.superview.transform, CGAffineTransformMakeScale(1, -1)); + if (subview.nativeID && [subview.nativeID isEqualToString:self.scrollViewNativeID]) { + [_rctScrollViewsArray setObject:scrollView forKey:subview.nativeID]; + _scrollViewToManage = scrollView.scrollView; + } } } - - if([subview isKindOfClass:[RCTScrollView class]]) - { - [rctScrollViewsArray addObject:(RCTScrollView*)subview]; - } } if ([subview isKindOfClass:NSClassFromString(@"RCTTextField")]) @@ -242,7 +254,7 @@ typedef NS_ENUM(NSUInteger, KeyboardTrackingScrollBehavior) { } } - for (RCTScrollView *scrollView in rctScrollViewsArray) + for (RCTScrollView *scrollView in [_rctScrollViewsArray allValues]) { if(scrollView.scrollView == _scrollViewToManage) { @@ -255,7 +267,7 @@ typedef NS_ENUM(NSUInteger, KeyboardTrackingScrollBehavior) { #if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_10_3 if (@available(iOS 11.0, *)) { if (_scrollViewToManage != nil) { - _scrollViewToManage.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; + _scrollViewToManage.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAlways; } } #endif @@ -267,6 +279,20 @@ typedef NS_ENUM(NSUInteger, KeyboardTrackingScrollBehavior) { [self addBottomViewIfNecessary]; } +- (void)resetScrollView:(NSString*) scrollViewNativeID { + for (RCTScrollView *scrollView in [_rctScrollViewsArray allValues]) + { + [scrollView removeScrollListener:self]; + } + [_rctScrollViewsArray removeAllObjects]; + _scrollViewToManage = nil; + _scrollViewNativeID = scrollViewNativeID; + + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + [self deferedInitializeAccessoryViewsAndHandleInsets]; + }); +} + - (void)setupTextView:(UITextView*)textView { if (textView != nil) @@ -339,11 +365,14 @@ typedef NS_ENUM(NSUInteger, KeyboardTrackingScrollBehavior) { - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { _observingInputAccessoryView.height = self.bounds.size.height; + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + [self updateTransformAndInsets]; + }); } - (void)observingInputAccessoryViewKeyboardWillDisappear:(ObservingInputAccessoryView *)observingInputAccessoryView { - _bottomViewHeight = kBottomViewHeight; + _bottomViewHeight = [self getBottomSafeArea]; [self updateBottomViewFrame]; } @@ -388,32 +417,42 @@ typedef NS_ENUM(NSUInteger, KeyboardTrackingScrollBehavior) { { if(self.scrollViewToManage != nil) { + if (!_initialOffsetIsSet) { + self.initialOffsetY = self.scrollViewToManage.contentOffset.y; + _initialOffsetIsSet = YES; + } + + if (_observingInputAccessoryView.keyboardState != KeyboardStateWillHide && _observingInputAccessoryView.keyboardState != KeyboardStateHidden) { + [self.scrollViewToManage setContentOffset:CGPointMake(self.scrollViewToManage.contentOffset.x, self.initialOffsetY) animated:NO]; + } + UIEdgeInsets insets = self.scrollViewToManage.contentInset; CGFloat bottomSafeArea = [self getBottomSafeArea]; CGFloat bottomInset = MAX(self.bounds.size.height, _observingInputAccessoryView.keyboardHeight + _observingInputAccessoryView.height); CGFloat originalBottomInset = self.scrollIsInverted ? insets.top : insets.bottom; CGPoint originalOffset = self.scrollViewToManage.contentOffset; + CGFloat keyboardHeight = _observingInputAccessoryView.keyboardHeight; - bottomInset += (_observingInputAccessoryView.keyboardHeight == 0 ? bottomSafeArea : 0); + bottomInset += (keyboardHeight == 0 ? bottomSafeArea : 0); if(self.scrollIsInverted) { - insets.top = bottomInset; + insets.top = (keyboardHeight == 0 ? bottomSafeArea : 0); } else { - insets.bottom = bottomInset; + insets.bottom = keyboardHeight; } self.scrollViewToManage.contentInset = insets; if(self.scrollBehavior == KeyboardTrackingScrollBehaviorScrollToBottomInvertedOnly && _scrollIsInverted) { - BOOL fisrtTime = _observingInputAccessoryView.keyboardHeight == 0 && _observingInputAccessoryView.keyboardState == KeyboardStateHidden; + BOOL firstTime = _observingInputAccessoryView.keyboardHeight == 0 && _observingInputAccessoryView.keyboardState == KeyboardStateHidden; BOOL willOpen = _observingInputAccessoryView.keyboardHeight != 0 && _observingInputAccessoryView.keyboardState == KeyboardStateHidden; BOOL isOpen = _observingInputAccessoryView.keyboardHeight != 0 && _observingInputAccessoryView.keyboardState == KeyboardStateShown; - if(fisrtTime || willOpen || (isOpen && !self.isDraggingScrollView)) + if(firstTime || willOpen || (isOpen && !self.isDraggingScrollView)) { - [self.scrollViewToManage setContentOffset:CGPointMake(self.scrollViewToManage.contentOffset.x, -self.scrollViewToManage.contentInset.top) animated:!fisrtTime]; + [self.scrollViewToManage setContentOffset:CGPointMake(self.scrollViewToManage.contentOffset.x, self.scrollViewToManage.contentOffset.y) animated:!firstTime]; } } else if(self.scrollBehavior == KeyboardTrackingScrollBehaviorFixedOffset && !self.isDraggingScrollView) @@ -422,16 +461,21 @@ typedef NS_ENUM(NSUInteger, KeyboardTrackingScrollBehavior) { self.scrollViewToManage.contentOffset = CGPointMake(originalOffset.x, originalOffset.y + insetsDiff); } - insets = self.scrollViewToManage.contentInset; - if(self.scrollIsInverted) - { - insets.top = bottomInset; + CGFloat kHeight = _observingInputAccessoryView.keyboardHeight; + if (kHeight != 0 && (_observingInputAccessoryView.keyboardState == KeyboardStateShown || _observingInputAccessoryView.keyboardState == KeyboardStateWillShow)) { + kHeight -= bottomSafeArea; } - else - { - insets.bottom = bottomInset; + + CGFloat positionY = self.normalList ? 0 : kHeight; + CGRect frame = CGRectMake(self.scrollViewToManage.frame.origin.x, positionY, + self.scrollViewToManage.frame.size.width, self.scrollViewToManage.frame.size.height); + self.scrollViewToManage.frame = frame; + + if (self.accessoriesContainer) { + CGFloat containerPositionY = self.normalList ? 0 : _observingInputAccessoryView.keyboardHeight; + self.accessoriesContainer.bounds = CGRectMake(self.accessoriesContainer.bounds.origin.x, containerPositionY, + self.accessoriesContainer.bounds.size.width, self.accessoriesContainer.bounds.size.height); } - self.scrollViewToManage.scrollIndicatorInsets = insets; } } @@ -448,7 +492,6 @@ typedef NS_ENUM(NSUInteger, KeyboardTrackingScrollBehavior) { if (self.addBottomView && _bottomView == nil) { _bottomView = [UIView new]; - _bottomView.backgroundColor = [UIColor whiteColor]; [self addSubview:_bottomView]; [self updateBottomViewFrame]; } @@ -467,6 +510,12 @@ typedef NS_ENUM(NSUInteger, KeyboardTrackingScrollBehavior) { } } +-(void)setBottomViewBackgroundColor:(UIColor*) color { + if (_bottomView != nil) { + _bottomView.backgroundColor = color; + } +} + #pragma mark - safe area -(void)safeAreaInsetsDidChange @@ -510,7 +559,7 @@ typedef NS_ENUM(NSUInteger, KeyboardTrackingScrollBehavior) { CGFloat accessoryTranslation = MIN(-bottomSafeArea, -_observingInputAccessoryView.keyboardHeight); if (_observingInputAccessoryView.keyboardHeight <= bottomSafeArea) { - _bottomViewHeight = kBottomViewHeight; + _bottomViewHeight = [self getBottomSafeArea]; } else if (_observingInputAccessoryView.keyboardState != KeyboardStateWillHide) { _bottomViewHeight = 0; } @@ -582,6 +631,8 @@ typedef NS_ENUM(NSUInteger, KeyboardTrackingScrollBehavior) { - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { self.isDraggingScrollView = YES; + _initialOffsetIsSet = NO; + self.initialOffsetY = scrollView.contentOffset.y; } - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset @@ -592,6 +643,15 @@ typedef NS_ENUM(NSUInteger, KeyboardTrackingScrollBehavior) { - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { self.isDraggingScrollView = NO; + self.initialOffsetY = scrollView.contentOffset.y; +} + +- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView { + self.initialOffsetY = scrollView.contentOffset.y; +} + +- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { + self.initialOffsetY = scrollView.contentOffset.y; } - (CGFloat)getKeyboardHeight @@ -633,7 +693,14 @@ RCT_REMAP_VIEW_PROPERTY(manageScrollView, manageScrollView, BOOL) RCT_REMAP_VIEW_PROPERTY(requiresSameParentToManageScrollView, requiresSameParentToManageScrollView, BOOL) RCT_REMAP_VIEW_PROPERTY(addBottomView, addBottomView, BOOL) RCT_REMAP_VIEW_PROPERTY(scrollToFocusedInput, scrollToFocusedInput, BOOL) +RCT_REMAP_VIEW_PROPERTY(inverted, scrollIsInverted, BOOL) RCT_REMAP_VIEW_PROPERTY(allowHitsOutsideBounds, allowHitsOutsideBounds, BOOL) +RCT_REMAP_VIEW_PROPERTY(normalList, normalList, BOOL) +RCT_EXPORT_VIEW_PROPERTY(scrollViewNativeID, NSString) +RCT_EXPORT_VIEW_PROPERTY(accessoriesContainerID, NSString) +RCT_CUSTOM_VIEW_PROPERTY(backgroundColor, UIColor, KeyboardTrackingView) { + [view setBottomViewBackgroundColor:[RCTConvert UIColor:json]]; +} + (BOOL)requiresMainQueueSetup { @@ -654,6 +721,20 @@ RCT_REMAP_VIEW_PROPERTY(allowHitsOutsideBounds, allowHitsOutsideBounds, BOOL) return [[KeyboardTrackingView alloc] init]; } +RCT_EXPORT_METHOD(resetScrollView:(nonnull NSNumber *)reactTag scrollViewNativeID:(NSString*)scrollViewNativeID) { + [self.bridge.uiManager addUIBlock: + ^(__unused RCTUIManager *uiManager, NSDictionary *viewRegistry) { + + KeyboardTrackingView *view = viewRegistry[reactTag]; + if (!view || ![view isKindOfClass:[KeyboardTrackingView class]]) { + RCTLogError(@"Error: cannot find KeyboardTrackingView with tag #%@", reactTag); + return; + } + + [view resetScrollView:scrollViewNativeID]; + }]; +} + RCT_EXPORT_METHOD(getNativeProps:(nonnull NSNumber *)reactTag resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { [self.bridge.uiManager addUIBlock: diff --git a/node_modules/react-native-keyboard-tracking-view/src/KeyboardTrackingView.android.js b/node_modules/react-native-keyboard-tracking-view/src/KeyboardTrackingView.android.js index af15edf..3ba0a3a 100644 --- a/node_modules/react-native-keyboard-tracking-view/src/KeyboardTrackingView.android.js +++ b/node_modules/react-native-keyboard-tracking-view/src/KeyboardTrackingView.android.js @@ -13,5 +13,6 @@ export default class KeyboardTrackingView extends PureComponent { async getNativeProps() { return {trackingViewHeight: 0, keyboardHeight: 0, contentTopInset: 0}; } + resetScrollView() {} scrollToStart() {} } diff --git a/node_modules/react-native-keyboard-tracking-view/src/KeyboardTrackingView.ios.js b/node_modules/react-native-keyboard-tracking-view/src/KeyboardTrackingView.ios.js index 5e2c207..727017c 100644 --- a/node_modules/react-native-keyboard-tracking-view/src/KeyboardTrackingView.ios.js +++ b/node_modules/react-native-keyboard-tracking-view/src/KeyboardTrackingView.ios.js @@ -25,6 +25,12 @@ export default class KeyboardTrackingView extends PureComponent { return {}; } + resetScrollView(scrollViewNativeID) { + if (this.ref && KeyboardTrackingViewManager && KeyboardTrackingViewManager.resetScrollView) { + KeyboardTrackingViewManager.resetScrollView(ReactNative.findNodeHandle(this.ref), scrollViewNativeID); + } + } + scrollToStart() { if (this.ref && KeyboardTrackingViewManager && KeyboardTrackingViewManager.scrollToStart) { KeyboardTrackingViewManager.scrollToStart(ReactNative.findNodeHandle(this.ref));