index.js 4.39 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/**
 * Created by greg on 2016-06-30.
 */

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';

18 19
//var AppleHealthKit = require('react-native-apple-healthkit');
import AppleHealthKit from 'react-native-apple-healthkit';
20 21 22

import History from './history';

23 24
const WPERMS = AppleHealthKit.Constants.Permissions.WRITE;
const RPERMS = AppleHealthKit.Constants.Permissions.READ;
25 26 27

const HKOPTIONS = {
    permissions: {
28
        read:  [RPERMS.StepCount, RPERMS.DistanceWalkingRunning],
29
        write: [WPERMS.StepCount],
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
    }
};


class Home extends Component {

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

    componentDidMount() {
45

46
        console.log('CONSTANTS: ', AppleHealthKit.Constants);
47 48
        //console.log('balls: ', ahk);

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
        AppleHealthKit.isAvailable((err,available) => {
            if(available){
                AppleHealthKit.initHealthKit(HKOPTIONS, (err, res) => {
                    if(this._handleHKError(err, 'initHealthKit')){
                        return;
                    }
                    this._fetchStepsToday();
                    this._fetchStepsHistory();
                });
            }
        });
    }

    componentWillUnmount() {

    }

    _fetchStepsToday() {
        AppleHealthKit.getStepCountForToday(null, (err, steps) => {
            if(this._handleHKError(err, 'getStepCountForToday')){
                return;
            }
            this.setState({stepsToday: steps});
        });
    }

    _fetchStepsHistory() {
        let options = {
            startDate: (new Date(2016,4,1)).toISOString(),
        };
        AppleHealthKit.getDailyStepSamples(options, (err, res) => {
            if(this._handleHKError(err, 'getDailyStepSamples')){
                return;
            }
            this.setState({stepHistory: res});
        });
85 86 87 88 89 90 91 92

        AppleHealthKit.getDistanceWalkingRunning(null, (err, res) => {
            if(this._handleHKError(err, 'getDistanceWalkingRunning')){
                return;
            }
            console.log('getDistanceWalkingRunning -res-> ', res);
        });

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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
    }

    _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.sceneContainerWithNavbar}>

                <View style={styles.stepsContainer}>
                    <Text>
                        STEPS: {this.state.stepsToday}
                    </Text>
                </View>

                <View style={styles.historyContainer}>
                    <History data={this.state.stepHistory} />
                </View>

            </View>
        );
    }

    _handleHKError(err, method) : boolean {
        if(err){
            let errStr = 'HealthKit_ERROR['+method+'] : ';
            if(typeof err === 'string'){
                errStr += err;
            } else if (typeof err === 'object' && err.message){
                errStr += err.message;
            }
            console.log(errStr);
            return true;
        }
        return false;
    }

}

reactMixin(Home.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 Steps
                </Text>
            </TouchableOpacity>
        );
    }
};


module.exports = Home;
export default Home;