index.js 6.49 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
    Text,
12 13
    View,
    NativeAppEventEmitter,
14
} from 'react-native';
15

16
import AppleHealthKit from 'react-native-apple-healthkit';
17
import styles from '../../styles/styles';
18 19
import History from './history';

20
// setup the HealthKit initialization options
21
const HKPERMS = AppleHealthKit.Constants.Permissions;
22 23
const HKOPTIONS = {
    permissions: {
24
        read:  [
25 26 27 28
            HKPERMS.StepCount,
            HKPERMS.DistanceWalkingRunning,
            HKPERMS.FlightsClimbed,
            HKPERMS.Height,
29 30
            HKPERMS.DateOfBirth,
            HKPERMS.BiologicalSex,
Greg Wilson's avatar
Greg Wilson committed
31
            HKPERMS.SleepAnalysis,
32
        ],
33
        write: [
34
            HKPERMS.StepCount
35
        ],
36 37 38
    }
};

39 40 41
/**
 * React Component
 */
42 43 44 45 46 47 48 49 50 51
class Home extends Component {

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

52 53 54 55 56
    /**
     * 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
     */
57 58 59 60 61 62 63
    componentDidMount() {
        AppleHealthKit.isAvailable((err,available) => {
            if(available){
                AppleHealthKit.initHealthKit(HKOPTIONS, (err, res) => {
                    if(this._handleHKError(err, 'initHealthKit')){
                        return;
                    }
64 65 66 67 68 69 70 71 72 73 74 75 76

                    AppleHealthKit.initStepCountObserver({}, () => {});

                    var subscription = NativeAppEventEmitter.addListener(
                        'change:steps',
                        (evt) => {
                            console.log('change:steps EVENT!! : ', evt);
                            this._fetchStepsToday();
                        }
                    );

                    this.sub = subscription;

77 78
                    this._fetchStepsToday();
                    this._fetchStepsHistory();
Greg Wilson's avatar
Greg Wilson committed
79 80

                    this._fetchSleepAnalysis();
81 82 83 84 85
                });
            }
        });
    }

86 87 88 89
    componentWillUnmount() {
        this.sub.remove();
    }

90 91 92 93 94
    /**
     * get today's step count from HealthKit. on success update
     * the component state
     * @private
     */
95
    _fetchStepsToday() {
96
        AppleHealthKit.getStepCount(null, (err, res) => {
97
            if(this._handleHKError(err, 'getStepCount')){
98 99
                return;
            }
100
            this.setState({stepsToday: res.value});
101 102 103
        });
    }

104 105 106 107 108
    /**
     * get the step history from options.startDate through the
     * current time. on success update the component state
     * @private
     */
109 110 111 112
    _fetchStepsHistory() {
        let options = {
            startDate: (new Date(2016,4,1)).toISOString(),
        };
113 114
        AppleHealthKit.getDailyStepCountSamples(options, (err, res) => {
            if(this._handleHKError(err, 'getDailyStepCountSamples')){
115 116 117 118 119 120
                return;
            }
            this.setState({stepHistory: res});
        });
    }

Greg Wilson's avatar
Greg Wilson committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136


    _fetchSleepAnalysis() {
        let options = {
            startDate: (new Date(2016,10,1)).toISOString(),
        };
        AppleHealthKit.getSleepSamples(options, (err, res) => {
            if(this._handleHKError(err, 'getSleepSamples')){
                return;
            }
            //this.setState({stepHistory: res});

            console.log('######################################')
            console.log('###           SLEEP SAMPLES        ###')
            console.log('######################################')
            console.log(res)
137

Greg Wilson's avatar
Greg Wilson committed
138 139 140 141 142 143
        });
    }




144 145 146 147 148
    /**
     * render the Navigator which will render the navigation
     * bar and the scene
     * @returns {XML}
     */
149 150 151 152 153 154 155 156 157 158 159 160
    render() {
        return (
            <Navigator
                renderScene={this.renderScene.bind(this)}
                navigator={this.props.navigator}
                navigationBar={
                    <Navigator.NavigationBar style={styles.navigationBar}
                                             routeMapper={NavigationBarRouteMapper} />
                }/>
        );
    }

161 162 163 164 165 166
    /**
     * render the scene
     * @param route
     * @param navigator
     * @returns {XML}
     */
167 168 169 170 171
    renderScene(route, navigator) {
        return (
            <View style={styles.sceneContainerWithNavbar}>

                <View style={styles.stepsContainer}>
172 173 174 175 176 177 178 179 180
                    <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}
181 182 183 184
                    </Text>
                </View>

                <View style={styles.historyContainer}>
185 186 187 188 189
                    <View style={styles.titleRow}>
                        <Text>
                            History
                        </Text>
                    </View>
190 191 192 193 194 195 196
                    <History data={this.state.stepHistory} />
                </View>

            </View>
        );
    }

197 198 199 200 201 202 203 204
    /**
     * if 'err' is truthy then log the error message and
     * return true indicating an error has occurred
     * @param err
     * @param method
     * @returns {boolean}
     * @private
     */
205 206 207
    _handleHKError(err, method) : boolean {
        if(err){
            let errStr = 'HealthKit_ERROR['+method+'] : ';
208
            errStr += (err && err.message) ? err.message : err;
209 210 211 212 213 214 215 216 217 218 219 220 221
            console.log(errStr);
            return true;
        }
        return false;
    }
}


var NavigationBarRouteMapper = {
    LeftButton(route, navigator, index, nextState) {
        return null;
    },
    RightButton(route, navigator, index, nextState) {
222 223 224 225 226 227 228 229
        return (
            <TouchableOpacity style={styles.navbarTitleTouchable}
                              onPress={() => { navigator.parentNavigator.push({name: 'Add'})}}>
                <Text style={styles.navbarPlusButton}>
                    +
                </Text>
            </TouchableOpacity>
        );
230 231 232 233 234 235 236 237 238 239 240 241 242 243
    },
    Title(route, navigator, index, nextState) {
        return (
            <TouchableOpacity style={styles.navbarTitleTouchable}>
                <Text style={styles.navbarTitle}>
                    HealthKit Steps
                </Text>
            </TouchableOpacity>
        );
    }
};

module.exports = Home;
export default Home;