Skip to content

refactor(iOS): Migrate RCTOnPageScrollEvent to swift #938

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/ios/Podfile.lock
Original file line number Diff line number Diff line change
@@ -2090,7 +2090,7 @@ SPEC CHECKSUMS:
React-logger: 26155dc23db5c9038794db915f80bd2044512c2e
React-Mapbuffer: ad1ba0205205a16dbff11b8ade6d1b3959451658
React-microtasksnativemodule: e771eb9eb6ace5884ee40a293a0e14a9d7a4343c
react-native-pager-view: 7f8b1f81aee3c953ea25851553e88b69a7c0413d
react-native-pager-view: bad41ab1a3d1391153c4a08bceae65328d244518
react-native-safe-area-context: 2500e4fe998caad50ad3bc51ec23ef951308569e
React-nativeconfig: aeed6e2a8ac02b2df54476afcc7c663416c12bf7
React-NativeModulesApple: c5b7813da94136f50ef084fa1ac077332dcfc658
46 changes: 46 additions & 0 deletions ios/Events/OnPageScroll.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import Foundation
import React

public protocol RCTEvent {}

@objc public class RCTOnPageScroll: NSObject, RCTEvent {
private var position: NSNumber
private var offset: NSNumber
@objc public var viewTag: NSNumber
@objc public var coalescingKey: UInt16

@objc public var eventName: String {
return "onPageScroll"
}

@objc public init(reactTag: NSNumber, position: NSNumber, offset: NSNumber, coalescingKey: UInt16) {
self.viewTag = reactTag
self.position = position
self.offset = offset
self.coalescingKey = coalescingKey
super.init()
}

@objc public func canCoalesce() -> Bool {
return true
}

public func coalesce(with newEvent: RCTEvent) -> RCTEvent {
return newEvent
}

@objc public class func moduleDotMethod() -> String {
return "RCTEventEmitter.receiveEvent"
}

@objc public func arguments() -> [Any] {
return [
viewTag,
RCTNormalizeInputEventName(eventName) ?? eventName,
[
"position": position,
"offset": offset
]
]
}
}
9 changes: 7 additions & 2 deletions ios/Fabric/RNCPagerViewComponentView.mm
Original file line number Diff line number Diff line change
@@ -10,7 +10,12 @@
#import "RCTFabricComponentsPlugins.h"
#import "React/RCTConversions.h"

#import "RCTOnPageScrollEvent.h"
#if __has_include("react_native_pager_view/react_native_pager_view-Swift.h")
#import "react_native_pager_view/react_native_pager_view-Swift.h"
#else
#import "react_native_pager_view-Swift.h"
#endif


using namespace facebook::react;

@@ -374,7 +379,7 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// This is temporary workaround to allow animations based on onPageScroll event
// until Fabric implements proper NativeAnimationDriver,
// see: https://github.com/facebook/react-native/blob/44f431b471c243c92284aa042d3807ba4d04af65/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm#L59
NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:[[RCTOnPageScrollEvent alloc] initWithReactTag:[NSNumber numberWithInt:self.tag] position:@(position) offset:@(interpolatedOffset)], @"event", nil];
NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:[[RCTOnPageScroll alloc] initWithReactTag:[NSNumber numberWithInt:self.tag] position:@(position) offset:@(interpolatedOffset) coalescingKey:0], @"event", nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"RCTNotifyEventDispatcherObserversOfEvent_DEPRECATED"
object:nil
userInfo:userInfo];
14 changes: 0 additions & 14 deletions ios/RCTOnPageScrollEvent.h

This file was deleted.

60 changes: 0 additions & 60 deletions ios/RCTOnPageScrollEvent.m

This file was deleted.

12 changes: 9 additions & 3 deletions ios/RNCPagerView.m
Original file line number Diff line number Diff line change
@@ -3,11 +3,17 @@
#import <React/RCTViewManager.h>

#import "UIViewController+CreateExtension.h"
#import "RCTOnPageScrollEvent.h"
#import "RCTOnPageScrollStateChanged.h"
#import "RCTOnPageSelected.h"
#import <math.h>

#if __has_include("react_native_pager_view/react_native_pager_view-Swift.h")
#import "react_native_pager_view/react_native_pager_view-Swift.h"
#else
#import "react_native_pager_view-Swift.h"
#endif


@interface RNCPagerView () <UIPageViewControllerDataSource, UIPageViewControllerDelegate, UIScrollViewDelegate, UIGestureRecognizerDelegate>

@property(nonatomic, assign) UIPanGestureRecognizer* panGestureRecognizer;
@@ -342,7 +348,7 @@ - (void)pageViewController:(UIPageViewController *)pageViewController
self.currentIndex = currentIndex;
self.currentView = currentVC.view;
[self.eventDispatcher sendEvent:[[RCTOnPageSelected alloc] initWithReactTag:self.reactTag position:@(currentIndex) coalescingKey:_coalescingKey++]];
[self.eventDispatcher sendEvent:[[RCTOnPageScrollEvent alloc] initWithReactTag:self.reactTag position:@(currentIndex) offset:@(0.0)]];
[self.eventDispatcher sendEvent:[[RCTOnPageScroll alloc] initWithReactTag:self.reactTag position:@(currentIndex) offset:@(0.0) coalescingKey:0]];
self.lastReportedIndex = currentIndex;
}
}
@@ -453,7 +459,7 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
float interpolatedOffset = absoluteOffset * labs(_destinationIndex - _currentIndex);

self.lastContentOffset = scrollView.contentOffset;
[self.eventDispatcher sendEvent:[[RCTOnPageScrollEvent alloc] initWithReactTag:self.reactTag position:@(position) offset:@(interpolatedOffset)]];
[self.eventDispatcher sendEvent:[[RCTOnPageScroll alloc] initWithReactTag:self.reactTag position:@(position) offset:@(interpolatedOffset) coalescingKey:0]];
}

- (NSString *)determineScrollDirection:(UIScrollView *)scrollView {
4 changes: 4 additions & 0 deletions ios/react-native-pager-view-Bridging-Header.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#import <React/RCTViewManager.h>
#import <React/RCTUIManager.h>
#import <React/UIView+React.h>
#import <React/RCTUtils.h>
2 changes: 1 addition & 1 deletion react-native-pager-view.podspec
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@ Pod::Spec.new do |s|
s.platforms = { :ios => "10.0", :visionos => "1.0" }
s.source = { :git => "https://github.com/callstack/react-native-pager-view.git", :tag => "#{s.version}" }

s.source_files = "ios/**/*.{h,m,mm}"
s.source_files = "ios/**/*.{h,m,mm,swift}"

# install_modules_dependencies has been defined in RN 0.70
# This check ensure that the library can work on older versions of RN