Skip to content

Commit

Permalink
Refactor to use delegate
Browse files Browse the repository at this point in the history
  • Loading branch information
mhoran committed Jan 3, 2025
1 parent 5d0e8a1 commit 29cf4af
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
12 changes: 7 additions & 5 deletions modules/keyboard-avoiding-view/ios/KeyboardAvoidingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import ExpoModulesCore

// This view will be used as a native component. Make sure to inherit from `ExpoView`
// to apply the proper styling (e.g. border radius and shadows).
class KeyboardAvoidingView: ExpoView, UIGestureRecognizerDelegate, ViewBoundsObserving {
class KeyboardAvoidingView: ExpoView, UIGestureRecognizerDelegate, ViewBoundsObserving,
ScrollViewWrapperDelegate
{
private let measurer = BoundsObservableView()
private let container = UIView()
private var scrollView: ScrollViewWrapper?
Expand Down Expand Up @@ -129,8 +131,8 @@ class KeyboardAvoidingView: ExpoView, UIGestureRecognizerDelegate, ViewBoundsObs
self.layoutIfNeeded()
}

@objc func scrollViewPanned(gesture: UIPanGestureRecognizer) {
guard scrollView?.isWrapperForScrollView(gesture.view) ?? false else { return }
@objc func scrollViewPanned(_ sender: ScrollViewWrapper, gesture: UIPanGestureRecognizer) {
guard let scrollView = self.scrollView && sender === scrollView else { return }

if gesture.state == .began {
isScrollViewPanning = true
Expand All @@ -144,7 +146,7 @@ class KeyboardAvoidingView: ExpoView, UIGestureRecognizerDelegate, ViewBoundsObs
// FIXME: Use a nativeID to find the ScrollView
if index == 0 {
scrollView = ScrollViewWrapper(view: childComponentView)
scrollView?.addPanGestureRecognizerTarget(self, action: #selector(scrollViewPanned))
scrollView.delegate = self
}
container.insertSubview(childComponentView, at: index)
}
Expand All @@ -162,7 +164,7 @@ class KeyboardAvoidingView: ExpoView, UIGestureRecognizerDelegate, ViewBoundsObs
// FIXME: Use a nativeID to find the ScrollView
if index == 0 {
scrollView = ScrollViewWrapper(view: subview)
scrollView?.addPanGestureRecognizerTarget(self, action: #selector(scrollViewPanned))
scrollView.delegate = self
}
container.insertSubview(subview, at: index)
}
Expand Down
15 changes: 11 additions & 4 deletions modules/keyboard-avoiding-view/ios/ScrollViewWrapper.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
#import <Foundation/Foundation.h>
#import <React/RCTView.h>

@class ScrollViewWrapper;

@protocol ScrollViewWrapperDelegate <NSObject>

- (void)scrollViewPanned:(nonnull ScrollViewWrapper *)sender
gesture:(nonnull UIPanGestureRecognizer *)gesture;

@end

@interface ScrollViewWrapper : NSObject

@property(nonatomic, weak) id<ScrollViewWrapperDelegate> delegate;

- (instancetype)initWithView:(UIView *)view;
- (void)setInsetsFromKeyboardHeight:(CGFloat)keyboardHeight;
- (nullable UIView *)view;
- (bool)isWrapperForScrollView:(nonnull id)scrollView
NS_SWIFT_NAME(isWrapperForScrollView(_:));
- (void)addPanGestureRecognizerTarget:(nonnull id)target :(nonnull SEL)action
NS_SWIFT_NAME(addPanGestureRecognizerTarget(_:action:));

@end
15 changes: 5 additions & 10 deletions modules/keyboard-avoiding-view/ios/ScrollViewWrapper.mm
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ - (instancetype)initWithView:(UIView *)view {
self = [super init];
if ([view isKindOfClass:[ReactScrollViewBase class]]) {
_scrollView = (ReactScrollViewBase *)view;
[_scrollView.scrollView.panGestureRecognizer
addTarget:self
action:@selector(forwardPanGestureToDelegate)];
}
return self;
}
Expand Down Expand Up @@ -45,16 +48,8 @@ - (nullable UIView *)view {
return (UIView *)_scrollView;
}

- (bool)isWrapperForScrollView:(id)scrollView {
return _scrollView != nil && _scrollView.scrollView == scrollView;
}

- (void)addPanGestureRecognizerTarget:(id)target :(SEL)action {
if (!_scrollView) {
return;
}

[_scrollView.scrollView.panGestureRecognizer addTarget:target action:action];
- (void)forwardPanGestureToDelegate:(UIPanGestureRecognizer *)gesture {
[self.delegate scrollViewPanned:self gesture:gesture];
}

@end

0 comments on commit 29cf4af

Please sign in to comment.