Skip to content

Commit

Permalink
refactor(iOS): Migrate RCTOnPageScrollEvent to swift
Browse files Browse the repository at this point in the history
  • Loading branch information
MrRefactor committed Dec 2, 2024
1 parent b7e4af4 commit 89e0a5b
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 81 deletions.
2 changes: 1 addition & 1 deletion example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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
Expand Up @@ -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;

Expand Down Expand Up @@ -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];
Expand Down
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
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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 {
Expand Down
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
Expand Up @@ -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
Expand Down

0 comments on commit 89e0a5b

Please sign in to comment.