UserThatNotMe.js 7.66 KB
'use strict';

import React from 'react';
import ReactNative from 'react-native';
import ScrollableTabView from 'react-native-scrollable-tab-view';
import ImmutablePropTypes from 'react-immutable-proptypes';
import PropTypes from 'prop-types';
import UserCenterTop from '../user/UserCenterTop';
import ListCell from '../home/ListCell';
import LoadMoreIndicator from '../../../common/components/LoadMoreIndicator';
import UserNavBar from '../user/UserNavBar';
import EmptyListTip from '../user/EmptyListTip';

const {
    View,
    Text,
    ListView,
    Platform,
    Dimensions,
    StyleSheet,
    Animated,
} = ReactNative;


let {width, height} = Dimensions.get('window');
let navbarHeight = (Platform.OS === 'android') ? 50 : 64;
let userBgHieght = Math.ceil((490 / 750) * width);
let sectionBarHeight = 46;

export default class UserThatNotMe extends React.Component {

    static propTypes = {
        user: ImmutablePropTypes.contains({
            avatar: PropTypes.string,
            backgroundImage: PropTypes.string,
            nickName: PropTypes.string.isRequired,
            sign: PropTypes.string,
        }),
        posts: ImmutablePropTypes.listOf(
            ImmutablePropTypes.contains({
                author: ImmutablePropTypes.contains({
                    avatar: PropTypes.string,
                    uid: PropTypes.number,
                    name: PropTypes.string,
                }),
                timeago: PropTypes.string.isRequired,
                isOwner: PropTypes.bool,
                isTop: PropTypes.bool,
                isLike: PropTypes.bool,
                id: PropTypes.number,
                title: PropTypes.string,
                desc: PropTypes.string,
                thumbs: ImmutablePropTypes.listOf(
                    ImmutablePropTypes.contains({
                        src: PropTypes.string,
                    })
                ),
                section: ImmutablePropTypes.contains({
                    id: PropTypes.number,
                    name: PropTypes.string,
                }),
                commentCount: PropTypes.number,
                likeCount: PropTypes.number,
            }),
        ),
        onPressPost: PropTypes.func,
        onPressAvatar: PropTypes.func,
        onPressSectionTag: PropTypes.func,
        onPressComment: PropTypes.func,
        onPressLike: PropTypes.func,
        onEndReached: PropTypes.func,
    };


    constructor(props) {
        super (props);

        this._renderHeader = this._renderHeader.bind(this);
        this._renderRow = this._renderRow.bind(this);
        this._renderSectionHeader = this._renderSectionHeader.bind(this);
        this._renderSeparator = this._renderSeparator.bind(this);
        this.dataSource = new ListView.DataSource({
            rowHasChanged: (r1, r2) => !Immutable.is(r1, r2),
            sectionHeaderHasChanged: (s1, s2) => !Immutable.is(s1, s2),
        });

        this.state = {
            scrollValue: new Animated.Value(0),
        };

        this.initialPageHeight = height - userBgHieght;
    }

    componentDidMount() {

    }

    _renderHeader() {
        return (
            <UserCenterTop
                userInfo={this.props.user}
            />
        );
    }

    _renderSectionHeader(sectionData, sectionID) {
        switch (sectionID) {
			case 'posts':
                if (this.props.posts.size == 0) {
                    return null;
                }

				return (
                    <View style={styles.sectionHeader}>
                        <Text style={styles.sectionHeaderText}>TA的发布</Text>
                        <View style={styles.sectionHeaderTopLine}/>
                        <View style={styles.sectionHeaderBottomLine}/>
                    </View>
				);

            default:
                return null;
		}
    }

    _renderSeparator(sectionID, rowID, adjacentRowHighlighted) {
        return (
            <View key={'separator' + sectionID + rowID} style={styles.separator}/>
        );
    }

    _renderRow(rowData, sectionID, rowID, highlightRow) {
        switch (sectionID) {
			case 'posts':
				return (
                    <ListCell
                        key={sectionID + rowID}
                        data={rowData}
                        onPressPost={this.props.onPressPost}
                        onPressAvatar={this.props.onPressAvatar}
                        onPressSectionTag={this.props.onPressSectionTag}
                        onPressComment={this.props.onPressComment}
                        onPressLike={this.props.onPressLike}
                    />
				);
            default:
                return null;
		}
    }

    render() {
        let {posts, endReached, isRefreshing, isLoadingMore, isFetching} = this.props;
        let dataSource = {
            posts: posts.toArray(),
        }

        return (
            <View style={styles.container}>
                <ListView
                    ref={(c) => {
                        this.listView = c;
                    }}
                    onScroll={Animated.event([{
                        nativeEvent: {
                            contentOffset: {
                                y: this.state.scrollValue
                            }
                        }
                    }])}
                    dataSource={this.dataSource.cloneWithRowsAndSections(dataSource)}
                    renderHeader={this._renderHeader}
                    renderRow={this._renderRow}
                    renderSectionHeader={this._renderSectionHeader}
                    renderSeparator={this._renderSeparator}
                    enableEmptySections={true}
                    enablePullToRefresh={false}
                    onEndReached={() => {
                        this.props.onEndReached && this.props.onEndReached();
                    }}
                    renderFooter={()=>{
                        if (endReached) {
                            if (posts.size == 0) {
                                return <EmptyListTip
                                    style={{height: this.initialPageHeight}}
                                    text={'TA还没有发布过帖子'}
                                />
                            } else {
                                return <LoadMoreIndicator
                                    isVisible={true}
                                    text={'暂无更多'}
                                />
                            }
                        } else {
                            return <LoadMoreIndicator
                                isVisible={isLoadingMore}
                                animating={isFetching}
                            />
                        }
                    }}
                />

                <UserNavBar
                    scrollValue={this.state.scrollValue}
                    channel={this.props.channel}
                    onBack={this.props.onBack}
                />

            </View>
        )
    }
}


let styles = StyleSheet.create({
    container: {
        flex: 1,
    },
    sectionHeader: {
        flexDirection: 'row',
        height: sectionBarHeight,
        alignItems: 'center',
        backgroundColor: 'white',
    },
    sectionHeaderText: {
        fontSize: 18,
        marginLeft: 17,
        fontFamily: 'Helvetica Light',
    },
    sectionHeaderTopLine: {
        position: 'absolute',
        left: 0,
        top: 0,
        width: width,
        height: 0.5,
        backgroundColor: '#e0e0e0',
    },
    sectionHeaderBottomLine: {
        position: 'absolute',
        left: 17,
        bottom: 0,
        width: width - 17,
        height: 0.5,
        backgroundColor: '#e0e0e0',
    },
    separator: {
        height: 0.5,
        backgroundColor: '#e0e0e0',
    },
});