Skip to content

Commit

Permalink
config for workoutevents to anchor workout
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-john-nguyen committed Jan 6, 2023
1 parent 7888bfd commit 89c66d2
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
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

0 comments on commit 89c66d2

Please sign in to comment.