@kingstinct/react-native-healthkit 13.0.1 → 13.0.2

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.
@@ -1,11 +1,12 @@
1
1
  import HealthKit
2
2
  import NitroModules
3
3
 
4
- func emptyStatisticsResponse(from: Date, to: Date) -> QueryStatisticsResponse {
4
+ func emptyStatisticsResponse(from: Date?, to: Date?) -> QueryStatisticsResponse {
5
5
  var response = QueryStatisticsResponse()
6
6
 
7
7
  response.startDate = from
8
8
  response.endDate = to
9
+ response.sources = []
9
10
 
10
11
  return response
11
12
  }
@@ -14,7 +15,7 @@ func queryStatisticsForQuantityInternal(
14
15
  quantityType: HKQuantityType,
15
16
  statistics: [StatisticsOptions],
16
17
  options: StatisticsQueryOptions?
17
- ) async throws -> HKStatistics {
18
+ ) async throws -> HKStatistics? {
18
19
  let predicate = createPredicateForSamples(options?.filter)
19
20
 
20
21
  return try await withCheckedThrowingContinuation { continuation in
@@ -25,7 +26,9 @@ func queryStatisticsForQuantityInternal(
25
26
  ) { (_, stats: HKStatistics?, error: Error?) in
26
27
  DispatchQueue.main.async {
27
28
  if let error = error {
28
- return continuation.resume(throwing: error)
29
+ return handleHKNoDataOrThrow(error: error, continuation: continuation, noDataFallback: {
30
+ return nil
31
+ })
29
32
  }
30
33
 
31
34
  if let stats = stats {
@@ -113,7 +116,7 @@ func queryStatisticsCollectionForQuantityInternal(
113
116
  anchorDate: Date,
114
117
  intervalComponents: IntervalComponents,
115
118
  options: StatisticsQueryOptions?
116
- ) async throws -> HKStatisticsCollection {
119
+ ) async throws -> HKStatisticsCollection? {
117
120
  let predicate = createPredicateForSamples(options?.filter)
118
121
 
119
122
  // Create date components from interval
@@ -148,7 +151,9 @@ func queryStatisticsCollectionForQuantityInternal(
148
151
 
149
152
  query.initialResultsHandler = { (_, results: HKStatisticsCollection?, error: Error?) in
150
153
  if let error = error {
151
- return continuation.resume(throwing: error)
154
+ return handleHKNoDataOrThrow(error: error, continuation: continuation, noDataFallback: {
155
+ return nil
156
+ })
152
157
  }
153
158
 
154
159
  guard let statistics = results else {
@@ -280,20 +285,19 @@ class QuantityTypeModule: HybridQuantityTypeModuleSpec {
280
285
  return Promise.async {
281
286
  let quantityType = try initializeQuantityType(identifier.stringValue)
282
287
 
283
- let gottenStats = try await queryStatisticsForQuantityInternal(
288
+ if let gottenStats = try await queryStatisticsForQuantityInternal(
284
289
  quantityType: quantityType,
285
290
  statistics: statistics,
286
291
  options: options
287
- )
288
-
289
- let unit = try await getUnitToUse(
290
- unitOverride: options?.unit,
291
- quantityType: quantityType
292
- )
293
-
294
- let response = serializeStatisticsPerSource(gottenStats: gottenStats, unit: unit)
295
-
296
- return response
292
+ ) {
293
+ let unit = try await getUnitToUse(
294
+ unitOverride: options?.unit,
295
+ quantityType: quantityType
296
+ )
297
+ return serializeStatisticsPerSource(gottenStats: gottenStats, unit: unit)
298
+ } else {
299
+ return []
300
+ }
297
301
  }
298
302
  }
299
303
 
@@ -304,22 +308,24 @@ class QuantityTypeModule: HybridQuantityTypeModuleSpec {
304
308
  return Promise.async {
305
309
  let quantityType = try initializeQuantityType(identifier.stringValue)
306
310
 
307
- let statistics = try await queryStatisticsCollectionForQuantityInternal(
311
+ if let statistics = try await queryStatisticsCollectionForQuantityInternal(
308
312
  quantityType: quantityType,
309
313
  statistics: statistics,
310
314
  anchorDate: anchorDate,
311
315
  intervalComponents: intervalComponents,
312
316
  options: options
313
- )
317
+ ) {
314
318
 
315
- let unit = try await getUnitToUse(
316
- unitOverride: options?.unit,
317
- quantityType: quantityType
318
- )
319
+ let unit = try await getUnitToUse(
320
+ unitOverride: options?.unit,
321
+ quantityType: quantityType
322
+ )
319
323
 
320
- return statistics.statistics().flatMap { statistics in
321
- return serializeStatisticsPerSource(gottenStats: statistics, unit: unit)
324
+ return statistics.statistics().flatMap { statistics in
325
+ return serializeStatisticsPerSource(gottenStats: statistics, unit: unit)
326
+ }
322
327
  }
328
+ return []
323
329
  }
324
330
  }
325
331
 
@@ -351,20 +357,23 @@ class QuantityTypeModule: HybridQuantityTypeModuleSpec {
351
357
  return Promise.async {
352
358
  let quantityType = try initializeQuantityType(identifier.stringValue)
353
359
 
354
- let gottenStats = try await queryStatisticsForQuantityInternal(
360
+ if let gottenStats = try await queryStatisticsForQuantityInternal(
355
361
  quantityType: quantityType,
356
362
  statistics: statistics,
357
363
  options: options
358
- )
359
-
360
- let unit = try await getUnitToUse(
361
- unitOverride: options?.unit,
362
- quantityType: quantityType
363
- )
364
+ ) {
365
+ let unit = try await getUnitToUse(
366
+ unitOverride: options?.unit,
367
+ quantityType: quantityType
368
+ )
364
369
 
365
- let response = serializeStatistics(gottenStats: gottenStats, unit: unit)
370
+ return serializeStatistics(gottenStats: gottenStats, unit: unit)
371
+ }
366
372
 
367
- return response
373
+ return emptyStatisticsResponse(
374
+ from: options?.filter?.date?.startDate,
375
+ to: options?.filter?.date?.endDate
376
+ )
368
377
  }
369
378
  }
370
379
 
@@ -375,22 +384,24 @@ class QuantityTypeModule: HybridQuantityTypeModuleSpec {
375
384
  return Promise.async {
376
385
  let quantityType = try initializeQuantityType(identifier.stringValue)
377
386
 
378
- let statistics = try await queryStatisticsCollectionForQuantityInternal(
387
+ if let statistics = try await queryStatisticsCollectionForQuantityInternal(
379
388
  quantityType: quantityType,
380
389
  statistics: statistics,
381
390
  anchorDate: anchorDate,
382
391
  intervalComponents: intervalComponents,
383
392
  options: options
384
- )
385
-
386
- let unit = try await getUnitToUse(
387
- unitOverride: options?.unit,
388
- quantityType: quantityType
389
- )
393
+ ) {
394
+ let unit = try await getUnitToUse(
395
+ unitOverride: options?.unit,
396
+ quantityType: quantityType
397
+ )
390
398
 
391
- return statistics.statistics().map { statistics in
392
- return serializeStatistics(gottenStats: statistics, unit: unit)
399
+ return statistics.statistics().map { statistics in
400
+ return serializeStatistics(gottenStats: statistics, unit: unit)
401
+ }
393
402
  }
403
+
404
+ return []
394
405
  }
395
406
  }
396
407
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kingstinct/react-native-healthkit",
3
- "version": "13.0.1",
3
+ "version": "13.0.2",
4
4
  "description": "React Native bindings for HealthKit",
5
5
  "main": "lib/commonjs/index.js",
6
6
  "module": "lib/module/index.js",