Skip to content

Commit e70c651

Browse files
fix(ios): replace stack screens in one native transition
1 parent 17ec4fd commit e70c651

3 files changed

Lines changed: 140 additions & 21 deletions

File tree

‎ios/helpers/stack/RNSStackNavigationController.mm‎

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
#import <React/RCTSurfaceTouchHandler.h>
44
#import "RCTSurfaceTouchHandler+RNSUtility.h"
55
#import "RNSContainer.h"
6-
#import "RNSDefines.h"
76
#import "RNSContainerItem.h"
7+
#import "RNSDefines.h"
8+
#import "RNSHeaderCoordinator.h"
89
#import "RNSLog.h"
910
#import "RNSParentContainerItemRegistry.h"
1011
#import "RNSStackNavigationBar.h"
1112
#import "RNSStackOperation.h"
1213
#import "RNSStackScreenComponentEventEmitter.h"
1314
#import "RNSStackScreenComponentView.h"
14-
#import "RNSHeaderCoordinator.h"
1515
#import "RNSViewFrameChangeDelegate.h"
1616

1717
@interface RNSStackNavigationController ()
@@ -312,26 +312,47 @@ - (void)performContainerUpdateIfNeeded
312312
}
313313

314314
_performingReactUpdate = YES;
315-
for ([[maybe_unused]] RNSPopOperation *op in _pendingPopOperations) {
316-
RCTAssert(
317-
self.allowsEmptyStack ? !self.isStackEmpty : [self.viewControllers count] > 1,
318-
@"[RNScreens] Attempt to pop last screen from the stack");
319-
RCTAssert(self.topViewController == op.stackScreen.controller, @"[RNScreens] Attempt to pop non-top screen");
320-
if (self.allowsEmptyStack && self.viewControllers.count == 1) {
321-
// UIKit cannot show an empty nested navigation controller when the split is collapsed.
322-
[self setViewControllers:@[ _emptyStackController ] animated:NO];
323-
} else {
324-
// Intermediate pops must finish synchronously before starting the final transition.
325-
[self popViewControllerAnimated:op == _pendingPopOperations.lastObject];
315+
if (_pendingPopOperations.count > 0 && _pendingPushOperations.count > 0) {
316+
// One React replacement must be one UIKit transition. Starting a push while
317+
// the final pop animates can leave the old screen visible with the new stack.
318+
NSMutableArray<UIViewController *> *controllers = [self.viewControllers mutableCopy];
319+
for (RNSPopOperation *op in _pendingPopOperations) {
320+
RCTAssert(
321+
self.allowsEmptyStack ? controllers.count > 0 : controllers.count > 1,
322+
@"[RNScreens] Attempt to pop last screen from the stack");
323+
RCTAssert(controllers.lastObject == op.stackScreen.controller, @"[RNScreens] Attempt to pop non-top screen");
324+
[controllers removeLastObject];
325+
}
326+
if (controllers.count > 0 && controllers.lastObject == _emptyStackController) {
327+
[controllers removeLastObject];
328+
}
329+
for (RNSPushOperation *op in _pendingPushOperations) {
330+
[controllers addObject:op.stackScreen.controller];
331+
}
332+
[self prepareToolbarForController:controllers.lastObject];
333+
[self setViewControllers:controllers animated:YES];
334+
} else {
335+
for ([[maybe_unused]] RNSPopOperation *op in _pendingPopOperations) {
336+
RCTAssert(
337+
self.allowsEmptyStack ? !self.isStackEmpty : [self.viewControllers count] > 1,
338+
@"[RNScreens] Attempt to pop last screen from the stack");
339+
RCTAssert(self.topViewController == op.stackScreen.controller, @"[RNScreens] Attempt to pop non-top screen");
340+
if (self.allowsEmptyStack && self.viewControllers.count == 1) {
341+
// UIKit cannot show an empty nested navigation controller when the split is collapsed.
342+
[self setViewControllers:@[ _emptyStackController ] animated:NO];
343+
} else {
344+
// Intermediate pops must finish synchronously before starting the final transition.
345+
[self popViewControllerAnimated:op == _pendingPopOperations.lastObject];
346+
}
326347
}
327-
}
328348

329-
for (RNSPushOperation *op in _pendingPushOperations) {
330-
if (self.allowsEmptyStack && self.isStackEmpty) {
331-
// The first screen is the root, so the placeholder must not appear in its back stack.
332-
[self setViewControllers:@[ op.stackScreen.controller ] animated:NO];
333-
} else {
334-
[self pushViewController:op.stackScreen.controller animated:op == _pendingPushOperations.lastObject];
349+
for (RNSPushOperation *op in _pendingPushOperations) {
350+
if (self.allowsEmptyStack && self.isStackEmpty) {
351+
// The first screen is the root, so the placeholder must not appear in its back stack.
352+
[self setViewControllers:@[ op.stackScreen.controller ] animated:NO];
353+
} else {
354+
[self pushViewController:op.stackScreen.controller animated:op == _pendingPushOperations.lastObject];
355+
}
335356
}
336357
}
337358

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-screens",
3-
"version": "5.0.0-t3.44",
3+
"version": "5.0.0-t3.45",
44
"description": "Native navigation primitives for your React Native app.",
55
"scripts": {
66
"submodules": "git submodule update --init --recursive && (cd react-navigation && yarn && yarn build && cd ../)",
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import { execFileSync } from 'node:child_process';
2+
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
3+
import { tmpdir } from 'node:os';
4+
import { join } from 'node:path';
5+
6+
// Compile the production update method against a recording navigation controller.
7+
// This verifies that one React replacement never starts competing UIKit transitions.
8+
const source = readFileSync(new URL('../../ios/helpers/stack/RNSStackNavigationController.mm', import.meta.url), 'utf8');
9+
const method = source.slice(source.indexOf('- (void)performContainerUpdateIfNeeded'), source.indexOf('#pragma mark - Debug'));
10+
const directory = mkdtempSync(join(tmpdir(), 'rns-stack-replacement-'));
11+
try {
12+
writeFileSync(join(directory, 'test.mm'), `
13+
#import <Foundation/Foundation.h>
14+
@compatibility_alias UIViewController NSObject;
15+
#define RCTAssert(condition, message) NSCAssert(condition, message)
16+
@interface Screen : NSObject
17+
@property NSObject *controller;
18+
@end
19+
@implementation Screen
20+
@end
21+
@interface RNSPopOperation : NSObject
22+
@property Screen *stackScreen;
23+
@end
24+
@implementation RNSPopOperation
25+
@end
26+
@interface RNSPushOperation : RNSPopOperation
27+
@end
28+
@implementation RNSPushOperation
29+
@end
30+
@interface Navigation : NSObject {
31+
@public
32+
NSMutableArray<RNSPushOperation *> *_pendingPushOperations;
33+
NSMutableArray<RNSPopOperation *> *_pendingPopOperations;
34+
NSObject *_emptyStackController;
35+
BOOL _performingReactUpdate;
36+
}
37+
@property NSMutableArray *viewControllers;
38+
@property NSMutableArray *transitions;
39+
@property BOOL allowsEmptyStack;
40+
- (void)performContainerUpdateIfNeeded;
41+
@end
42+
@implementation Navigation
43+
- (BOOL)hasPendingOperations { return _pendingPushOperations.count || _pendingPopOperations.count; }
44+
- (BOOL)isStackEmpty { return !self.viewControllers.count || self.viewControllers.firstObject == _emptyStackController; }
45+
- (NSObject *)topViewController { return self.viewControllers.lastObject; }
46+
- (void)prepareToolbarForController:(NSObject *)controller {}
47+
- (void)dumpStackModel {}
48+
- (void)setViewControllers:(NSArray *)controllers animated:(BOOL)animated {
49+
self.viewControllers = [controllers mutableCopy];
50+
if (animated) [self.transitions addObject:@"set"];
51+
}
52+
- (void)popViewControllerAnimated:(BOOL)animated {
53+
[self.viewControllers removeLastObject];
54+
if (animated) [self.transitions addObject:@"pop"];
55+
}
56+
- (void)pushViewController:(NSObject *)controller animated:(BOOL)animated {
57+
[self.viewControllers addObject:controller];
58+
if (animated) [self.transitions addObject:@"push"];
59+
}
60+
${method}
61+
@end
62+
static Screen *screen(NSString *name) { Screen *s = [Screen new]; s.controller = name; return s; }
63+
static Navigation *navigation(NSArray *controllers, BOOL empty) {
64+
Navigation *n = [Navigation new]; n.viewControllers = [controllers mutableCopy];
65+
n.transitions = [NSMutableArray new]; n.allowsEmptyStack = empty;
66+
n->_pendingPushOperations = [NSMutableArray new]; n->_pendingPopOperations = [NSMutableArray new];
67+
n->_emptyStackController = @"empty"; return n;
68+
}
69+
static void pop(Navigation *n, NSString *name) {
70+
RNSPopOperation *op = [RNSPopOperation new]; op.stackScreen = screen(name); [n->_pendingPopOperations addObject:op];
71+
}
72+
static void push(Navigation *n, NSString *name) {
73+
RNSPushOperation *op = [RNSPushOperation new]; op.stackScreen = screen(name); [n->_pendingPushOperations addObject:op];
74+
}
75+
int main() { @autoreleasepool {
76+
Navigation *n = navigation(@[@"home", @"pr", @"chooser", @"local"], NO);
77+
pop(n, @"local"); pop(n, @"chooser"); pop(n, @"pr"); push(n, @"draft");
78+
[n performContainerUpdateIfNeeded];
79+
NSCAssert(([n.viewControllers isEqual:@[@"home", @"draft"]]), @"replacement destination");
80+
NSCAssert([n.transitions isEqual:@[@"set"]], @"replacement must have one atomic transition, got %@", n.transitions);
81+
NSCAssert(!n.hasPendingOperations, @"replacement must drain operations");
82+
n = navigation(@[@"pr", @"chooser", @"local"], YES);
83+
pop(n, @"local"); pop(n, @"chooser"); pop(n, @"pr"); push(n, @"draft");
84+
[n performContainerUpdateIfNeeded];
85+
NSCAssert([n.viewControllers isEqual:@[@"draft"]], @"empty column replacement destination");
86+
NSCAssert([n.transitions isEqual:@[@"set"]], @"empty column replacement transition");
87+
n = navigation(@[@"home", @"draft", @"chooser"], NO); pop(n, @"chooser"); [n performContainerUpdateIfNeeded];
88+
NSCAssert(([n.viewControllers isEqual:@[@"home", @"draft"]] && [n.transitions isEqual:@[@"pop"]]), @"ordinary back unchanged");
89+
n = navigation(@[@"home"], NO); push(n, @"draft"); [n performContainerUpdateIfNeeded];
90+
NSCAssert(([n.viewControllers isEqual:@[@"home", @"draft"]] && [n.transitions isEqual:@[@"push"]]), @"ordinary push unchanged");
91+
puts("4 stack update scenarios passed");
92+
} }
93+
`);
94+
execFileSync('xcrun', ['clang++', '-fobjc-arc', '-framework', 'Foundation', join(directory, 'test.mm'), '-o', join(directory, 'test')], { stdio: 'inherit' });
95+
execFileSync(join(directory, 'test'), { stdio: 'inherit' });
96+
} finally {
97+
rmSync(directory, { recursive: true, force: true });
98+
}

0 commit comments

Comments
 (0)