RecommendContentOne.js 6.37 KB
'use strict';

import React from 'react';
import ReactNative from 'react-native';
import {getSlicedUrl} from '../../../classify/utils/Utils';
import Immutable, {Map} from 'immutable';
import DeviceInfo from 'react-native-device-info';

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

export default class RecommendContentOne extends React.Component {
    constructor(props) {
      super(props);
    }

    shouldComponentUpdate(nextProps){

        if (Immutable.is(nextProps.resource, this.props.resource)) {
            return false;
        } else {
            return true;
        }
    }

    render() {
		let fontFamilyStyle = {};
		if (Platform.OS === 'ios') {
			let systemVersion = DeviceInfo.getSystemVersion();
			systemVersion = parseFloat(systemVersion);
			if (systemVersion >= 9.0) {
				fontFamilyStyle = {fontFamily: 'PingFang SC'};
			}
		}

        let data = this.props.resource.get('data');
        let res = data.toJS();
		let big_imageData = res.big_image;
		let list = res.list;
		let newSrc = getSlicedUrl(big_imageData[0].src, 320, 200, 2);
        let newSrcUrl = big_imageData[0].url;
		let image0 = getSlicedUrl(list[0].src, 320, 200, 2);
        let image0_url = list[0].url;
		let image1 = getSlicedUrl(list[1].src, 320, 200, 2);
        let image1_url = list[1].url;
		let image2 = getSlicedUrl(list[2].src, 320, 200, 2);
        let image2_url = list[2].url;
		let image3 = getSlicedUrl(list[3].src, 320, 200, 2);
        let image3_url = list[3].url;
		let image4 = getSlicedUrl(list[4].src, 320, 200, 2);
        let image4_url = list[4].url;

        let title = res.title;

		if (!this.props.resource) {
			return null;
		}

		return(
			<View style={styles.container}>
				<View style={{width: width,height: 0.5,backgroundColor: '#e5e5e5',}}/>
				<View style={styles.title}>
					<Text style={[styles.text, fontFamilyStyle]}>{title.title}</Text>
				</View>
                <TouchableOpacity activeOpacity={0.5}  onPress={() => {
                    this.props.onPressRecommendItem && this.props.onPressRecommendItem(newSrcUrl);
                }}>
    				<Image
    					source={{uri: newSrc}}
    					style={styles.largeImage}
    					resizeMode={'contain'}
    				></Image>
                </TouchableOpacity>
				<View style={styles.detailContainer}>
                    <TouchableOpacity activeOpacity={0.5}  onPress={() => {
                        this.props.onPressRecommendItem && this.props.onPressRecommendItem(image0_url);
                    }}>
    					<View style={styles.thumbnailLeft}>
    						<Image
    							source={{uri: image0}}
    							style={styles.thumbnailLeft}
    						></Image>
    					</View>
                    </TouchableOpacity>
					<View style={{width: 0.5,height: detailImageH,backgroundColor: '#e5e5e5',}}/>
					<View style={styles.thumbnailRight}>
                        <TouchableOpacity activeOpacity={0.5}  onPress={() => {
                            this.props.onPressRecommendItem && this.props.onPressRecommendItem(image1_url);
                        }}>
    						<View style={styles.thumbnail}>
    							<Image
    								source={{uri: image1}}
    								style={styles.thumbnail}
    								resizeMode={'contain'}
    							></Image>
    						</View>
                        </TouchableOpacity>
						<View style={{width: thumbW,height: 0.5,backgroundColor: '#e5e5e5',}}/>
                        <TouchableOpacity activeOpacity={0.5}  onPress={() => {
                            this.props.onPressRecommendItem && this.props.onPressRecommendItem(image2_url);
                        }}>
    						<View style={styles.thumbnail}>
    							<Image
    								source={{uri: image3}}
    								style={styles.thumbnail}
    								resizeMode={'contain'}
    							></Image>
    						</View>
                        </TouchableOpacity>
					</View>
					<View style={{width: 0.5,height: detailImageH,backgroundColor: '#e5e5e5',}}/>
					<View style={styles.thumbnailRight}>
                        <TouchableOpacity activeOpacity={0.5}  onPress={() => {
                            this.props.onPressRecommendItem && this.props.onPressRecommendItem(image3_url);
                        }}>
    						<View style={styles.thumbnail}>
    							<Image
    								source={{uri: image2}}
    								style={styles.thumbnail}
    								resizeMode={'contain'}
    							></Image>
						    </View>
                        </TouchableOpacity>
						<View style={{width: thumbW,height: 0.5,backgroundColor: '#e5e5e5',}}/>
                        <TouchableOpacity activeOpacity={0.5}  onPress={() => {
                            this.props.onPressRecommendItem && this.props.onPressRecommendItem(image4_url);
                        }}>
					    	<View style={styles.thumbnail}>
    							<Image
    								source={{uri: image4}}
    								style={styles.thumbnail}
    								resizeMode={'contain'}
    							></Image>
    						</View>
                        </TouchableOpacity>
				    	</View>
				    </View>
				<View style={{width: width,height: 0.5,backgroundColor: '#e5e5e5',}}/>
			</View>
		);
	}
};


let {width, height} = Dimensions.get('window');
let itemWidth= Math.ceil(width/3);
let itemHeight = itemWidth;
let largeImageW = width;
let largeImageH = Math.ceil((200/320)*width);
let detailImageH = Math.ceil((190/320)*width);
let image0W = Math.ceil((258/380)*detailImageH);
let thumbW = (width - image0W)/2;
let thumbH = detailImageH/2;

let styles = StyleSheet.create({
	container: {
		height: detailImageH + largeImageH + 40,
		width:width,
        backgroundColor: 'white',
    },
	title: {
		alignItems: 'center',
		justifyContent: 'center',
		height: 39,
		width:width,
        backgroundColor: 'white',
    },
	text: {
		textAlign: 'center',
		fontSize: 16,
		fontWeight: 'bold',
		color: '#444',
    },
	largeImage: {
		width: largeImageW,
		height: largeImageH,
		backgroundColor: 'white',
	},
	detailContainer: {
		flexDirection: 'row',
		alignItems: 'center',
        width: width - 1,
        height: detailImageH - 0.5,
        backgroundColor: '#f0f0f0',
	},
	thumbnailLeft: {
		width: image0W,
		height: detailImageH,
		backgroundColor: '#f0f0f0',
	},
	thumbnailRight: {
		width: (width - image0W)/2,
		height: detailImageH,
		backgroundColor: '#f0f0f0',
		flexDirection: 'column',
	},
    thumbnail: {
		width: thumbW,
		height: thumbH,
        backgroundColor: '#f0f0f0',
	},
});