SuperMan.js 3.41 KB
'use strict';

import React from 'react';
import ReactNative from 'react-native';
import ImmutablePropTypes from 'react-immutable-proptypes';
import SlicedImage from '../../../common/components/SlicedImage';

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

export default class SuperMan extends React.Component {

    static propTypes = {
        uid: React.PropTypes.number,
        avatar: React.PropTypes.string,
        msgCount: React.PropTypes.string,
        onSaveingTheWorld: React.PropTypes.func,
    };

    constructor(props) {
        super (props);


    }

    renderText() {
        if (this.props.uid == 0 ) {
            return (
                <Text style={styles.loginText}>请登录</Text>
            );
        } else {
            return null;
        }
    }

    renderMsgCount() {
        if (this.props.uid > 0 && parseInt(this.props.msgCount) !== 0) {
            return (
                <View style={styles.textContainer}>
                    <Text style={styles.text}>{this.props.msgCount}</Text>
                </View>
            );
        } else {
            return null;
        }
    }

    render() {
        let avatarContainerStyle = this.props.uid > 0 && this.props.avatar ? styles.avatarContainer : null;
        let avatarStyle = this.props.uid > 0 && this.props.avatar ? styles.avatarBorder : null;
        let defaultSource = this.props.uid > 0 && this.props.avatar ? null : require('../../images/avatar/superman.png');
        return (
            <View style={[styles.container, this.props.fly]}>
                <TouchableOpacity activeOpacity={0.8} onPress={() => {
                    this.props.onSaveingTheWorld && this.props.onSaveingTheWorld();
                }}>
                    <View style={avatarContainerStyle}>
                        <SlicedImage style={[styles.avatar, avatarStyle]} source={{uri: this.props.avatar}} resizeMode={'cover'} defaultSource={defaultSource}/>
                    </View>
                    {this.renderText()}
                    {this.renderMsgCount()}
                </TouchableOpacity>
            </View>
        );
    }
}

let styles = StyleSheet.create({
    container: {
        position: 'absolute',
        right: 20,
        bottom: 20,
        flexDirection: 'column',
        alignItems: 'center',
        backgroundColor: 'transparent',
        width: 45,
        height: 45,
        justifyContent: 'center',
    },
    avatarContainer: {
        width: 44,
        height: 44,
        borderRadius: 22,
        backgroundColor: 'white',
        shadowColor: 'darkgray',
        shadowOpacity: 1,
        shadowOffset: {width: 0, height: 0},
    },
    avatar: {
        width: 44,
        height: 44,
        borderRadius: 22,
    },
    avatarBorder: {
        borderColor: 'white',
        borderWidth: 1,
    },
    loginText: {
        marginTop: 2,
        fontSize: 10,
        textAlign: 'center',
    },
    textContainer: {
        position: 'absolute',
        top: -4,
        right: -4,
        height: 18,
        borderRadius: 9,
        borderColor: 'red',
        borderWidth: 1,
        justifyContent: 'center',
        alignItems: 'center',
        opacity: 1,
        backgroundColor: 'white',
    },
    text: {
        fontSize: 12,
        color: 'red',
        textAlign: 'center',
        marginHorizontal: 6,
        marginVertical: 6,
        backgroundColor: 'transparent',
    },
});