Community.js 11.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
'use strict';

import React from 'react';
import ReactNative, {
	AppRegistry,
	Platform,
	StyleSheet,
	Dimensions,
	TouchableOpacity,
	View,
	Text,
	Image,
	Alert,
	Animated,
	Keyboard,
} 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 subjectPostInitialState from './reducers/subject/subjectPostInitialState';
import userInitialState from './reducers/user/userInitialState';
import userThatNotMeInitialState from './reducers/userThatNotMe/userThatNotMeInitialState';
import likeListInitialState from './reducers/likeList/likeListInitialState';

import HomeContainer from './containers/HomeContainer';
import SectionContainer from './containers/SectionContainer';
import UserContainer from	'./containers/UserContainer';
import UserThatNotMeContainer from	'./containers/UserThatNotMeContainer';
import MessageCenterContainer from './containers/MessageCenterContainer';
import LikeMessageContainer from './containers/LikeMessageContainer';
import SubjectPostContainer from './containers/SubjectPostContainer';
import PostingContainer from './containers/PostingContainer';
import SettingContainer from './containers/UserSettingContainer';
import LikeListContainer from './containers/LikeListContainer';
import SystemMessageContainer from './containers/SystemMessageContainer';
import NavBar from './components/NavBar';
import NavBarTitle from './components/home/NavBarTitle';

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 {
	modifyUserInfo,
} from './reducers/user/userActions';

import {
	onRightPressed
} from './reducers/subject/subjectPostActions';

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 = {
		route: {},
		app: (new appInitialState()),
    	home: (new homeInitialState()),
		section: (new sectionInitialState()),
		posting: (new postingInitialState()),
		user: (new userInitialState()),
		subject: (new subjectPostInitialState()),
		userThatNotMe: (new userThatNotMeInitialState()),
		likeList: (new likeListInitialState()),
	};
	return _initState;
}

export default function community(platform) {

  	let YH_Community = React.createClass({

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

		componentWillUnmount() {
			this.storeSceneSubscribe && this.storeSceneSubscribe();
		},

		render() {

		  	const store = configureStore(getInitialState());

			this.storeSceneSubscribe = store.subscribe(() => {
				let route = store.getState().route;
				if (!route || !route.scene) {
					return;
				}
				let scene = route.scene;
				if (scene.sceneKey === 'Home') {
					ReactNative.NativeModules.YH_CommunityHelper.showTabBar();
				} else {
					ReactNative.NativeModules.YH_CommunityHelper.hideTabBar();
				}
			});

			ReactNative.NativeModules.YH_CommunityHelper.uid()
				.then(uid => {
					store.dispatch(setUid(uid));
				})
				.catch(error => {
					__DEV__ && console.log(error);
				});

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

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

			store.dispatch(setPlatform(platform));
			store.dispatch(setContianer(container));
			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}
								getSceneStyle={(props) => {
									return this.navPushStyle(props);
								}}
							/>

							<Scene
								key="Section"
								title="版块"
								hideNavBar={false}
								component={SectionContainer}
								initial={false}
								clone={true}
								rightTitle={null}
								rightButtonImage={rightImage}
								onRight={this.homeOnRight}
								getSceneStyle={(props) => {
									return this.navPushStyle(props);
								}}
								getTitle={(childState) => {
									let sid = store.getState().section.sid;
									let item = store.getState().section.items.get(sid);
									let name = item.get('name');
									return name ? name : '版块';
								}}
							/>

							<Scene
								key="User"
								title="个人中心"
								hideNavBar={true}
								component={UserContainer}
								initial={false}
								getSceneStyle={(props) => {
									return this.navPushStyle(props);
								}}
							/>

							<Scene
								key="MessageCenter"
								title="消息"
								hideNavBar={false}
								component={MessageCenterContainer}
								initial={false}
								getSceneStyle={(props) => {
									return this.navPushStyle(props);
								}}
							/>

							<Scene
								key="LikeMessage"
								title="收到的赞"
								hideNavBar={false}
								component={LikeMessageContainer}
								initial={false}
								getSceneStyle={(props) => {
									return this.navPushStyle(props);
								}}
							/>

							<Scene
								key="SystemMessage"
								title="系统消息"
								hideNavBar={false}
								component={SystemMessageContainer}
								initial={false}
								onBack={this.onBack}
								getSceneStyle={(props) => {
									return this.navPushStyle(props);
								}}
							/>

							<Scene
								key="Setting"
								title="社区资料"
								hideNavBar={false}
								component={SettingContainer}
								initial={false}
								rightTitle='确认'
								onRight={this.settingOnRight}
								rightButtonTextStyle={{color: 'white'}}
								getSceneStyle={(props) => {
									return this.navPushStyle(props);
								}}
								onBack={(navigationState) => {
									let dismissKeyboard = require('dismissKeyboard');
									dismissKeyboard();
									Actions.pop();
								}}
							/>

							<Scene
								key="UserThatNotMe"
								title="用户中心"
								hideNavBar={true}
								clone={true}
								component={UserThatNotMeContainer}
								initial={false}
								getSceneStyle={(props) => {
									return this.navPushStyle(props);
								}}
							/>


							<Scene
								key="SubjectPost"
								title="主题帖"
								hideNavBar={false}
								component={SubjectPostContainer}
								initial={false}
								onBack={this.onBack}
								rightButtonImage={require('./images/posting/share.png')}
								onRight={this.subjectPostOnRight}
								getSid={()=>{
									let sid = store.getState().subject.sid;
									return sid;
								}}
								getSceneStyle={(props) => {
									return this.navPushStyle(props);
								}}
								onBack={(navigationState) => {
									let dismissKeyboard = require('dismissKeyboard');
									dismissKeyboard();
									Actions.pop();
								}}
							/>

							<Scene
								key='LikeList'
								hideNavBar={false}
								component={LikeListContainer}
								initial={false}
								clone={true}
								getSceneStyle={(props) => {
									return this.navPushStyle(props);
								}}
							/>

							<Scene
								key='Posting'
								title='主题帖'
								hideNavBar={false}
								component={PostingContainer}
								initial={false}
								getSceneStyle={(props) => {
									return this.navPushStyle(props);
								}}
								rightTitle='发送'
								onRight={() => {
									store.dispatch(startPosting());
								}}
								rightButtonTextStyle={[styles.barRightButtonText, {color: store.getState().posting.canSubmit ? 'white' : '#a0a0a0'}]}
								onBack={(navigationState) => {
									let dismissKeyboard = require('dismissKeyboard');
									dismissKeyboard();
									Actions.pop();
								}}
							/>

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

		renderHomeTitle(navProps) {
			return (
				<NavBarTitle {...navProps}/>
			);
		},

		homeOnRight(state) {
			state.dispatch(startEditPost());
		},

		settingOnRight(state) {
			state.dispatch(modifyUserInfo());
		},

		subjectPostOnRight(state) {
			let sid = state.getSid();
			state.dispatch(onRightPressed(sid));
		},
		/*
		 * 自定义导航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,
	},
});