<commit_before>// @flow
import React, { Component } from 'react';
import { View, Animated, Platform, StyleSheet } from 'react-native';
import Animation from 'lottie-react-native';

export default class LottieAnimation extends Component<void, PropsType, StateType> {
  state = {
    progress: new Animated.Value(0),
  };

  shouldComponentUpdate(nextProps: PropsType) {
    return this.props.source.nm !== nextProps.source.nm;
  }

  componentDidMount() {
    this.animate();
  }

  componentDidUpdate(prevProps: PropsType) {
    if (this.props.source.nm !== prevProps.source.nm) {
      this.animate();
    }
  }

  animate = () => {
    const animation = this.props.animation || this.getAnimation();
    this.state.progress.setValue(0);
    animation.start(this.props.loop ? this.animate : null);
  };

  getAnimation = () =>
    Animated.timing(this.state.progress, {
      toValue: 1,
      delay: this.props.delay,
      easing: this.props.easing ? this.props.easing : t => t,
      duration: this.props.duration || Math.round(this.props.source.op / this.props.source.fr * 1000),
      useNativeDriver: true,
    });

  render() {
    const { style, source, ...rest } = this.props;
    const aspectRatio = source.w / source.h;
    const styleObject = StyleSheet.flatten(style);
    return (
      <View
        style={[
          {
            aspectRatio,
          },
          (!styleObject || (styleObject.width === undefined && styleObject.height === undefined)) && {
            width: source.w,
          },
          style,
        ]}
      >
        <Animation
          progress={this.state.progress}
          style={{
            aspectRatio,
            width: '100%',
          }}
          source={Platform.select({
            ios: source,
            android: {
              ...source,
              fr: Math.round(source.fr),
              ip: Math.round(source.ip),
              op: Math.round(source.op),
            },
          })}
          {...rest}
        />
      </View>
    );
  }
}

type StateType = {
  progress: Animated.Value,
};

type PropsType = {
  style: any,
  source: Object, // require('path/to/animation.json')
  animation?: any, // CompositeAnimation
  loop?: boolean,
  duration?: number,
  delay?: number,
  easing?: Function,
};<commit_msg>Change LottieAnimation API<commit_after>// @flow
import React, { Component } from 'react';
import { View, Animated, Platform, StyleSheet } from 'react-native';
import Animation from 'lottie-react-native';

const linearEasing = t => t;

export default class LottieAnimation extends Component<void, PropsType, StateType> {
  state = {
    progress: new Animated.Value(0),
  };

  shouldComponentUpdate(nextProps: PropsType) {
    return this.props.source.nm !== nextProps.source.nm;
  }

  componentDidMount() {
    this.animate();
  }

  componentDidUpdate(prevProps: PropsType) {
    if (this.props.source.nm !== prevProps.source.nm) {
      this.animate();
    }
  }

  animate = () => {
    const animation = this.props.animation ? this.props.animation(this.state.progress) : this.getDefaultAnimation();
    this.state.progress.setValue(0);
    animation.start(this.props.loop ? this.animate : null);
  };

  getDefaultAnimation = () =>
    Animated.timing(this.state.progress, {
      toValue: 1,
      delay: this.props.delay,
      easing: this.props.easing ? this.props.easing : linearEasing,
      duration: this.props.duration || Math.round(this.props.source.op / this.props.source.fr * 1000),
      useNativeDriver: true,
    });

  render() {
    const { style, source, ...rest } = this.props;
    const aspectRatio = source.w / source.h;
    const styleObject = StyleSheet.flatten(style);
    return (
      <View
        style={[
          {
            aspectRatio,
          },
          (!styleObject || (styleObject.width === undefined && styleObject.height === undefined)) && {
            width: source.w,
          },
          style,
        ]}
      >
        <Animation
          progress={this.state.progress}
          style={{
            aspectRatio,
            width: '100%',
          }}
          source={Platform.select({
            ios: source,
            android: {
              ...source,
              fr: Math.round(source.fr),
              ip: Math.round(source.ip),
              op: Math.round(source.op),
            },
          })}
          {...rest}
        />
      </View>
    );
  }
}

type StateType = {
  progress: Animated.Value,
};

type PropsType = {
  style: any,
  source: Object, // require('path/to/animation.json')
  animation?: Animated.Value => Object, // Animated.Value => CompositeAnimation
  loop?: boolean,
  duration?: number,
  delay?: number,
  easing?: Function,
};
{'fraction_non_alphanumeric': 0.1438544531415274, 'fraction_numerical': 0.0042310133276919825, 'mean_word_length': 2.160427807486631, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5660929', 'n_tokens_mistral': 1441, 'n_tokens_neox': 1407, 'n_words': 280}