Skip to content

Commit d44b58a

Browse files
authored
fix(ios): multiple onPageSelected calling (#455)
1 parent 23cff53 commit d44b58a

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed

example/ios/Podfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ PODS:
185185
- React-cxxreact (= 0.63.4)
186186
- React-jsi (= 0.63.4)
187187
- React-jsinspector (0.63.4)
188-
- react-native-pager-view (5.2.1):
188+
- react-native-pager-view (5.4.4):
189189
- React-Core
190190
- react-native-safe-area-context (3.2.0):
191191
- React-Core
@@ -382,7 +382,7 @@ SPEC CHECKSUMS:
382382
React-jsi: a0418934cf48f25b485631deb27c64dc40fb4c31
383383
React-jsiexecutor: 93bd528844ad21dc07aab1c67cb10abae6df6949
384384
React-jsinspector: 58aef7155bc9a9683f5b60b35eccea8722a4f53a
385-
react-native-pager-view: 2864530d861ab2970beb2cd0b4cf2292cd30cd84
385+
react-native-pager-view: 5ab4d0b4b44d89f77310cb3eb8129745f274ce55
386386
react-native-safe-area-context: f0906bf8bc9835ac9a9d3f97e8bde2a997d8da79
387387
React-RCTActionSheet: 89a0ca9f4a06c1f93c26067af074ccdce0f40336
388388
React-RCTAnimation: 1bde3ecc0c104c55df246eda516e0deb03c4e49b

example/src/App.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ function App() {
5454
key={example.name}
5555
style={styles.exampleTouchable}
5656
onPress={() => {
57+
//@ts-ignore
5758
navigation.navigate(example.name);
5859
}}
5960
>

ios/ReactNativePageView.m

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -159,14 +159,16 @@ - (void)setupInitialController {
159159
[self setReactViewControllers:self.initialPage
160160
with:initialController
161161
direction:UIPageViewControllerNavigationDirectionForward
162-
animated:YES];
162+
animated:YES
163+
shouldCallOnPageSelected:YES];
163164
}
164165
}
165166

166167
- (void)setReactViewControllers:(NSInteger)index
167168
with:(UIViewController *)controller
168169
direction:(UIPageViewControllerNavigationDirection)direction
169-
animated:(BOOL)animated {
170+
animated:(BOOL)animated
171+
shouldCallOnPageSelected:(BOOL)shouldCallOnPageSelected {
170172
if (self.reactPageViewController == nil) {
171173
return;
172174
}
@@ -183,7 +185,9 @@ - (void)setReactViewControllers:(NSInteger)index
183185

184186
if (strongSelf.eventDispatcher) {
185187
if (strongSelf.lastReportedIndex != strongSelf.currentIndex) {
186-
[strongSelf.eventDispatcher sendEvent:[[RCTOnPageSelected alloc] initWithReactTag:strongSelf.reactTag position:@(index) coalescingKey:coalescingKey]];
188+
if (shouldCallOnPageSelected) {
189+
[strongSelf.eventDispatcher sendEvent:[[RCTOnPageSelected alloc] initWithReactTag:strongSelf.reactTag position:@(index) coalescingKey:coalescingKey]];
190+
}
187191
strongSelf.lastReportedIndex = strongSelf.currentIndex;
188192
}
189193
}
@@ -236,33 +240,39 @@ - (void)goTo:(NSInteger)index animated:(BOOL)animated {
236240
long diff = labs(index - _currentIndex);
237241

238242
if (isForward && diff > 0) {
239-
for (NSInteger i=_currentIndex+1; i<=index; i++) {
240-
[self goToViewController:i direction:direction animated:animated];
243+
for (NSInteger i=_currentIndex; i<=index; i++) {
244+
if (i == _currentIndex) {
245+
continue;
246+
}
247+
[self goToViewController:i direction:direction animated:animated shouldCallOnPageSelected: i == index];
241248
}
242249
}
243250

244251
if (!isForward && diff > 0) {
245-
for (NSInteger i=_currentIndex-1; i>=index; i--) {
246-
if (i >=0) {
247-
[self goToViewController:i direction:direction animated:animated];
252+
for (NSInteger i=_currentIndex; i>=index; i--) {
253+
if (index == _currentIndex) {
254+
continue;
248255
}
256+
[self goToViewController:i direction:direction animated:animated shouldCallOnPageSelected: i == index];
249257
}
250258
}
251259

252260
if (diff == 0) {
253-
[self goToViewController:index direction:direction animated:animated];
261+
[self goToViewController:index direction:direction animated:animated shouldCallOnPageSelected:YES];
254262
}
255263
}
256264

257265
- (void)goToViewController:(NSInteger)index
258266
direction:(UIPageViewControllerNavigationDirection)direction
259-
animated:(BOOL)animated {
267+
animated:(BOOL)animated
268+
shouldCallOnPageSelected:(BOOL)shouldCallOnPageSelected {
260269
UIView *viewToDisplay = self.reactSubviews[index];
261270
UIViewController *controllerToDisplay = [self findAndCacheControllerForView:viewToDisplay];
262271
[self setReactViewControllers:index
263272
with:controllerToDisplay
264273
direction:direction
265-
animated:animated];
274+
animated:animated
275+
shouldCallOnPageSelected:shouldCallOnPageSelected];
266276
}
267277

268278
- (UIViewController *)findAndCacheControllerForView:(UIView *)viewToDisplay {
@@ -320,7 +330,6 @@ - (void)pageViewController:(UIPageViewController *)pageViewController
320330
self.currentIndex = currentIndex;
321331
self.currentView = currentVC.view;
322332
self.reactPageIndicatorView.currentPage = currentIndex;
323-
324333
[self.eventDispatcher sendEvent:[[RCTOnPageSelected alloc] initWithReactTag:self.reactTag position:@(currentIndex) coalescingKey:_coalescingKey++]];
325334
[self.eventDispatcher sendEvent:[[RCTOnPageScrollEvent alloc] initWithReactTag:self.reactTag position:@(currentIndex) offset:@(0.0)]];
326335
self.lastReportedIndex = currentIndex;

0 commit comments

Comments
 (0)