YH_RefreshingIndicator.js 3.65 KB
'use strict';

import React from 'react';
import ReactNative from 'react-native';
import ImagesAsGif from '../components/ImagesAsGif';

const {
    PropTypes,
    isValidElement,
    createElement,
} = React;

const {
    View,
    Text,
    Image,
    TouchableOpacity,
    ActivityIndicator,
    StyleSheet,
    Dimensions,
} = ReactNative;

let {width, height} = Dimensions.get('window');

export default class YH_RefreshingIndicator extends React.Component {

    static propTypes = {
        activityIndicatorComponent: PropTypes.oneOfType([PropTypes.func, PropTypes.element]),
        stylesheet: PropTypes.object,
        isTouching: PropTypes.bool,
        isRefreshing: PropTypes.bool,
        isWaitingForRelease: PropTypes.bool,
    };

    static defaultProps = {
        activityIndicatorComponent: ActivityIndicator,
        isTouching: false,
        isRefreshing: false,
        isWaitingForRelease: false,
    };

    constructor(props) {
        super (props);



    }

    renderActivityIndicator(styles) {
        var activityIndicator
        if (this.props.isTouching && this.props.isWaitingForRelease) {
            activityIndicator = <Image source={require('./images/Boy/refresh_000.png')}/>
        } else if (this.props.isTouching && !this.props.isWaitingForRelease) {
            activityIndicator = <Image source={require('./images/Boy/refresh_000.png')}/>
        } else if (!this.props.isTouching && this.props.isWaitingForRelease) {
            activityIndicator = <Image source={require('./images/Boy/refresh_000.png')}/>
        } else if (this.props.isRefreshing) {
            let images = [
                require('./images/Boy/refresh_000.png'),
                require('./images/Boy/refresh_001.png'),
                require('./images/Boy/refresh_002.png'),
                require('./images/Boy/refresh_003.png'),
                require('./images/Boy/refresh_004.png'),
                require('./images/Boy/refresh_005.png'),
                require('./images/Boy/refresh_006.png'),
                require('./images/Boy/refresh_007.png'),
                require('./images/Boy/refresh_008.png'),
                require('./images/Boy/refresh_009.png'),
                require('./images/Boy/refresh_010.png'),
                require('./images/Boy/refresh_011.png'),
                require('./images/Boy/refresh_012.png'),
            ];
            activityIndicator = <ImagesAsGif images={images} duration={100} />
            // activityIndicator = <Image source={require('./images/Boy/loading.gif')} style={{width: 80, height: 20}}/>
        }

        if (activityIndicator) {
            if (isValidElement(activityIndicator)) return activityIndicator
            // is a component class, not an element
            return createElement(activityIndicator, {style: styles.activityIndicator})
        }

        return null;
    }

    render() {
        var styles = Object.assign({}, stylesheet, this.props.stylesheet)
        return (
            <View style={[styles.wrapper]}>
                <View style={[styles.container, styles.loading, styles.content]}>
                    <View style={[styles.stack]}>
                        {this.renderActivityIndicator(styles)}
                    </View>
                </View>
            </View>
        );
    }
}

let stylesheet = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'flex-start',
        flexDirection: 'row',
    },
    stack: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        flexDirection: 'column',
    },
    wrapper: {
        height: 45,
    },
    content: {
        marginTop: 10,
        height: 40,
    },
});