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

Add number of touches on Android and iOS if needed #152

Merged
merged 6 commits into from
May 7, 2018

Conversation

osdnk
Copy link
Contributor

@osdnk osdnk commented Apr 23, 2018

Motivation

#134
This PR refers to changes made someday by @tsapeta involving passing natively number of touches given out-of-box on iOS
I just provide same mechanism on Android which seems to be quite useful.

Changes

Added addditional field in GestureHandler.java, getter and few lines to update this field. Now it's also exported to js bridge

Copy link
Member

@kmagiera kmagiera left a comment

Choose a reason for hiding this comment

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

Can you also update description and link to the issue this is resolving.

Also, can you please respond to my inline comment>

@@ -133,6 +133,7 @@ public void configure(NativeViewGestureHandler handler, ReadableMap config) {
@Override
public void extractEventData(NativeViewGestureHandler handler, WritableMap eventData) {
eventData.putBoolean("pointerInside", handler.isWithinBounds());
eventData.putDouble("numberOfTouches", handler.getMaxNumberOfPointers());
Copy link
Member

Choose a reason for hiding this comment

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

Not sure if it was intended to provide a max number of pointers here but my intuition for this particular event param would be that it represents the current number of touches (much like pointerInside refers to the current state of the handler).
Also consider the edge case when this method is used for the state change event and make sure this is consistent with the behaviour on iOS.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did observe its behaviour on iOS. Well, you're right that numberOfTouches return number oft touches in current situation and I gonna change it.
However, don't you consider introduction the mechanism of returning total number of touches involved in gesture?

@osdnk osdnk changed the title Add max number of touches on Android Add number of touches on Android Apr 24, 2018
Copy link
Member

@kmagiera kmagiera left a comment

Choose a reason for hiding this comment

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

Almost there 💪

Just a few final comments

@@ -161,6 +166,9 @@ public final void handle(MotionEvent event) {
}
return;
}
if (mNumberOfPointers != event.getPointerCount()) {
Copy link
Member

Choose a reason for hiding this comment

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

this check doesn't save much, can you get rid of it?

@@ -133,6 +133,7 @@ public void configure(NativeViewGestureHandler handler, ReadableMap config) {
@Override
public void extractEventData(NativeViewGestureHandler handler, WritableMap eventData) {
eventData.putBoolean("pointerInside", handler.isWithinBounds());
eventData.putDouble("numberOfTouches", handler.getNumberOfPointers());
Copy link
Member

Choose a reason for hiding this comment

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

OMG. I don't know how this slipped into here under this name. Do you mind renaming to numberOfPointers here and on iOS for consistency?

Copy link
Contributor

Choose a reason for hiding this comment

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

Renaming is not a good idea since some people are already using this. I just borrowed the name that is used on iOS 🤔 If you really want to change it, it's better to set a getter for numberOfTouches in nativeEvent that returns numberOfPointers and warns that it's deprecated.

Copy link
Contributor Author

@osdnk osdnk Apr 24, 2018

Choose a reason for hiding this comment

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

@tsapeta, I do not consider it as good motivation. Well, I don't suppose we need to care about fields which are not documented and appear on the only one platform
However, gonna wait with this change until @kmagiera opinion

Copy link
Contributor

@tsapeta tsapeta Apr 24, 2018

Choose a reason for hiding this comment

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

Fields returned in the event are not documented at all, so certainly people are checking what it has and trying on their own. If someone has reported a problem, it means that someone is using it.
Also, we don't have a changelog where we could explain that change and how to fix it.

Copy link
Member

Choose a reason for hiding this comment

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

the lib is still in alpha so breaking changes is just sth people will have to deal with when upgrading. Let's make sure to communicate this change to people reporting the issue and also when the next release comes out. We don't have capacity at the moment to support backward compatibility.

Also, we now publish change logs in the releases section on GH

Copy link
Member

Choose a reason for hiding this comment

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

Also, we have event field definition in typescript def file, so lets make sure to update it there too

@@ -35,6 +35,7 @@
private float mHitSlop[];

private boolean mShouldCancelWhenOutside;
private int mNumberOfPointers = 1;
Copy link
Member

Choose a reason for hiding this comment

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

Doesn't matter too much but 0 would be a more appropriate initial value as there are no pointers when handler gets instantiated

@@ -161,6 +166,9 @@ public final void handle(MotionEvent event) {
}
return;
}
if (mNumberOfPointers != event.getPointerCount()) {
mNumberOfPointers = event.getPointerCount();
Copy link
Member

Choose a reason for hiding this comment

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

You may want to move it up a bit as there is some logic that may cause the gesture to fail in which case it would emit an event with an incorrect number of pointers

@@ -370,6 +378,8 @@ public void extractEventData(RotationGestureHandler handler, WritableMap eventDa
eventData.putDouble("anchorX", PixelUtil.toDIPFromPixel(handler.getAnchorX()));
eventData.putDouble("anchorY", PixelUtil.toDIPFromPixel(handler.getAnchorY()));
eventData.putDouble("velocity", handler.getVelocity());
eventData.putDouble("numberOfTouches", handler.getNumberOfPointers());
Copy link
Member

Choose a reason for hiding this comment

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

Also, I'm curious why did you skip fling, tap and long press handlers when adding this new parameter? Looks like we can add it directly to the base class's HandlerFactory.extractEventData and call super in all subclasses (you can use https://developer.android.com/reference/android/support/annotation/CallSuper.html to ensure that it gets called)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kmagiera ,
Well, I wish to follow current iOS state and not to add this field where it was not before.
I found it also quite useles on fling as long as iOS requires exact number of pointers.
However, I will add it everywhere for consciousness

@osdnk
Copy link
Contributor Author

osdnk commented Apr 25, 2018

I will add corresponing numberOfPointers on Tap on iOS when PR related to changes in Tap will be merged

@osdnk osdnk changed the title Add number of touches on Android Add number of touches on Android and iOS if needed Apr 25, 2018
Copy link
Member

@kmagiera kmagiera left a comment

Choose a reason for hiding this comment

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

Almost there. Added a few comments inline

@@ -27,5 +27,11 @@ - (void)configure:(NSDictionary *)config
}
}

- (RNGestureHandlerEventExtraData *)eventExtraData:(UISwipeGestureRecognizer *)recognizer
Copy link
Member

Choose a reason for hiding this comment

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

Note that this changes behavior for Fling (same for long press). They've previously been using base class implementation from RNGestureHandler.m:
image

It sends position and absolute position. You perhaps want to add "withNumberOfTouches" there instead

@@ -99,21 +99,21 @@ - (void)handleTouchDown:(UIView *)sender forEvent:(UIEvent *)event

[self sendEventsInState:RNGestureHandlerStateActive
forViewWithTag:sender.reactTag
withExtraData:[RNGestureHandlerEventExtraData forPointerInside:YES]];
withExtraData:[RNGestureHandlerEventExtraData forPointerInside:YES withNumberOfTouches: _recognizer.numberOfTouches]];
Copy link
Member

Choose a reason for hiding this comment

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

nit:
withNumberOfTouches: _recognizer.numberOfTouches]];

should be:
withNumberOfTouches:_recognizer.numberOfTouches]];

(no space after colon)

Same applies below

+ (RNGestureHandlerEventExtraData *)forPointerInside:(BOOL)pointerInside;
+ (RNGestureHandlerEventExtraData *)forPointerInside:(BOOL)pointerInside
withNumberOfTouches:(NSUInteger)numberOfTouches;
+ (RNGestureHandlerEventExtraData *)withNumberOfTouches:(NSUInteger)numberOfTouches;
Copy link
Member

Choose a reason for hiding this comment

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

This won't be necessary if you add withNumberOfTouches to the very first method and use it from base class RNGestureHandler

@@ -99,21 +99,21 @@ - (void)handleTouchDown:(UIView *)sender forEvent:(UIEvent *)event

[self sendEventsInState:RNGestureHandlerStateActive
forViewWithTag:sender.reactTag
withExtraData:[RNGestureHandlerEventExtraData forPointerInside:YES]];
withExtraData:[RNGestureHandlerEventExtraData forPointerInside:YES withNumberOfTouches:_recognizer.numberOfTouches]];
Copy link
Member

Choose a reason for hiding this comment

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

I believe numberOfTouches should be extracted from event not _recognizer in this case

Copy link
Member

Choose a reason for hiding this comment

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

Or just stop sending numberOfPoiners for UIControl subclasses as it may not be that important

@"numberOfPointers": @(numberOfTouches)}];
}

+ (RNGestureHandlerEventExtraData *)withNumberOfTouches:(NSUInteger)numberOfTouches
Copy link
Member

Choose a reason for hiding this comment

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

not used

Copy link
Member

@kmagiera kmagiera left a comment

Choose a reason for hiding this comment

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

🎉

@kmagiera kmagiera merged commit 6dca457 into master May 7, 2018
@kmagiera kmagiera deleted the number-of-touches-android branch May 7, 2018 08:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants