index.js 5.31 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
            HKPERMS.DateOfBirth,
            HKPERMS.BiologicalSex,
29
        ],
30
        write: [
31
            HKPERMS.StepCount
32
        ],
33 34 35
    }
};

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

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

49 50 51 52 53
    /**
     * 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
     */
54 55 56 57 58 59 60 61 62 63 64 65 66 67
    componentDidMount() {
        AppleHealthKit.isAvailable((err,available) => {
            if(available){
                AppleHealthKit.initHealthKit(HKOPTIONS, (err, res) => {
                    if(this._handleHKError(err, 'initHealthKit')){
                        return;
                    }
                    this._fetchStepsToday();
                    this._fetchStepsHistory();
                });
            }
        });
    }

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

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

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

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

                <View style={styles.stepsContainer}>
127 128 129 130 131 132 133 134 135
                    <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}
136 137 138 139
                    </Text>
                </View>

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

            </View>
        );
    }

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


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

module.exports = Home;
export default Home;