Skip to content

Commit 89c66d2

Browse files
config for workoutevents to anchor workout
1 parent 7888bfd commit 89c66d2

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-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 {

0 commit comments

Comments
 (0)