-
Notifications
You must be signed in to change notification settings - Fork 25
Add Health Connect Syncing #109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| /* | ||
| * SPDX-License-Identifier: GPL-3.0-or-later | ||
| * Copyright (c) 2026. The LibreFit Contributors | ||
| * | ||
| * LibreFit is subject to additional terms covering author attribution and trademark usage; | ||
| * see the ADDITIONAL_TERMS.md and TRADEMARK_POLICY.md files in the project root. | ||
| */ | ||
|
|
||
| package org.librefit.health | ||
|
|
||
| import android.content.Context | ||
| import androidx.health.connect.client.HealthConnectClient | ||
| import androidx.health.connect.client.HealthConnectClient.Companion.SDK_AVAILABLE | ||
| import androidx.health.connect.client.permission.HealthPermission | ||
| import androidx.health.connect.client.records.BodyFatRecord | ||
| import androidx.health.connect.client.records.Record | ||
| import androidx.health.connect.client.records.WeightRecord | ||
| import androidx.health.connect.client.records.metadata.Metadata | ||
| import androidx.health.connect.client.units.Mass | ||
| import androidx.health.connect.client.units.Percentage | ||
| import dagger.hilt.android.qualifiers.ApplicationContext | ||
| import org.librefit.db.entity.Measurement | ||
| import java.time.ZoneId | ||
| import javax.inject.Inject | ||
| import javax.inject.Singleton | ||
|
|
||
| @Singleton | ||
| class HealthConnectRepository @Inject constructor( | ||
| @param:ApplicationContext private val context: Context | ||
| ) { | ||
| val writePermissions: Set<String> = setOf( | ||
| HealthPermission.getWritePermission(BodyFatRecord::class), | ||
| HealthPermission.getWritePermission(WeightRecord::class) | ||
| ) | ||
|
|
||
| private val healthConnectClient: HealthConnectClient by lazy { | ||
| HealthConnectClient.getOrCreate(context) | ||
| } | ||
|
|
||
| fun isAvailable(): Boolean { | ||
| return HealthConnectClient.getSdkStatus(context) == SDK_AVAILABLE | ||
| } | ||
|
|
||
| suspend fun hasWritePermissions(): Boolean { | ||
| if (!isAvailable()) return false | ||
|
|
||
| return healthConnectClient.permissionController | ||
| .getGrantedPermissions() | ||
| .containsAll(writePermissions) | ||
| } | ||
|
|
||
| suspend fun exportMeasurements(measurements: List<Measurement>): Int { | ||
| if (!hasWritePermissions()) { | ||
| throw SecurityException("Missing Health Connect write permissions") | ||
| } | ||
|
|
||
| val records = measurements.flatMap { it.toHealthConnectRecords() } | ||
| if (records.isEmpty()) return 0 | ||
|
|
||
| healthConnectClient.insertRecords(records) | ||
| return records.size | ||
| } | ||
|
|
||
| private fun Measurement.toHealthConnectRecords(): List<Record> { | ||
| val zone = ZoneId.systemDefault() | ||
| val zonedDate = date.atZone(zone) | ||
| val instant = zonedDate.toInstant() | ||
| val zoneOffset = zonedDate.offset | ||
| val recordVersion = instant.toEpochMilli() | ||
|
|
||
| return buildList { | ||
| if (bodyWeight > 0.0) { | ||
| add( | ||
| WeightRecord( | ||
| metadata = Metadata.manualEntry( | ||
| clientRecordId = "librefit-measurement-$id-body-weight", | ||
| clientRecordVersion = recordVersion | ||
| ), | ||
| time = instant, | ||
| zoneOffset = zoneOffset, | ||
| weight = Mass.kilograms(bodyWeight) | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| if (bodyFatPercentage > 0) { | ||
| add( | ||
| BodyFatRecord( | ||
| metadata = Metadata.manualEntry( | ||
| clientRecordId = "librefit-measurement-$id-body-fat", | ||
| clientRecordVersion = recordVersion | ||
| ), | ||
| time = instant, | ||
| zoneOffset = zoneOffset, | ||
| percentage = Percentage(bodyFatPercentage.toDouble()) | ||
| ) | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /* | ||
| * SPDX-License-Identifier: GPL-3.0-or-later | ||
| * Copyright (c) 2026. The LibreFit Contributors | ||
| * | ||
| * LibreFit is subject to additional terms covering author attribution and trademark usage; | ||
| * see the ADDITIONAL_TERMS.md and TRADEMARK_POLICY.md files in the project root. | ||
| */ | ||
|
|
||
| package org.librefit.ui.screens.settings | ||
|
|
||
| data class HealthConnectState( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move this in |
||
| val status: HealthConnectStatus = HealthConnectStatus.UNAVAILABLE, | ||
| val isEnabled: Boolean = false, | ||
| val exportedRecords: Int? = null | ||
| ) { | ||
| val isAvailable: Boolean | ||
| get() = status != HealthConnectStatus.UNAVAILABLE | ||
|
|
||
| val hasPermissions: Boolean | ||
| get() = status == HealthConnectStatus.READY || | ||
| status == HealthConnectStatus.EXPORTING | ||
|
|
||
| val isExporting: Boolean | ||
| get() = status == HealthConnectStatus.EXPORTING | ||
|
|
||
| val isChecked: Boolean | ||
| get() = isEnabled && hasPermissions | ||
| } | ||
|
|
||
| enum class HealthConnectStatus { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move this in |
||
| UNAVAILABLE, | ||
| NEEDS_PERMISSIONS, | ||
| READY, | ||
| EXPORTING | ||
| } | ||
|
|
||
| enum class HealthConnectClickAction { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move this in |
||
| NONE, | ||
| REQUEST_PERMISSIONS | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.