RCTAppleHealthKit+Methods_Mindfulness.m 1.46 KB
Newer Older
skleest's avatar
skleest committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//
//  RCTAppleHealthKit+Methods_Mindfulness.m
//  RCTAppleHealthKit
//
//


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

@implementation RCTAppleHealthKit (Methods_Sleep)


- (void)mindfulness_saveMindfulSession:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{
skleest's avatar
typos  
skleest committed
17
    double value = [RCTAppleHealthKit doubleFromOptions:input key:@"value" withDefault:(double)0];
skleest's avatar
skleest committed
18 19 20 21 22 23 24 25 26
    NSDate *startDate = [RCTAppleHealthKit dateFromOptions:input key:@"startDate" withDefault:nil];
    NSDate *endDate = [RCTAppleHealthKit dateFromOptions:input key:@"endDate" withDefault:[NSDate date]];

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

    HKCategoryType *type = [HKCategoryType categoryTypeForIdentifier: HKCategoryTypeIdentifierMindfulSession];
skleest's avatar
typos  
skleest committed
27
    HKCategorySample *sample = [HKCategorySample categorySampleWithType:type value:value startDate:startDate endDate:endDate];
skleest's avatar
skleest committed
28 29 30 31 32


    [self.healthStore saveObject:sample withCompletion:^(BOOL success, NSError *error) {
        if (!success) {
            NSLog(@"An error occured saving the mindful session sample %@. The error was: %@.", sample, error);
skleest's avatar
skleest committed
33
            callback(@[RCTMakeError(@"An error occured saving the mindful session sample", error, nil)]);
skleest's avatar
skleest committed
34 35 36 37 38 39 40 41 42
            return;
        }
        callback(@[[NSNull null], @(value)]);
    }];

}


@end