Skip to content
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_Mindfulness.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@

- (void)mindfulness_getMindfulSession:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)mindfulness_saveMindfulSession:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)mindfulness_deleteAllMindfulSessions:(RCTResponseSenderBlock)callback;

@end
33 changes: 33 additions & 0 deletions RCTAppleHealthKit/RCTAppleHealthKit+Methods_Mindfulness.m
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,38 @@ - (void)mindfulness_saveMindfulSession:(NSDictionary *)input callback:(RCTRespon

}

- (void)mindfulness_deleteAllMindfulSessions:(RCTResponseSenderBlock)callback
{
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:HKSampleSortIdentifierEndDate ascending:false];
HKSampleType *mindfulType = [HKObjectType categoryTypeForIdentifier:HKCategoryTypeIdentifierMindfulSession];

// Get all samples from 1970 until now
NSDate *startDate = [NSDate dateWithTimeIntervalSince1970:0];
NSDate *endDate = [NSDate date];
NSPredicate *predicate = [HKQuery predicateForSamplesWithStartDate:startDate endDate:endDate options:HKQueryOptionNone];

HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:mindfulType predicate:predicate limit:0 sortDescriptors:@[sortDescriptor] resultsHandler:^(HKSampleQuery * _Nonnull query, NSArray<__kindof HKSample *> * _Nullable results, NSError * _Nullable error) {
if (error) {
NSLog(@"An error occured while deleting the mindful sessions %@. The error was: ", error);
callback(@[RCTMakeError(@"An error occured while deleting the glucose sample", error, nil)]);
return;
}
if (results.count == 0) {
callback(@[[NSNull null], @0]);
return;
}
[self.healthStore deleteObjects:results withCompletion:^(BOOL success, NSError * _Nullable error) {
if (!success) {
NSLog(@"An error occured while deleting the mindful sessions %@. The error was: ", error);
callback(@[RCTMakeError(@"An error occured while deleting the glucose sample", error, nil)]);
return;
}
callback(@[[NSNull null], @(results.count)]);
}];
}];

[self.healthStore executeQuery:query];
}


@end
6 changes: 6 additions & 0 deletions RCTAppleHealthKit/RCTAppleHealthKit.m
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,12 @@ + (BOOL)requiresMainQueueSetup
[self mindfulness_saveMindfulSession:input callback:callback];
}

RCT_EXPORT_METHOD(deleteAllMindfulSessions:(RCTResponseSenderBlock)callback)
{
[self _initializeHealthStore];
[self mindfulness_deleteAllMindfulSessions:(RCTResponseSenderBlock)callback];
}

RCT_EXPORT_METHOD(saveWorkout:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
{
[self _initializeHealthStore];
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ All the documentation is under the [docs](/docs) folder. They are split into the

- [getMindfulSession](docs/getMindfulSession.md)
- [saveMindfulSession](/docs/saveMindfulSession.md)
- [deleteAllMindfulSessions](/docs/deleteAllMindfulSessions.md)

### Sleep Methods

Expand Down
1 change: 1 addition & 0 deletions SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@

* [getMindfulSession](docs/getMindfulSession.md)
* [saveMindfulSession](/docs/saveMindfulSession.md)
* [deleteAllMindfulSessions](/docs/deleteAllMindfulSessions.md)

## Sleep Methods

Expand Down
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@

- [getMindfulSession](docs/getMindfulSession.md)
- [saveMindfulSession](saveMindfulSession.md)
- [deleteAllMindfulSessions](deleteAllMindfulSessions.md)

### Sleep Methods

Expand Down
24 changes: 24 additions & 0 deletions docs/deleteAllMindfulSessions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# deleteAllMindfulSessions

Delete all mindful sessions from HealthKit.

`deleteAllMindfulSessions` accepts a callback:

Example usage:

```javascript
AppleHealthKit.deleteAllMindfulSessions((err: string, result: number) => {
if (err) {
console.log(err)
return
}
// sample successfully deleted
console.log(result)
})
```

Example output (if 20 objects are deleted):

```json
20
```
7 changes: 5 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,10 @@ declare module 'react-native-health' {
callback: (error: string, result: HealthValue) => void,
): void

deleteAllMindfulSessions(
callback: (error: string, result: number) => void,
): void

getWorkoutRouteSamples(
options: { id: string },
callback: (err: string, results: WorkoutRouteQueryResults) => void,
Expand Down Expand Up @@ -462,7 +466,6 @@ declare module 'react-native-health' {
callback: (error: string, result: HealthValue) => void,
): void


Constants: Constants
}

Expand Down Expand Up @@ -546,7 +549,7 @@ declare module 'react-native-health' {
PausedOrResumeRequest = 'pause or resume request',
Lap = 'lap',
Segment = 'segment',
Marker = 'marker'
Marker = 'marker',
}

export type HKWorkoutEventType = {
Expand Down