Community.js 9.83 KB
'use strict';

import React from 'react';
import ReactNative, {
	AppRegistry,
	Platform,
	StyleSheet,
	Dimensions,
	TouchableOpacity,
	View,
	Text,
	Image,
	Alert,
	Animated,
} from 'react-native';

import {
	Provider,
	connect
} from 'react-redux';

import RNRF, {
	Router,
	Scene,
	TabBar,
	Actions
} from 'react-native-router-flux';

import configureStore from './store/configureStore';

import appInitialState from './reducers/app/appInitialState';
import homeInitialState from './reducers/home/homeInitialState';
import sectionInitialState from './reducers/section/sectionInitialState';
import postingInitialState from './reducers/posting/postingInitialState';

import userInitialState from './reducers/user/userInitialState';

import HomeContainer from './containers/HomeContainer';
import SectionContainer from './containers/SectionContainer';
import UserContainer from	'./containers/UserContainer';
import SubjectPostContainer from './containers/SubjectPostContainer';
import PostingContainer from './containers/PostingContainer';

import NavBar from './components/NavBar';

import {
	setPlatform,
	setContianer,
	setChannel,
} from './reducers/app/appActions';

import {
	setUid,
} from './reducers/user/userActions';

import Immutable, {List, Record} from 'immutable';

import {
	startPosting,
	mergeCachedPosting,
	getPostUser,
	startEditPost,
} from './reducers/posting/postingActions';
import PostingService from './services/PostingService';

/**
 *
 * ## Initial state
 * Create instances for the keys of each structure in snowflake
 * @returns {Object} object with 4 keys
 */
function getInitialState() {
	const _initState = {
		app: (new appInitialState()),
    	home: (new homeInitialState()),
		section: (new sectionInitialState()),
		posting: (new postingInitialState()),
		user: (new userInitialState()),
	};
	return _initState;
}


let titleMarginLeft = Math.ceil(70 * Dimensions.get('window').width / 320);
let titleWidth = Math.ceil(180 * Dimensions.get('window').width / 320);
let titleItemWidth = Math.ceil(titleWidth / 3) - 3;


export default function community(platform) {

  	let YH_Community = React.createClass({

		contanerIsYohoBuy() {
			return parseInt(this.props.container) === 1;
		},

		render() {

		  	const store = configureStore(getInitialState());

			ReactNative.NativeModules.YH_CommunityHelper.uid()
				.then(uid => {
					store.dispatch(setUid(parseInt(uid)));
				});

		  	//Connect w/ the Router
		  	const NewRouter = connect()(Router);

			let contaner = parseInt(this.props.container);

			store.dispatch(setPlatform(platform));
			store.dispatch(setContianer(contaner));
			store.dispatch(setChannel(this.props.channel));
			store.dispatch(mergeCachedPosting());

			let navBarStyle = this.contanerIsYohoBuy() ? null : {backgroundColor: 'white',};
			let extraTitleStyle = this.contanerIsYohoBuy() ? null : {color: 'black',};

			let backImage = this.contanerIsYohoBuy() ? require('./images/home/menu_back1.png') : require('./images/home/menu_back2.png');
			let leftImage = this.contanerIsYohoBuy() ? require('./images/home/menu_burger1.png') : require('./images/home/menu_burger2.png');
			let rightImage = this.contanerIsYohoBuy() ? require('./images/home/menu_write1.png') : require('./images/home/menu_write2.png');

		  	// setup the router table with App selected as the initial component
		  	return (
				<Provider store={store}>
					<NewRouter hideNavBar={false} sceneStyle={styles.scene}>
						<Scene
							key="root"
							hideNavBar={false}
							hideTabBar={true}
							navBar={NavBar}
							navigationBarStyle={navBarStyle}
							titleStyle={[styles.navTitle, extraTitleStyle]}
							leftButtonStyle={styles.leftButton}
							rightButtonStyle={styles.rightButton}
							backButtonImage={backImage}
							getSceneStyle={(props) => {
								return this.navPushStyle(props);
							}}
							getChannel={() => store.getState().app.channel}
						>
							<Scene
								key="Home"
								title="社区"
								hideNavBar={false}
								component={HomeContainer}
								initial={true}
								leftTitle={null}
								leftButtonImage={leftImage}
								onLeft={() => {
									ReactNative.NativeModules.YH_CommunityHelper.toggleDrawer();
								}}
								rightTitle={null}
								rightButtonImage={rightImage}
								onRight={this.homeOnRight}
								renderTitle={this.contanerIsYohoBuy() ? this.renderHomeTitle : null}
								getPostingState={
									() => {
										return store.getState().posting.inPosting;
									}
								}
							/>

							<Scene
								key="Section"
								title="版块"
								hideNavBar={false}
								component={SectionContainer}
								initial={false}
								rightTitle={null}
								rightButtonImage={rightImage}
								onRight={this.homeOnRight}
								getTitle={(childState) => {
									let name = store.getState().section.get('name');
									return name ? name : '版块';
								}}
								getPostingState={
									() => {
										return store.getState().posting.inPosting;
									}
								}
							/>

							<Scene
								key="User"
								title="用户中心"
								hideNavBar={true}
								component={UserContainer}
								initial={false}
							/>


							<Scene
								key="SubjectPost"
								title="主题帖"
								hideNavBar={false}
								component={SubjectPostContainer}
								initial={false}
							/>

							<Scene
								key='Posting'
								title='主题帖'
								hideNavBar={false}
								component={PostingContainer}
								initial={false}
								rightTitle='发送'
								onRight={()=>{
									if (store.getState().posting.dataValid) {
										Actions.pop();
										store.dispatch(startPosting(store.getState().posting.assets.toJS()));
									} else {
										// 发布按钮:
										// 1.用户编辑内容时,“正文或标题”和“图片”其一必须有内容,否则右上角发布icon置灰,点击文本弹窗提示请输入内容。
										// 2.必须有选择的版块,否则右上角发布icon置灰,点击文本弹窗提示请选择版块。两者都没有优先提示请输入内容。
										//
										let message = '未知';
										if (!store.getState().posting.title.length&&!store.getState().posting.content.length) {
											message = '请填写标题和内容';
										} else if (!store.getState().posting.currentBoardName.length) {
											message = '请务必选择模块';
										}
										Alert.alert('提示',message);
									}
								}}
								rightButtonTextStyle={[styles.barRightButtonText,{color:store.getState().posting.dataValid?'white':'#a0a0a0'}]}
							/>

						</Scene>
					</NewRouter>
		    	</Provider>
		 	);
		},

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



			return (
				<Animated.View style={[styles.title,]}>
					<TouchableOpacity onPress={() => {
						ReactNative.NativeModules.YH_CommunityHelper.hideRNNavBar(0);
					}}>
						<Text style={[styles.text, {}]}></Text>
					</TouchableOpacity>

					<View style={[{}]}>
						<Text style={[styles.text, {marginLeft: -10.5}]}>社区</Text>
						<Image
							style={{
								position: 'absolute',
								left: titleItemWidth / 2,
								top: -10,
								width: 20,
								height: 10,
								marginLeft: -10
							}}
							resizeMode={'contain'}
							source={require('./images/home/nav_new.png')}
						/>
					</View>

					<TouchableOpacity onPress={() => {
			            ReactNative.NativeModules.YH_CommunityHelper.hideRNNavBar(2);
					}}>
						<Image style={{left: -5.5, top: -0.5}} source={require('./images/home/sd_show_ic.png')}/>
						<Image
							style={{
								position: 'absolute',
								left: titleItemWidth / 2 + 0,
								top: -10,
								width: 20,
								height: 10,
							}}
							resizeMode={'contain'}
							source={require('./images/home/nav_hot.png')}
						/>
					</TouchableOpacity>


					<View style={{
						backgroundColor: 'white',
						position: 'absolute',
						width: 45,
						height: 2,
						left: (titleWidth - 45) / 2 + 0,
						top: 30,
					}}/>
				</Animated.View>
			);
		},

		homeOnRight(state) {
			console.log(state);
			state.dispatch(startEditPost(state.getPostingState()));
		},
		/*
		 * 自定义导航push动画
		 *
		 * 参考 node_modules/react-native/Libraries/CustomComponents/
		 *     Navgationxperimental/NavigationCardStackStyleInterpolator
		 * 		function forHorizontal(props)
		 */
		navPushStyle(props) {
			const {
			  layout,
			  position,
			  scene,
			} = props;

			const index = scene.index;
			const inputRange = [index - 1, index, index + 1];
			const width = layout.initWidth;

			const opacity = position.interpolate({
				inputRange,
				outputRange: [1, 1, 0.3],
			});

			const scale = position.interpolate({
				inputRange,
				outputRange: [1, 1, 1],
			});

			const translateY = 0;
			const translateX = position.interpolate({
				inputRange,
				outputRange: [width, 0, -10],
			});

			return {
				opacity,
				transform: [
					{ scale },
					{ translateX },
					{ translateY },
				],
			};
		},
	});

	AppRegistry.registerComponent('YH_Community', () => YH_Community);
}

let styles = StyleSheet.create({
	scene: {
		backgroundColor: '#F0F0F0',
	},
    navTitle: {
        color: 'white',
		marginLeft: 60,
		marginRight: 60,
    },
	leftButton: {
		width: 60,
		left: 6,
		marginTop: 3.5,
	},
	rightButton: {
	  	width: 60,
		// right: 6,
		// marginTop: 3.5,
	},
	barRightButtonText: {
	  color: 'white',
	  textAlign: 'right',
	  fontSize: 17,
	},
	title: {
		flexDirection: 'row',
		justifyContent: 'space-between',
		alignItems: 'center',
		width: titleWidth,
		marginTop: 12,
		marginLeft: titleMarginLeft,
		  position: 'absolute',
		...Platform.select({
			ios: {
				top: 20,
			},
			android: {
				top: 5,
			},
		}),
		left: 0,
		right: 0,
		backgroundColor: 'transparent',
    },
	text: {
		textAlign: 'center',
		fontSize: 18,
        fontWeight: 'bold',
        color: '#ffffff',
		width: titleItemWidth,
		marginTop: 1,
	},
});