Commit 76d9f7ef authored by Greg Wilson's avatar Greg Wilson

created new categories for body and fitness identifiers and refactored code to reflect changes

parent 794c50d3
...@@ -12,6 +12,8 @@ ...@@ -12,6 +12,8 @@
3774C89B1D2095450000B3F3 /* RCTAppleHealthKit+Queries.m in Sources */ = {isa = PBXBuildFile; fileRef = 3774C89A1D2095450000B3F3 /* RCTAppleHealthKit+Queries.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 */; }; 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 */; }; 3774C8A11D20A6B90000B3F3 /* RCTAppleHealthKit+Utils.m in Sources */ = {isa = PBXBuildFile; fileRef = 3774C8A01D20A6B90000B3F3 /* RCTAppleHealthKit+Utils.m */; };
3774C8D41D20C6390000B3F3 /* RCTAppleHealthKit+Methods_Body.m in Sources */ = {isa = PBXBuildFile; fileRef = 3774C8D31D20C6390000B3F3 /* RCTAppleHealthKit+Methods_Body.m */; };
3774C8D71D20C65F0000B3F3 /* RCTAppleHealthKit+Methods_Fitness.m in Sources */ = {isa = PBXBuildFile; fileRef = 3774C8D61D20C65F0000B3F3 /* RCTAppleHealthKit+Methods_Fitness.m */; };
/* End PBXBuildFile section */ /* End PBXBuildFile section */
/* Begin PBXCopyFilesBuildPhase section */ /* Begin PBXCopyFilesBuildPhase section */
...@@ -37,6 +39,10 @@ ...@@ -37,6 +39,10 @@
3774C89D1D2095850000B3F3 /* RCTAppleHealthKit+TypesAndPermissions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "RCTAppleHealthKit+TypesAndPermissions.m"; sourceTree = "<group>"; }; 3774C89D1D2095850000B3F3 /* RCTAppleHealthKit+TypesAndPermissions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "RCTAppleHealthKit+TypesAndPermissions.m"; sourceTree = "<group>"; };
3774C89F1D20A6B90000B3F3 /* RCTAppleHealthKit+Utils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "RCTAppleHealthKit+Utils.h"; sourceTree = "<group>"; }; 3774C89F1D20A6B90000B3F3 /* RCTAppleHealthKit+Utils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "RCTAppleHealthKit+Utils.h"; sourceTree = "<group>"; };
3774C8A01D20A6B90000B3F3 /* RCTAppleHealthKit+Utils.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "RCTAppleHealthKit+Utils.m"; sourceTree = "<group>"; }; 3774C8A01D20A6B90000B3F3 /* RCTAppleHealthKit+Utils.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "RCTAppleHealthKit+Utils.m"; sourceTree = "<group>"; };
3774C8D21D20C6390000B3F3 /* RCTAppleHealthKit+Methods_Body.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "RCTAppleHealthKit+Methods_Body.h"; sourceTree = "<group>"; };
3774C8D31D20C6390000B3F3 /* RCTAppleHealthKit+Methods_Body.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "RCTAppleHealthKit+Methods_Body.m"; sourceTree = "<group>"; };
3774C8D51D20C65F0000B3F3 /* RCTAppleHealthKit+Methods_Fitness.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "RCTAppleHealthKit+Methods_Fitness.h"; sourceTree = "<group>"; };
3774C8D61D20C65F0000B3F3 /* RCTAppleHealthKit+Methods_Fitness.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "RCTAppleHealthKit+Methods_Fitness.m"; sourceTree = "<group>"; };
/* End PBXFileReference section */ /* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */ /* Begin PBXFrameworksBuildPhase section */
...@@ -77,6 +83,10 @@ ...@@ -77,6 +83,10 @@
3774C89D1D2095850000B3F3 /* RCTAppleHealthKit+TypesAndPermissions.m */, 3774C89D1D2095850000B3F3 /* RCTAppleHealthKit+TypesAndPermissions.m */,
3774C89F1D20A6B90000B3F3 /* RCTAppleHealthKit+Utils.h */, 3774C89F1D20A6B90000B3F3 /* RCTAppleHealthKit+Utils.h */,
3774C8A01D20A6B90000B3F3 /* RCTAppleHealthKit+Utils.m */, 3774C8A01D20A6B90000B3F3 /* RCTAppleHealthKit+Utils.m */,
3774C8D21D20C6390000B3F3 /* RCTAppleHealthKit+Methods_Body.h */,
3774C8D31D20C6390000B3F3 /* RCTAppleHealthKit+Methods_Body.m */,
3774C8D51D20C65F0000B3F3 /* RCTAppleHealthKit+Methods_Fitness.h */,
3774C8D61D20C65F0000B3F3 /* RCTAppleHealthKit+Methods_Fitness.m */,
); );
path = RCTAppleHealthKit; path = RCTAppleHealthKit;
sourceTree = "<group>"; sourceTree = "<group>";
...@@ -139,8 +149,10 @@ ...@@ -139,8 +149,10 @@
files = ( files = (
3774C89B1D2095450000B3F3 /* RCTAppleHealthKit+Queries.m in Sources */, 3774C89B1D2095450000B3F3 /* RCTAppleHealthKit+Queries.m in Sources */,
3774C8A11D20A6B90000B3F3 /* RCTAppleHealthKit+Utils.m in Sources */, 3774C8A11D20A6B90000B3F3 /* RCTAppleHealthKit+Utils.m in Sources */,
3774C8D41D20C6390000B3F3 /* RCTAppleHealthKit+Methods_Body.m in Sources */,
3774C8931D2092F20000B3F3 /* RCTAppleHealthKit.m in Sources */, 3774C8931D2092F20000B3F3 /* RCTAppleHealthKit.m in Sources */,
3774C89E1D2095850000B3F3 /* RCTAppleHealthKit+TypesAndPermissions.m in Sources */, 3774C89E1D2095850000B3F3 /* RCTAppleHealthKit+TypesAndPermissions.m in Sources */,
3774C8D71D20C65F0000B3F3 /* RCTAppleHealthKit+Methods_Fitness.m in Sources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };
......
//
// RCTAppleHealthKit+Methods_Body.h
// RCTAppleHealthKit
//
// Created by Greg Wilson on 2016-06-26.
// Copyright © 2016 Greg Wilson. All rights reserved.
//
#import "RCTAppleHealthKit.h"
@interface RCTAppleHealthKit (Methods_Body)
- (void)body_getCurrentWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)body_saveWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
@end
//
// RCTAppleHealthKit+Methods_Body.m
// RCTAppleHealthKit
//
// Created by Greg Wilson on 2016-06-26.
// Copyright © 2016 Greg Wilson. All rights reserved.
//
#import "RCTAppleHealthKit+Methods_Body.h"
#import "RCTAppleHealthKit+Queries.h"
@implementation RCTAppleHealthKit (Methods_Body)
- (void)body_getCurrentWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{
// Query to get the user's latest weight, if it exists.
HKQuantityType *weightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass];
[self fetchMostRecentQuantitySampleOfType:weightType predicate:nil completion:^(HKQuantity *mostRecentQuantity, NSError *error) {
if (!mostRecentQuantity) {
NSLog(@"Either an error occured fetching the user's weight information or none has been stored yet. In your app, try to handle this gracefully.");
callback(@[RCTMakeError(@"Either an error occured fetching the user's weight information or none has been stored yet. In your app, try to handle this gracefully.", nil, nil)]);
}
else {
// Determine the weight in the required unit.
HKUnit *weightUnit = [HKUnit poundUnit];
double usersWeight = [mostRecentQuantity doubleValueForUnit:weightUnit];
callback(@[[NSNull null], @(usersWeight)]);
}
}];
}
- (void)body_saveWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{
double weight= [[input objectForKey:@"weight"] doubleValue];
HKUnit *poundUnit = [HKUnit poundUnit];
HKQuantity *weightQuantity = [HKQuantity quantityWithUnit:poundUnit doubleValue:weight];
HKQuantityType *weightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass];
NSDate *now = [NSDate date];
HKQuantitySample *weightSample = [HKQuantitySample quantitySampleWithType:weightType quantity:weightQuantity startDate:now endDate:now];
[self.healthStore saveObject:weightSample withCompletion:^(BOOL success, NSError *error) {
if (!success) {
NSLog(@"An error occured saving the weight sample %@. In your app, try to handle this gracefully. The error was: %@.", weightSample, error);
callback(@[RCTMakeError(@"An error occured saving the weight sample", nil, nil)]);
return;
// abort();
}
callback(@[[NSNull null], @(weight)]);
}];
}
@end
//
// RCTAppleHealthKit+Methods_Fitness.h
// RCTAppleHealthKit
//
// Created by Greg Wilson on 2016-06-26.
// Copyright © 2016 Greg Wilson. All rights reserved.
//
#import "RCTAppleHealthKit.h"
@interface RCTAppleHealthKit (Methods_Fitness)
- (void)fitness_getStepCountForToday:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
@end
//
// RCTAppleHealthKit+Methods_Fitness.m
// RCTAppleHealthKit
//
// Created by Greg Wilson on 2016-06-26.
// Copyright © 2016 Greg Wilson. All rights reserved.
//
#import "RCTAppleHealthKit+Methods_Fitness.h"
#import "RCTAppleHealthKit+Queries.h"
@implementation RCTAppleHealthKit (Methods_Fitness)
- (void)fitness_getStepCountForToday:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{
HKQuantityType *stepCountType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
HKUnit *stepsUnit = [HKUnit countUnit];
[self fetchSumOfSamplesTodayForType:stepCountType unit:stepsUnit completion:^(double totalSteps, NSError *error) {
if (!totalSteps) {
NSLog(@"Either an error occured fetching the user's step count information or none has been stored yet. In your app, try to handle this gracefully.");
callback(@[RCTMakeError(@"Either an error occured fetching the user's step count information or none has been stored yet. In your app, try to handle this gracefully.", nil, nil)]);
return;
}
callback(@[[NSNull null], @(totalSteps)]);
}];
}
@end
...@@ -14,7 +14,6 @@ ...@@ -14,7 +14,6 @@
- (void)fetchMostRecentQuantitySampleOfType:(HKQuantityType *)quantityType predicate:(NSPredicate *)predicate completion:(void (^)(HKQuantity *, NSError *))completion { NSSortDescriptor *timeSortDescriptor = [[NSSortDescriptor alloc] initWithKey:HKSampleSortIdentifierEndDate ascending:NO]; - (void)fetchMostRecentQuantitySampleOfType:(HKQuantityType *)quantityType predicate:(NSPredicate *)predicate completion:(void (^)(HKQuantity *, NSError *))completion { NSSortDescriptor *timeSortDescriptor = [[NSSortDescriptor alloc] initWithKey:HKSampleSortIdentifierEndDate ascending:NO];
// Since we are interested in retrieving the user's latest sample, we sort the samples in descending order, and set the limit to 1. We are not filtering the data, and so the predicate is set to nil.
HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:quantityType HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:quantityType
predicate:predicate predicate:predicate
limit:1 limit:1
...@@ -24,7 +23,6 @@ ...@@ -24,7 +23,6 @@
if (completion) { if (completion) {
completion(nil, error); completion(nil, error);
} }
return; return;
} }
...@@ -32,7 +30,6 @@ ...@@ -32,7 +30,6 @@
// If quantity isn't in the database, return nil in the completion block. // If quantity isn't in the database, return nil in the completion block.
HKQuantitySample *quantitySample = results.firstObject; HKQuantitySample *quantitySample = results.firstObject;
HKQuantity *quantity = quantitySample.quantity; HKQuantity *quantity = quantitySample.quantity;
completion(quantity, error); completion(quantity, error);
} }
}]; }];
...@@ -45,12 +42,14 @@ ...@@ -45,12 +42,14 @@
- (void)fetchSumOfSamplesTodayForType:(HKQuantityType *)quantityType unit:(HKUnit *)unit completion:(void (^)(double, NSError *))completionHandler { - (void)fetchSumOfSamplesTodayForType:(HKQuantityType *)quantityType unit:(HKUnit *)unit completion:(void (^)(double, NSError *))completionHandler {
NSPredicate *predicate = [RCTAppleHealthKit predicateForSamplesToday]; NSPredicate *predicate = [RCTAppleHealthKit predicateForSamplesToday];
HKStatisticsQuery *query = [[HKStatisticsQuery alloc] initWithQuantityType:quantityType quantitySamplePredicate:predicate options:HKStatisticsOptionCumulativeSum completionHandler:^(HKStatisticsQuery *query, HKStatistics *result, NSError *error) { HKStatisticsQuery *query = [[HKStatisticsQuery alloc] initWithQuantityType:quantityType
quantitySamplePredicate:predicate
options:HKStatisticsOptionCumulativeSum
completionHandler:^(HKStatisticsQuery *query, HKStatistics *result, NSError *error) {
HKQuantity *sum = [result sumQuantity]; HKQuantity *sum = [result sumQuantity];
if (completionHandler) { if (completionHandler) {
double value = [sum doubleValueForUnit:unit]; double value = [sum doubleValueForUnit:unit];
completionHandler(value, error); completionHandler(value, error);
} }
}]; }];
......
...@@ -80,7 +80,6 @@ ...@@ -80,7 +80,6 @@
} }
return readPermSet; return readPermSet;
// return [self dataTypesToRead];
} }
...@@ -99,7 +98,6 @@ ...@@ -99,7 +98,6 @@
} }
return writePermSet; return writePermSet;
// return [self dataTypesToWrite];
} }
@end @end
...@@ -18,9 +18,9 @@ ...@@ -18,9 +18,9 @@
- (void)isHealthKitAvailable:(RCTResponseSenderBlock)callback; - (void)isHealthKitAvailable:(RCTResponseSenderBlock)callback;
- (void)initializeHealthKit:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)initializeHealthKit:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)getHealthKitUserWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; //- (void)getHealthKitUserWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)saveHealthKitUserWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; //- (void)saveHealthKitUserWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)getHealthKitUserStepCount:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; //- (void)getHealthKitUserStepCount:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)getModuleInfo:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)getModuleInfo:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
@end @end
...@@ -11,6 +11,9 @@ ...@@ -11,6 +11,9 @@
#import "RCTAppleHealthKit+TypesAndPermissions.h" #import "RCTAppleHealthKit+TypesAndPermissions.h"
#import "RCTAppleHealthKit+Utils.h" #import "RCTAppleHealthKit+Utils.h"
#import "RCTAppleHealthKit+Methods_Body.h"
#import "RCTAppleHealthKit+Methods_Fitness.h"
@implementation RCTAppleHealthKit @implementation RCTAppleHealthKit
RCT_EXPORT_MODULE(); RCT_EXPORT_MODULE();
...@@ -28,17 +31,17 @@ RCT_EXPORT_METHOD(initHealthKit:(NSDictionary *)input callback:(RCTResponseSende ...@@ -28,17 +31,17 @@ RCT_EXPORT_METHOD(initHealthKit:(NSDictionary *)input callback:(RCTResponseSende
RCT_EXPORT_METHOD(getCurrentWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) RCT_EXPORT_METHOD(getCurrentWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
{ {
[self getHealthKitUserWeight:input callback:callback]; [self body_getCurrentWeight:input callback:callback];
} }
RCT_EXPORT_METHOD(saveWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) RCT_EXPORT_METHOD(saveWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
{ {
[self saveHealthKitUserWeight:input callback:callback]; [self body_saveWeight:input callback:callback];
} }
RCT_EXPORT_METHOD(getStepCountForToday:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) RCT_EXPORT_METHOD(getStepCountForToday:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
{ {
[self getHealthKitUserStepCount:input callback:callback]; [self fitness_getStepCountForToday:input callback:callback];
} }
RCT_EXPORT_METHOD(getInfo:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) RCT_EXPORT_METHOD(getInfo:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
...@@ -66,7 +69,7 @@ RCT_EXPORT_METHOD(getInfo:(NSDictionary *)input callback:(RCTResponseSenderBlock ...@@ -66,7 +69,7 @@ RCT_EXPORT_METHOD(getInfo:(NSDictionary *)input callback:(RCTResponseSenderBlock
NSSet *writeDataTypes = [self dataTypesToWrite]; NSSet *writeDataTypes = [self dataTypesToWrite];
NSSet *readDataTypes = [self dataTypesToRead]; NSSet *readDataTypes = [self dataTypesToRead];
// get permissions from input object provided by JS options object argument // get permissions from input object provided by JS options argument
NSDictionary* permissions =[input objectForKey:@"permissions"]; NSDictionary* permissions =[input objectForKey:@"permissions"];
if(permissions != nil){ if(permissions != nil){
NSArray* readPermsArray = [permissions objectForKey:@"read"]; NSArray* readPermsArray = [permissions objectForKey:@"read"];
...@@ -94,82 +97,80 @@ RCT_EXPORT_METHOD(getInfo:(NSDictionary *)input callback:(RCTResponseSenderBlock ...@@ -94,82 +97,80 @@ RCT_EXPORT_METHOD(getInfo:(NSDictionary *)input callback:(RCTResponseSenderBlock
callback(@[[NSNull null], @true]); callback(@[[NSNull null], @true]);
}); });
} }
}]; }];
} else { } else {
callback(@[RCTMakeError(@"HealthKit not available....", nil, nil)]); callback(@[RCTMakeError(@"HealthKit not available....", nil, nil)]);
} }
} }
//
//- (void)getHealthKitUserWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
//{
//// NSMassFormatter *massFormatter = [[NSMassFormatter alloc] init];
//// massFormatter.unitStyle = NSFormattingUnitStyleLong;
//
// // NSMassFormatterUnit weightFormatterUnit = NSMassFormatterUnitPound;
// // NSString *weightUnitString = [massFormatter unitStringFromValue:10 unit:weightFormatterUnit];
// // NSString *localizedWeightUnitDescriptionFormat = NSLocalizedString(@"Weight (%@)", nil);
//
// // Query to get the user's latest weight, if it exists.
// HKQuantityType *weightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass];
//
// [self fetchMostRecentQuantitySampleOfType:weightType predicate:nil completion:^(HKQuantity *mostRecentQuantity, NSError *error) {
// if (!mostRecentQuantity) {
// NSLog(@"Either an error occured fetching the user's weight information or none has been stored yet. In your app, try to handle this gracefully.");
// callback(@[RCTMakeError(@"Either an error occured fetching the user's weight information or none has been stored yet. In your app, try to handle this gracefully.", nil, nil)]);
// }
// else {
// // Determine the weight in the required unit.
// HKUnit *weightUnit = [HKUnit poundUnit];
// double usersWeight = [mostRecentQuantity doubleValueForUnit:weightUnit];
//
// callback(@[[NSNull null], @(usersWeight)]);
// }
// }];
//}
- (void)getHealthKitUserWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback //
{ //- (void)saveHealthKitUserWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
// NSMassFormatter *massFormatter = [[NSMassFormatter alloc] init]; //{
// massFormatter.unitStyle = NSFormattingUnitStyleLong; // double weight= [[input objectForKey:@"weight"] doubleValue];
//
// NSMassFormatterUnit weightFormatterUnit = NSMassFormatterUnitPound; // HKUnit *poundUnit = [HKUnit poundUnit];
// NSString *weightUnitString = [massFormatter unitStringFromValue:10 unit:weightFormatterUnit]; // HKQuantity *weightQuantity = [HKQuantity quantityWithUnit:poundUnit doubleValue:weight];
// NSString *localizedWeightUnitDescriptionFormat = NSLocalizedString(@"Weight (%@)", nil); //
// HKQuantityType *weightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass];
// Query to get the user's latest weight, if it exists. // NSDate *now = [NSDate date];
HKQuantityType *weightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass]; //
// HKQuantitySample *weightSample = [HKQuantitySample quantitySampleWithType:weightType quantity:weightQuantity startDate:now endDate:now];
[self fetchMostRecentQuantitySampleOfType:weightType predicate:nil completion:^(HKQuantity *mostRecentQuantity, NSError *error) { //
if (!mostRecentQuantity) { // [self.healthStore saveObject:weightSample withCompletion:^(BOOL success, NSError *error) {
NSLog(@"Either an error occured fetching the user's weight information or none has been stored yet. In your app, try to handle this gracefully."); // if (!success) {
callback(@[RCTMakeError(@"Either an error occured fetching the user's weight information or none has been stored yet. In your app, try to handle this gracefully.", nil, nil)]); // NSLog(@"An error occured saving the weight sample %@. In your app, try to handle this gracefully. The error was: %@.", weightSample, error);
} // callback(@[RCTMakeError(@"An error occured saving the weight sample", nil, nil)]);
else { // return;
// Determine the weight in the required unit. // // abort();
HKUnit *weightUnit = [HKUnit poundUnit]; // }
double usersWeight = [mostRecentQuantity doubleValueForUnit:weightUnit]; // callback(@[[NSNull null], @(weight)]);
// }];
callback(@[[NSNull null], @(usersWeight)]); //}
}
}];
} //- (void)getHealthKitUserStepCount:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
//{
// HKQuantityType *stepCountType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
- (void)saveHealthKitUserWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback // HKUnit *stepsUnit = [HKUnit countUnit];
{ //
double weight= [[input objectForKey:@"weight"] doubleValue]; // [self fetchSumOfSamplesTodayForType:stepCountType unit:stepsUnit completion:^(double totalSteps, NSError *error) {
// if (!totalSteps) {
HKUnit *poundUnit = [HKUnit poundUnit]; // NSLog(@"Either an error occured fetching the user's step count information or none has been stored yet. In your app, try to handle this gracefully.");
HKQuantity *weightQuantity = [HKQuantity quantityWithUnit:poundUnit doubleValue:weight]; // callback(@[RCTMakeError(@"Either an error occured fetching the user's step count information or none has been stored yet. In your app, try to handle this gracefully.", nil, nil)]);
// return;
HKQuantityType *weightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass]; // }
NSDate *now = [NSDate date]; //
// callback(@[[NSNull null], @(totalSteps)]);
HKQuantitySample *weightSample = [HKQuantitySample quantitySampleWithType:weightType quantity:weightQuantity startDate:now endDate:now]; // }];
//}
[self.healthStore saveObject:weightSample withCompletion:^(BOOL success, NSError *error) {
if (!success) {
NSLog(@"An error occured saving the weight sample %@. In your app, try to handle this gracefully. The error was: %@.", weightSample, error);
callback(@[RCTMakeError(@"An error occured saving the weight sample", nil, nil)]);
return;
// abort();
}
callback(@[[NSNull null], @(weight)]);
}];
}
- (void)getHealthKitUserStepCount:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{
HKQuantityType *stepCountType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
HKUnit *stepsUnit = [HKUnit countUnit];
[self fetchSumOfSamplesTodayForType:stepCountType unit:stepsUnit completion:^(double totalSteps, NSError *error) {
if (!totalSteps) {
NSLog(@"Either an error occured fetching the user's step count information or none has been stored yet. In your app, try to handle this gracefully.");
callback(@[RCTMakeError(@"Either an error occured fetching the user's step count information or none has been stored yet. In your app, try to handle this gracefully.", nil, nil)]);
return;
}
callback(@[[NSNull null], @(totalSteps)]);
}];
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment