index.js 5.24 KB
Newer Older
1 2 3 4 5 6 7 8 9
/**
 * Created by greg on 2016-06-30.
 */

import React, { Component } from 'react';
import {
    Navigator,
    TouchableOpacity,
    ScrollView,
10
    Image,
11 12 13
    Text,
    View
} from 'react-native';
14
import AppleHealthKit from 'react-native-apple-healthkit';
15
import styles from '../../styles/styles';
16 17
import History from './history';

18
// setup the HealthKit initialization options
19
const HKPERMS = AppleHealthKit.Constants.Permissions;
20 21
const HKOPTIONS = {
    permissions: {
22
        read:  [
23 24 25 26
            HKPERMS.StepCount,
            HKPERMS.DistanceWalkingRunning,
            HKPERMS.FlightsClimbed,
            HKPERMS.Height,
27
        ],
28
        write: [
29
            HKPERMS.StepCount
30
        ],
31 32 33
    }
};

34 35 36
/**
 * React Component
 */
37 38 39 40 41 42 43 44 45 46
class Home extends Component {

    constructor(props) {
        super(props);
        this.state = {
            stepsToday: 0,
            stepHistory: [],
        };
    }

47 48 49 50 51
    /**
     * if HealthKit is available on the device then initialize it
     * with the permissions set above in HKOPTIONS. on successful
     * initialization fetch today's steps and the step history
     */
52 53 54 55 56 57 58 59 60 61 62 63 64 65
    componentDidMount() {
        AppleHealthKit.isAvailable((err,available) => {
            if(available){
                AppleHealthKit.initHealthKit(HKOPTIONS, (err, res) => {
                    if(this._handleHKError(err, 'initHealthKit')){
                        return;
                    }
                    this._fetchStepsToday();
                    this._fetchStepsHistory();
                });
            }
        });
    }

66 67 68 69 70
    /**
     * get today's step count from HealthKit. on success update
     * the component state
     * @private
     */
71
    _fetchStepsToday() {
72 73
        AppleHealthKit.getStepCount(null, (err, steps) => {
            if(this._handleHKError(err, 'getStepCount')){
74 75 76 77 78 79
                return;
            }
            this.setState({stepsToday: steps});
        });
    }

80 81 82 83 84
    /**
     * get the step history from options.startDate through the
     * current time. on success update the component state
     * @private
     */
85 86 87 88
    _fetchStepsHistory() {
        let options = {
            startDate: (new Date(2016,4,1)).toISOString(),
        };
89 90
        AppleHealthKit.getDailyStepCountSamples(options, (err, res) => {
            if(this._handleHKError(err, 'getDailyStepCountSamples')){
91 92 93 94 95 96
                return;
            }
            this.setState({stepHistory: res});
        });
    }

97 98 99 100 101
    /**
     * render the Navigator which will render the navigation
     * bar and the scene
     * @returns {XML}
     */
102 103 104 105 106 107 108 109 110 111 112 113
    render() {
        return (
            <Navigator
                renderScene={this.renderScene.bind(this)}
                navigator={this.props.navigator}
                navigationBar={
                    <Navigator.NavigationBar style={styles.navigationBar}
                                             routeMapper={NavigationBarRouteMapper} />
                }/>
        );
    }

114 115 116 117 118 119
    /**
     * render the scene
     * @param route
     * @param navigator
     * @returns {XML}
     */
120 121 122 123 124
    renderScene(route, navigator) {
        return (
            <View style={styles.sceneContainerWithNavbar}>

                <View style={styles.stepsContainer}>
125 126 127 128 129 130 131 132 133
                    <Image style={styles.stepsIcon}
                           source={require('../../assets/images/steps.png')}>

                    </Image>
                    <Text style={styles.stepsLabel}>
                        Today's Steps
                    </Text>
                    <Text style={styles.stepsValue}>
                        {this.state.stepsToday}
134 135 136 137
                    </Text>
                </View>

                <View style={styles.historyContainer}>
138 139 140 141 142
                    <View style={styles.titleRow}>
                        <Text>
                            History
                        </Text>
                    </View>
143 144 145 146 147 148 149
                    <History data={this.state.stepHistory} />
                </View>

            </View>
        );
    }

150 151 152 153 154 155 156 157
    /**
     * if 'err' is truthy then log the error message and
     * return true indicating an error has occurred
     * @param err
     * @param method
     * @returns {boolean}
     * @private
     */
158 159 160
    _handleHKError(err, method) : boolean {
        if(err){
            let errStr = 'HealthKit_ERROR['+method+'] : ';
161
            errStr += (err && err.message) ? err.message : err;
162 163 164 165 166 167 168 169 170 171 172 173 174
            console.log(errStr);
            return true;
        }
        return false;
    }
}


var NavigationBarRouteMapper = {
    LeftButton(route, navigator, index, nextState) {
        return null;
    },
    RightButton(route, navigator, index, nextState) {
175 176 177 178 179 180 181 182
        return (
            <TouchableOpacity style={styles.navbarTitleTouchable}
                              onPress={() => { navigator.parentNavigator.push({name: 'Add'})}}>
                <Text style={styles.navbarPlusButton}>
                    +
                </Text>
            </TouchableOpacity>
        );
183 184 185 186 187 188 189 190 191 192 193 194 195 196
    },
    Title(route, navigator, index, nextState) {
        return (
            <TouchableOpacity style={styles.navbarTitleTouchable}>
                <Text style={styles.navbarTitle}>
                    HealthKit Steps
                </Text>
            </TouchableOpacity>
        );
    }
};

module.exports = Home;
export default Home;