Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept different units on getWaterSamples #366

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions RCTAppleHealthKit/RCTAppleHealthKit+Methods_Dietary.m
Original file line number Diff line number Diff line change
Expand Up @@ -567,34 +567,45 @@ - (void)getWater:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
- (void)getWaterSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{
HKQuantityType *dietaryWaterType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryWater];
HKUnit *literUnit = [HKUnit literUnit];
NSDictionary *unitMapping = @{
@"liter": [HKUnit literUnit],
@"fluidOunceUS": [HKUnit fluidOunceUSUnit],
@"fluidOunceImperial": [HKUnit fluidOunceImperialUnit],
@"cupUS": [HKUnit cupUSUnit],
@"cupImperial": [HKUnit cupImperialUnit],
@"pintUS": [HKUnit pintUSUnit],
@"pintImperial": [HKUnit pintImperialUnit],
};
NSString *unitString = input[@"unit"];

// Default to literUnit if unitString is nil or not found in the unitMapping dictionary
HKUnit *unit = unitMapping[unitString] ?: [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];

NSPredicate *predicate = [RCTAppleHealthKit predicateForSamplesBetweenDates:startDate endDate:endDate];
[self fetchQuantitySamplesOfType:dietaryWaterType
unit:literUnit
unit:unit
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;
NSLog(@"An error occurred while retrieving the water sample %@. The error was: ", error);
callback(@[RCTMakeError(@"An error occurred while retrieving the water sample", error, nil)]);
}
}];
}
Expand Down
7 changes: 7 additions & 0 deletions docs/units.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,11 @@ The following units are supported by the library
pound
second
mlPerKgMin
liter
fluidOunceUSnceUS
fluidOunceImperialnceImperial
cupUS
cupImperialrial
pintUS
pintImperialerial
```
7 changes: 7 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,13 @@ declare module 'react-native-health' {
percent = 'percent',
pound = 'pound',
second = 'second',
liter = 'liter',
fluidOunceUS = 'fluidOunceUS',
fluidOunceImperial = 'fluidOunceImperial',
cupUS = 'cupUS',
cupImperial = 'cupImperial',
pintUS = 'pintUS',
pintImperial = 'pintImperial',
}

export enum HealthStatusCode {
Expand Down
7 changes: 7 additions & 0 deletions src/constants/Units.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,11 @@ export const Units = {
pound: 'pound',
second: 'second',
mlPerKgMin: 'mlPerKgMin',
liter: 'liter',
fluidOunceUSnceUS: 'fluidOunceUSnceUS',
fluidOunceImperialnceImperial: 'fluidOunceImperialnceImperial',
cupUS: 'cupUS',
cupImperialrial: 'cupImperialrial',
pintUS: 'pintUS',
pintImperialerial: 'pintImperialerial',
}