From 3292d8fe03602ac75e154263ad5565f3095e80b5 Mon Sep 17 00:00:00 2001 From: Janic Duplessis Date: Thu, 4 Aug 2016 13:11:37 -0700 Subject: [PATCH] Implement native Animated value listeners on Android Summary: Adds support for `Animated.Value#addListener` for native driven nodes on Android. This is based on work by skevy in the exponent RN fork. Also adds a UIExplorer example. ** Test plan ** Run unit tests Tested that by adding a listener to a native driven animated node and checked that the listener callback is called properly. Also tested that it doesn't crash on iOS that doesn't support this yet. Closes https://github.com/facebook/react-native/pull/8844 Differential Revision: D3670906 fbshipit-source-id: 15700ed7b93db140d907ce80af4dae6be3102135 --- UIExplorer/js/NativeAnimationsExample.js | 70 ++++++++++++++++++++---- 1 file changed, 60 insertions(+), 10 deletions(-) diff --git a/UIExplorer/js/NativeAnimationsExample.js b/UIExplorer/js/NativeAnimationsExample.js index ef92dae605f..bd7add201d2 100644 --- a/UIExplorer/js/NativeAnimationsExample.js +++ b/UIExplorer/js/NativeAnimationsExample.js @@ -22,16 +22,15 @@ */ 'use strict'; -var React = require('react'); -var ReactNative = require('react-native'); -var { +const React = require('react'); +const ReactNative = require('react-native'); +const { View, Text, Animated, StyleSheet, TouchableWithoutFeedback, } = ReactNative; -var UIExplorerButton = require('./UIExplorerButton'); class Tester extends React.Component { state = { @@ -47,12 +46,8 @@ class Tester extends React.Component { ...this.props.config, toValue: this.current, }; - try { - Animated[this.props.type](this.state.native, { ...config, useNativeDriver: true }).start(); - } catch (e) { - // uncomment this if you want to get the redbox errors! - throw e; - } + + Animated[this.props.type](this.state.native, { ...config, useNativeDriver: true }).start(); Animated[this.props.type](this.state.js, { ...config, useNativeDriver: false }).start(); }; @@ -78,6 +73,52 @@ class Tester extends React.Component { } } +class ValueListenerExample extends React.Component { + state = { + anim: new Animated.Value(0), + progress: 0, + }; + _current = 0; + + componentDidMount() { + this.state.anim.addListener((e) => this.setState({ progress: e.value })); + } + + componentWillUnmount() { + this.state.anim.removeAllListeners(); + } + + _onPress = () => { + this._current = this._current ? 0 : 1; + const config = { + duration: 1000, + toValue: this._current, + }; + + Animated.timing(this.state.anim, { ...config, useNativeDriver: true }).start(); + }; + + render() { + return ( + + + + + + Value: {this.state.progress} + + + ); + } +} + const styles = StyleSheet.create({ row: { padding: 10, @@ -304,4 +345,13 @@ exports.examples = [ ); }, }, + { + title: 'Animated value listener', + platform: 'android', + render: function() { + return ( + + ); + }, + }, ];