Skip to content

Workout Events and Duration To getAnchoredWorkouts Method #271

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

Merged
merged 4 commits into from
Jul 3, 2023
Merged
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
6 changes: 5 additions & 1 deletion RCTAppleHealthKit/RCTAppleHealthKit+Queries.m
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,8 @@ - (void)fetchAnchoredWorkouts:(HKSampleType *)type
double energy = [[sample totalEnergyBurned] doubleValueForUnit:[HKUnit kilocalorieUnit]];
double distance = [[sample totalDistance] doubleValueForUnit:[HKUnit mileUnit]];
NSString *type = [RCTAppleHealthKit stringForHKWorkoutActivityType:[sample workoutActivityType]];
NSArray *workoutEvents = [RCTAppleHealthKit formatWorkoutEvents:[sample workoutEvents]];
NSTimeInterval duration = [sample duration];

NSString *startDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.startDate];
NSString *endDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.endDate];
Expand Down Expand Up @@ -546,7 +548,9 @@ - (void)fetchAnchoredWorkouts:(HKSampleType *)type
@"device": device,
@"distance" : @(distance),
@"start" : startDateString,
@"end" : endDateString
@"end" : endDateString,
@"duration": @(duration),
@"workoutEvents": workoutEvents
};

[data addObject:elem];
Expand Down
2 changes: 1 addition & 1 deletion RCTAppleHealthKit/RCTAppleHealthKit+Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ extern NSString * const kMetadataKey;
+ (HKWorkoutActivityType)hkWorkoutActivityTypeFromOptions: (NSDictionary *)options key: (NSString *)key withDefault: (HKWorkoutActivityType)defaultValue;
+ (HKQuantity *)hkQuantityFromOptions:(NSDictionary *)options valueKey: (NSString *)valueKey unitKey: (NSString *)unitKey;
+ (NSDictionary *)metadataFromOptions:(NSDictionary *)options withDefault:(NSDictionary *)defaultValue;

+ (NSArray *)formatWorkoutEvents:(NSArray *)workoutEvents;
+ (NSMutableArray *)reverseNSMutableArray:(NSMutableArray *)array;
+ (NSString*) stringForHKWorkoutActivityType:(int) enumValue;

Expand Down
49 changes: 49 additions & 0 deletions RCTAppleHealthKit/RCTAppleHealthKit+Utils.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,55 @@ @implementation RCTAppleHealthKit (Utils)

#pragma mark - Utilities

+ (NSArray *)formatWorkoutEvents:(NSArray *)workoutEvents
{
NSMutableArray *formattedWorkEvents = [[NSMutableArray alloc] init];

for (id workoutEvent in workoutEvents) {
NSNumber *eventType = [workoutEvent valueForKey:@"type"];
NSString *eventDescription = @"";

switch([eventType intValue]) {
case (int)HKWorkoutEventTypePause:
eventDescription = @"pause";
break;
case (int)HKWorkoutEventTypeResume:
eventDescription = @"resume";
break;
case (int)HKWorkoutEventTypeMotionPaused:
eventDescription = @"motion paused";
break;
case (int)HKWorkoutEventTypeMotionResumed:
eventDescription = @"motion resumed";
break;
case (int)HKWorkoutEventTypePauseOrResumeRequest:
eventDescription = @"pause or resume request";
break;
case (int)HKWorkoutEventTypeLap:
eventDescription = @"lap";
break;
case (int)HKWorkoutEventTypeSegment:
eventDescription = @"segment";
break;
case (int)HKWorkoutEventTypeMarker:
eventDescription = @"marker";
default:
eventDescription = @"";
}


NSObject *formattedEvent = @{
@"eventTypeInt":eventType,
@"eventType": eventDescription,
@"startDate": [RCTAppleHealthKit buildISO8601StringFromDate:[[workoutEvent dateInterval] startDate]],
@"endDate": [RCTAppleHealthKit buildISO8601StringFromDate:[[workoutEvent dateInterval] endDate]]
};
[formattedWorkEvents addObject: formattedEvent];
}

return formattedWorkEvents;
}

+ (NSDate *)parseISO8601DateFromString:(NSString *)date
{
@try {
Expand Down
7 changes: 7 additions & 0 deletions docs/getAnchoredWorkouts.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,12 @@ The resulting workouts in the array will look as the following:
distance: Number, // [[sample totalDistance] doubleValueForUnit:[HKUnit mileUnit]]
start: String, // [RCTAppleHealthKit buildISO8601StringFromDate:sample.startDate];
end: String, // [RCTAppleHealthKit buildISO8601StringFromDate:sample.endDate];
duration: Number, // NSTimeInterval (seconds)
workoutEvents: {
endDate: String, // [RCTAppleHealthKit buildISO8601StringFromDate:sample.endDate];
startDate: String, // [RCTAppleHealthKit buildISO8601StringFromDate:sample.startDate]
eventTypeInt: Number,
eventType: String, // EventType enum
}[]
}
```
20 changes: 20 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,24 @@ declare module 'react-native-health' {
locations: LocationValue[]
}

export enum EventType {
Pause = 'pause',
Resume = 'resume',
MotionPaused = 'motion paused',
MotionResumed = 'motion resumed',
PausedOrResumeRequest = 'pause or resume request',
Lap = 'lap',
Segment = 'segment',
Marker = 'marker'
}

export type HKWorkoutEventType = {
endDate: string
startDate: string
eventTypeInt: number
eventType: EventType
}

export interface HKWorkoutQueriedSampleType {
activityId: number
activityName: string
Expand All @@ -511,6 +529,8 @@ declare module 'react-native-health' {
distance: number
start: string
end: string
duration: number
workoutEvents: HKWorkoutEventType[]
}


Expand Down