Skip to content
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

fix: remove unnecessary checks for creating snapshot #2485

Merged
merged 4 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
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
11 changes: 1 addition & 10 deletions ios/RNSScreenStack.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1183,16 +1183,7 @@ - (nullable RNSScreenView *)childScreenForTag:(react::Tag)tag
- (void)unmountChildComponentView:(UIView<RCTComponentViewProtocol> *)childComponentView index:(NSInteger)index
{
RNSScreenView *screenChildComponent = (RNSScreenView *)childComponentView;

// We should only do a snapshot of a screen that is on the top.
// We also check `_presentedModals` since if you push 2 modals, second one is not a "child" of _controller.
// Also, when dissmised with a gesture, the screen already is not under the window, so we don't need to apply
// snapshot.
if (screenChildComponent.window != nil &&
((screenChildComponent == _controller.visibleViewController.view && _presentedModals.count < 2) ||
screenChildComponent == [_presentedModals.lastObject view])) {
[screenChildComponent.controller setViewToSnapshot];
}
[screenChildComponent.controller setViewToSnapshot];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that this simplifies the logic, but I don't think this is quite right.

Imagine you have Stack with three screens: "A, B, C". You pop from C to top, thus resulting stack is "A". This code would do snapshots of both C and B, while we only want to do a snapshot of C.

If you describe what problem exactly you intend to solve we might be able to figure out better solution.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this code:

if (self.view.window != nil) {
return true for B? I think its view is not in the window since it is detached. Would have to check though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked. It is nil then so it should work correctly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, I've forgotten that UINavigationController will keep all the controllers lower in the stack in detached state and that's it why it will work correctly.


RCTAssert(
screenChildComponent.reactSuperview == self,
Expand Down
44 changes: 23 additions & 21 deletions ios/RNSScreenStackHeaderConfig.mm
Original file line number Diff line number Diff line change
Expand Up @@ -856,27 +856,29 @@ - (void)unmountChildComponentView:(UIView<RCTComponentViewProtocol> *)childCompo

- (void)replaceNavigationBarViewsWithSnapshotOfSubview:(RNSScreenStackHeaderSubview *)childComponentView
{
UINavigationItem *navitem = _screenView.controller.navigationItem;
UIView *snapshot = [childComponentView snapshotViewAfterScreenUpdates:NO];

// This code should be kept in sync with analogous switch statement in
// `+ [RNSScreenStackHeaderConfig updateViewController: withConfig: animated:]` method.
switch (childComponentView.type) {
case RNSScreenStackHeaderSubviewTypeLeft:
navitem.leftBarButtonItem.customView = snapshot;
break;
case RNSScreenStackHeaderSubviewTypeCenter:
case RNSScreenStackHeaderSubviewTypeTitle:
navitem.titleView = snapshot;
break;
case RNSScreenStackHeaderSubviewTypeRight:
navitem.rightBarButtonItem.customView = snapshot;
break;
case RNSScreenStackHeaderSubviewTypeSearchBar:
case RNSScreenStackHeaderSubviewTypeBackButton:
break;
default:
RCTLogError(@"[RNScreens] Unhandled subview type: %ld", childComponentView.type);
if (childComponentView.window != nil) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also added this check since we got many warning of snapshotting a view that is not in hierarchy when dismissing many screens with header items at once.

UINavigationItem *navitem = _screenView.controller.navigationItem;
UIView *snapshot = [childComponentView snapshotViewAfterScreenUpdates:NO];

// This code should be kept in sync with analogous switch statement in
// `+ [RNSScreenStackHeaderConfig updateViewController: withConfig: animated:]` method.
switch (childComponentView.type) {
case RNSScreenStackHeaderSubviewTypeLeft:
navitem.leftBarButtonItem.customView = snapshot;
break;
case RNSScreenStackHeaderSubviewTypeCenter:
case RNSScreenStackHeaderSubviewTypeTitle:
navitem.titleView = snapshot;
break;
case RNSScreenStackHeaderSubviewTypeRight:
navitem.rightBarButtonItem.customView = snapshot;
break;
case RNSScreenStackHeaderSubviewTypeSearchBar:
case RNSScreenStackHeaderSubviewTypeBackButton:
break;
default:
RCTLogError(@"[RNScreens] Unhandled subview type: %ld", childComponentView.type);
}
}
}

Expand Down
Loading