@kingstinct/react-native-healthkit 8.0.1 → 8.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -89,6 +89,16 @@ RCT_EXTERN_METHOD(saveCategorySample:(NSString)typeIdentifier
89
89
  reject:(RCTPromiseRejectBlock)reject
90
90
  )
91
91
 
92
+ RCT_EXTERN_METHOD(queryWorkoutSamplesWithAnchor:(NSString)energyUnitString
93
+ distanceUnitString:(NSString)distanceUnitString
94
+ from:(NSDate)from
95
+ to:(NSDate)to
96
+ limit:(NSInteger)limit
97
+ anchor:(NSString)anchor
98
+ resolve:(RCTPromiseResolveBlock)resolve
99
+ reject:(RCTPromiseRejectBlock)reject
100
+ )
101
+
92
102
  RCT_EXTERN_METHOD(queryWorkoutSamples:(NSString)energyUnitString
93
103
  distanceUnitString:(NSString)distanceUnitString
94
104
  from:(NSDate)from
@@ -717,6 +717,76 @@ class ReactNativeHealthkit: RCTEventEmitter {
717
717
  store.execute(query)
718
718
  }
719
719
 
720
+ func mapWorkout(workout: HKWorkout, distanceUnit: HKUnit, energyUnit: HKUnit) -> NSMutableDictionary {
721
+ let endDate = self._dateFormatter.string(from: workout.endDate)
722
+ let startDate = self._dateFormatter.string(from: workout.startDate)
723
+
724
+ let dict: NSMutableDictionary = [
725
+ "uuid": workout.uuid.uuidString,
726
+ "device": serializeDevice(_device: workout.device) as Any,
727
+ "duration": workout.duration,
728
+ "totalDistance": serializeQuantity(unit: distanceUnit, quantity: workout.totalDistance) as Any,
729
+ "totalEnergyBurned": serializeQuantity(unit: energyUnit, quantity: workout.totalEnergyBurned) as Any,
730
+ "totalSwimmingStrokeCount": serializeQuantity(unit: HKUnit.count(), quantity: workout.totalSwimmingStrokeCount) as Any,
731
+ "workoutActivityType": workout.workoutActivityType.rawValue,
732
+ "startDate": startDate,
733
+ "endDate": endDate,
734
+ "metadata": serializeMetadata(metadata: workout.metadata),
735
+ "sourceRevision": serializeSourceRevision(_sourceRevision: workout.sourceRevision) as Any
736
+ ]
737
+
738
+ // this is used for our laps functionality to get markers
739
+ // https://developer.apple.com/documentation/healthkit/hkworkoutevent
740
+ var eventArray: [[String: Any]] = []
741
+ if let events = workout.workoutEvents {
742
+ for event in events {
743
+ let eventStartDate = self._dateFormatter.string(from: event.dateInterval.start)
744
+ let eventEndDate = self._dateFormatter.string(from: event.dateInterval.end)
745
+ let eventDict: [String: Any] = [
746
+ "type": event.type.rawValue, // https://developer.apple.com/documentation/healthkit/hkworkouteventtype
747
+ "startDate": eventStartDate,
748
+ "endDate": eventEndDate
749
+ ]
750
+ eventArray.append(eventDict)
751
+ }
752
+ }
753
+ dict["events"] = eventArray
754
+
755
+ // also used for our laps functionality to get activities for custom workouts defined by the user
756
+ // https://developer.apple.com/documentation/healthkit/hkworkout/1615340-init
757
+ // it seems this might be depricated in the latest beta so this might need updating!
758
+ var activitiesArray: [[String: Any]] = []
759
+ if #available(iOS 16.0, *) {
760
+ let activities: [HKWorkoutActivity] = workout.workoutActivities
761
+
762
+ if !activities.isEmpty {
763
+ for activity in activities {
764
+ var activityStartDate = ""
765
+ var activityEndDate = ""
766
+ if let start = activity.startDate as Date? {
767
+ activityStartDate = self._dateFormatter.string(from: start)
768
+ }
769
+ if let end = activity.endDate as Date? {
770
+ activityEndDate = self._dateFormatter.string(from: end)
771
+ }
772
+ let activityDict: [String: Any] = [
773
+ "startDate": activityStartDate,
774
+ "endDate": activityEndDate,
775
+ "uuid": activity.uuid.uuidString,
776
+ "duration": activity.duration
777
+ ]
778
+ activitiesArray.append(activityDict)
779
+ }
780
+ }
781
+ }
782
+ dict["activities"] = activitiesArray
783
+
784
+ if #available(iOS 11, *) {
785
+ dict.setValue(serializeQuantity(unit: HKUnit.count(), quantity: workout.totalFlightsClimbed), forKey: "totalFlightsClimbed")
786
+ }
787
+ return dict
788
+ }
789
+
720
790
  @objc(queryWorkoutSamples:distanceUnitString:from:to:limit:ascending:resolve:reject:)
721
791
  func queryWorkoutSamples(energyUnitString: String, distanceUnitString: String, from: Date, to: Date, limit: Int, ascending: Bool, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
722
792
  guard let store = _store else {
@@ -741,76 +811,15 @@ class ReactNativeHealthkit: RCTEventEmitter {
741
811
  let arr: NSMutableArray = []
742
812
 
743
813
  for s in samples {
744
- if let workout = s as? HKWorkout {
745
- let endDate = self._dateFormatter.string(from: workout.endDate)
746
- let startDate = self._dateFormatter.string(from: workout.startDate)
747
-
748
- let dict: NSMutableDictionary = [
749
- "uuid": workout.uuid.uuidString,
750
- "device": serializeDevice(_device: workout.device) as Any,
751
- "duration": workout.duration,
752
- "totalDistance": serializeQuantity(unit: distanceUnit, quantity: workout.totalDistance) as Any,
753
- "totalEnergyBurned": serializeQuantity(unit: energyUnit, quantity: workout.totalEnergyBurned) as Any,
754
- "totalSwimmingStrokeCount": serializeQuantity(unit: HKUnit.count(), quantity: workout.totalSwimmingStrokeCount) as Any,
755
- "workoutActivityType": workout.workoutActivityType.rawValue,
756
- "startDate": startDate,
757
- "endDate": endDate,
758
- "metadata": serializeMetadata(metadata: workout.metadata),
759
- "sourceRevision": serializeSourceRevision(_sourceRevision: workout.sourceRevision) as Any
760
- ]
761
-
762
- // this is used for our laps functionality to get markers
763
- // https://developer.apple.com/documentation/healthkit/hkworkoutevent
764
- var eventArray: [[String: Any]] = []
765
- if let events = workout.workoutEvents {
766
- for event in events {
767
- let eventStartDate = self._dateFormatter.string(from: event.dateInterval.start)
768
- let eventEndDate = self._dateFormatter.string(from: event.dateInterval.end)
769
- let eventDict: [String: Any] = [
770
- "type": event.type.rawValue, // https://developer.apple.com/documentation/healthkit/hkworkouteventtype
771
- "startDate": eventStartDate,
772
- "endDate": eventEndDate
773
- ]
774
- eventArray.append(eventDict)
775
- }
776
- }
777
- dict["events"] = eventArray
778
-
779
- // also used for our laps functionality to get activities for custom workouts defined by the user
780
- // https://developer.apple.com/documentation/healthkit/hkworkout/1615340-init
781
- // it seems this might be depricated in the latest beta so this might need updating!
782
- var activitiesArray: [[String: Any]] = []
783
- if #available(iOS 16.0, *) {
784
- let activities: [HKWorkoutActivity] = workout.workoutActivities
785
-
786
- if !activities.isEmpty {
787
- for activity in activities {
788
- var activityStartDate = ""
789
- var activityEndDate = ""
790
- if let start = activity.startDate as Date? {
791
- activityStartDate = self._dateFormatter.string(from: start)
792
- }
793
- if let end = activity.endDate as Date? {
794
- activityEndDate = self._dateFormatter.string(from: end)
795
- }
796
- let activityDict: [String: Any] = [
797
- "startDate": activityStartDate,
798
- "endDate": activityEndDate,
799
- "uuid": activity.uuid.uuidString,
800
- "duration": activity.duration
801
- ]
802
- activitiesArray.append(activityDict)
803
- }
804
- }
805
- }
806
- dict["activities"] = activitiesArray
807
-
808
- if #available(iOS 11, *) {
809
- dict.setValue(serializeQuantity(unit: HKUnit.count(), quantity: workout.totalFlightsClimbed), forKey: "totalFlightsClimbed")
810
- }
811
-
812
- arr.add(dict)
813
- }
814
+ if let workout = s as? HKWorkout {
815
+ let dict = self.mapWorkout(
816
+ workout: workout,
817
+ distanceUnit: distanceUnit,
818
+ energyUnit: energyUnit
819
+ )
820
+
821
+ arr.add(dict)
822
+ }
814
823
  }
815
824
 
816
825
  return resolve(arr)
@@ -821,6 +830,64 @@ class ReactNativeHealthkit: RCTEventEmitter {
821
830
  store.execute(q)
822
831
  }
823
832
 
833
+ @objc(queryWorkoutSamplesWithAnchor:distanceUnitString:from:to:limit:anchor:resolve:reject:)
834
+ func queryWorkoutSamplesWithAnchor(energyUnitString: String, distanceUnitString: String, from: Date, to: Date, limit: Int, anchor: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
835
+ guard let store = _store else {
836
+ return reject(INIT_ERROR, INIT_ERROR_MESSAGE, nil)
837
+ }
838
+
839
+ let from = dateOrNilIfZero(date: from)
840
+ let to = dateOrNilIfZero(date: to)
841
+
842
+ let predicate = createPredicate(from: from, to: to)
843
+
844
+ let limit = limitOrNilIfZero(limit: limit)
845
+
846
+ let energyUnit = HKUnit.init(from: energyUnitString)
847
+ let distanceUnit = HKUnit.init(from: distanceUnitString)
848
+
849
+ let actualAnchor = deserializeHKQueryAnchor(anchor: anchor)
850
+
851
+ let q = HKAnchoredObjectQuery(type: .workoutType(), predicate: predicate, anchor: actualAnchor, limit: limit) { (
852
+ _: HKAnchoredObjectQuery,
853
+ s: [HKSample]?,
854
+ deletedSamples: [HKDeletedObject]?,
855
+ newAnchor: HKQueryAnchor?,
856
+ error: Error?
857
+ ) in
858
+ guard let err = error else {
859
+ guard let samples = s else {
860
+ return resolve([])
861
+ }
862
+
863
+ let arr: NSMutableArray = []
864
+
865
+ for s in samples {
866
+ if let workout = s as? HKWorkout {
867
+ let dict = self.mapWorkout(
868
+ workout: workout,
869
+ distanceUnit: distanceUnit,
870
+ energyUnit: energyUnit
871
+ )
872
+
873
+ arr.add(dict)
874
+ }
875
+ }
876
+
877
+ return resolve([
878
+ "samples": arr as Any,
879
+ "deletedSamples": deletedSamples?.map({ sample in
880
+ return serializeDeletedSample(sample: sample)
881
+ }) as Any,
882
+ "newAnchor": serializeAnchor(anchor: newAnchor) as Any
883
+ ])
884
+ }
885
+ reject(GENERIC_ERROR, err.localizedDescription, err)
886
+ }
887
+
888
+ store.execute(q)
889
+ }
890
+
824
891
  @objc(queryQuantitySamples:unitString:from:to:limit:ascending:resolve:reject:)
825
892
  func queryQuantitySamples(
826
893
  typeIdentifier: String,