From a00cbb0e0e84a341e31a00aff1543faec36b4dfe Mon Sep 17 00:00:00 2001 From: Amir Hoseinian Date: Mon, 16 Oct 2023 13:20:41 +0200 Subject: [PATCH] Add delete all mindful sessions method --- .../RCTAppleHealthKit+Methods_Mindfulness.h | 1 + .../RCTAppleHealthKit+Methods_Mindfulness.m | 33 +++++++++++++++++++ RCTAppleHealthKit/RCTAppleHealthKit.m | 6 ++++ README.md | 1 + SUMMARY.md | 1 + docs/README.md | 1 + docs/deleteAllMindfulSessions.md | 24 ++++++++++++++ index.d.ts | 7 ++-- 8 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 docs/deleteAllMindfulSessions.md diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Mindfulness.h b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Mindfulness.h index 64d1c634..c0b66805 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Mindfulness.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Mindfulness.h @@ -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 diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Mindfulness.m b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Mindfulness.m index da9ec78b..efa73933 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Mindfulness.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Mindfulness.m @@ -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 diff --git a/RCTAppleHealthKit/RCTAppleHealthKit.m b/RCTAppleHealthKit/RCTAppleHealthKit.m index e8ddce17..368b0764 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit.m @@ -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]; diff --git a/README.md b/README.md index e2901e2c..a9d8f3c0 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/SUMMARY.md b/SUMMARY.md index fa2153d7..61b8ef9e 100644 --- a/SUMMARY.md +++ b/SUMMARY.md @@ -96,6 +96,7 @@ * [getMindfulSession](docs/getMindfulSession.md) * [saveMindfulSession](/docs/saveMindfulSession.md) +* [deleteAllMindfulSessions](/docs/deleteAllMindfulSessions.md) ## Sleep Methods diff --git a/docs/README.md b/docs/README.md index 5c056854..fccf39d2 100644 --- a/docs/README.md +++ b/docs/README.md @@ -96,6 +96,7 @@ - [getMindfulSession](docs/getMindfulSession.md) - [saveMindfulSession](saveMindfulSession.md) +- [deleteAllMindfulSessions](deleteAllMindfulSessions.md) ### Sleep Methods diff --git a/docs/deleteAllMindfulSessions.md b/docs/deleteAllMindfulSessions.md new file mode 100644 index 00000000..dff2f324 --- /dev/null +++ b/docs/deleteAllMindfulSessions.md @@ -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 +``` diff --git a/index.d.ts b/index.d.ts index 0cf0e280..6f5de31c 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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, @@ -462,7 +466,6 @@ declare module 'react-native-health' { callback: (error: string, result: HealthValue) => void, ): void - Constants: Constants } @@ -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 = {