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.

watchOS limitation: There is no openHealthApp() method on watchOS because the platform does not support launching external apps via URL schemes. Users should manage HealthKit permissions from the Health app on their paired iPhone.

Functions

Link copied to clipboard
actual suspend fun disableBackgroundDelivery()

Disables HealthKit background delivery for step-count data.

actual suspend fun disableBackgroundDelivery()

Disables HealthKit background delivery for step-count data.

Link copied to clipboard
actual suspend fun enableBackgroundDelivery(onDataAvailable: () -> Unit?)
actual suspend fun enableBackgroundDelivery(onDataAvailable: () -> Unit?)

Enables HealthKit background delivery for step-count data.

actual suspend fun enableBackgroundDelivery(onDataAvailable: () -> Unit?)

Enables HealthKit background delivery for step-count data.

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 fetchHealthDataForRange(startDate: LocalDate, endDate: LocalDate): List<HealthData>
actual suspend fun fetchHealthDataForRange(startDate: LocalDate, endDate: LocalDate): List<HealthData>
actual suspend fun fetchHealthDataForRange(startDate: LocalDate, endDate: LocalDate): List<HealthData>
Link copied to clipboard
actual suspend fun fetchStepDataSources(): List<HealthKitDataSource>

Queries HealthKit for all apps and devices that have contributed step-count data.

actual suspend fun fetchStepDataSources(): List<HealthKitDataSource>

Queries HealthKit for all apps and devices that have contributed step-count data.

getAuthorizationRequestStatus
Link copied to clipboard
suspend fun getAuthorizationRequestStatus(): HealthKitAuthRequestStatus

Checks whether the HealthKit authorization dialog has already been presented.

suspend fun getAuthorizationRequestStatus(): HealthKitAuthRequestStatus

Checks whether the HealthKit authorization dialog has already been presented.

Link copied to clipboard

Returns the set of Health Connect permission strings that have been granted to this app.

Link copied to clipboard

Returns a comprehensive HealthConnectDiagnostics snapshot that developers can log or display in a debug/settings screen.

getHealthKitDiagnostics
Link copied to clipboard
suspend fun getHealthKitDiagnostics(): HealthKitDiagnostics

Returns a comprehensive HealthKitDiagnostics snapshot that developers can log or display in a debug/settings screen.

suspend fun getHealthKitDiagnostics(): HealthKitDiagnostics

Returns a comprehensive com.advantahealth.api.health.model.HealthKitDiagnostics snapshot that developers can log or display in a debug/settings screen.

Link copied to clipboard
actual suspend fun getPermissionStatus(): HealthPermissionStatus
actual suspend fun getPermissionStatus(): HealthPermissionStatus
Link copied to clipboard

Returns the current availability status of the Health Connect SDK.

getStepTrackingPermissionStatus
Link copied to clipboard
suspend fun getStepTrackingPermissionStatus(): HealthPermissionStatus

Infers whether the user has granted read permission for step-count data.

suspend fun getStepTrackingPermissionStatus(): HealthPermissionStatus

Infers whether the user has granted read permission for step-count data.

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
fun installOrUpdateHealthConnect(context: ERROR CLASS: Symbol not found for android.content.Context?? = null)

Redirects the user to the Google Play Store to install or update Health Connect.

isAvailable
Link copied to clipboard

Convenience check — returns true when the Health Connect SDK is fully available and ready to use on this device.

Returns true when HealthKit is available on this device.

Returns true when HealthKit is available on this device.

Link copied to clipboard

Checks whether the background-read permission is granted.

Link copied to clipboard

Checks whether the history-read permission is granted.

Link copied to clipboard

Opens the Apple Health app.

Link copied to clipboard
fun openHealthConnectSettings(context: ERROR CLASS: Symbol not found for android.content.Context)

Opens the Health Connect settings screen (the system Health Connect app).

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