@kingstinct/react-native-healthkit 6.1.1 → 7.0.1

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.
Files changed (268) hide show
  1. package/README.md +7 -5
  2. package/app.plugin.js +23 -4
  3. package/ios/Constants.swift +28 -0
  4. package/ios/Helpers.swift +152 -0
  5. package/ios/ReactNativeHealthkit.m +24 -0
  6. package/ios/ReactNativeHealthkit.swift +534 -728
  7. package/ios/Serializers.swift +206 -0
  8. package/lib/commonjs/hooks/useHealthkitAuthorization.js +0 -6
  9. package/lib/commonjs/hooks/useHealthkitAuthorization.js.map +1 -1
  10. package/lib/commonjs/hooks/useHealthkitAuthorization.test.js +2 -9
  11. package/lib/commonjs/hooks/useHealthkitAuthorization.test.js.map +1 -1
  12. package/lib/commonjs/hooks/useIsHealthDataAvailable.js +0 -6
  13. package/lib/commonjs/hooks/useIsHealthDataAvailable.js.map +1 -1
  14. package/lib/commonjs/hooks/useIsHealthDataAvailable.test.js +1 -6
  15. package/lib/commonjs/hooks/useIsHealthDataAvailable.test.js.map +1 -1
  16. package/lib/commonjs/hooks/useMostRecentCategorySample.js +1 -7
  17. package/lib/commonjs/hooks/useMostRecentCategorySample.js.map +1 -1
  18. package/lib/commonjs/hooks/useMostRecentQuantitySample.js +0 -10
  19. package/lib/commonjs/hooks/useMostRecentQuantitySample.js.map +1 -1
  20. package/lib/commonjs/hooks/useMostRecentWorkout.js +0 -10
  21. package/lib/commonjs/hooks/useMostRecentWorkout.js.map +1 -1
  22. package/lib/commonjs/hooks/useSources.js +0 -5
  23. package/lib/commonjs/hooks/useSources.js.map +1 -1
  24. package/lib/commonjs/hooks/useStatisticsForQuantity.js +1 -7
  25. package/lib/commonjs/hooks/useStatisticsForQuantity.js.map +1 -1
  26. package/lib/commonjs/hooks/useSubscribeToChanges.js +0 -8
  27. package/lib/commonjs/hooks/useSubscribeToChanges.js.map +1 -1
  28. package/lib/commonjs/index.ios.js +6 -33
  29. package/lib/commonjs/index.ios.js.map +1 -1
  30. package/lib/commonjs/index.js +7 -11
  31. package/lib/commonjs/index.js.map +1 -1
  32. package/lib/commonjs/jest.setup.js +3 -1
  33. package/lib/commonjs/jest.setup.js.map +1 -1
  34. package/lib/commonjs/native-types.js +166 -250
  35. package/lib/commonjs/native-types.js.map +1 -1
  36. package/lib/commonjs/test-utils.js +1 -3
  37. package/lib/commonjs/test-utils.js.map +1 -1
  38. package/lib/commonjs/types.js +0 -2
  39. package/lib/commonjs/types.js.map +1 -1
  40. package/lib/commonjs/utils/deleteQuantitySample.js +0 -4
  41. package/lib/commonjs/utils/deleteQuantitySample.js.map +1 -1
  42. package/lib/commonjs/utils/deleteSamples.js +0 -4
  43. package/lib/commonjs/utils/deleteSamples.js.map +1 -1
  44. package/lib/commonjs/utils/deserializeCategorySample.js +2 -3
  45. package/lib/commonjs/utils/deserializeCategorySample.js.map +1 -1
  46. package/lib/commonjs/utils/deserializeCategorySample.test.js +3 -5
  47. package/lib/commonjs/utils/deserializeCategorySample.test.js.map +1 -1
  48. package/lib/commonjs/utils/deserializeCorrelation.js +2 -7
  49. package/lib/commonjs/utils/deserializeCorrelation.js.map +1 -1
  50. package/lib/commonjs/utils/deserializeHeartbeatSeriesSample.js +2 -3
  51. package/lib/commonjs/utils/deserializeHeartbeatSeriesSample.js.map +1 -1
  52. package/lib/commonjs/utils/deserializeSample.js +2 -3
  53. package/lib/commonjs/utils/deserializeSample.js.map +1 -1
  54. package/lib/commonjs/utils/deserializeWorkout.js +2 -3
  55. package/lib/commonjs/utils/deserializeWorkout.js.map +1 -1
  56. package/lib/commonjs/utils/ensureMetadata.js +0 -2
  57. package/lib/commonjs/utils/ensureMetadata.js.map +1 -1
  58. package/lib/commonjs/utils/ensureUnit.js +0 -5
  59. package/lib/commonjs/utils/ensureUnit.js.map +1 -1
  60. package/lib/commonjs/utils/getDateOfBirth.js +0 -4
  61. package/lib/commonjs/utils/getDateOfBirth.js.map +1 -1
  62. package/lib/commonjs/utils/getMostRecentCategorySample.js +1 -5
  63. package/lib/commonjs/utils/getMostRecentCategorySample.js.map +1 -1
  64. package/lib/commonjs/utils/getMostRecentQuantitySample.js +1 -5
  65. package/lib/commonjs/utils/getMostRecentQuantitySample.js.map +1 -1
  66. package/lib/commonjs/utils/getMostRecentWorkout.js +0 -4
  67. package/lib/commonjs/utils/getMostRecentWorkout.js.map +1 -1
  68. package/lib/commonjs/utils/getPreferredUnit.js +0 -4
  69. package/lib/commonjs/utils/getPreferredUnit.js.map +1 -1
  70. package/lib/commonjs/utils/getPreferredUnits.js +0 -4
  71. package/lib/commonjs/utils/getPreferredUnits.js.map +1 -1
  72. package/lib/commonjs/utils/getPreferredUnitsTyped.js +0 -11
  73. package/lib/commonjs/utils/getPreferredUnitsTyped.js.map +1 -1
  74. package/lib/commonjs/utils/getRequestStatusForAuthorization.js +4 -6
  75. package/lib/commonjs/utils/getRequestStatusForAuthorization.js.map +1 -1
  76. package/lib/commonjs/utils/prepareOptions.js +0 -4
  77. package/lib/commonjs/utils/prepareOptions.js.map +1 -1
  78. package/lib/commonjs/utils/queryCategorySamples.js +3 -14
  79. package/lib/commonjs/utils/queryCategorySamples.js.map +1 -1
  80. package/lib/commonjs/utils/queryCategorySamplesWithAnchor.js +22 -0
  81. package/lib/commonjs/utils/queryCategorySamplesWithAnchor.js.map +1 -0
  82. package/lib/commonjs/utils/queryCorrelationSamples.js +1 -7
  83. package/lib/commonjs/utils/queryCorrelationSamples.js.map +1 -1
  84. package/lib/commonjs/utils/queryHeartbeatSeriesSamples.js +3 -13
  85. package/lib/commonjs/utils/queryHeartbeatSeriesSamples.js.map +1 -1
  86. package/lib/commonjs/utils/queryHeartbeatSeriesSamplesWithAnchor.js +22 -0
  87. package/lib/commonjs/utils/queryHeartbeatSeriesSamplesWithAnchor.js.map +1 -0
  88. package/lib/commonjs/utils/queryQuantitySamples.js +3 -15
  89. package/lib/commonjs/utils/queryQuantitySamples.js.map +1 -1
  90. package/lib/commonjs/utils/queryQuantitySamplesWithAnchor.js +24 -0
  91. package/lib/commonjs/utils/queryQuantitySamplesWithAnchor.js.map +1 -0
  92. package/lib/commonjs/utils/querySources.js +0 -4
  93. package/lib/commonjs/utils/querySources.js.map +1 -1
  94. package/lib/commonjs/utils/queryStatisticsForQuantity.js +3 -7
  95. package/lib/commonjs/utils/queryStatisticsForQuantity.js.map +1 -1
  96. package/lib/commonjs/utils/queryWorkouts.js +1 -8
  97. package/lib/commonjs/utils/queryWorkouts.js.map +1 -1
  98. package/lib/commonjs/utils/requestAuthorization.js +4 -6
  99. package/lib/commonjs/utils/requestAuthorization.js.map +1 -1
  100. package/lib/commonjs/utils/saveCategorySample.js +0 -4
  101. package/lib/commonjs/utils/saveCategorySample.js.map +1 -1
  102. package/lib/commonjs/utils/saveCorrelationSample.js +3 -7
  103. package/lib/commonjs/utils/saveCorrelationSample.js.map +1 -1
  104. package/lib/commonjs/utils/saveQuantitySample.js +0 -4
  105. package/lib/commonjs/utils/saveQuantitySample.js.map +1 -1
  106. package/lib/commonjs/utils/saveWorkoutSample.js +3 -8
  107. package/lib/commonjs/utils/saveWorkoutSample.js.map +1 -1
  108. package/lib/commonjs/utils/serializeDate.js +0 -2
  109. package/lib/commonjs/utils/serializeDate.js.map +1 -1
  110. package/lib/commonjs/utils/serializeDate.test.js +0 -2
  111. package/lib/commonjs/utils/serializeDate.test.js.map +1 -1
  112. package/lib/commonjs/utils/subscribeToChanges.js +0 -7
  113. package/lib/commonjs/utils/subscribeToChanges.js.map +1 -1
  114. package/lib/module/hooks/useHealthkitAuthorization.js +0 -2
  115. package/lib/module/hooks/useHealthkitAuthorization.js.map +1 -1
  116. package/lib/module/hooks/useHealthkitAuthorization.test.js +1 -1
  117. package/lib/module/hooks/useHealthkitAuthorization.test.js.map +1 -1
  118. package/lib/module/hooks/useIsHealthDataAvailable.js +0 -3
  119. package/lib/module/hooks/useIsHealthDataAvailable.js.map +1 -1
  120. package/lib/module/hooks/useIsHealthDataAvailable.test.js +1 -1
  121. package/lib/module/hooks/useIsHealthDataAvailable.test.js.map +1 -1
  122. package/lib/module/hooks/useMostRecentCategorySample.js +1 -3
  123. package/lib/module/hooks/useMostRecentCategorySample.js.map +1 -1
  124. package/lib/module/hooks/useMostRecentQuantitySample.js +0 -5
  125. package/lib/module/hooks/useMostRecentQuantitySample.js.map +1 -1
  126. package/lib/module/hooks/useMostRecentWorkout.js +0 -5
  127. package/lib/module/hooks/useMostRecentWorkout.js.map +1 -1
  128. package/lib/module/hooks/useSources.js +0 -2
  129. package/lib/module/hooks/useSources.js.map +1 -1
  130. package/lib/module/hooks/useStatisticsForQuantity.js +1 -3
  131. package/lib/module/hooks/useStatisticsForQuantity.js.map +1 -1
  132. package/lib/module/hooks/useSubscribeToChanges.js +0 -5
  133. package/lib/module/hooks/useSubscribeToChanges.js.map +1 -1
  134. package/lib/module/index.ios.js +6 -0
  135. package/lib/module/index.ios.js.map +1 -1
  136. package/lib/module/index.js +8 -8
  137. package/lib/module/index.js.map +1 -1
  138. package/lib/module/jest.setup.js +3 -0
  139. package/lib/module/jest.setup.js.map +1 -1
  140. package/lib/module/native-types.js +134 -201
  141. package/lib/module/native-types.js.map +1 -1
  142. package/lib/module/test-utils.js +0 -2
  143. package/lib/module/test-utils.js.map +1 -1
  144. package/lib/module/types.js.map +1 -1
  145. package/lib/module/utils/deleteQuantitySample.js +0 -2
  146. package/lib/module/utils/deleteQuantitySample.js.map +1 -1
  147. package/lib/module/utils/deleteSamples.js +0 -2
  148. package/lib/module/utils/deleteSamples.js.map +1 -1
  149. package/lib/module/utils/deserializeCategorySample.js +2 -2
  150. package/lib/module/utils/deserializeCategorySample.js.map +1 -1
  151. package/lib/module/utils/deserializeCategorySample.test.js +3 -2
  152. package/lib/module/utils/deserializeCategorySample.test.js.map +1 -1
  153. package/lib/module/utils/deserializeCorrelation.js +2 -4
  154. package/lib/module/utils/deserializeCorrelation.js.map +1 -1
  155. package/lib/module/utils/deserializeHeartbeatSeriesSample.js +2 -2
  156. package/lib/module/utils/deserializeHeartbeatSeriesSample.js.map +1 -1
  157. package/lib/module/utils/deserializeSample.js +2 -2
  158. package/lib/module/utils/deserializeSample.js.map +1 -1
  159. package/lib/module/utils/deserializeWorkout.js +2 -2
  160. package/lib/module/utils/deserializeWorkout.js.map +1 -1
  161. package/lib/module/utils/ensureMetadata.js +0 -1
  162. package/lib/module/utils/ensureMetadata.js.map +1 -1
  163. package/lib/module/utils/ensureUnit.js +0 -3
  164. package/lib/module/utils/ensureUnit.js.map +1 -1
  165. package/lib/module/utils/getDateOfBirth.js +0 -2
  166. package/lib/module/utils/getDateOfBirth.js.map +1 -1
  167. package/lib/module/utils/getMostRecentCategorySample.js +1 -3
  168. package/lib/module/utils/getMostRecentCategorySample.js.map +1 -1
  169. package/lib/module/utils/getMostRecentQuantitySample.js +1 -3
  170. package/lib/module/utils/getMostRecentQuantitySample.js.map +1 -1
  171. package/lib/module/utils/getMostRecentWorkout.js +0 -2
  172. package/lib/module/utils/getMostRecentWorkout.js.map +1 -1
  173. package/lib/module/utils/getPreferredUnit.js +0 -2
  174. package/lib/module/utils/getPreferredUnit.js.map +1 -1
  175. package/lib/module/utils/getPreferredUnits.js +0 -2
  176. package/lib/module/utils/getPreferredUnits.js.map +1 -1
  177. package/lib/module/utils/getPreferredUnitsTyped.js +0 -8
  178. package/lib/module/utils/getPreferredUnitsTyped.js.map +1 -1
  179. package/lib/module/utils/getRequestStatusForAuthorization.js +4 -4
  180. package/lib/module/utils/getRequestStatusForAuthorization.js.map +1 -1
  181. package/lib/module/utils/prepareOptions.js +0 -2
  182. package/lib/module/utils/prepareOptions.js.map +1 -1
  183. package/lib/module/utils/queryCategorySamples.js +3 -10
  184. package/lib/module/utils/queryCategorySamples.js.map +1 -1
  185. package/lib/module/utils/queryCategorySamplesWithAnchor.js +14 -0
  186. package/lib/module/utils/queryCategorySamplesWithAnchor.js.map +1 -0
  187. package/lib/module/utils/queryCorrelationSamples.js +1 -3
  188. package/lib/module/utils/queryCorrelationSamples.js.map +1 -1
  189. package/lib/module/utils/queryHeartbeatSeriesSamples.js +3 -9
  190. package/lib/module/utils/queryHeartbeatSeriesSamples.js.map +1 -1
  191. package/lib/module/utils/queryHeartbeatSeriesSamplesWithAnchor.js +14 -0
  192. package/lib/module/utils/queryHeartbeatSeriesSamplesWithAnchor.js.map +1 -0
  193. package/lib/module/utils/queryQuantitySamples.js +3 -10
  194. package/lib/module/utils/queryQuantitySamples.js.map +1 -1
  195. package/lib/module/utils/queryQuantitySamplesWithAnchor.js +16 -0
  196. package/lib/module/utils/queryQuantitySamplesWithAnchor.js.map +1 -0
  197. package/lib/module/utils/querySources.js +0 -2
  198. package/lib/module/utils/querySources.js.map +1 -1
  199. package/lib/module/utils/queryStatisticsForQuantity.js +3 -4
  200. package/lib/module/utils/queryStatisticsForQuantity.js.map +1 -1
  201. package/lib/module/utils/queryWorkouts.js +1 -3
  202. package/lib/module/utils/queryWorkouts.js.map +1 -1
  203. package/lib/module/utils/requestAuthorization.js +4 -4
  204. package/lib/module/utils/requestAuthorization.js.map +1 -1
  205. package/lib/module/utils/saveCategorySample.js +0 -2
  206. package/lib/module/utils/saveCategorySample.js.map +1 -1
  207. package/lib/module/utils/saveCorrelationSample.js +3 -4
  208. package/lib/module/utils/saveCorrelationSample.js.map +1 -1
  209. package/lib/module/utils/saveQuantitySample.js +0 -2
  210. package/lib/module/utils/saveQuantitySample.js.map +1 -1
  211. package/lib/module/utils/saveWorkoutSample.js +3 -5
  212. package/lib/module/utils/saveWorkoutSample.js.map +1 -1
  213. package/lib/module/utils/serializeDate.js +0 -1
  214. package/lib/module/utils/serializeDate.js.map +1 -1
  215. package/lib/module/utils/serializeDate.test.js.map +1 -1
  216. package/lib/module/utils/subscribeToChanges.js +0 -3
  217. package/lib/module/utils/subscribeToChanges.js.map +1 -1
  218. package/lib/typescript/src/hooks/useMostRecentCategorySample.d.ts +2 -3
  219. package/lib/typescript/src/hooks/useMostRecentQuantitySample.d.ts +2 -3
  220. package/lib/typescript/src/index.ios.d.ts +4 -1
  221. package/lib/typescript/src/native-types.d.ts +66 -63
  222. package/lib/typescript/src/types.d.ts +3 -3
  223. package/lib/typescript/src/utils/deleteQuantitySample.d.ts +1 -1
  224. package/lib/typescript/src/utils/deleteSamples.d.ts +1 -1
  225. package/lib/typescript/src/utils/getMostRecentCategorySample.d.ts +1 -1
  226. package/lib/typescript/src/utils/getMostRecentQuantitySample.d.ts +1 -1
  227. package/lib/typescript/src/utils/getMostRecentWorkout.d.ts +1 -1
  228. package/lib/typescript/src/utils/getPreferredUnit.d.ts +1 -1
  229. package/lib/typescript/src/utils/getPreferredUnits.d.ts +1 -1
  230. package/lib/typescript/src/utils/queryCategorySamples.d.ts +3 -8
  231. package/lib/typescript/src/utils/queryCategorySamplesWithAnchor.d.ts +10 -0
  232. package/lib/typescript/src/utils/queryCorrelationSamples.d.ts +1 -1
  233. package/lib/typescript/src/utils/queryHeartbeatSeriesSamples.d.ts +2 -2
  234. package/lib/typescript/src/utils/queryHeartbeatSeriesSamplesWithAnchor.d.ts +10 -0
  235. package/lib/typescript/src/utils/queryQuantitySamples.d.ts +4 -9
  236. package/lib/typescript/src/utils/queryQuantitySamplesWithAnchor.d.ts +12 -0
  237. package/lib/typescript/src/utils/querySources.d.ts +1 -1
  238. package/package.json +18 -17
  239. package/src/hooks/useHealthkitAuthorization.test.ts +1 -1
  240. package/src/hooks/useIsHealthDataAvailable.test.ts +1 -1
  241. package/src/hooks/useMostRecentCategorySample.ts +3 -4
  242. package/src/hooks/useMostRecentQuantitySample.ts +2 -3
  243. package/src/hooks/useStatisticsForQuantity.ts +1 -1
  244. package/src/index.ios.tsx +6 -0
  245. package/src/index.tsx +10 -5
  246. package/src/jest.setup.ts +3 -0
  247. package/src/native-types.ts +28 -7
  248. package/src/utils/deserializeCategorySample.test.ts +1 -1
  249. package/src/utils/getMostRecentCategorySample.ts +1 -1
  250. package/src/utils/getMostRecentQuantitySample.ts +1 -1
  251. package/src/utils/queryCategorySamples.ts +6 -18
  252. package/src/utils/queryCategorySamplesWithAnchor.ts +39 -0
  253. package/src/utils/queryCorrelationSamples.ts +2 -2
  254. package/src/utils/queryHeartbeatSeriesSamples.ts +3 -8
  255. package/src/utils/queryHeartbeatSeriesSamplesWithAnchor.ts +33 -0
  256. package/src/utils/queryQuantitySamples.ts +8 -18
  257. package/src/utils/queryQuantitySamplesWithAnchor.ts +46 -0
  258. package/src/utils/queryStatisticsForQuantity.ts +1 -1
  259. package/src/utils/queryWorkouts.ts +1 -1
  260. package/src/utils/saveCorrelationSample.ts +1 -1
  261. package/src/utils/saveWorkoutSample.ts +1 -1
  262. package/android/.project +0 -34
  263. package/android/.settings/org.eclipse.buildship.core.prefs +0 -13
  264. package/android/build.gradle +0 -130
  265. package/android/gradle.properties +0 -4
  266. package/android/src/main/AndroidManifest.xml +0 -4
  267. package/android/src/main/java/com/kingstinctreactnativehealthkit/ReactNativeHealthkitModule.kt +0 -24
  268. package/android/src/main/java/com/kingstinctreactnativehealthkit/ReactNativeHealthkitPackage.kt +0 -20
package/README.md CHANGED
@@ -37,11 +37,13 @@ this will give you defaults that make the app build without any further configur
37
37
  ```json
38
38
  {
39
39
  "expo": {
40
- "plugins": ["@kingstinct/react-native-healthkit", {
41
- "NSHealthShareUsageDescription": "Your own custom usage description",
42
- "NSHealthUpdateUsageDescription": false, // if you have no plans to update data, you can skip adding it to your info.plist
43
- "background": false // if you have no plans to use it in background mode, skip adding it to the entitlements
44
- }]
40
+ "plugins": [
41
+ ["@kingstinct/react-native-healthkit", {
42
+ "NSHealthShareUsageDescription": "Your own custom usage description",
43
+ "NSHealthUpdateUsageDescription": false, // if you have no plans to update data, you can skip adding it to your info.plist
44
+ "background": false // if you have no plans to use it in background mode, skip adding it to the entitlements
45
+ }]
46
+ ]
45
47
  }
46
48
  }
47
49
  ```
package/app.plugin.js CHANGED
@@ -40,16 +40,35 @@ const {
40
40
  */
41
41
  const withEntitlementsPlugin = (config, { background }) => withEntitlementsPlist(config, (config) => {
42
42
  config.modResults['com.apple.developer.healthkit'] = true
43
- config.modResults['com.apple.developer.healthkit.background-delivery'] = background !== false
43
+
44
+ // background is enabled by default, but possible to opt-out from
45
+ // (haven't seen any drawbacks from having it enabled)
46
+ if (background !== false) {
47
+ config.modResults['com.apple.developer.healthkit.background-delivery'] = true
48
+ }
49
+
44
50
  return config
45
51
  })
46
52
 
47
53
  /**
48
54
  * @type {ConfigPlugin<InfoPlistConfig>}
49
55
  */
50
- const withInfoPlistPlugin = (config, { NSHealthShareUsageDescription, NSHealthUpdateUsageDescription }) => withInfoPlist(config, (config) => {
51
- config.modResults.NSHealthShareUsageDescription = NSHealthShareUsageDescription || (NSHealthShareUsageDescription === false ? undefined : `${config.name} wants to read your health data`)
52
- config.modResults.NSHealthUpdateUsageDescription = NSHealthUpdateUsageDescription || (NSHealthUpdateUsageDescription === false ? undefined : `${config.name} wants to update your health data`)
56
+ const withInfoPlistPlugin = (config,
57
+ /**
58
+ * @type {{NSHealthShareUsageDescription: string | boolean, NSHealthUpdateUsageDescription: string | boolean}}
59
+ * */
60
+ {
61
+ NSHealthShareUsageDescription,
62
+ NSHealthUpdateUsageDescription,
63
+ }) => withInfoPlist(config, (config) => {
64
+ if (NSHealthShareUsageDescription !== false) {
65
+ config.modResults.NSHealthShareUsageDescription = NSHealthShareUsageDescription ?? `${config.name} wants to read your health data`
66
+ }
67
+
68
+ if (NSHealthUpdateUsageDescription !== false) {
69
+ config.modResults.NSHealthUpdateUsageDescription = NSHealthUpdateUsageDescription ?? `${config.name} wants to update your health data`
70
+ }
71
+
53
72
  return config
54
73
  })
55
74
 
@@ -0,0 +1,28 @@
1
+ //
2
+ // Constants.swift
3
+ // kingstinct-react-native-healthkit
4
+ //
5
+ // Created by Robert Herber on 2023-05-31.
6
+ //
7
+
8
+ import Foundation
9
+ import HealthKit
10
+
11
+ let INIT_ERROR = "HEALTHKIT_INIT_ERROR"
12
+ let INIT_ERROR_MESSAGE = "HealthKit not initialized"
13
+ let TYPE_IDENTIFIER_ERROR = "HEALTHKIT_TYPE_IDENTIFIER_NOT_RECOGNIZED_ERROR"
14
+ let GENERIC_ERROR = "HEALTHKIT_ERROR"
15
+
16
+ let HKCharacteristicTypeIdentifier_PREFIX = "HKCharacteristicTypeIdentifier"
17
+ let HKQuantityTypeIdentifier_PREFIX = "HKQuantityTypeIdentifier"
18
+ let HKCategoryTypeIdentifier_PREFIX = "HKCategoryTypeIdentifier"
19
+ let HKCorrelationTypeIdentifier_PREFIX = "HKCorrelationTypeIdentifier"
20
+ let HKActivitySummaryTypeIdentifier = "HKActivitySummaryTypeIdentifier"
21
+ let HKAudiogramTypeIdentifier = "HKAudiogramTypeIdentifier"
22
+ let HKWorkoutTypeIdentifier = "HKWorkoutTypeIdentifier"
23
+ let HKWorkoutRouteTypeIdentifier = "HKWorkoutRouteTypeIdentifier"
24
+ let HKDataTypeIdentifierHeartbeatSeries = "HKDataTypeIdentifierHeartbeatSeries"
25
+
26
+ let SpeedUnit = HKUnit(from: "m/s") // HKUnit.meter().unitDivided(by: HKUnit.second())
27
+ // Support for MET data: HKAverageMETs 8.24046 kcal/hr·kg
28
+ let METUnit = HKUnit(from: "kcal/hr·kg")
@@ -0,0 +1,152 @@
1
+ //
2
+ // Helpers.swift
3
+ // kingstinct-react-native-healthkit
4
+ //
5
+ // Created by Robert Herber on 2023-05-31.
6
+ //
7
+
8
+ import Foundation
9
+ import HealthKit
10
+
11
+ func dateOrNilIfZero(date: Date) -> Date? {
12
+ return date.timeIntervalSince1970 > 0 ? date : nil
13
+ }
14
+
15
+ func limitOrNilIfZero(limit: Int) -> Int {
16
+ return limit == 0 ? HKObjectQueryNoLimit : limit
17
+ }
18
+
19
+ func createPredicate(from: Date?, to: Date?) -> NSPredicate? {
20
+ if from != nil || to != nil {
21
+ return HKQuery.predicateForSamples(withStart: from, end: to, options: [.strictEndDate, .strictStartDate])
22
+ } else {
23
+ return nil
24
+ }
25
+ }
26
+
27
+ func getSortDescriptors(ascending: Bool) -> [NSSortDescriptor] {
28
+ return [NSSortDescriptor(key: HKSampleSortIdentifierStartDate, ascending: ascending)]
29
+ }
30
+
31
+ func base64StringToHKQueryAnchor(base64String: String) -> HKQueryAnchor? {
32
+ // Step 1: Decode the base64 string to a Data object
33
+ guard let data = Data(base64Encoded: base64String) else {
34
+ print("Error: Invalid base64 string")
35
+ return nil
36
+ }
37
+
38
+ // Step 2: Use NSKeyedUnarchiver to unarchive the data and create an HKQueryAnchor object
39
+ do {
40
+ let unarchiver = try NSKeyedUnarchiver(forReadingFrom: data)
41
+ unarchiver.requiresSecureCoding = true
42
+ let anchor = try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data)
43
+
44
+ return anchor as? HKQueryAnchor
45
+ } catch {
46
+ print("Error: Unable to unarchive HKQueryAnchor object: \(error)")
47
+ return nil
48
+ }
49
+ }
50
+
51
+ func sampleTypeFromString(typeIdentifier: String) -> HKSampleType? {
52
+ if typeIdentifier.starts(with: HKQuantityTypeIdentifier_PREFIX) {
53
+ let identifier = HKQuantityTypeIdentifier.init(rawValue: typeIdentifier)
54
+ return HKSampleType.quantityType(forIdentifier: identifier) as HKSampleType?
55
+ }
56
+
57
+ if typeIdentifier.starts(with: HKCategoryTypeIdentifier_PREFIX) {
58
+ let identifier = HKCategoryTypeIdentifier.init(rawValue: typeIdentifier)
59
+ return HKSampleType.categoryType(forIdentifier: identifier) as HKSampleType?
60
+ }
61
+
62
+ if typeIdentifier.starts(with: HKCorrelationTypeIdentifier_PREFIX) {
63
+ let identifier = HKCorrelationTypeIdentifier.init(rawValue: typeIdentifier)
64
+ return HKSampleType.correlationType(forIdentifier: identifier) as HKSampleType?
65
+ }
66
+
67
+ if #available(iOS 13, *) {
68
+ if typeIdentifier == HKAudiogramTypeIdentifier {
69
+ return HKSampleType.audiogramSampleType()
70
+ }
71
+ }
72
+
73
+ if typeIdentifier == HKWorkoutTypeIdentifier {
74
+ return HKSampleType.workoutType()
75
+ }
76
+
77
+ return nil
78
+ }
79
+
80
+ func objectTypesFromDictionary(typeIdentifiers: NSDictionary) -> Set<HKObjectType> {
81
+ var share = Set<HKObjectType>()
82
+ for item in typeIdentifiers {
83
+ if item.value as! Bool {
84
+ let objectType = objectTypeFromString(typeIdentifier: item.key as! String)
85
+ if objectType != nil {
86
+ share.insert(objectType!)
87
+ }
88
+ }
89
+ }
90
+ return share
91
+ }
92
+
93
+ func sampleTypesFromDictionary(typeIdentifiers: NSDictionary) -> Set<HKSampleType> {
94
+ var share = Set<HKSampleType>()
95
+ for item in typeIdentifiers {
96
+ if item.value as! Bool {
97
+ let sampleType = sampleTypeFromString(typeIdentifier: item.key as! String)
98
+ if sampleType != nil {
99
+ share.insert(sampleType!)
100
+ }
101
+ }
102
+ }
103
+ return share
104
+ }
105
+
106
+ func objectTypeFromString(typeIdentifier: String) -> HKObjectType? {
107
+ if typeIdentifier.starts(with: HKCharacteristicTypeIdentifier_PREFIX) {
108
+ let identifier = HKCharacteristicTypeIdentifier.init(rawValue: typeIdentifier)
109
+ return HKObjectType.characteristicType(forIdentifier: identifier) as HKObjectType?
110
+ }
111
+
112
+ if typeIdentifier.starts(with: HKQuantityTypeIdentifier_PREFIX) {
113
+ let identifier = HKQuantityTypeIdentifier.init(rawValue: typeIdentifier)
114
+ return HKObjectType.quantityType(forIdentifier: identifier) as HKObjectType?
115
+ }
116
+
117
+ if typeIdentifier.starts(with: HKCategoryTypeIdentifier_PREFIX) {
118
+ let identifier = HKCategoryTypeIdentifier.init(rawValue: typeIdentifier)
119
+ return HKObjectType.categoryType(forIdentifier: identifier) as HKObjectType?
120
+ }
121
+
122
+ if typeIdentifier.starts(with: HKCorrelationTypeIdentifier_PREFIX) {
123
+ let identifier = HKCorrelationTypeIdentifier.init(rawValue: typeIdentifier)
124
+ return HKObjectType.correlationType(forIdentifier: identifier) as HKObjectType?
125
+ }
126
+
127
+ if typeIdentifier == HKActivitySummaryTypeIdentifier {
128
+ return HKObjectType.activitySummaryType()
129
+ }
130
+
131
+ if #available(iOS 13, *) {
132
+ if typeIdentifier == HKAudiogramTypeIdentifier {
133
+ return HKObjectType.audiogramSampleType()
134
+ }
135
+
136
+ if typeIdentifier == HKDataTypeIdentifierHeartbeatSeries {
137
+ return HKObjectType.seriesType(forIdentifier: typeIdentifier)
138
+ }
139
+ }
140
+
141
+ if typeIdentifier == HKWorkoutTypeIdentifier {
142
+ return HKObjectType.workoutType()
143
+ }
144
+
145
+ if #available(iOS 11.0, *) {
146
+ if typeIdentifier == HKWorkoutRouteTypeIdentifier {
147
+ return HKObjectType.seriesType(forIdentifier: typeIdentifier)
148
+ }
149
+ }
150
+
151
+ return nil
152
+ }
@@ -103,6 +103,13 @@ RCT_EXTERN_METHOD(queryHeartbeatSeriesSamples:(NSDate)from
103
103
  to:(NSDate)to
104
104
  limit:(NSInteger)limit
105
105
  ascending:(BOOL)ascending
106
+ resolve:(RCTPromiseResolveBlock)resolve
107
+ reject:(RCTPromiseRejectBlock)reject
108
+ )
109
+
110
+ RCT_EXTERN_METHOD(queryHeartbeatSeriesSamplesWithAnchor:(NSDate)from
111
+ to:(NSDate)to
112
+ limit:(NSInteger)limit
106
113
  anchor:(NSString)anchor
107
114
  resolve:(RCTPromiseResolveBlock)resolve
108
115
  reject:(RCTPromiseRejectBlock)reject
@@ -113,6 +120,14 @@ RCT_EXTERN_METHOD(queryCategorySamples:(NSString)typeIdentifier
113
120
  to:(NSDate)to
114
121
  limit:(NSInteger)limit
115
122
  ascending:(BOOL)ascending
123
+ resolve:(RCTPromiseResolveBlock)resolve
124
+ reject:(RCTPromiseRejectBlock)reject
125
+ )
126
+
127
+ RCT_EXTERN_METHOD(queryCategorySamplesWithAnchor:(NSString)typeIdentifier
128
+ from:(NSDate)from
129
+ to:(NSDate)to
130
+ limit:(NSInteger)limit
116
131
  anchor:(NSString)anchor
117
132
  resolve:(RCTPromiseResolveBlock)resolve
118
133
  reject:(RCTPromiseRejectBlock)reject
@@ -149,6 +164,15 @@ RCT_EXTERN_METHOD(queryQuantitySamples:(NSString)typeIdentifier
149
164
  to:(NSDate)to
150
165
  limit:(NSInteger)limit
151
166
  ascending:(BOOL)ascending
167
+ resolve:(RCTPromiseResolveBlock)resolve
168
+ reject:(RCTPromiseRejectBlock)reject
169
+ )
170
+
171
+ RCT_EXTERN_METHOD(queryQuantitySamplesWithAnchor:(NSString)typeIdentifier
172
+ unitString:(NSString)unitString
173
+ from:(NSDate)from
174
+ to:(NSDate)to
175
+ limit:(NSInteger)limit
152
176
  anchor:(NSString)anchor
153
177
  resolve:(RCTPromiseResolveBlock)resolve
154
178
  reject:(RCTPromiseRejectBlock)reject