<commit_before>"use strict";

import React from "react-native";
let {
    Dimensions,
    StyleSheet,
    View,
    ScrollView,
    PropTypes,
    Component,
    TouchableWithoutFeedback
} = React;

import styles from "./styles";

let { width } = Dimensions.get("window");

export default class Carousel extends Component {

    displayName = "Carousel";

    static propTypes = {
        pageStyle: PropTypes.object,
        pageWidth: PropTypes.number,
        children: PropTypes.array,
        initialPage: PropTypes.number,
        onPageChange: PropTypes.func,
        sneak: PropTypes.number
    };

    static defaultProps = {
        initialPage: 0,
        pageStyle: null,
        pageWidth: width - 100,
        sneak: 20
    };

    state = {
        activePage: this.props.initialPage || 0
    };

    componentWillMount() {
        this.calculateGap(this.props);
    }

    componentDidMount() {
        if (this.props.initialPage > 0) {
            this.goToPage(this.props.initialPage);
        }
    }

    componentWillReceiveProps(props) {
        this.calculateGap(props);
    }

    calculateGap(props) {
        /*
         ------------
        |      v--- page
        |-   ----   -|
        | | |    | | |
        | | |    | | |
        | | |    | | |
        |-   ----   -|
        |^-- sneak   |
        |         ^--- gap
         ------------
        */
        let { sneak, pageWidth } = props;
        if (pageWidth > width) {
            throw new Error("invalid pageWith");
        }
        let gap = (width - (2 * sneak) - pageWidth) / 2;
        this.setState({gap: gap});
    }

    goToPage(position) {
        let { pageWidth } = this.props;
        let { gap } = this.state;
        let pagePosition = position * (pageWidth + gap);
        this.scrollView.scrollTo(0, pagePosition);
    }

    handleScrollEnd = (e) => {
        let { pageWidth } = this.props;
        let { gap } = this.state;
        let pageOffset = pageWidth + gap;
        //select page based on the position of the middle of the screen.
        let currentPosition = e.nativeEvent.contentOffset.x + (width / 2);
        // var activePage = e.nativeEvent.contentOffset.x;// / this.props.width;
        let currentPage = ~~(currentPosition / pageOffset);
        this.scrollView.scrollTo(0, currentPage * pageOffset);
        this.setState({activePage:currentPage});

        if (this.props.onPageChange) {
            this.props.onPageChange(currentPage);
        }
    };

    render() {

        let { sneak, pageWidth } = this.props;
        let { gap } = this.state;
        let computedStyles = StyleSheet.create({
            scrollView: {
                paddingLeft: sneak + gap / 2,
                paddingRight: sneak + gap / 2
            },
            page: {
                width: pageWidth,
                justifyContent: "center",
                marginLeft: gap / 2,
                marginRight: gap / 2
            }
        });<commit_msg>Fix for scrollTo(y,x,animated) deprecation<commit_after>"use strict";

import React from "react-native";
let {
    Dimensions,
    StyleSheet,
    View,
    ScrollView,
    PropTypes,
    Component,
    TouchableWithoutFeedback
} = React;

import styles from "./styles";

let { width } = Dimensions.get("window");

export default class Carousel extends Component {

    displayName = "Carousel";

    static propTypes = {
        pageStyle: PropTypes.object,
        pageWidth: PropTypes.number,
        children: PropTypes.array,
        initialPage: PropTypes.number,
        onPageChange: PropTypes.func,
        sneak: PropTypes.number
    };

    static defaultProps = {
        initialPage: 0,
        pageStyle: null,
        pageWidth: width - 100,
        sneak: 20
    };

    state = {
        activePage: this.props.initialPage || 0
    };

    componentWillMount() {
        this.calculateGap(this.props);
    }

    componentDidMount() {
        if (this.props.initialPage > 0) {
            this.goToPage(this.props.initialPage);
        }
    }

    componentWillReceiveProps(props) {
        this.calculateGap(props);
    }

    calculateGap(props) {
        /*
         ------------
        |      v--- page
        |-   ----   -|
        | | |    | | |
        | | |    | | |
        | | |    | | |
        |-   ----   -|
        |^-- sneak   |
        |         ^--- gap
         ------------
        */
        let { sneak, pageWidth } = props;
        if (pageWidth > width) {
            throw new Error("invalid pageWith");
        }
        let gap = (width - (2 * sneak) - pageWidth) / 2;
        this.setState({gap: gap});
    }

    goToPage(position) {
        let { pageWidth } = this.props;
        let { gap } = this.state;
        let pagePosition = position * (pageWidth + gap);
        this.scrollView.scrollTo({y: 0, x: pagePosition});
    }

    handleScrollEnd = (e) => {
        let { pageWidth } = this.props;
        let { gap } = this.state;
        let pageOffset = pageWidth + gap;
        //select page based on the position of the middle of the screen.
        let currentPosition = e.nativeEvent.contentOffset.x + (width / 2);
        // var activePage = e.nativeEvent.contentOffset.x;// / this.props.width;
        let currentPage = ~~(currentPosition / pageOffset);
        this.scrollView.scrollTo({y: 0, x: currentPage * pageOffset});
        this.setState({activePage:currentPage});

        if (this.props.onPageChange) {
            this.props.onPageChange(currentPage);
        }
    };

    render() {

        let { sneak, pageWidth } = this.props;
        let { gap } = this.state;
        let computedStyles = StyleSheet.create({
            scrollView: {
                paddingLeft: sneak + gap / 2,
                paddingRight: sneak + gap / 2
            },
            page: {
                width: pageWidth,
                justifyContent: "center",
                marginLeft: gap / 2,
                marginRight: gap / 2
            }
        });
{'fraction_non_alphanumeric': 0.1279011521122057, 'fraction_numerical': 0.005677074636834196, 'mean_word_length': 1.5642123287671232, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '429477', 'n_tokens_mistral': 1769, 'n_tokens_neox': 1711, 'n_words': 371}