<commit_before>import React, { Component } from 'react';
import { View, Animated } from 'react-native';
import _ from 'lodash';

class TriggeringView extends Component {

  constructor(props) {
    super(props);
    this.state = {
      touched: false,
      hidden: false,
    };
    this.onScroll = this.onScroll.bind(this);
  }

  componentWillMount() {
    if (!this.context.scrollY) {
      return;
    }
    this.listenerId = this.context.scrollY.addListener(this.onScroll);
  }

  componentWillReceiveProps(nextProps, nextContext) {
    if (!this.context.scrollY) {
      return;
    }
    this.context.scrollY.removeListener(this.listenerId);
    nextContext.scrollY.addListener(this.onScroll);
  }

  onScroll() {
    this.containerView.measure((x, y, width, height, pageX, pageY) => {
      this.triggerEvents(this.context.scrollPageY, pageY, pageY + height);
    });
  }

  triggerEvents(value, top, bottom) {
    if (!this.state.touched && value >= top) {
      this.setState({ touched: true });
      this.props.onBeginHidden();
      this.props.onTouchTop(true);
    } else if (this.state.touched && value < top) {
      this.setState({ touched: false });
      this.props.onDisplay();
      this.props.onTouchTop(false);
    }

    if (!this.state.hidden && value >= bottom) {
      this.setState({ hidden: true });
      this.props.onHide();
      this.props.onTouchBottom(true);
    } else if (this.state.hidden && value < bottom) {
      this.setState({ hidden: false });
      this.props.onBeginDisplayed();
      this.props.onTouchBottom(false);
    }
  }

  render() {
    const viewProps = _.pick(this.props, _.keys(View.propTypes));
    return (
      <View
        ref={(view) => { this.containerView = view; }}
        {...viewProps}
      >
        { this.props.children }
      </View>
    );
  }
}
TriggeringView.propTypes = {
  onBeginHidden: React.PropTypes.func,
  onHide: React.PropTypes.func,
  onBeginDisplayed: React.PropTypes.func,
  onDisplay: React.PropTypes.func,
  onTouchTop: React.PropTypes.func,
  onTouchBottom: React.PropTypes.func,
  ...View.propTypes,
};

TriggeringView.defaultProps = {
  onBeginHidden: () => {},
  onHide: () => {},
  onBeginDisplayed: () => {},
  onDisplay: () => {},
  onTouchTop: () => {},
  onTouchBottom: () => {},
};

TriggeringView.contextTypes = {<commit_msg>Fix TriggerView not calling measure callback<commit_after>import React, { Component } from 'react';
import { View, Animated } from 'react-native';
import _ from 'lodash';

class TriggeringView extends Component {

  constructor(props) {
    super(props);
    this.state = {
      touched: false,
      hidden: false,
    };
    this.onScroll = this.onScroll.bind(this);
  }

  componentWillMount() {
    if (!this.context.scrollY) {
      return;
    }
    this.listenerId = this.context.scrollY.addListener(this.onScroll);
  }

  componentWillReceiveProps(nextProps, nextContext) {
    if (!this.context.scrollY) {
      return;
    }
    this.context.scrollY.removeListener(this.listenerId);
    nextContext.scrollY.addListener(this.onScroll);
  }

  onScroll() {
    this.containerView.measure((x, y, width, height, pageX, pageY) => {
      this.triggerEvents(this.context.scrollPageY, pageY, pageY + height);
    });
  }

  triggerEvents(value, top, bottom) {
    if (!this.state.touched && value >= top) {
      this.setState({ touched: true });
      this.props.onBeginHidden();
      this.props.onTouchTop(true);
    } else if (this.state.touched && value < top) {
      this.setState({ touched: false });
      this.props.onDisplay();
      this.props.onTouchTop(false);
    }

    if (!this.state.hidden && value >= bottom) {
      this.setState({ hidden: true });
      this.props.onHide();
      this.props.onTouchBottom(true);
    } else if (this.state.hidden && value < bottom) {
      this.setState({ hidden: false });
      this.props.onBeginDisplayed();
      this.props.onTouchBottom(false);
    }
  }

  render() {
    const viewProps = _.pick(this.props, _.keys(View.propTypes));
    return (
      <View
        ref={(view) => { this.containerView = view; }}
        collapsable={false}
        {...viewProps}
      >
        { this.props.children }
      </View>
    );
  }
}
TriggeringView.propTypes = {
  onBeginHidden: React.PropTypes.func,
  onHide: React.PropTypes.func,
  onBeginDisplayed: React.PropTypes.func,
  onDisplay: React.PropTypes.func,
  onTouchTop: React.PropTypes.func,
  onTouchBottom: React.PropTypes.func,
  ...View.propTypes,
};

TriggeringView.defaultProps = {
  onBeginHidden: () => {},
  onHide: () => {},
  onBeginDisplayed: () => {},
  onDisplay: () => {},
  onTouchTop: () => {},
  onTouchBottom: () => {},
};

TriggeringView.contextTypes = {
{'fraction_non_alphanumeric': 0.1564021164021164, 'fraction_numerical': 0.0, 'mean_word_length': 2.961441743503772, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 27, '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': '4188427', 'n_tokens_mistral': 1542, 'n_tokens_neox': 1529, 'n_words': 258}