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

[iOS] Fabric: Added ScrollEndDragEvent for scrollEndDrag event #48319

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

zhongwuzw
Copy link
Contributor

Summary:

Fixes #42533 .

Changelog:

[IOS] [FIXED] - Fabric: Added ScrollEndDragEvent for scrollEndDrag event

Test Plan:

Repro please see #42533 .

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Dec 18, 2024
@facebook-github-bot facebook-github-bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Dec 18, 2024
Copy link
Contributor

@cipolleschi cipolleschi left a comment

Choose a reason for hiding this comment

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

Thank you so much for working on this. I left some comments as the current solution is not exactly equivalent to the current implementation and we can introduce subtle bugs.

@@ -530,9 +530,8 @@ - (BOOL)_shouldDisableScrollInteraction
return NO;
}

- (ScrollViewEventEmitter::Metrics)_scrollViewMetrics
- (void)_setScrollViewMetrics:(ScrollViewEventEmitter::Metrics &)metrics
Copy link
Contributor

Choose a reason for hiding this comment

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

this approach is not really iOS-sy...

What do you feel about having two methods: _scrollViewMetrics and _scrollViewMetricsWithVelocity:(CGPoint)velocity andTargetContentOffset:(CGPoint)targetContentOffset?

The first one, behaves as before. The second one does:

auto metrics = [self __scrollViewMetrics];
metrics.targetContentOffset.x = targetContentOffset->x;
metrics.targetContentOffset.y = targetContentOffset->y;
metrics.velocity.x = velocity.x;
metrics.velocity.y = velocity.y;
return metrics;

This should also minimize the changes in RCTScrollViewComponentView.

dispatchScrollViewEvent("scrollEndDrag", scrollEvent);
const ScrollEndDragEvent& scrollEvent) const {
dispatchEvent(
"scrollEndDrag", std::make_shared<ScrollEndDragEvent>(scrollEvent));
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure we need to create a new pointer with make_shared here.
The type of the scrollEvent is already the right one. If previously we were passing the scrollEvent, we should be able to pass the scrollEvent even now, types should match.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@cipolleschi Previously we call dispatchScrollViewEvent but it std::make_shared to create ScrollEvent shared ptr.

Copy link
Contributor

Choose a reason for hiding this comment

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

ScrollEndDragEvent event is a subclass of ScrollEvent, we probably don't need to change this method at all, as you are allowed to pass a subclass to a method that accepts a superclass. The language should take care of it. (LSP - Liskov substitution principle)

Can you remove these changes and test if this works properly without these?

Copy link
Contributor

@cipolleschi cipolleschi left a comment

Choose a reason for hiding this comment

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

I left a couple of extra comments that I missed yesterday, sorry!

dispatchScrollViewEvent("scrollEndDrag", scrollEvent);
const ScrollEndDragEvent& scrollEvent) const {
dispatchEvent(
"scrollEndDrag", std::make_shared<ScrollEndDragEvent>(scrollEvent));
Copy link
Contributor

Choose a reason for hiding this comment

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

ScrollEndDragEvent event is a subclass of ScrollEvent, we probably don't need to change this method at all, as you are allowed to pass a subclass to a method that accepts a superclass. The language should take care of it. (LSP - Liskov substitution principle)

Can you remove these changes and test if this works properly without these?

Comment on lines 24 to 28
using EndDragMetrics = ScrollEndDragEvent;

void onScroll(const ScrollEvent& scrollEvent) const;
void onScrollBeginDrag(const ScrollEvent& scrollEvent) const;
void onScrollEndDrag(const ScrollEvent& scrollEvent) const;
void onScrollEndDrag(const ScrollEndDragEvent& scrollEvent) const;
Copy link
Contributor

Choose a reason for hiding this comment

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

Same for LSP, these changes should not be needed.

@@ -26,7 +26,15 @@ void ScrollViewEventEmitter::onScrollBeginDrag(

void ScrollViewEventEmitter::onScrollEndDrag(
const ScrollEvent& scrollEvent) const {
dispatchScrollViewEvent("scrollEndDrag", scrollEvent);
const auto* endDragScrollEvent =
dynamic_cast<const ScrollEndDragEvent*>(&scrollEvent);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@cipolleschi Hi, this is the another way by dynamic_cast scrollEvent to ScrollEndDragEvent if we removed those changes.

Copy link
Contributor

@cipolleschi cipolleschi Dec 20, 2024

Choose a reason for hiding this comment

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

uhm... sorry for the extra back and forth.
I tested locally and actually I prefer the previous version with the properly typed methods and without the cast.
I haven't realized that even if the dispatchScrollViewEvent takes an endDragScrollEvent, it then creates a shared pointer of type ScrollEvent: creating the new event pointer with explicit ScrollEvent type makes us lose the information of the actual subclass and therefore we can't serialize the event properly. :(

I modified the imported code, nothing required to do on your side! 😉

@facebook-github-bot
Copy link
Contributor

@cipolleschi has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[new architecture] onScrollEndDrag missing event property
3 participants