From fefbb1998d40736be7bd11a95d85c91b21cfdb26 Mon Sep 17 00:00:00 2001 From: Greg Wilson Date: Tue, 28 Jun 2016 18:07:14 -0400 Subject: [PATCH] changed dateFormat in time methods and added methods to get values from the input options --- RCTAppleHealthKit/RCTAppleHealthKit+Utils.h | 4 ++ RCTAppleHealthKit/RCTAppleHealthKit+Utils.m | 77 ++++++++++++++++++++- 2 files changed, 79 insertions(+), 2 deletions(-) diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Utils.h b/RCTAppleHealthKit/RCTAppleHealthKit+Utils.h index 08d547c..4bc0c15 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Utils.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Utils.h @@ -15,4 +15,8 @@ + (NSPredicate *)predicateForSamplesToday; + (NSPredicate *)predicateForSamplesOnDay:(NSDate *)date; + (NSPredicate *)predicateForSamplesOnDayFromTimestamp:(NSString *)timestamp; ++ (double)doubleValueFromOptions:(NSDictionary *)options; ++ (NSDate *)dateFromOptionsDefaultNow:(NSDictionary *)options; ++ (HKUnit *)hkUnitFromOptions:(NSDictionary *)options; + @end diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Utils.m b/RCTAppleHealthKit/RCTAppleHealthKit+Utils.m index 43c8cfb..0f0a4cb 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Utils.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Utils.m @@ -17,7 +17,8 @@ NSDateFormatter *dateFormatter = [NSDateFormatter new]; NSLocale *posix = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]; dateFormatter.locale = posix; - dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ssZZZZZ"; +// dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ssZZZZZ"; + dateFormatter.dateFormat = @"yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSSZ"; return [dateFormatter dateFromString:date]; } @@ -26,7 +27,8 @@ NSDateFormatter *dateFormatter = [NSDateFormatter new]; NSLocale *posix = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]; dateFormatter.locale = posix; - dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ssZZZZZ"; +// dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ssZZZZZ"; + dateFormatter.dateFormat = @"yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSSZ"; return [dateFormatter stringFromDate:date]; } @@ -47,4 +49,75 @@ return [HKQuery predicateForSamplesWithStartDate:startDate endDate:endDate options:HKQueryOptionStrictStartDate]; } + + ++ (double)doubleValueFromOptions:(NSDictionary *)options { + double value = [[options objectForKey:@"value"] doubleValue]; + return value; +} + + ++ (NSDate *)dateFromOptionsDefaultNow:(NSDictionary *)options { + NSString *dateString = [options objectForKey:@"date"]; + if(dateString != nil){ + NSDate *date = [RCTAppleHealthKit parseISO8601DateFromString:dateString]; + if(date == nil){ + // probably not a good idea... should return an error or just the null pointer + date = [NSDate date]; + } + return date; + } + NSDate *now = [NSDate date]; + return now; +} + ++ (HKUnit *)hkUnitFromOptions:(NSDictionary *)options { + NSString *unitString = [options objectForKey:@"unit"]; + HKUnit *theUnit; + + if([unitString isEqualToString:@"gram"]){ + theUnit = [HKUnit gramUnit]; + } + if([unitString isEqualToString:@"pound"]){ + theUnit = [HKUnit poundUnit]; + } + if([unitString isEqualToString:@"meter"]){ + theUnit = [HKUnit meterUnit]; + } + if([unitString isEqualToString:@"inch"]){ + theUnit = [HKUnit inchUnit]; + } + if([unitString isEqualToString:@"foot"]){ + theUnit = [HKUnit footUnit]; + } + if([unitString isEqualToString:@"second"]){ + theUnit = [HKUnit secondUnit]; + } + if([unitString isEqualToString:@"minute"]){ + theUnit = [HKUnit minuteUnit]; + } + if([unitString isEqualToString:@"hour"]){ + theUnit = [HKUnit hourUnit]; + } + if([unitString isEqualToString:@"day"]){ + theUnit = [HKUnit dayUnit]; + } + if([unitString isEqualToString:@"joule"]){ + theUnit = [HKUnit jouleUnit]; + } + if([unitString isEqualToString:@"calorie"]){ + theUnit = [HKUnit calorieUnit]; + } + if([unitString isEqualToString:@"count"]){ + theUnit = [HKUnit countUnit]; + } + if([unitString isEqualToString:@"percent"]){ + theUnit = [HKUnit percentUnit]; + } + return theUnit; +} + + + + @end -- 2.26.2