@asiriindatissa/capacitor-health 8.4.1 → 8.4.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.
package/README.md CHANGED
@@ -136,8 +136,10 @@ To query workout sessions, you need to request read permission for `'workouts'`:
136
136
 
137
137
  ```ts
138
138
  // Request permission to read workouts
139
+ // IMPORTANT: To see totalEnergyBurned and totalDistance in workout data,
140
+ // you MUST also request permissions for 'calories' (or 'totalCalories') and 'distance'
139
141
  await Health.requestAuthorization({
140
- read: ['workouts', 'steps', 'heartRate'], // Include 'workouts' for queryWorkouts()
142
+ read: ['workouts', 'calories', 'totalCalories', 'distance'], // Include data types for workout metrics
141
143
  write: [],
142
144
  });
143
145
 
@@ -147,9 +149,18 @@ const { workouts } = await Health.queryWorkouts({
147
149
  endDate: new Date().toISOString(),
148
150
  limit: 10,
149
151
  });
152
+
153
+ // Each workout may include:
154
+ // - workoutType, duration, startDate, endDate (always present)
155
+ // - totalEnergyBurned (if calories/totalCalories permission granted and data exists)
156
+ // - totalDistance (if distance permission granted and data exists)
150
157
  ```
151
158
 
152
- Note: `'workouts'` is a special read-only permission type. You cannot write workouts with this plugin.
159
+ **Note:**
160
+
161
+ - `'workouts'` is a special read-only permission type. You cannot write workouts with this plugin.
162
+ - Workout energy and distance data are aggregated from separate Health Connect records during the workout time period. If you don't request permissions for `calories`/`totalCalories` and `distance`, these fields will be missing from workout results.
163
+ - If `totalEnergyBurned` or `totalDistance` are missing despite having permissions, it means no calorie or distance data was recorded during that workout period in Health Connect.
153
164
 
154
165
  ## API
155
166
 
@@ -400,9 +400,13 @@ class HealthManager {
400
400
  // Removed dataOriginFilter to get distance from all sources during workout time
401
401
  )
402
402
  val result = client.aggregate(aggregateRequest)
403
- result[DistanceRecord.DISTANCE_TOTAL]?.inMeters
403
+ val distance = result[DistanceRecord.DISTANCE_TOTAL]?.inMeters
404
+ if (distance == null) {
405
+ android.util.Log.d("HealthManager", "No distance data found for workout ${session.startTime} to ${session.endTime}")
406
+ }
407
+ distance
404
408
  } catch (e: Exception) {
405
- android.util.Log.d("HealthManager", "Distance aggregation failed for workout: ${e.message}", e)
409
+ android.util.Log.w("HealthManager", "Distance aggregation failed for workout: ${e.message}", e)
406
410
  null // Permission might not be granted or no data available
407
411
  }
408
412
 
@@ -413,21 +417,34 @@ class HealthManager {
413
417
  timeRangeFilter = timeRange
414
418
  )
415
419
  val result = client.aggregate(aggregateRequest)
416
- result[ActiveCaloriesBurnedRecord.ACTIVE_CALORIES_TOTAL]?.inKilocalories
417
- } catch (e: Exception) {
418
- android.util.Log.d("HealthManager", "Active calories aggregation failed, trying total calories: ${e.message}", e)
419
- // Fall back to total calories
420
- try {
421
- val aggregateRequest = AggregateRequest(
422
- metrics = setOf(TotalCaloriesBurnedRecord.ENERGY_TOTAL),
423
- timeRangeFilter = timeRange
424
- )
425
- val result = client.aggregate(aggregateRequest)
426
- result[TotalCaloriesBurnedRecord.ENERGY_TOTAL]?.inKilocalories
427
- } catch (e2: Exception) {
428
- android.util.Log.d("HealthManager", "Total calories aggregation also failed: ${e2.message}", e2)
429
- null // Permission might not be granted or no data available
420
+ val activeCalories = result[ActiveCaloriesBurnedRecord.ACTIVE_CALORIES_TOTAL]?.inKilocalories
421
+ if (activeCalories != null && activeCalories > 0) {
422
+ android.util.Log.d("HealthManager", "Found active calories: $activeCalories kcal")
423
+ activeCalories
424
+ } else {
425
+ android.util.Log.d("HealthManager", "No active calories found, trying total calories")
426
+ // Fall back to total calories
427
+ try {
428
+ val totalRequest = AggregateRequest(
429
+ metrics = setOf(TotalCaloriesBurnedRecord.ENERGY_TOTAL),
430
+ timeRangeFilter = timeRange
431
+ )
432
+ val totalResult = client.aggregate(totalRequest)
433
+ val totalCalories = totalResult[TotalCaloriesBurnedRecord.ENERGY_TOTAL]?.inKilocalories
434
+ if (totalCalories != null && totalCalories > 0) {
435
+ android.util.Log.d("HealthManager", "Found total calories: $totalCalories kcal")
436
+ } else {
437
+ android.util.Log.w("HealthManager", "No calorie data (active or total) available for workout ${session.startTime} to ${session.endTime}")
438
+ }
439
+ totalCalories
440
+ } catch (e2: Exception) {
441
+ android.util.Log.w("HealthManager", "Total calories aggregation failed: ${e2.message}", e2)
442
+ null
443
+ }
430
444
  }
445
+ } catch (e: Exception) {
446
+ android.util.Log.w("HealthManager", "Active calories aggregation failed: ${e.message}", e)
447
+ null // Permission might not be granted or no data available
431
448
  }
432
449
 
433
450
  return WorkoutAggregatedData(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@asiriindatissa/capacitor-health",
3
- "version": "8.4.1",
3
+ "version": "8.4.2",
4
4
  "description": "Capacitor plugin to interact with data from Health Connect on Android",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",