Skip to content

Commit

Permalink
Merge pull request #240 from AaronDsilva97/master
Browse files Browse the repository at this point in the history
get water samples method added
  • Loading branch information
macelai committed Jan 5, 2023
2 parents 1dbc199 + ed90df6 commit 0f25912
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 0 deletions.
1 change: 1 addition & 0 deletions RCTAppleHealthKit/RCTAppleHealthKit+Methods_Dietary.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

- (void)saveWater:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)getWater:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)getWaterSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;

- (void)dietary_getEnergyConsumedSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)dietary_getProteinSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
Expand Down
35 changes: 35 additions & 0 deletions RCTAppleHealthKit/RCTAppleHealthKit+Methods_Dietary.m
Original file line number Diff line number Diff line change
Expand Up @@ -533,4 +533,39 @@ - (void)getWater:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
}];
}

- (void)getWaterSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{
HKQuantityType *dietaryWaterType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryWater];
HKUnit *literUnit = [HKUnit literUnit];
NSUInteger limit = [RCTAppleHealthKit uintFromOptions:input key:@"limit" withDefault:HKObjectQueryNoLimit];
BOOL ascending = [RCTAppleHealthKit boolFromOptions:input key:@"ascending" withDefault:false];
NSDate *startDate = [RCTAppleHealthKit dateFromOptions:input key:@"startDate" withDefault:nil];
NSDate *endDate = [RCTAppleHealthKit dateFromOptions:input key:@"endDate" withDefault:[NSDate date]];
BOOL includeManuallyAdded = [RCTAppleHealthKit boolFromOptions:input key:@"includeManuallyAdded" withDefault:true];


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

NSPredicate * predicate = [RCTAppleHealthKit predicateForSamplesBetweenDates:startDate endDate:endDate];

[self fetchQuantitySamplesOfType:dietaryWaterType
unit:literUnit
predicate:predicate
ascending:ascending
limit:limit
completion:^(NSArray *results, NSError *error) {
if(results){
callback(@[[NSNull null], results]);
return;
} else {
NSLog(@"An error occured while retrieving the water sample %@. The error was: ", error);
callback(@[RCTMakeError(@"An error occured while retrieving the water sample", error, nil)]);
return;
}
}];
}

@end
7 changes: 7 additions & 0 deletions RCTAppleHealthKit/RCTAppleHealthKit.m
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,16 @@ + (BOOL)requiresMainQueueSetup

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

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

RCT_EXPORT_METHOD(getHeartRateSamples:(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 @@ -200,6 +200,7 @@ they are splitted in the following categories
- [saveFood](/docs/saveFood.md)
- [saveWater](/docs/saveWater.md)
- [getWater](/docs/getWater.md)
- [getWaterSamples](/docs/getWaterSamples.md)
### Fitness Methods
Expand Down
1 change: 1 addition & 0 deletions SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
* [saveFood](/docs/saveFood.md)
* [saveWater](/docs/saveWater.md)
* [getWater](/docs/getWater.md)
* [getWaterSamples](/docs/getWaterSamples.md)

## Fitness Methods

Expand Down
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ There is a gitbook version for the documentation on this [link](https://vinicius
- [saveFood](saveFood.md)
- [saveWater](saveWater.md)
- [getWater](getWater.md)
- [getWaterSamples](getWaterSamples.md)
- [getTotalFatSamples](getTotalFatSamples.md)

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

Get the total amount of consumed water in litres for a specific time period

Example input options:

```javascript
let options = {
unit: 'litre', // optional: default: litre
startDate: new Date(2021, 0, 0).toISOString(), // required
endDate: new Date().toISOString(), // required
includeManuallyAdded: false, // optional: default true
ascending: false, // optional; default false
limit: 10, // optional; default no limit
}
```

Call the method:

```javascript
AppleHealthKit.getWaterSamples(
(options: HealthInputOptions),
(err: Object, results: Array<HealthValue>) => {
if (err) {
return
}
console.log(results)
},
)
```

Example output:

```json
[
{
"id": "3d366e60-4f7c-4f72-b0ce-479ea19279b8", // The universally unique identifier (UUID) for this HealthKit object.
"endDate": "2021-03-22T16:34:00.000-0300",
"sourceId": "com.apple.Health",
"sourceName": "Health",
"startDate": "2021-03-22T15:34:00.000-0300",
"value": "0.6"
}
]
```
5 changes: 5 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,11 @@ declare module 'react-native-health' {
callback: (err: string, results: HealthValue) => void,
): void

getWaterSamples(
options: HealthInputOptions,
callback: (err: string, results: Array<HealthValue>) => void,
): void

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

0 comments on commit 0f25912

Please sign in to comment.