/**
* 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],
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});
});
}
_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;