RCTAppleHealthKit+Methods_Sleep.m 2.99 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 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