/** * 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'; //var AppleHealthKit = require('react-native-apple-healthkit'); import AppleHealthKit from 'react-native-apple-healthkit'; import History from './history'; const WPERMS = AppleHealthKit.Constants.Permissions.WRITE; const RPERMS = AppleHealthKit.Constants.Permissions.READ; const HKOPTIONS = { permissions: { read: [ RPERMS.StepCount, RPERMS.DistanceWalkingRunning, RPERMS.FlightsClimbed, RPERMS.Height, ], write: [WPERMS.StepCount], } }; class Home extends Component { constructor(props) { super(props); this.state = { stepsToday: 0, stepHistory: [], }; } componentDidMount() { console.log('CONSTANTS: ', AppleHealthKit.Constants); //console.log('balls: ', ahk); 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}); }); AppleHealthKit.getDistanceWalkingRunning(null, (err, res) => { if(this._handleHKError(err, 'getDistanceWalkingRunning')){ return; } console.log('getDistanceWalkingRunning -res-> ', res); }); AppleHealthKit.getFlightsClimbed(null, (err, res) => { if(this._handleHKError(err, 'getFlightsClimbed')){ return; } console.log('getFlightsClimbed -res-> ', res); }); let sampleOptions = { startDate: (new Date(2016,4,1)).toISOString(), }; AppleHealthKit.getHeightSamples(sampleOptions, (err, samples) => { if(this._handleHKError(err, 'getHeightSamples')){ return; } console.log('getHeightSamples: ', samples); }); } _onPressItem(key) { console.log('_onPressItem() ==> ', key); let self = this; this.requestAnimationFrame(() => { this.props.navigator.push({ name: key }); }) } render() { return ( }/> ); } renderScene(route, navigator) { return ( STEPS: {this.state.stepsToday} ); } _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 ( HealthKit Steps ); } }; module.exports = Home; export default Home;