diff --git a/RCTAppleHealthKit.xcodeproj/project.pbxproj b/RCTAppleHealthKit.xcodeproj/project.pbxproj index d3ab2a5b767f905686242735639c588d22a0cdd1..6ef3d6e66ba402c1da4ba8191eec62e948bd6912 100644 --- a/RCTAppleHealthKit.xcodeproj/project.pbxproj +++ b/RCTAppleHealthKit.xcodeproj/project.pbxproj @@ -11,6 +11,7 @@ 3774C8931D2092F20000B3F3 /* RCTAppleHealthKit.m in Sources */ = {isa = PBXBuildFile; fileRef = 3774C8921D2092F20000B3F3 /* RCTAppleHealthKit.m */; }; 3774C89B1D2095450000B3F3 /* RCTAppleHealthKit+Queries.m in Sources */ = {isa = PBXBuildFile; fileRef = 3774C89A1D2095450000B3F3 /* RCTAppleHealthKit+Queries.m */; }; 3774C89E1D2095850000B3F3 /* RCTAppleHealthKit+TypesAndPermissions.m in Sources */ = {isa = PBXBuildFile; fileRef = 3774C89D1D2095850000B3F3 /* RCTAppleHealthKit+TypesAndPermissions.m */; }; + 3774C8A11D20A6B90000B3F3 /* RCTAppleHealthKit+Utils.m in Sources */ = {isa = PBXBuildFile; fileRef = 3774C8A01D20A6B90000B3F3 /* RCTAppleHealthKit+Utils.m */; }; /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ @@ -34,6 +35,8 @@ 3774C89A1D2095450000B3F3 /* RCTAppleHealthKit+Queries.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "RCTAppleHealthKit+Queries.m"; sourceTree = ""; }; 3774C89C1D2095850000B3F3 /* RCTAppleHealthKit+TypesAndPermissions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "RCTAppleHealthKit+TypesAndPermissions.h"; sourceTree = ""; }; 3774C89D1D2095850000B3F3 /* RCTAppleHealthKit+TypesAndPermissions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "RCTAppleHealthKit+TypesAndPermissions.m"; sourceTree = ""; }; + 3774C89F1D20A6B90000B3F3 /* RCTAppleHealthKit+Utils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "RCTAppleHealthKit+Utils.h"; sourceTree = ""; }; + 3774C8A01D20A6B90000B3F3 /* RCTAppleHealthKit+Utils.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "RCTAppleHealthKit+Utils.m"; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -72,6 +75,8 @@ 3774C89A1D2095450000B3F3 /* RCTAppleHealthKit+Queries.m */, 3774C89C1D2095850000B3F3 /* RCTAppleHealthKit+TypesAndPermissions.h */, 3774C89D1D2095850000B3F3 /* RCTAppleHealthKit+TypesAndPermissions.m */, + 3774C89F1D20A6B90000B3F3 /* RCTAppleHealthKit+Utils.h */, + 3774C8A01D20A6B90000B3F3 /* RCTAppleHealthKit+Utils.m */, ); path = RCTAppleHealthKit; sourceTree = ""; @@ -133,6 +138,7 @@ buildActionMask = 2147483647; files = ( 3774C89B1D2095450000B3F3 /* RCTAppleHealthKit+Queries.m in Sources */, + 3774C8A11D20A6B90000B3F3 /* RCTAppleHealthKit+Utils.m in Sources */, 3774C8931D2092F20000B3F3 /* RCTAppleHealthKit.m in Sources */, 3774C89E1D2095850000B3F3 /* RCTAppleHealthKit+TypesAndPermissions.m in Sources */, ); diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h index 57769a414a1b9ab9ca05a1aaddaaca67fac2c87e..45139dffd11e5874818af91da77dbb3099e67743 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h @@ -12,6 +12,5 @@ - (void)fetchMostRecentQuantitySampleOfType:(HKQuantityType *)quantityType predicate:(NSPredicate *)predicate completion:(void (^)(HKQuantity *mostRecentQuantity, NSError *error))completion; - (void)fetchSumOfSamplesTodayForType:(HKQuantityType *)quantityType unit:(HKUnit *)unit completion:(void (^)(double, NSError *))completionHandler; -- (NSPredicate *)predicateForSamplesToday; @end diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m index 3e34e6deac48b94b800c321e809fba9eb89082e5..2dba35bf169e21de6dbe48000295764462958a76 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m @@ -7,6 +7,7 @@ // #import "RCTAppleHealthKit+Queries.h" +#import "RCTAppleHealthKit+Utils.h" @implementation RCTAppleHealthKit (Queries) @@ -35,14 +36,14 @@ completion(quantity, error); } }]; - + [self.healthStore executeQuery:query]; } - (void)fetchSumOfSamplesTodayForType:(HKQuantityType *)quantityType unit:(HKUnit *)unit completion:(void (^)(double, NSError *))completionHandler { - NSPredicate *predicate = [self predicateForSamplesToday]; + NSPredicate *predicate = [RCTAppleHealthKit predicateForSamplesToday]; HKStatisticsQuery *query = [[HKStatisticsQuery alloc] initWithQuantityType:quantityType quantitySamplePredicate:predicate options:HKStatisticsOptionCumulativeSum completionHandler:^(HKStatisticsQuery *query, HKStatistics *result, NSError *error) { HKQuantity *sum = [result sumQuantity]; @@ -57,22 +58,4 @@ [self.healthStore executeQuery:query]; } - - - -#pragma mark - Convenience - -- (NSPredicate *)predicateForSamplesToday { - NSCalendar *calendar = [NSCalendar currentCalendar]; - - NSDate *now = [NSDate date]; - - NSDate *startDate = [calendar startOfDayForDate:now]; - NSDate *endDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:1 toDate:startDate options:0]; - - return [HKQuery predicateForSamplesWithStartDate:startDate endDate:endDate options:HKQueryOptionStrictStartDate]; -} - - - @end diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Utils.h b/RCTAppleHealthKit/RCTAppleHealthKit+Utils.h new file mode 100644 index 0000000000000000000000000000000000000000..08d547c273e2b153a1a3e589ad095b5e6a36216c --- /dev/null +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Utils.h @@ -0,0 +1,18 @@ +// +// RCTAppleHealthKit+Utils.h +// RCTAppleHealthKit +// +// Created by Greg Wilson on 2016-06-26. +// Copyright © 2016 Greg Wilson. All rights reserved. +// + +#import "RCTAppleHealthKit.h" + +@interface RCTAppleHealthKit (Utils) + ++ (NSDate *)parseISO8601DateFromString:(NSString *)date; ++ (NSString *)buildISO8601StringFromDate:(NSDate *)date; ++ (NSPredicate *)predicateForSamplesToday; ++ (NSPredicate *)predicateForSamplesOnDay:(NSDate *)date; ++ (NSPredicate *)predicateForSamplesOnDayFromTimestamp:(NSString *)timestamp; +@end diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Utils.m b/RCTAppleHealthKit/RCTAppleHealthKit+Utils.m new file mode 100644 index 0000000000000000000000000000000000000000..43c8cfb5f62ae06473f7025931b606d91afb0afa --- /dev/null +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Utils.m @@ -0,0 +1,50 @@ +// +// RCTAppleHealthKit+Utils.m +// RCTAppleHealthKit +// +// Created by Greg Wilson on 2016-06-26. +// Copyright © 2016 Greg Wilson. All rights reserved. +// + +#import "RCTAppleHealthKit+Utils.h" + +@implementation RCTAppleHealthKit (Utils) + +#pragma mark - Utilities + ++ (NSDate *)parseISO8601DateFromString:(NSString *)date +{ + NSDateFormatter *dateFormatter = [NSDateFormatter new]; + NSLocale *posix = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]; + dateFormatter.locale = posix; + dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ssZZZZZ"; + return [dateFormatter dateFromString:date]; +} + ++ (NSString *)buildISO8601StringFromDate:(NSDate *)date +{ + NSDateFormatter *dateFormatter = [NSDateFormatter new]; + NSLocale *posix = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]; + dateFormatter.locale = posix; + dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ssZZZZZ"; + return [dateFormatter stringFromDate:date]; +} + ++ (NSPredicate *)predicateForSamplesToday { + NSDate *now = [NSDate date]; + return [RCTAppleHealthKit predicateForSamplesOnDay:now]; +} + ++ (NSPredicate *)predicateForSamplesOnDayFromTimestamp:(NSString *)timestamp { + NSDate *day = [RCTAppleHealthKit parseISO8601DateFromString:timestamp]; + return [RCTAppleHealthKit predicateForSamplesOnDay:day]; +} + ++ (NSPredicate *)predicateForSamplesOnDay:(NSDate *)date { + NSCalendar *calendar = [NSCalendar currentCalendar]; + NSDate *startDate = [calendar startOfDayForDate:date]; + NSDate *endDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:1 toDate:startDate options:0]; + return [HKQuery predicateForSamplesWithStartDate:startDate endDate:endDate options:HKQueryOptionStrictStartDate]; +} + +@end diff --git a/RCTAppleHealthKit/RCTAppleHealthKit.m b/RCTAppleHealthKit/RCTAppleHealthKit.m index abcd1cf7ed0d2a94377abbf8abf469cf15b95533..3a8969ca7f3229a0810bb16fb3c76ea52e1e658f 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit.m @@ -9,6 +9,7 @@ #import "RCTAppleHealthKit.h" #import "RCTAppleHealthKit+Queries.h" #import "RCTAppleHealthKit+TypesAndPermissions.h" +#import "RCTAppleHealthKit+Utils.h" @implementation RCTAppleHealthKit