HomeContainer.js 3.68 KB
'use strict'

import React, {Component} from 'react';
import ReactNative, {
    StyleSheet,
    Dimensions,
    Platform,
    View,
    Text,
    NativeModules,
    InteractionManager,
    NativeAppEventEmitter,
} from 'react-native'

import {bindActionCreators} from 'redux';
import {connect} from 'react-redux';
import {Map} from 'immutable';
import * as homeActions from '../reducers/home/homeActions';
import * as appActions from '../reducers/app/appActions';

import Home from '../components/home/Home';

const actions = [
    homeActions,
    appActions
];

function mapStateToProps(state) {
    return {
        ...state
    };
}

function mapDispatchToProps(dispatch) {

    const creators = Map()
        .merge(...actions)
        .filter(value => typeof value === 'function')
        .toObject();

    return {
        actions: bindActionCreators(creators, dispatch),
        dispatch
    };
}

class HomeContainer extends Component {
    constructor(props) {
        super(props);

        this.onRefresh = this.onRefresh.bind(this);
        this.onEndReached = this.onEndReached.bind(this);
        this.onPressAnnounceItem = this.onPressAnnounceItem.bind(this);
        this.onPressSlideItem = this.onPressSlideItem.bind(this);
        this.onPressPicItem = this.onPressPicItem.bind(this);
        this.onPressImageItem = this.onPressImageItem.bind(this);


        this.subscription = NativeAppEventEmitter.addListener(
            'ChannelDidChangeEvent',
            (reminder) => {
                this.props.actions.setChannel(reminder.channel);
                this.home && this.home.trigggePullToRefresh();
            }
        );
    }

    componentDidMount() {
        ReactNative.NativeModules.YH_CommonHelper.currentChannel()
        .then(result =>{
            this.props.actions.setChannel(result[0]);
            this.home && this.home.trigggePullToRefresh();
        })
    }

    componentWillUnmount() {
        this.subscription && this.subscription.remove();
    }

    onRefresh() {
        InteractionManager.runAfterInteractions(() => {
            this.props.actions.fetchFloor();
        });
    }

    onEndReached() {
        InteractionManager.runAfterInteractions(() => {

        });
    }

    onPressAnnounceItem(url, index) {
        if (!url) {
            return;
        }

        ReactNative.NativeModules.YH_CommonHelper.jumpWithUrl(url);
    }

    onPressSlideItem(url, index) {
        if (!url) {
            return;
        }
        
        ReactNative.NativeModules.YH_CommonHelper.jumpWithUrl(url);
    }

    onPressPicItem(url, index) {
        if (!url) {
            return;
        }
        
        ReactNative.NativeModules.YH_CommonHelper.jumpWithUrl(url);
    }

    onPressImageItem(data, index=0){
        if(data && data.url){
            ReactNative.NativeModules.YH_CommonHelper.jumpWithUrl(data.url);
        }
    }

    render() {
        let {app, home} = this.props;
        return (
            <View style={styles.container}>
                <Home
                    ref={(c) => {
                        this.home = c;
                    }}
                    data={home}
                    onRefresh={this.onRefresh}
                    onEndReached={this.onEndReached}
                    channel={app.channel}
                    onPressAnnounceItem={this.onPressAnnounceItem}
                    onPressSlideItem={this.onPressSlideItem}
                    onPressPicItem={this.onPressPicItem}
                    onPressImageItem={this.onPressImageItem}
                />
            </View>
        );
    }
}

let styles = StyleSheet.create({
    container: {
        flex: 1,
    },

});

export default connect(mapStateToProps, mapDispatchToProps)(HomeContainer);