From d4bd095ff70381479ab8c404891ca6a903ed8b04 Mon Sep 17 00:00:00 2001 From: Jacopo Notarstefano Date: Fri, 3 Jan 2020 12:00:36 +0100 Subject: [PATCH] Add getDistanceSwimming method --- .../RCTAppleHealthKit+Methods_Fitness.h | 1 + .../RCTAppleHealthKit+Methods_Fitness.m | 24 +++++++++++++++++ RCTAppleHealthKit/RCTAppleHealthKit.m | 5 ++++ README.md | 1 + docs/getDistanceSwimming().md | 27 +++++++++++++++++++ 5 files changed, 58 insertions(+) create mode 100644 docs/getDistanceSwimming().md diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h index d4a8152a..6a41d4af 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h @@ -19,6 +19,7 @@ - (void)fitness_initializeStepEventObserver:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)fitness_getDistanceWalkingRunningOnDay:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)fitness_getDailyDistanceWalkingRunningSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; +- (void)fitness_getDistanceSwimmingOnDay:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)fitness_getDailyDistanceSwimmingSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)fitness_getDistanceCyclingOnDay:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)fitness_getDailyDistanceCyclingSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m index 6ca17408..b58cbe24 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m @@ -244,6 +244,30 @@ - (void)fitness_getDailyDistanceWalkingRunningSamples:(NSDictionary *)input call }]; } +- (void)fitness_getDistanceSwimmingOnDay:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback +{ + HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input key:@"unit" withDefault:[HKUnit meterUnit]]; + NSDate *date = [RCTAppleHealthKit dateFromOptions:input key:@"date" withDefault:[NSDate date]]; + + HKQuantityType *quantityType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceSwimming]; + + [self fetchSumOfSamplesOnDayForType:quantityType unit:unit day:date completion:^(double distance, NSDate *startDate, NSDate *endDate, NSError *error) { + if (!distance) { + callback(@[RCTJSErrorFromNSError(error)]); + return; + } + + NSDictionary *response = @{ + @"value" : @(distance), + @"startDate" : [RCTAppleHealthKit buildISO8601StringFromDate:startDate], + @"endDate" : [RCTAppleHealthKit buildISO8601StringFromDate:endDate], + }; + + + callback(@[[NSNull null], response]); + }]; +} + - (void)fitness_getDailyDistanceSwimmingSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback { HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input key:@"unit" withDefault:[HKUnit meterUnit]]; diff --git a/RCTAppleHealthKit/RCTAppleHealthKit.m b/RCTAppleHealthKit/RCTAppleHealthKit.m index a7219a1f..19c98835 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit.m @@ -160,6 +160,11 @@ @implementation RCTAppleHealthKit [self fitness_getDailyDistanceWalkingRunningSamples:input callback:callback]; } +RCT_EXPORT_METHOD(getDistanceSwimming:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) +{ + [self fitness_getDistanceSwimmingOnDay:input callback:callback]; +} + RCT_EXPORT_METHOD(getDailyDistanceSwimmingSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) { [self fitness_getDailyDistanceSwimmingSamples:input callback:callback]; diff --git a/README.md b/README.md index e13d5394..fcb3df5e 100644 --- a/README.md +++ b/README.md @@ -133,6 +133,7 @@ The available Healthkit permissions to use with `initHealthKit` | BodyTemperature | [HKQuantityTypeIdentifierBodyTemperature](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifierbodytemperature?language=objc) | ✓ | | | DateOfBirth | [HKCharacteristicTypeIdentifierDateOfBirth](https://developer.apple.com/reference/Healthkit/hkcharacteristictypeidentifierdateofbirth?language=objc) | ✓ | | | DistanceCycling | [HKQuantityTypeIdentifierDistanceCycling](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifierdistancecycling?language=objc) | ✓ | ✓ | +| DistanceSwimming | [HKQuantityTypeIdentifierDistanceSwimming](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifierdistanceswimming?language=objc) | ✓ | ✓ | | DistanceWalkingRunning | [HKQuantityTypeIdentifierDistanceWalkingRunning](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifierdistancewalkingrunning?language=objc) | ✓ | ✓ | | FlightsClimbed | [HKQuantityTypeIdentifierFlightsClimbed](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifierflightsclimbed?language=objc) | ✓ | ✓ | | HeartRate | [HKQuantityTypeIdentifierHeartRate](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifierheartrate?language=objc) | ✓ | | diff --git a/docs/getDistanceSwimming().md b/docs/getDistanceSwimming().md new file mode 100644 index 00000000..9905ef5a --- /dev/null +++ b/docs/getDistanceSwimming().md @@ -0,0 +1,27 @@ +Get the total distance swimming on a specific day. + +`getDistanceSwimming` accepts an options object containing optional *`date: ISO8601Timestamp`* and *`unit: string`*. If `date` is not provided it will default to the current time. `unit` defaults to `meter`. + +```javascript +let options = { + unit: 'mile', // optional; default 'meter' + date: (new Date(2016,5,1)).toISOString(), // optional; default now +}; +``` + +```javascript +AppleHealthKit.getDistanceSwimming(options: Object, (err: Object, results: Object) => { + if (err) { + return; + } + console.log(results) +}); +``` + +```javascript +{ + value: 1.45, + startDate: '2016-07-08T12:00:00.000-0400', + endDate: '2016-07-08T12:00:00.000-0400' +} +```