Skip to content

Commit

Permalink
Merge pull request #232 from magus/dev/noah/fitness-saveWalkingRunnin…
Browse files Browse the repository at this point in the history
…gDistance

feat: AppleHealthKit.saveWalkingRunningDistance
  • Loading branch information
macelai authored Aug 4, 2022
2 parents dcddee8 + bd00971 commit 38603c6
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- (void)fitness_getSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)fitness_getDailyStepSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)fitness_saveSteps:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)fitness_saveWalkingRunningDistance:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)fitness_initializeStepEventObserver:(NSDictionary *)input hasListeners:(bool)hasListeners callback:(RCTResponseSenderBlock)callback;
- (void)fitness_getDistanceWalkingRunningOnDay:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)fitness_getDailyDistanceWalkingRunningSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
Expand Down
26 changes: 26 additions & 0 deletions RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,32 @@ - (void)fitness_saveSteps:(NSDictionary *)input callback:(RCTResponseSenderBlock
}];
}

- (void)fitness_saveWalkingRunningDistance:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{
double distance = [RCTAppleHealthKit doubleValueFromOptions:input];
NSDate *startDate = [RCTAppleHealthKit dateFromOptions:input key:@"startDate" withDefault:nil];
NSDate *endDate = [RCTAppleHealthKit dateFromOptions:input key:@"endDate" withDefault:[NSDate date]];
HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input key:@"unit" withDefault:[HKUnit meterUnit]];

if(startDate == nil || endDate == nil){
callback(@[RCTMakeError(@"startDate and endDate are required in options", nil, nil)]);
return;
}

HKQuantity *quantity = [HKQuantity quantityWithUnit:unit doubleValue:distance];
HKQuantityType *type = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceWalkingRunning];
HKQuantitySample *sample = [HKQuantitySample quantitySampleWithType:type quantity:quantity startDate:startDate endDate:endDate];

[self.healthStore saveObject:sample withCompletion:^(BOOL success, NSError *error) {
if (!success) {
callback(@[RCTJSErrorFromNSError(error)]);
return;
}
callback(@[[NSNull null], @(distance)]);
}];
}



- (void)fitness_initializeStepEventObserver:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{
Expand Down
6 changes: 6 additions & 0 deletions RCTAppleHealthKit/RCTAppleHealthKit.m
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,12 @@ + (BOOL)requiresMainQueueSetup
[self fitness_saveSteps:input callback:callback];
}

RCT_EXPORT_METHOD(saveWalkingRunningDistance:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
{
[self _initializeHealthStore];
[self fitness_saveWalkingRunningDistance:input callback:callback];
}

RCT_EXPORT_METHOD(getDistanceWalkingRunning:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
{
[self _initializeHealthStore];
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ they are splitted in the following categories
- [getDailyFlightsClimbedSamples](/docs/getDailyFlightsClimbedSamples.md)
- [getFlightsClimbed](/docs/getFlightsClimbed.md)
- [saveSteps](/docs/saveSteps.md)
- [saveWalkingRunningDistance](/docs/saveWalkingRunningDistance.md)
### Hearing Methods
Expand Down
1 change: 1 addition & 0 deletions SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
* [getDailyFlightsClimbedSamples](/docs/getDailyFlightsClimbedSamples.md)
* [getFlightsClimbed](/docs/getFlightsClimbed.md)
* [saveSteps](/docs/saveSteps.md)
- [saveWalkingRunningDistance](/docs/saveWalkingRunningDistance.md)

## Hearing Methods

Expand Down
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ There is a gitbook version for the documentation on this [link](https://vinicius
- [getDailyFlightsClimbedSamples](getDailyFlightsClimbedSamples.md)
- [getFlightsClimbed](getFlightsClimbed.md)
- [saveSteps](saveSteps.md)
- [saveWalkingRunningDistance](/docs/saveWalkingRunningDistance.md)

### Hearing Methods

Expand Down
36 changes: 36 additions & 0 deletions docs/saveWalkingRunningDistance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# saveWalkingRunningDistance

Save a walking + running distance sample.

A distance sample represents the amount traveled during a specific period of time. A sample should be a precise as possible, with `startDate` and `endDate` representing the range of time the steps were taken in. The unit of distance can be specified by `units` key in `options` below, defaulting to meters.

Example input options:

```javascript
// 300m over 5 min ~= 1 m/s (walking speed)
let options = {
value: 300, // meters (default)
startDate: new Date(2022, 6, 11, 6, 0, 0).toISOString(),
endDate: new Date(2016, 6, 11, 6, 5, 0).toISOString(),
}
```

Call the method:

```javascript
AppleHealthKit.saveWalkingRunningDistance(
(options: HealthInputOptions),
(err: Object, results: number) => {
if (this._handleHKError(err, 'saveWalkingRunningDistance')) {
return
}
// walking + running distance sample successfully saved
},
)
```

Example output:

```json
300
```
5 changes: 5 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ declare module 'react-native-health' {
callback: (error: string, result: HealthValue) => void,
): void

saveWalkingRunningDistance(
options: HealthValueOptions,
callback: (error: string, result: HealthValue) => void,
): void

getDistanceWalkingRunning(
options: HealthInputOptions,
callback: (err: string, results: HealthValue) => void,
Expand Down

0 comments on commit 38603c6

Please sign in to comment.