dashboard.js 3.9 KB
Newer Older
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
/**
 * Created by greg on 2016-06-27.
 */

import React, { Component } from 'react';
import {
    Navigator,
    TouchableOpacity,
    ScrollView,
    Text,
    View
} from 'react-native';
import TimerMixin from 'react-timer-mixin';
var reactMixin = require('react-mixin');

import styles from '../../styles/styles';
import BodyStore from '../../stores/body';
import DashboardItem from './item';


class Dashboard extends Component {

    constructor(props) {
        super(props);
        this.state = this._getStateObject();
    }

    componentDidMount() {
        this.unsub = BodyStore.listen(this._onBodyStoreEvent.bind(this));
    }

    componentWillUnmount() {
        this.unsub();
    }

    _onBodyStoreEvent(evt) {
        this.setState(this._getStateObject())
    }

    _getStateObject() {
        return {
            weightFormatted: BodyStore.GetWeightFormatted(),
            heightFormatted: BodyStore.GetHeightFormatted(),
            bmiFormatted: BodyStore.GetBMIFormatted(),
            bodyFatFormatted: BodyStore.GetBodyFatPercentageFormatted(),
            leanBodyMassFormatted: BodyStore.GetLeanBodyMassFormatted(),
        };
    }

    _onPressItem(key) {
        console.log('_onPressItem() ==> ', key);
        let self = this;
        this.requestAnimationFrame(() => {
            this.props.navigator.push({
                name: key
            });
        })
    }


    render() {
        return (
            <Navigator
                renderScene={this.renderScene.bind(this)}
                navigator={this.props.navigator}
                navigationBar={
                    <Navigator.NavigationBar style={styles.navigationBar}
                                             routeMapper={NavigationBarRouteMapper} />
                }/>
        );
    }

    renderScene(route, navigator) {
        return (
            <View style={styles.sceneContainerNavbar}>
                <ScrollView automaticallyAdjustContentInsets={false} style={{flex:1,}}>

                    <DashboardItem icon="scale"
                                   label="Weight"
                                   value={this.state.weightFormatted}
                                   onPress={this._onPressItem.bind(this, 'Weight')} />
                    <DashboardItem icon="ruler"
                                   label="Height"
                                   value={this.state.heightFormatted}
                                   onPress={this._onPressItem.bind(this, 'Height')} />
                    <DashboardItem icon="bmi"
                                   label="BMI"
                                   value={this.state.bmiFormatted}
                                   onPress={this._onPressItem.bind(this, 'BodyMassIndex')} />
                    <DashboardItem icon="bodyfat"
                                   label="Body Fat Percentage"
                                   value={this.state.bodyFatFormatted}
                                   onPress={this._onPressItem.bind(this, 'BodyFatPercentage')} />
                    <DashboardItem icon="musclemass"
                                   label="Lean Body Mass"
                                   value={this.state.leanBodyMassFormatted}
                                   onPress={this._onPressItem.bind(this, 'LeanBodyMass')} />

                </ScrollView>
            </View>
        );
    }
}

reactMixin(Dashboard.prototype, TimerMixin);

var NavigationBarRouteMapper = {
    LeftButton(route, navigator, index, nextState) {
        return null;
    },
    RightButton(route, navigator, index, nextState) {
        return null;
    },
    Title(route, navigator, index, nextState) {
        return (
            <TouchableOpacity style={styles.navbarTitleTouchable}>
                <Text style={styles.navbarTitle}>
                    HealthKit Body Measurements
                </Text>
            </TouchableOpacity>
        );
    }
};


module.exports = Dashboard;
export default Dashboard;