From ea7b043775d344a100412bf3933767406f6e5dc0 Mon Sep 17 00:00:00 2001 From: Greg Wilson Date: Sun, 6 Nov 2016 14:29:31 -0500 Subject: [PATCH] added new Methods_Sleep category --- .../RCTAppleHealthKit+Methods_Sleep.h | 7 ++ .../RCTAppleHealthKit+Methods_Sleep.m | 95 +++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 RCTAppleHealthKit/RCTAppleHealthKit+Methods_Sleep.h create mode 100644 RCTAppleHealthKit/RCTAppleHealthKit+Methods_Sleep.m diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Sleep.h b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Sleep.h new file mode 100644 index 0000000..c683625 --- /dev/null +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Sleep.h @@ -0,0 +1,7 @@ +#import "RCTAppleHealthKit.h" + +@interface RCTAppleHealthKit (Methods_Sleep) + +- (void)sleep_getSleepSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; + +@end diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Sleep.m b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Sleep.m new file mode 100644 index 0000000..0431a67 --- /dev/null +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Sleep.m @@ -0,0 +1,95 @@ + +#import "RCTAppleHealthKit+Methods_Sleep.h" +#import "RCTAppleHealthKit+Queries.h" +#import "RCTAppleHealthKit+Utils.h" + +@implementation RCTAppleHealthKit (Methods_Sleep) + + + +- (void)sleep_getSleepSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback +{ + NSDate *startDate = [RCTAppleHealthKit dateFromOptions:input key:@"startDate" withDefault:nil]; + NSDate *endDate = [RCTAppleHealthKit dateFromOptions:input key:@"endDate" withDefault:[NSDate date]]; + if(startDate == nil){ + callback(@[RCTMakeError(@"startDate is required in options", nil, nil)]); + return; + } + + NSPredicate *predicate = [RCTAppleHealthKit predicateForSamplesBetweenDates:startDate endDate:endDate]; + NSUInteger *limit = [RCTAppleHealthKit uintFromOptions:input key:@"limit" withDefault:HKObjectQueryNoLimit]; + + + [self fetchSleepCategorySamplesForPredicate:predicate + limit:limit + completion:^(NSArray *results, NSError *error) { + if(results){ + callback(@[[NSNull null], results]); + return; + } else { + NSLog(@"error getting sleep samples: %@", error); + callback(@[RCTMakeError(@"error getting sleep samples", nil, nil)]); + return; + } + }]; + + + + + + + + + + + + +// fetchSleepCategorySamplesForPredicate + + + + + // HKCategoryType *categoryType = + // [HKObjectType categoryTypeForIdentifier:HKCategoryTypeIdentifierSleepAnalysis]; + // + // HKCategorySample *categorySample = + // [HKCategorySample categorySampleWithType:categoryType + // value:HKCategoryValueSleepAnalysisAsleep + // startDate:startDate + // endDate:endDate]; + + + + + + // HKQuantityType *weightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass]; + // + // HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input]; + // if(unit == nil){ + // unit = [HKUnit poundUnit]; + // } + // + // [self fetchMostRecentQuantitySampleOfType:weightType + // predicate:nil + // completion:^(HKQuantity *mostRecentQuantity, NSDate *startDate, NSDate *endDate, NSError *error) { + // if (!mostRecentQuantity) { + // NSLog(@"error getting latest weight: %@", error); + // callback(@[RCTMakeError(@"error getting latest weight", error, nil)]); + // } + // else { + // // Determine the weight in the required unit. + // double usersWeight = [mostRecentQuantity doubleValueForUnit:unit]; + // + // NSDictionary *response = @{ + // @"value" : @(usersWeight), + // @"startDate" : [RCTAppleHealthKit buildISO8601StringFromDate:startDate], + // @"endDate" : [RCTAppleHealthKit buildISO8601StringFromDate:endDate], + // }; + // + // callback(@[[NSNull null], response]); + // } + // }]; +} + + +@end -- 2.26.2