From 40fca1ff0425913e8cfbebb3e2e58f9d6397a586 Mon Sep 17 00:00:00 2001 From: caiofelipeam Date: Tue, 20 Feb 2024 20:51:41 -0300 Subject: [PATCH] Accept different units on getWaterSamples --- .../RCTAppleHealthKit+Methods_Dietary.m | 33 ++++++++++++------- docs/units.md | 7 ++++ index.d.ts | 7 ++++ src/constants/Units.js | 7 ++++ 4 files changed, 43 insertions(+), 11 deletions(-) diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Dietary.m b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Dietary.m index 9d532ee4..b4d7a128 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Dietary.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Dietary.m @@ -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)]); } }]; } diff --git a/docs/units.md b/docs/units.md index 73af8cef..4e52e57a 100644 --- a/docs/units.md +++ b/docs/units.md @@ -25,4 +25,11 @@ The following units are supported by the library pound second mlPerKgMin + liter + fluidOunceUSnceUS + fluidOunceImperialnceImperial + cupUS + cupImperialrial + pintUS + pintImperialerial ``` diff --git a/index.d.ts b/index.d.ts index 0cf0e280..b32272d7 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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 { diff --git a/src/constants/Units.js b/src/constants/Units.js index b0328c19..f7ef8516 100644 --- a/src/constants/Units.js +++ b/src/constants/Units.js @@ -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', }