Skip to content

Commit 29d8aa3

Browse files
authored
Merge pull request #271 from dev-john-nguyen/master
Workout Events and Duration To getAnchoredWorkouts Method
2 parents fcb9831 + a30a2a6 commit 29d8aa3

File tree

5 files changed

+82
-2
lines changed

5 files changed

+82
-2
lines changed

RCTAppleHealthKit/RCTAppleHealthKit+Queries.m

+5-1
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,8 @@ - (void)fetchAnchoredWorkouts:(HKSampleType *)type
515515
double energy = [[sample totalEnergyBurned] doubleValueForUnit:[HKUnit kilocalorieUnit]];
516516
double distance = [[sample totalDistance] doubleValueForUnit:[HKUnit mileUnit]];
517517
NSString *type = [RCTAppleHealthKit stringForHKWorkoutActivityType:[sample workoutActivityType]];
518+
NSArray *workoutEvents = [RCTAppleHealthKit formatWorkoutEvents:[sample workoutEvents]];
519+
NSTimeInterval duration = [sample duration];
518520

519521
NSString *startDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.startDate];
520522
NSString *endDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.endDate];
@@ -546,7 +548,9 @@ - (void)fetchAnchoredWorkouts:(HKSampleType *)type
546548
@"device": device,
547549
@"distance" : @(distance),
548550
@"start" : startDateString,
549-
@"end" : endDateString
551+
@"end" : endDateString,
552+
@"duration": @(duration),
553+
@"workoutEvents": workoutEvents
550554
};
551555

552556
[data addObject:elem];

RCTAppleHealthKit/RCTAppleHealthKit+Utils.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ extern NSString * const kMetadataKey;
3737
+ (HKWorkoutActivityType)hkWorkoutActivityTypeFromOptions: (NSDictionary *)options key: (NSString *)key withDefault: (HKWorkoutActivityType)defaultValue;
3838
+ (HKQuantity *)hkQuantityFromOptions:(NSDictionary *)options valueKey: (NSString *)valueKey unitKey: (NSString *)unitKey;
3939
+ (NSDictionary *)metadataFromOptions:(NSDictionary *)options withDefault:(NSDictionary *)defaultValue;
40-
40+
+ (NSArray *)formatWorkoutEvents:(NSArray *)workoutEvents;
4141
+ (NSMutableArray *)reverseNSMutableArray:(NSMutableArray *)array;
4242
+ (NSString*) stringForHKWorkoutActivityType:(int) enumValue;
4343

RCTAppleHealthKit/RCTAppleHealthKit+Utils.m

+49
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,55 @@ @implementation RCTAppleHealthKit (Utils)
1515

1616
#pragma mark - Utilities
1717

18+
+ (NSArray *)formatWorkoutEvents:(NSArray *)workoutEvents
19+
{
20+
NSMutableArray *formattedWorkEvents = [[NSMutableArray alloc] init];
21+
22+
for (id workoutEvent in workoutEvents) {
23+
NSNumber *eventType = [workoutEvent valueForKey:@"type"];
24+
NSString *eventDescription = @"";
25+
26+
switch([eventType intValue]) {
27+
case (int)HKWorkoutEventTypePause:
28+
eventDescription = @"pause";
29+
break;
30+
case (int)HKWorkoutEventTypeResume:
31+
eventDescription = @"resume";
32+
break;
33+
case (int)HKWorkoutEventTypeMotionPaused:
34+
eventDescription = @"motion paused";
35+
break;
36+
case (int)HKWorkoutEventTypeMotionResumed:
37+
eventDescription = @"motion resumed";
38+
break;
39+
case (int)HKWorkoutEventTypePauseOrResumeRequest:
40+
eventDescription = @"pause or resume request";
41+
break;
42+
case (int)HKWorkoutEventTypeLap:
43+
eventDescription = @"lap";
44+
break;
45+
case (int)HKWorkoutEventTypeSegment:
46+
eventDescription = @"segment";
47+
break;
48+
case (int)HKWorkoutEventTypeMarker:
49+
eventDescription = @"marker";
50+
default:
51+
eventDescription = @"";
52+
}
53+
54+
55+
NSObject *formattedEvent = @{
56+
@"eventTypeInt":eventType,
57+
@"eventType": eventDescription,
58+
@"startDate": [RCTAppleHealthKit buildISO8601StringFromDate:[[workoutEvent dateInterval] startDate]],
59+
@"endDate": [RCTAppleHealthKit buildISO8601StringFromDate:[[workoutEvent dateInterval] endDate]]
60+
};
61+
[formattedWorkEvents addObject: formattedEvent];
62+
}
63+
64+
return formattedWorkEvents;
65+
}
66+
1867
+ (NSDate *)parseISO8601DateFromString:(NSString *)date
1968
{
2069
@try {

docs/getAnchoredWorkouts.md

+7
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,12 @@ The resulting workouts in the array will look as the following:
5959
distance: Number, // [[sample totalDistance] doubleValueForUnit:[HKUnit mileUnit]]
6060
start: String, // [RCTAppleHealthKit buildISO8601StringFromDate:sample.startDate];
6161
end: String, // [RCTAppleHealthKit buildISO8601StringFromDate:sample.endDate];
62+
duration: Number, // NSTimeInterval (seconds)
63+
workoutEvents: {
64+
endDate: String, // [RCTAppleHealthKit buildISO8601StringFromDate:sample.endDate];
65+
startDate: String, // [RCTAppleHealthKit buildISO8601StringFromDate:sample.startDate]
66+
eventTypeInt: Number,
67+
eventType: String, // EventType enum
68+
}[]
6269
}
6370
```

index.d.ts

+20
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,24 @@ declare module 'react-native-health' {
538538
locations: LocationValue[]
539539
}
540540

541+
export enum EventType {
542+
Pause = 'pause',
543+
Resume = 'resume',
544+
MotionPaused = 'motion paused',
545+
MotionResumed = 'motion resumed',
546+
PausedOrResumeRequest = 'pause or resume request',
547+
Lap = 'lap',
548+
Segment = 'segment',
549+
Marker = 'marker'
550+
}
551+
552+
export type HKWorkoutEventType = {
553+
endDate: string
554+
startDate: string
555+
eventTypeInt: number
556+
eventType: EventType
557+
}
558+
541559
export interface HKWorkoutQueriedSampleType {
542560
activityId: number
543561
activityName: string
@@ -551,6 +569,8 @@ declare module 'react-native-health' {
551569
distance: number
552570
start: string
553571
end: string
572+
duration: number
573+
workoutEvents: HKWorkoutEventType[]
554574
}
555575

556576
export interface ElectrocardiogramSampleValue extends BaseValue {

0 commit comments

Comments
 (0)