AFHealthTracking

actual object AFHealthTracking

AFHealthTracking is an object responsible for managing health data interactions. It provides functionalities to request permissions, check permission status, sync health data, fetch health data by date, and revoke permissions.

This object utilizes Android's Health Connect API to read health data such as steps, calories, and distance. It maintains a set of required health permissions and manages them per ComponentActivity.

Historical health data can be fetched for a specific date using fetchHealthDataByDate.

Sample Usage:

1. Requesting Permissions (in an Android ComponentActivity):

class MyActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
lifecycleScope.launch {
try {
val granted = AFHealthTracking.requestPermissions(this@MyActivity)
if (granted) {
// Permissions granted, proceed with health data operations
} else {
// Permissions denied
}
} catch (e: Throwable) {
// Handle exceptions, e.g., Health Connect not installed
Log.e("HealthTracking", "Error requesting permissions", e)
}
}
}
}

2. Checking Permissions:

// In a coroutine scope
val hasPermissions = AFHealthTracking.hasAllPermissions(activityContext) // activityContext is your ComponentActivity
if (hasPermissions) {
// All required permissions are granted
}

3. Fetching Historical Health Data:

// In a coroutine scope
try {
val healthDataForYesterday = AFHealthTracking.fetchHealthDataByDate(day = 25, month = 12, year = 2023)
println("Steps on 2023-12-25: ${healthDataForYesterday.steps}")
println("Calories on 2023-12-25: ${healthDataForYesterday.calories}")
} catch (e: Throwable) {
actual object AFHealthTracking

Provides access to health data on iOS using HealthKit.

This object encapsulates the logic for requesting permissions, checking permissions, syncing data (though HealthKit provides real-time data, so explicit sync is often not needed), fetching health data by date, and attempting to revoke permissions (which is not programmatically possible with HealthKit).

It interacts with the HKHealthStore to perform these operations and uses a Logger for internal logging.

The primary health data types it works with are:

  • Step Count (HKQuantityTypeIdentifierStepCount)

  • Active Energy Burned (HKQuantityTypeIdentifierActiveEnergyBurned)

  • Distance Walking/Running (HKQuantityTypeIdentifierDistanceWalkingRunning)

All public functions are suspending functions and can throw exceptions, typically related to HealthKit availability or authorization issues.

actual object AFHealthTracking

Provides access to health data on watchOS using HealthKit.

This object encapsulates the logic for requesting permissions, checking permissions, syncing data (though HealthKit provides real-time data, so explicit sync is often not needed), fetching health data by date, and attempting to revoke permissions (which is not programmatically possible with HealthKit).

It interacts with the HKHealthStore to perform these operations and uses a Logger for internal logging.

The primary health data types it works with are:

  • Step Count (HKQuantityTypeIdentifierStepCount)

  • Active Energy Burned (HKQuantityTypeIdentifierActiveEnergyBurned)

  • Distance Walking/Running (HKQuantityTypeIdentifierDistanceWalkingRunning)

All public functions are suspending functions and can throw exceptions, typically related to HealthKit availability or authorization issues.

Functions

Link copied to clipboard
actual suspend fun fetchHealthDataByDate(day: Int, month: Int, year: Int): HealthData
actual suspend fun fetchHealthDataByDate(day: Int, month: Int, year: Int): HealthData
actual suspend fun fetchHealthDataByDate(day: Int, month: Int, year: Int): HealthData
Link copied to clipboard
actual suspend fun hasPermissions(context: Any?): Boolean
actual suspend fun hasPermissions(context: Any?): Boolean
actual suspend fun hasPermissions(context: Any?): Boolean
Link copied to clipboard
actual suspend fun requestPermissions(context: Any?): Boolean
actual suspend fun requestPermissions(context: Any?): Boolean
actual suspend fun requestPermissions(context: Any?): Boolean
Link copied to clipboard
actual suspend fun revokePermissions(): Boolean
actual suspend fun revokePermissions(): Boolean
actual suspend fun revokePermissions(): Boolean