Skip to content
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

Add sexual activity and double support percentage #241

Open
wants to merge 6 commits into
base: development
Choose a base branch
from
Open
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
58 changes: 38 additions & 20 deletions RCTAppleHealthKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
64E0E73F1D37947B00EAB905 /* RCTAppleHealthKit+Methods_Results.m in Sources */ = {isa = PBXBuildFile; fileRef = 64E0E73E1D37947B00EAB905 /* RCTAppleHealthKit+Methods_Results.m */; };
8C640F40269DF69C004CAFED /* RCTAppleHealthKit+Methods_Hearing.m in Sources */ = {isa = PBXBuildFile; fileRef = 8C640F3F269DF69C004CAFED /* RCTAppleHealthKit+Methods_Hearing.m */; };
8C79D960268E081A00DBDC40 /* RCTAppleHealthKit+Methods_ClinicalRecords.m in Sources */ = {isa = PBXBuildFile; fileRef = 8C79D95F268E081A00DBDC40 /* RCTAppleHealthKit+Methods_ClinicalRecords.m */; };
9328B25028AFCD2400CA9755 /* RCTAppleHealthKit+Methods_Dietary.m in Sources */ = {isa = PBXBuildFile; fileRef = 9328B24A28AFCD2400CA9755 /* RCTAppleHealthKit+Methods_Dietary.m */; };
9328B25128AFCD2400CA9755 /* RCTAppleHealthKit+Methods_Mobility.m in Sources */ = {isa = PBXBuildFile; fileRef = 9328B24B28AFCD2400CA9755 /* RCTAppleHealthKit+Methods_Mobility.m */; };
9328B25228AFCD2400CA9755 /* RCTAppleHealthKit+Methods_Reproductive.m in Sources */ = {isa = PBXBuildFile; fileRef = 9328B24F28AFCD2400CA9755 /* RCTAppleHealthKit+Methods_Reproductive.m */; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
Expand Down Expand Up @@ -75,6 +78,12 @@
8C640F3F269DF69C004CAFED /* RCTAppleHealthKit+Methods_Hearing.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "RCTAppleHealthKit+Methods_Hearing.m"; sourceTree = "<group>"; };
8C79D95E268E07DB00DBDC40 /* RCTAppleHealthKit+Methods_ClinicalRecords.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "RCTAppleHealthKit+Methods_ClinicalRecords.h"; sourceTree = "<group>"; };
8C79D95F268E081A00DBDC40 /* RCTAppleHealthKit+Methods_ClinicalRecords.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "RCTAppleHealthKit+Methods_ClinicalRecords.m"; sourceTree = "<group>"; };
9328B24A28AFCD2400CA9755 /* RCTAppleHealthKit+Methods_Dietary.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "RCTAppleHealthKit+Methods_Dietary.m"; sourceTree = "<group>"; };
9328B24B28AFCD2400CA9755 /* RCTAppleHealthKit+Methods_Mobility.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "RCTAppleHealthKit+Methods_Mobility.m"; sourceTree = "<group>"; };
9328B24C28AFCD2400CA9755 /* RCTAppleHealthKit+Methods_Reproductive.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "RCTAppleHealthKit+Methods_Reproductive.h"; sourceTree = "<group>"; };
9328B24D28AFCD2400CA9755 /* RCTAppleHealthKit+Methods_Mobility.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "RCTAppleHealthKit+Methods_Mobility.h"; sourceTree = "<group>"; };
9328B24E28AFCD2400CA9755 /* RCTAppleHealthKit+Methods_Dietary.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "RCTAppleHealthKit+Methods_Dietary.h"; sourceTree = "<group>"; };
9328B24F28AFCD2400CA9755 /* RCTAppleHealthKit+Methods_Reproductive.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "RCTAppleHealthKit+Methods_Reproductive.m"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -107,40 +116,46 @@
3774C88F1D2092F20000B3F3 /* RCTAppleHealthKit */ = {
isa = PBXGroup;
children = (
4F8095C72523C39D00A84ADB /* RCTAppleHealthKit+Methods_LabTests.h */,
4F8095C82523C39D00A84ADB /* RCTAppleHealthKit+Methods_LabTests.m */,
9328B24E28AFCD2400CA9755 /* RCTAppleHealthKit+Methods_Dietary.h */,
9328B24A28AFCD2400CA9755 /* RCTAppleHealthKit+Methods_Dietary.m */,
9328B24D28AFCD2400CA9755 /* RCTAppleHealthKit+Methods_Mobility.h */,
9328B24B28AFCD2400CA9755 /* RCTAppleHealthKit+Methods_Mobility.m */,
9328B24C28AFCD2400CA9755 /* RCTAppleHealthKit+Methods_Reproductive.h */,
9328B24F28AFCD2400CA9755 /* RCTAppleHealthKit+Methods_Reproductive.m */,
3774C8901D2092F20000B3F3 /* RCTAppleHealthKit.h */,
3774C8921D2092F20000B3F3 /* RCTAppleHealthKit.m */,
58C81E6E1F84F6970005DD48 /* RCTAppleHealthKit+Methods_Activity.h */,
58C81E6D1F84F6970005DD48 /* RCTAppleHealthKit+Methods_Activity.m */,
3774C8D21D20C6390000B3F3 /* RCTAppleHealthKit+Methods_Body.h */,
3774C8D31D20C6390000B3F3 /* RCTAppleHealthKit+Methods_Body.m */,
377D44F11D247D0A004E35CB /* RCTAppleHealthKit+Methods_Characteristic.h */,
377D44F21D247D0A004E35CB /* RCTAppleHealthKit+Methods_Characteristic.m */,
8C79D95E268E07DB00DBDC40 /* RCTAppleHealthKit+Methods_ClinicalRecords.h */,
8C79D95F268E081A00DBDC40 /* RCTAppleHealthKit+Methods_ClinicalRecords.m */,
3774C8D51D20C65F0000B3F3 /* RCTAppleHealthKit+Methods_Fitness.h */,
3774C8D61D20C65F0000B3F3 /* RCTAppleHealthKit+Methods_Fitness.m */,
8C640F3E269DF680004CAFED /* RCTAppleHealthKit+Methods_Hearing.h */,
8C640F3F269DF69C004CAFED /* RCTAppleHealthKit+Methods_Hearing.m */,
4F8095C72523C39D00A84ADB /* RCTAppleHealthKit+Methods_LabTests.h */,
4F8095C82523C39D00A84ADB /* RCTAppleHealthKit+Methods_LabTests.m */,
61232F911E303758000A5026 /* RCTAppleHealthKit+Methods_Mindfulness.h */,
61232F921E303758000A5026 /* RCTAppleHealthKit+Methods_Mindfulness.m */,
64E0E73D1D37947B00EAB905 /* RCTAppleHealthKit+Methods_Results.h */,
64E0E73E1D37947B00EAB905 /* RCTAppleHealthKit+Methods_Results.m */,
37837E7B1DCFE270000201A0 /* RCTAppleHealthKit+Methods_Sleep.h */,
37837E7C1DCFE270000201A0 /* RCTAppleHealthKit+Methods_Sleep.m */,
27B60163269770030034AB4E /* RCTAppleHealthKit+Methods_Summary.h */,
27B60164269770850034AB4E /* RCTAppleHealthKit+Methods_Summary.m */,
64C42D481D351A8800A0A8F7 /* RCTAppleHealthKit+Methods_Vitals.h */,
64C42D491D351A8800A0A8F7 /* RCTAppleHealthKit+Methods_Vitals.m */,
3774C8901D2092F20000B3F3 /* RCTAppleHealthKit.h */,
3774C8921D2092F20000B3F3 /* RCTAppleHealthKit.m */,
358330DD20931B8A000F0B94 /* RCTAppleHealthKit+Methods_Workout.h */,
358330DE20931B8A000F0B94 /* RCTAppleHealthKit+Methods_Workout.m */,
3774C8991D2095450000B3F3 /* RCTAppleHealthKit+Queries.h */,
3774C89A1D2095450000B3F3 /* RCTAppleHealthKit+Queries.m */,
3774C89C1D2095850000B3F3 /* RCTAppleHealthKit+TypesAndPermissions.h */,
3774C89D1D2095850000B3F3 /* RCTAppleHealthKit+TypesAndPermissions.m */,
3774C89F1D20A6B90000B3F3 /* RCTAppleHealthKit+Utils.h */,
3774C8A01D20A6B90000B3F3 /* RCTAppleHealthKit+Utils.m */,
3774C8D21D20C6390000B3F3 /* RCTAppleHealthKit+Methods_Body.h */,
3774C8D31D20C6390000B3F3 /* RCTAppleHealthKit+Methods_Body.m */,
3774C8D51D20C65F0000B3F3 /* RCTAppleHealthKit+Methods_Fitness.h */,
3774C8D61D20C65F0000B3F3 /* RCTAppleHealthKit+Methods_Fitness.m */,
377D44F11D247D0A004E35CB /* RCTAppleHealthKit+Methods_Characteristic.h */,
377D44F21D247D0A004E35CB /* RCTAppleHealthKit+Methods_Characteristic.m */,
37837E7B1DCFE270000201A0 /* RCTAppleHealthKit+Methods_Sleep.h */,
37837E7C1DCFE270000201A0 /* RCTAppleHealthKit+Methods_Sleep.m */,
358330DD20931B8A000F0B94 /* RCTAppleHealthKit+Methods_Workout.h */,
358330DE20931B8A000F0B94 /* RCTAppleHealthKit+Methods_Workout.m */,
8C640F3E269DF680004CAFED /* RCTAppleHealthKit+Methods_Hearing.h */,
8C640F3F269DF69C004CAFED /* RCTAppleHealthKit+Methods_Hearing.m */,
27B60163269770030034AB4E /* RCTAppleHealthKit+Methods_Summary.h */,
27B60164269770850034AB4E /* RCTAppleHealthKit+Methods_Summary.m */,
8C79D95E268E07DB00DBDC40 /* RCTAppleHealthKit+Methods_ClinicalRecords.h */,
8C79D95F268E081A00DBDC40 /* RCTAppleHealthKit+Methods_ClinicalRecords.m */,
);
path = RCTAppleHealthKit;
sourceTree = "<group>";
Expand Down Expand Up @@ -203,11 +218,14 @@
buildActionMask = 2147483647;
files = (
3774C89B1D2095450000B3F3 /* RCTAppleHealthKit+Queries.m in Sources */,
9328B25228AFCD2400CA9755 /* RCTAppleHealthKit+Methods_Reproductive.m in Sources */,
3774C8A11D20A6B90000B3F3 /* RCTAppleHealthKit+Utils.m in Sources */,
8C640F40269DF69C004CAFED /* RCTAppleHealthKit+Methods_Hearing.m in Sources */,
9328B25028AFCD2400CA9755 /* RCTAppleHealthKit+Methods_Dietary.m in Sources */,
37837E7D1DCFE270000201A0 /* RCTAppleHealthKit+Methods_Sleep.m in Sources */,
3774C8D41D20C6390000B3F3 /* RCTAppleHealthKit+Methods_Body.m in Sources */,
61232F931E303758000A5026 /* RCTAppleHealthKit+Methods_Mindfulness.m in Sources */,
9328B25128AFCD2400CA9755 /* RCTAppleHealthKit+Methods_Mobility.m in Sources */,
64E0E73F1D37947B00EAB905 /* RCTAppleHealthKit+Methods_Results.m in Sources */,
358330DF20931B8A000F0B94 /* RCTAppleHealthKit+Methods_Workout.m in Sources */,
64C42D4A1D351A8800A0A8F7 /* RCTAppleHealthKit+Methods_Vitals.m in Sources */,
Expand Down
14 changes: 14 additions & 0 deletions RCTAppleHealthKit/RCTAppleHealthKit+Methods_Mobility.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// RCTAppleHealthKit+Methods_Mindfulness.h
// RCTAppleHealthKit
//
// This source code is licensed under the MIT-style license found in the
// LICENSE file in the root directory of this source tree.

#import "RCTAppleHealthKit.h"

@interface RCTAppleHealthKit (Methods_Mobility)

- (void)mobility_getDoubleSupportPercentage:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;

@end
44 changes: 44 additions & 0 deletions RCTAppleHealthKit/RCTAppleHealthKit+Methods_Mobility.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// RCTAppleHealthKit+Methods_Results.m
// RCTAppleHealthKit
//
// This source code is licensed under the MIT-style license found in the
// LICENSE file in the root directory of this source tree.

#import "RCTAppleHealthKit+Methods_Reproductive.h"
#import "RCTAppleHealthKit+Queries.h"
#import "RCTAppleHealthKit+Utils.h"

@implementation RCTAppleHealthKit (Methods_Mobility)

- (void)mobility_getDoubleSupportPercentage:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{
if (@available(iOS 14.0, *)) {
HKUnit *unit = [HKUnit percentUnit];
NSDate *startDate = [RCTAppleHealthKit dateFromOptions:input key:@"startDate" withDefault:nil];
NSDate *endDate = [RCTAppleHealthKit dateFromOptions:input key:@"endDate" withDefault:[NSDate date]];
double limit = [RCTAppleHealthKit doubleFromOptions:input key:@"limit" withDefault:HKObjectQueryNoLimit];
BOOL ascending = [RCTAppleHealthKit boolFromOptions:input key:@"ascending" withDefault:false];
HKQuantityType *type = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierWalkingDoubleSupportPercentage];
NSPredicate *predicate = [RCTAppleHealthKit predicateForSamplesBetweenDates:startDate endDate:endDate];

[self fetchQuantitySamplesOfType:type
unit:unit
predicate:predicate
ascending:ascending
limit:limit
completion:^(NSArray *results, NSError *error) {
if(results){
callback(@[[NSNull null], results]);
return;
} else {
callback(@[RCTJSErrorFromNSError(error), [NSNull null]]);
return;
}
}];
} else {
callback(@[@"DoubleSupportPercentage is only available on iOS 14.0 or newer", [NSNull null]]);
}
}

@end
15 changes: 15 additions & 0 deletions RCTAppleHealthKit/RCTAppleHealthKit+Methods_Reproductive.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// RCTAppleHealthKit+Methods_Mindfulness.h
// RCTAppleHealthKit
//
// This source code is licensed under the MIT-style license found in the
// LICENSE file in the root directory of this source tree.

#import "RCTAppleHealthKit.h"

@interface RCTAppleHealthKit (Methods_Reproductive)

- (void)reproductive_getSexualActivity:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)reproductive_saveSexualActivity:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;

@end
88 changes: 88 additions & 0 deletions RCTAppleHealthKit/RCTAppleHealthKit+Methods_Reproductive.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
//
// RCTAppleHealthKit+Methods_Results.m
// RCTAppleHealthKit
//
// This source code is licensed under the MIT-style license found in the
// LICENSE file in the root directory of this source tree.

#import "RCTAppleHealthKit+Methods_Reproductive.h"
#import "RCTAppleHealthKit+Queries.h"
#import "RCTAppleHealthKit+Utils.h"

@implementation RCTAppleHealthKit (Methods_Reproductive)

- (void)reproductive_getSexualActivity:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{
NSDate *startDate = [RCTAppleHealthKit dateFromOptions:input key:@"startDate" withDefault:nil];
NSDate *endDate = [RCTAppleHealthKit dateFromOptions:input key:@"endDate" withDefault:[NSDate date]];
double limit = [RCTAppleHealthKit doubleFromOptions:input key:@"limit" withDefault:HKObjectQueryNoLimit];

NSSortDescriptor *timeSortDescriptor = [[NSSortDescriptor alloc]
initWithKey:HKSampleSortIdentifierEndDate
ascending:NO
];

HKCategoryType *type = [HKCategoryType categoryTypeForIdentifier:HKCategoryTypeIdentifierSexualActivity];
NSPredicate *predicate = [RCTAppleHealthKit predicateForSamplesBetweenDates:startDate endDate:endDate];

HKSampleQuery *query = [[HKSampleQuery alloc]
initWithSampleType:type
predicate:predicate
limit: limit
sortDescriptors:@[timeSortDescriptor]
resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error) {

if (error != nil) {
NSLog(@"error with fetchCumulativeSumStatisticsCollection: %@", error);
callback(@[RCTMakeError(@"error with fetchCumulativeSumStatisticsCollection", error, nil)]);
return;
}

NSMutableArray *data = [NSMutableArray arrayWithCapacity:(10)];

for (HKCategorySample *sample in results) {
NSLog(@"sample for sexual activity %@", sample);
NSString *startDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.startDate];
NSString *endDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.endDate];

NSDictionary *elem = @{
@"startDate" : startDateString,
@"endDate" : endDateString,
@"metadata": sample.metadata == nil ? [NSNull null] : sample.metadata,
};

[data addObject:elem];
}
callback(@[[NSNull null], data]);
}
];
[self.healthStore executeQuery:query];
}

- (void)reproductive_saveSexualActivity:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{
double value = [RCTAppleHealthKit doubleFromOptions:input key:@"value" withDefault:(double)0];
NSDate *startDate = [RCTAppleHealthKit dateFromOptions:input key:@"startDate" withDefault:nil];
NSDate *endDate = [RCTAppleHealthKit dateFromOptions:input key:@"endDate" withDefault:[NSDate date]];
NSDictionary *metadata = [RCTAppleHealthKit metadataFromOptions:input withDefault: @{}];

if(startDate == nil || endDate == nil){
callback(@[RCTMakeError(@"startDate and endDate are required in options", nil, nil)]);
return;
}

HKCategoryType *type = [HKCategoryType categoryTypeForIdentifier: HKCategoryTypeIdentifierSexualActivity];
HKCategorySample *sample = [HKCategorySample categorySampleWithType:type value:value startDate:startDate endDate:endDate metadata:metadata];


[self.healthStore saveObject:sample withCompletion:^(BOOL success, NSError *error) {
if (!success) {
callback(@[RCTJSErrorFromNSError(error)]);
return;
}
callback(@[[NSNull null], @(value)]);
}];

}

@end
Loading