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

[NativeAnimated][Android] Add support for animated events #9253

Closed

Conversation

janicduplessis
Copy link
Contributor

@janicduplessis janicduplessis commented Aug 5, 2016

This adds support for Animated.event driven natively. This is WIP and would like feedback on how this is implemented.

At the moment, it works by providing a mapping between a view tag, an event name, an event path and an animated value when a view has a prop with a AnimatedEvent object. Then we can hook into EventDispatcher, check for events that target our view + event name and update the animated value using the event path.

For now it works with the onScroll event but it should be generic enough to work with anything.

@ghost
Copy link

ghost commented Aug 5, 2016

By analyzing the blame information on this pull request, we identified @kmagiera and @ericvicenti to be potential reviewers.

@ghost ghost added GH Review: review-needed CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. labels Aug 5, 2016
@janicduplessis
Copy link
Contributor Author

cc @brentvatne

@janicduplessis
Copy link
Contributor Author

janicduplessis commented Aug 5, 2016

Other thought:

  • Should Animated Events be represented somehow as a node in the graph? I went with a separate data structure to just get it working.
  • We need to map event names properly. On Android events have different names than in JS, ie topScroll for the onScroll prop. There is a mapping of that in UIManager so it should be possible to use that to convert it properly.
  • Is there another way to attach the event to the view? Right now if the view uses a native driven event it need to be an animated view, ie: Animated.ScrollView.

@ghost ghost 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 Aug 5, 2016
const prop = nextProps[key];
if (prop instanceof AnimatedEvent && prop.__isNative) {
// TODO: Map event names using the map in UIManager.
if (key === 'onScroll') {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Which props aside from onScroll do you think we might want to be able to bind natively to with Animated.event?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Actually pan responders would be a good example of these kinds of events

Copy link
Contributor

Choose a reason for hiding this comment

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

@brentvatne this approach will only work for "direct" events. That is because touch handling has a bubbling logic written in JS and we cannot determine which view is going to be responsible for handling the touch without running some JS code

@brentvatne
Copy link
Collaborator

This is so exciting!

Should Animated Events be represented somehow as a node in the graph? I went with a separate data structure to just get it working.

Not sure about this one, will leave it to someone else to comment on.

Is there another way to attach the event to the view? Right now if the view uses a native driven event it need to be an animated view, ie: Animated.ScrollView.

Another option would be to just make ScrollView take care of attaching the event, but this doesn't work for other views and leaks the responsibility for this outside of the Animated codebase.

We need to know:

  • what event to watch for (name of the prop -> event mapping)
  • react tag for the view
  • when to detach the event

I'm sure that there are ways we could work around this but I can't think of any solid ones, so I'm for Animated.ScrollView here.

cc @vjeux @sahrens

@ghost ghost 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 Aug 6, 2016
@foghina
Copy link
Contributor

foghina commented Aug 11, 2016

cc @kmagiera

@ghost ghost 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 Aug 11, 2016
@@ -9,6 +9,8 @@

package com.facebook.react.animated;

import android.util.Log;
Copy link
Contributor

Choose a reason for hiding this comment

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

revert this

@ghost ghost 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 Aug 11, 2016
@facebook-github-bot
Copy link
Contributor

@janicduplessis updated the pull request.

1 similar comment
@ghost
Copy link

ghost commented Aug 11, 2016

@janicduplessis updated the pull request.

@ghost ghost 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 Aug 11, 2016
@ghost
Copy link

ghost commented Aug 11, 2016

@janicduplessis updated the pull request.

@ghost ghost 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 Aug 11, 2016
}

_attachNativeEvents(newProps) {
if (newProps !== this.props) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This can perhaps be optimised so that we don't reattach native props every time when non relevant property gets changed. Also, I'm not sure if this statement can even evaluate to true (if props hasn't been updated componentWillReceiveProps will not be triggered)

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 think the common case is to create a new event in every render so it should be fine to reattach everytime, I'll take another look see if I can improve it though.

<ScrollView
  onScroll={Animated.event({...})
/>

Copy link
Contributor

Choose a reason for hiding this comment

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

We should perhaps add to the documentation that creating event in render method is not the most effective way

@janicduplessis janicduplessis changed the title [WIP][NativeAnimated][Android] Add support for animated events [NativeAnimated][Android] Add support for animated events Aug 12, 2016
@ghost
Copy link

ghost commented Aug 12, 2016

@janicduplessis updated the pull request.

@ghost ghost 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 Aug 12, 2016
@ghost
Copy link

ghost commented Aug 15, 2016

@janicduplessis updated the pull request.

@ghost ghost 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 Aug 15, 2016
@ghost
Copy link

ghost commented Aug 15, 2016

@janicduplessis updated the pull request.

@ghost ghost 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 Sep 9, 2016
@ghost
Copy link

ghost commented Sep 10, 2016

@janicduplessis updated the pull request - view changes - changes since last import

@ghost
Copy link

ghost commented Sep 10, 2016

@janicduplessis updated the pull request - view changes - changes since last import

@ghost ghost 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 Sep 10, 2016
@foghina
Copy link
Contributor

foghina commented Sep 16, 2016

@facebook-github-bot shipit

@foghina
Copy link
Contributor

foghina commented Sep 16, 2016

Just importing it again, see if I can fix the test somehow.

@ghost ghost added the Import Started This pull request has been imported. This does not imply the PR has been approved. label Sep 16, 2016
@ghost
Copy link

ghost commented Sep 16, 2016

Thanks for importing. If you are an FB employee go to Phabricator to review internal test results.

@ghost ghost 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 Sep 16, 2016
@ghost
Copy link

ghost commented Sep 16, 2016

I tried to merge this pull request into the Facebook internal repo but some checks failed. To unblock yourself please check the following: Does this pull request pass all open source tests on GitHub? If not please fix those. Does the code still apply cleanly on top of GitHub master? If not can please rebase. In all other cases this means some internal test failed, for example a part of a fb app won't work with this pull request. I've added the Import Failed label to this pull request so it is easy for someone at fb to find the pull request and check what failed. If you don't see anyone comment in a few days feel free to comment mentioning one of the core contributors to the project so they get a notification.

@ghost ghost added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. and removed Import Started This pull request has been imported. This does not imply the PR has been approved. labels Sep 16, 2016
@nihgwu
Copy link
Contributor

nihgwu commented Sep 18, 2016

@janicduplessis how can I use this in a ListView, I've just tried but failed with warning Invalid prop 'onScroll' of type 'object' supplied to 'ListView', expected 'function'

@ghost ghost 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 Sep 18, 2016
@janicduplessis
Copy link
Contributor Author

To use it with ListView you'll need to create an AnimatedListView using Animated.createAnimatedComponent.

@nihgwu
Copy link
Contributor

nihgwu commented Sep 18, 2016

I did that but still failed, have you tried it?

???? iPhone

? 2016?9?19??01:25?Janic Duplessis <notifications@git.luolix.topmailto:notifications@github.com> ???

To use it with ListView you'll need to create an AnimatedListView using Animated.createAnimatedComponent.

You are receiving this because you commented.
Reply to this email directly, view it on GitHubhttps://github.com//pull/9253#issuecomment-247860836, or mute the threadhttps://github.com/notifications/unsubscribe-auth/ACeY8kSDrw2975LYMGSiQ0_dhMyuQrGzks5qrXP2gaJpZM4JeI2-.

@janicduplessis
Copy link
Contributor Author

Ok, I think I know why. Does it work if you add

  getScrollableNode: function() {
    if (this._scrollComponent && this._scrollComponent.getScrollableNode) {
      return this._scrollComponent.getScrollableNode();
    }
  },

in ListView.js

@nihgwu
Copy link
Contributor

nihgwu commented Sep 19, 2016

@janicduplessis It's definitely awesome, working so great, say goodbye to bad scrolling effects, thank you again, wish this can be merged soon

@ghost ghost 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 Sep 19, 2016
@ghost ghost closed this in 6565929 Sep 19, 2016
@foghina
Copy link
Contributor

foghina commented Sep 19, 2016

BOOM.

@nihgwu
Copy link
Contributor

nihgwu commented Sep 19, 2016

@janicduplessis what about to add support for ListView

@janicduplessis
Copy link
Contributor Author

I'll make another PR for the ListView fix.

rozele pushed a commit to microsoft/react-native-windows that referenced this pull request Sep 27, 2016
Summary:
This adds support for `Animated.event` driven natively. This is WIP and would like feedback on how this is implemented.

At the moment, it works by providing a mapping between a view tag, an event name, an event path and an animated value when a view has a prop with a `AnimatedEvent` object. Then we can hook into `EventDispatcher`, check for events that target our view + event name and update the animated value using the event path.

For now it works with the onScroll event but it should be generic enough to work with anything.
Closes facebook/react-native#9253

Differential Revision: D3759844

Pulled By: foghina

fbshipit-source-id: 86989c705847955bd65e6cf5a7d572ec7ccd3eb4
nihgwu added a commit to nihgwu/react-native that referenced this pull request Dec 7, 2016
facebook-github-bot pushed a commit that referenced this pull request Dec 14, 2016
Summary:
suggested by janicduplessis in #9253 (comment)
I'm using this in my own apps
Closes #11339

Differential Revision: D4325343

fbshipit-source-id: f1da26a2107093865f04e1d81245b33482776001
DanielMSchmidt pushed a commit to DanielMSchmidt/react-native that referenced this pull request Jan 4, 2017
Summary:
suggested by janicduplessis in facebook#9253 (comment)
I'm using this in my own apps
Closes facebook#11339

Differential Revision: D4325343

fbshipit-source-id: f1da26a2107093865f04e1d81245b33482776001
This pull request was closed.
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.