@flomentumsolutions/capacitor-health-extended 0.0.16 → 0.0.18

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.
@@ -144,13 +144,19 @@ class HealthPlugin : Plugin() {
144
144
  call.resolve(result)
145
145
  }
146
146
 
147
- // Helper to ensure client is initialized
147
+ // Helper to ensure HealthConnectClient is ready; attempts init lazily
148
148
  private fun ensureClientInitialized(call: PluginCall): Boolean {
149
- if (!available) {
150
- call.reject("Health Connect client not initialized. Call isHealthAvailable() first.")
151
- return false
149
+ if (available) return true
150
+
151
+ return try {
152
+ healthConnectClient = HealthConnectClient.getOrCreate(context)
153
+ available = true
154
+ true
155
+ } catch (e: Exception) {
156
+ Log.e(tag, "Failed to initialise HealthConnectClient", e)
157
+ call.reject("Health Connect is not available on this device.")
158
+ false
152
159
  }
153
- return true
154
160
  }
155
161
 
156
162
  // Check if a set of permissions are granted
@@ -264,7 +270,7 @@ class HealthPlugin : Plugin() {
264
270
  private fun getMetricAndMapper(dataType: String): MetricAndMapper {
265
271
  return when (dataType) {
266
272
  "steps" -> metricAndMapper("steps", CapHealthPermission.READ_STEPS, StepsRecord.COUNT_TOTAL) { it?.toDouble() }
267
- "active-calories" -> metricAndMapper(
273
+ "active-calories", "activeCalories" -> metricAndMapper(
268
274
  "calories",
269
275
  CapHealthPermission.READ_ACTIVE_CALORIES,
270
276
  ActiveCaloriesBurnedRecord.ACTIVE_CALORIES_TOTAL
@@ -295,7 +301,7 @@ class HealthPlugin : Plugin() {
295
301
  CoroutineScope(Dispatchers.IO).launch {
296
302
  try {
297
303
  val result = when (dataType) {
298
- "heart-rate" -> readLatestHeartRate()
304
+ "heart-rate", "heartRate" -> readLatestHeartRate()
299
305
  "weight" -> readLatestWeight()
300
306
  "height" -> readLatestHeight()
301
307
  "steps" -> readLatestSteps()
@@ -364,11 +370,11 @@ class HealthPlugin : Plugin() {
364
370
  timeRangeFilter = TimeRangeFilter.after(Instant.EPOCH),
365
371
  pageSize = 1
366
372
  )
367
- val result = healthConnectClient.readRecords(request)
368
- val record = result.records.firstOrNull() ?: throw Exception("No weight data found")
373
+ val record = healthConnectClient.readRecords(request).records.firstOrNull()
374
+
369
375
  return JSObject().apply {
370
- put("value", record.weight.inKilograms)
371
- put("timestamp", record.time.epochSecond * 1000) // Convert to milliseconds like iOS
376
+ put("value", record?.weight?.inKilograms ?: 0)
377
+ put("timestamp", (record?.time?.epochSecond ?: 0) * 1000)
372
378
  put("unit", "kg")
373
379
  }
374
380
  }
@@ -382,11 +388,10 @@ class HealthPlugin : Plugin() {
382
388
  timeRangeFilter = TimeRangeFilter.after(Instant.EPOCH),
383
389
  pageSize = 1
384
390
  )
385
- val result = healthConnectClient.readRecords(request)
386
- val record = result.records.firstOrNull() ?: throw Exception("No step data found")
391
+ val record = healthConnectClient.readRecords(request).records.firstOrNull()
387
392
  return JSObject().apply {
388
- put("value", record.count)
389
- put("timestamp", record.endTime.epochSecond * 1000) // Convert to milliseconds like iOS
393
+ put("value", record?.count ?: 0)
394
+ put("timestamp", (record?.endTime?.epochSecond ?: 0) * 1000)
390
395
  put("unit", "count")
391
396
  }
392
397
  }
@@ -400,11 +405,11 @@ class HealthPlugin : Plugin() {
400
405
  timeRangeFilter = TimeRangeFilter.after(Instant.EPOCH),
401
406
  pageSize = 1
402
407
  )
403
- val result = healthConnectClient.readRecords(request)
404
- val record = result.records.firstOrNull() ?: throw Exception("No HRV data found")
408
+ val record = healthConnectClient.readRecords(request).records.firstOrNull()
409
+
405
410
  return JSObject().apply {
406
- put("value", record.heartRateVariabilityMillis)
407
- put("timestamp", record.time.epochSecond * 1000) // Convert to milliseconds like iOS
411
+ put("value", record?.heartRateVariabilityMillis ?: 0)
412
+ put("timestamp", (record?.time?.epochSecond ?: 0) * 1000)
408
413
  put("unit", "ms")
409
414
  }
410
415
  }
@@ -418,12 +423,12 @@ class HealthPlugin : Plugin() {
418
423
  timeRangeFilter = TimeRangeFilter.after(Instant.EPOCH),
419
424
  pageSize = 1
420
425
  )
421
- val result = healthConnectClient.readRecords(request)
422
- val record = result.records.firstOrNull() ?: throw Exception("No blood pressure data found")
426
+ val record = healthConnectClient.readRecords(request).records.firstOrNull()
427
+
423
428
  return JSObject().apply {
424
- put("systolic", record.systolic.inMillimetersOfMercury)
425
- put("diastolic", record.diastolic.inMillimetersOfMercury)
426
- put("timestamp", record.time.epochSecond * 1000) // Convert to milliseconds like iOS
429
+ put("systolic", record?.systolic?.inMillimetersOfMercury ?: 0)
430
+ put("diastolic", record?.diastolic?.inMillimetersOfMercury ?: 0)
431
+ put("timestamp", (record?.time?.epochSecond ?: 0) * 1000)
427
432
  put("unit", "mmHg")
428
433
  }
429
434
  }
@@ -437,11 +442,11 @@ class HealthPlugin : Plugin() {
437
442
  timeRangeFilter = TimeRangeFilter.after(Instant.EPOCH),
438
443
  pageSize = 1
439
444
  )
440
- val result = healthConnectClient.readRecords(request)
441
- val record = result.records.firstOrNull() ?: throw Exception("No height data found")
445
+ val record = healthConnectClient.readRecords(request).records.firstOrNull()
446
+
442
447
  return JSObject().apply {
443
- put("value", record.height.inMeters)
444
- put("timestamp", record.time.epochSecond * 1000) // Convert to milliseconds like iOS
448
+ put("value", record?.height?.inMeters ?: 0)
449
+ put("timestamp", (record?.time?.epochSecond ?: 0) * 1000)
445
450
  put("unit", "m")
446
451
  }
447
452
  }
@@ -455,11 +460,10 @@ class HealthPlugin : Plugin() {
455
460
  timeRangeFilter = TimeRangeFilter.after(Instant.EPOCH),
456
461
  pageSize = 1
457
462
  )
458
- val result = healthConnectClient.readRecords(request)
459
- val record = result.records.firstOrNull() ?: throw Exception("No distance data found")
463
+ val record = healthConnectClient.readRecords(request).records.firstOrNull()
460
464
  return JSObject().apply {
461
- put("value", record.distance.inMeters)
462
- put("timestamp", record.endTime.epochSecond * 1000) // Convert to milliseconds like iOS
465
+ put("value", record?.distance?.inMeters ?: 0)
466
+ put("timestamp", (record?.endTime?.epochSecond ?: 0) * 1000)
463
467
  put("unit", "m")
464
468
  }
465
469
  }
@@ -473,11 +477,10 @@ class HealthPlugin : Plugin() {
473
477
  timeRangeFilter = TimeRangeFilter.after(Instant.EPOCH),
474
478
  pageSize = 1
475
479
  )
476
- val result = healthConnectClient.readRecords(request)
477
- val record = result.records.firstOrNull() ?: throw Exception("No active calories data found")
480
+ val record = healthConnectClient.readRecords(request).records.firstOrNull()
478
481
  return JSObject().apply {
479
- put("value", record.energy.inKilocalories)
480
- put("timestamp", record.endTime.epochSecond * 1000) // Convert to milliseconds like iOS
482
+ put("value", record?.energy?.inKilocalories ?: 0)
483
+ put("timestamp", (record?.endTime?.epochSecond ?: 0) * 1000)
481
484
  put("unit", "kcal")
482
485
  }
483
486
  }
@@ -491,11 +494,10 @@ class HealthPlugin : Plugin() {
491
494
  timeRangeFilter = TimeRangeFilter.after(Instant.EPOCH),
492
495
  pageSize = 1
493
496
  )
494
- val result = healthConnectClient.readRecords(request)
495
- val record = result.records.firstOrNull() ?: throw Exception("No total calories data found")
497
+ val record = healthConnectClient.readRecords(request).records.firstOrNull()
496
498
  return JSObject().apply {
497
- put("value", record.energy.inKilocalories)
498
- put("timestamp", record.endTime.epochSecond * 1000) // Convert to milliseconds like iOS
499
+ put("value", record?.energy?.inKilocalories ?: 0)
500
+ put("timestamp", (record?.endTime?.epochSecond ?: 0) * 1000)
499
501
  put("unit", "kcal")
500
502
  }
501
503
  }
@@ -617,7 +619,7 @@ class HealthPlugin : Plugin() {
617
619
  )
618
620
 
619
621
  return response.map {
620
- val mappedValue = metricAndMapper.getValue(it.result)
622
+ val mappedValue = metricAndMapper.getValue(it.result) ?: 0.0
621
623
  AggregatedSample(it.startTime, it.endTime, mappedValue)
622
624
  }
623
625
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flomentumsolutions/capacitor-health-extended",
3
- "version": "0.0.16",
3
+ "version": "0.0.18",
4
4
  "description": "Capacitor plugin for Apple HealthKit and Google Health Connect Platform",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",