Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete Steps method, Correct RN namespace implementation #21

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- (void)body_getLatestWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)body_getWeightSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)body_saveWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)body_deleteWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;

- (void)body_getLatestBodyMassIndex:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)body_saveBodyMassIndex:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
Expand Down
54 changes: 54 additions & 0 deletions RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,60 @@ - (void)body_saveWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)c
}


- (void)body_deleteWeight:(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 || endDate == nil){
callback(@[RCTMakeError(@"startDate and endDate are required in options", nil, nil)]);
return;
}

HKQuantityType *type = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass];

// the predicate used to execute the query
NSPredicate *queryPredicate = [HKSampleQuery predicateForSamplesWithStartDate:startDate endDate:endDate options:HKQueryOptionNone];

// prepare the query
HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:type predicate:queryPredicate limit:100 sortDescriptors:nil resultsHandler:^(HKSampleQuery * _Nonnull query, NSArray<__kindof HKSample *> * _Nullable results, NSError * _Nullable error) {

if (error) {

NSLog(@"Error: %@", error.description);

callback(@[RCTMakeError(@"An error occured deleting the weights sample", error.description, nil)]);

} else {

// now that we retrieved the samples, we can delete it/them
[self.healthStore deleteObject:[results firstObject] withCompletion:^(BOOL success, NSError * _Nullable error) {

if (success) {

NSLog(@"Successfully deleted entries from health kit");

callback(@[[NSNull null], @"success"]);

} else {

NSLog(@"Error: %@", error.description);

callback(@[RCTMakeError(@"An error occured deleting the weights sample", error.description, nil)]);


}
}];

}
}];

[self.healthStore executeQuery:query];

}



- (void)body_getLatestBodyMassIndex:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{
HKQuantityType *bmiType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMassIndex];
Expand Down
1 change: 1 addition & 0 deletions RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- (void)fitness_getStepCountOnDay:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)fitness_getDailyStepSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)fitness_saveSteps:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)fitness_deleteSteps:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)fitness_initializeStepEventObserver:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)fitness_getDistanceWalkingRunningOnDay:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)fitness_getDistanceCyclingOnDay:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
Expand Down
58 changes: 58 additions & 0 deletions RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@
#import "RCTAppleHealthKit+Queries.h"
#import "RCTAppleHealthKit+Utils.h"

#if __has_include(<React/RCTAssert.h>)
#import <React/RCTBridgeModule.h>
#import <React/RCTEventDispatcher.h>
#else
#import "RCTBridgeModule.h"
#import "RCTEventDispatcher.h"
#endif

@implementation RCTAppleHealthKit (Methods_Fitness)

Expand Down Expand Up @@ -107,6 +112,59 @@ - (void)fitness_saveSteps:(NSDictionary *)input callback:(RCTResponseSenderBlock
}


- (void)fitness_deleteSteps:(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 || endDate == nil){
callback(@[RCTMakeError(@"startDate and endDate are required in options", nil, nil)]);
return;
}

HKQuantityType *type = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];

// the predicate used to execute the query
NSPredicate *queryPredicate = [HKSampleQuery predicateForSamplesWithStartDate:startDate endDate:endDate options:HKQueryOptionNone];

// prepare the query
HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:type predicate:queryPredicate limit:100 sortDescriptors:nil resultsHandler:^(HKSampleQuery * _Nonnull query, NSArray<__kindof HKSample *> * _Nullable results, NSError * _Nullable error) {

if (error) {

NSLog(@"Error: %@", error.description);

callback(@[RCTMakeError(@"An error occured deleting the step count sample", error.description, nil)]);

} else {

// now that we retrieved the samples, we can delete it/them
[self.healthStore deleteObject:[results firstObject] withCompletion:^(BOOL success, NSError * _Nullable error) {

if (success) {

NSLog(@"Successfully deleted entry from health kit");

callback(@[[NSNull null], @"success"]);

} else {

NSLog(@"Error: %@", error.description);

callback(@[RCTMakeError(@"An error occured deleting the step count sample", error.description, nil)]);


}
}];

}
}];

[self.healthStore executeQuery:query];

}


- (void)fitness_initializeStepEventObserver:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{
HKSampleType *sampleType =
Expand Down
2 changes: 1 addition & 1 deletion RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ - (NSDictionary *)readPermsDict {
@"ActiveEnergyBurned" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierActiveEnergyBurned],
@"FlightsClimbed" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierFlightsClimbed],
@"NikeFuel" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierNikeFuel],
@"AppleExerciseTime" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierAppleExerciseTime],
@"AppleExerciseTime" : (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_9_3) ? [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierAppleExerciseTime] : NULL,
// Nutrition Identifiers
@"DietaryEnergy" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryEnergyConsumed],
// Vital Signs Identifiers
Expand Down
11 changes: 9 additions & 2 deletions RCTAppleHealthKit/RCTAppleHealthKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@

#import <Foundation/Foundation.h>
#import <HealthKit/HealthKit.h>
#import <React/RCTBridgeModule.h>
#import <React/RCTUtils.h>

#if __has_include(<React/RCTAssert.h>)
#import <React/RCTLog.h>
#import <React/RCTUtils.h>
#import <React/RCTBridgeModule.h>
#else
#import "RCTLog.h"
#import "RCTUtils.h"
#import "RCTBridgeModule.h"
#endif

@interface RCTAppleHealthKit : NSObject <RCTBridgeModule>

Expand Down
15 changes: 15 additions & 0 deletions RCTAppleHealthKit/RCTAppleHealthKit.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@
#import "RCTAppleHealthKit+Methods_Results.h"
#import "RCTAppleHealthKit+Methods_Sleep.h"

#if __has_include(<React/RCTAssert.h>)
#import <React/RCTBridgeModule.h>
#import <React/RCTEventDispatcher.h>
#else
#import "RCTBridgeModule"
#import "RCTEventDispatcher.h"
#endif

@implementation RCTAppleHealthKit
@synthesize bridge = _bridge;
Expand Down Expand Up @@ -64,6 +69,11 @@ @implementation RCTAppleHealthKit
[self body_saveWeight:input callback:callback];
}

RCT_EXPORT_METHOD(deleteWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
{
[self body_deleteWeight:input callback:callback];
}

RCT_EXPORT_METHOD(getLatestHeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
{
[self body_getLatestHeight:input callback:callback];
Expand Down Expand Up @@ -114,6 +124,11 @@ @implementation RCTAppleHealthKit
[self fitness_saveSteps:input callback:callback];
}

RCT_EXPORT_METHOD(deleteSteps:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
{
[self fitness_deleteSteps:input callback:callback];
}

RCT_EXPORT_METHOD(getDistanceWalkingRunning:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
{
[self fitness_getDistanceWalkingRunningOnDay:input callback:callback];
Expand Down
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ A React Native bridge module for interacting with [Apple HealthKit] data.
* [getDailyStepCountSamples](#getdailystepcountsamples)
* [initStepCountObserver](#initstepcountobserver)
* [saveSteps](#savesteps)
* [deleteSteps](#deletesteps)
* [getDistanceWalkingRunning](#getdistancewalkingrunning)
* [getDistanceCycling](#getdistancecycling)
* [getFlightsClimbed](#getflightsclimbed)
Expand Down Expand Up @@ -442,6 +443,34 @@ AppleHealthKit.saveSteps(options, (err, res) => {

___


___

#### **`deleteSteps`**
Delete steps within time period.

Function to delete steps within specified time period. Important this method can delete only steps written by your app. (Apple policy)

`deleteSteps` accepts an options object containing required *`startDate: ISO8601Timestamp`*, and *`endDate: ISO8601Timestamp`*.
```javascript
// startDate and endDate are 30 minutes apart.
let options = {
startDate: (new Date(2016,6,2,6,0,0)).toISOString(),
endDate: (new Date(2016,6,2,6,30,0)).toISOString()
};
```

```javascript
AppleHealthKit.deleteSteps(options, (err, result) => {
console.log("ERROR DELETING STEPS", err);
console.log("SUCCESSFULLY DELETED STEPS", result);
});

```

___


#### **`getDistanceWalkingRunning`**
Get the total distance walking/running on a specific day.

Expand Down
10 changes: 5 additions & 5 deletions constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ const PERMISSIONS = {
AppleExerciseTime: 'AppleExerciseTime',
DietaryEnergy: 'DietaryEnergy',
HeartRate: 'HeartRate',
BodyTemperature: 'BodyTemperature',
BloodPressureSystolic: 'BloodPressureSystolic',
BloodPressureDiastolic: 'BloodPressureDiastolic',
RespiratoryRate: 'RespiratoryRate',
BloodGlucose: 'BloodGlucose',
BodyTemperature: 'BodyTemperature',
BloodPressureSystolic: 'BloodPressureSystolic',
BloodPressureDiastolic: 'BloodPressureDiastolic',
RespiratoryRate: 'RespiratoryRate',
BloodGlucose: 'BloodGlucose',
SleepAnalysis: 'SleepAnalysis',
};

Expand Down
8 changes: 1 addition & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,5 @@
"ios"
],
"author": "Greg Wilson",
"license": "MIT",
"devDependencies": {
"react-native": ">=0.28.0"
},
"peerDependencies": {
"react-native": ">=0.28.0"
}
"license": "MIT"
}