Commit f15ce594 authored by Greg Wilson's avatar Greg Wilson

updated README with initStepCountObserver docs

parent 1a15bc03
...@@ -20,10 +20,8 @@ A React Native bridge module for interacting with [Apple HealthKit] data. ...@@ -20,10 +20,8 @@ A React Native bridge module for interacting with [Apple HealthKit] data.
* [getBiologicalSex](#getbiologicalsex) * [getBiologicalSex](#getbiologicalsex)
* [getDateOfBirth](#getdateofbirth) * [getDateOfBirth](#getdateofbirth)
* [getStepCount](#getstepcount) * [getStepCount](#getstepcount)
* ~~[getStepCountForToday](#getstepcountfortoday)~~
* ~~[getStepCountForDay](#getstepcountforday)~~
* [getDailyStepCountSamples](#getdailystepcountsamples) * [getDailyStepCountSamples](#getdailystepcountsamples)
* ~~[getMultiDayStepCounts](#getmultidaystepcounts)~~ * [initStepCountObserver](#initstepcountobserver)
* [saveSteps](#savesteps) * [saveSteps](#savesteps)
* [getDistanceWalkingRunning](#getdistancewalkingrunning) * [getDistanceWalkingRunning](#getdistancewalkingrunning)
* [getDistanceCycling](#getdistancecycling) * [getDistanceCycling](#getdistancecycling)
...@@ -302,40 +300,6 @@ AppleHealthKit.getStepCount(options: Object, (err: Object, steps: Object) => { ...@@ -302,40 +300,6 @@ AppleHealthKit.getStepCount(options: Object, (err: Object, steps: Object) => {
___ ___
#### ~~**`getStepCountForToday`**~~
**removed** - replaced by `getStepCount`
get the aggregated total steps for the current day starting and ending at midnight
```javascript
AppleHealthKit.getStepCountForToday(null, (err: Object, steps: number) => {
if(this._handleHealthKitError(err, 'getStepCountForToday')){
return;
}
// use steps...
});
```
___
#### ~~**`getStepCountForDay`**~~
**removed** - replaced by `getStepCount`
get the the aggregated total steps for the day provided as `date` in options object. the `date` field expects an ISO date string as its value
```javascript
let d = new Date(2016,5,27);
let options = {
date: d.toISOString()
};
AppleHealthKit.getStepCountForDay(options: Object, (err: Object, steps: number) => {
if(this._handleHealthKitError(err, 'getStepCountForDay')){
return;
}
// steps is the step count for day 'd'
});
```
___
#### **`getDailyStepCountSamples`** #### **`getDailyStepCountSamples`**
Get the total steps per day over a specified date range. Get the total steps per day over a specified date range.
...@@ -375,32 +339,69 @@ The function will be called with an array of elements. Each element is an object ...@@ -375,32 +339,69 @@ The function will be called with an array of elements. Each element is an object
___ ___
#### ~~**`getMultiDayStepCounts`**~~ #### **`initStepCountObserver`**
**removed** - replaced by `getDailyStepCountSamples` Setup an HKObserverQuery for step count (HKQuantityTypeIdentifierStepCount) that will
trigger an event listenable on react-native `NativeAppEventEmitter` when the
HealthKit step count has changed.
Get the total steps per day over a specified date range. The `initStepCountObserver` method must be called before adding a listener to
NativeAppEventEmitter. After the step count observer has been initialized you can
listen to the NativeAppEventEmitter `change:steps` event and re-fetch relevent
step count data in the event handler.
The `initStepCountObserver` method should be called after HealthKit has been
successfully initialized (AppleHealthKit.initHealthKit has been called without
error).
`getMultiDayStepCounts` accepts an options object containing required *`startDate: ISO8601Timestamp`* and optional *`endDate: ISO8601Timestamp`*. if `endDate` is not provided it will default to the current time
```javascript
let options = {
startDate: (new Date(2016,5,1)).toISOString() // required
endDate: (new Date()).toISOString() // optional; default now
};
```
the function will be called with an array of elements `res` containing date and step count information
```javascript ```javascript
AppleHealthKit.getMultiDayStepCounts(options: Object, (err: Object, res: Array<Array<string|number>>) => { // import NativeAppEventEmitter from react-native
if(this._handleHealthKitError(err, 'getMultiDayStepCounts')){ import {
Navigator,
View,
...
NativeAppEventEmitter,
} from 'react-native';
...
// initialize the step count observer and add an event
// listener for 'change:steps' event after HealthKit has
// been successfully initialized.
AppleHealthKit.initHealthKit(HKOPTIONS, (err, res) => {
if(this._handleHKError(err, 'initHealthKit')){
return; return;
} }
// 'res' is array of [ISOTimestamp: string, stepCount: number] arrays
// sorted ascending from startDate through endDate // initialize the step count observer
for(let i=0; i<res.length; ++i){ AppleHealthKit.initStepCountObserver({}, () => {});
let elem = res[i];
// elem[0] is ISOTimestamp : string // add event listener for 'change:steps' and handle the
// elem[1] is step count : number // event in the event handler function.
} //
// when adding a listener, a 'subscription' object is
// returned that must be used to remove the listener
// when the component unmounts. The subscription object
// must be accessible to any function/method/instance
// that will be unsubscribing from the event.
this.sub = NativeAppEventEmitter.addListener(
'change:steps',
(evt) => {
// a 'change:steps' event has been received. step
// count data should be re-fetched from HealthKit.
this._fetchStepCountData();
}
);
...
}); });
...
// when the component where the listener was added unmounts
// (or whenever the listener should be removed), call the
// `remove` method of the subscription object.
componentWillUnmount() {
this.sub.remove();
}
``` ```
___ ___
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment