generated from vitoksmile/compose-multiplatform-ios-android-template
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9993e77
commit c631fd8
Showing
15 changed files
with
346 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,14 @@ | ||
# HealthKMM | ||
|
||
Kotlin Multiplatform Mobile wrapper for HealthKit on iOS and Google Fit and Health Connect on Android. | ||
Kotlin Multiplatform Mobile wrapper for HealthKit on iOS and ~~Google Fit and~~ Health Connect on Android. | ||
|
||
> Google Fitness API is being deprecated and Health Connect the plugin will transition into the API as the Health Connect | ||
The library supports: | ||
- handling permissions to access health data using the `isAvailable`, `isAuthorized`, `requestAuthorization`, `revokeAuthorization` methods. | ||
|
||
Note that for Android, the target phone **needs** to have ~~[Google Fit](https://www.google.com/fit/) or~~ [Health Connect](https://health.google/health-connect-android/) (which is currently in beta) installed and have access to the internet, otherwise this library will not work. | ||
|
||
## Data Types | ||
- STEPS | ||
- WEIGHT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
core/src/androidMain/kotlin/com/vitoksmile/kmm/health/HealthConnectDataType.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.vitoksmile.kmm.health | ||
|
||
import androidx.health.connect.client.permission.HealthPermission | ||
import androidx.health.connect.client.records.StepsRecord | ||
import androidx.health.connect.client.records.WeightRecord | ||
|
||
internal fun HealthDataType.toHealthPermission( | ||
isRead: Boolean = false, | ||
isWrite: Boolean = false | ||
): String { | ||
require(isRead != isWrite) | ||
|
||
return (when (this) { | ||
HealthDataType.STEPS -> StepsRecord::class | ||
HealthDataType.WEIGHT -> WeightRecord::class | ||
}).let { | ||
if (isRead) { | ||
HealthPermission.getReadPermission(it) | ||
} else { | ||
HealthPermission.getWritePermission(it) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
core/src/androidMain/kotlin/com/vitoksmile/kmm/health/HealthConnectPermissionActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package com.vitoksmile.kmm.health | ||
|
||
import android.content.Context | ||
import android.content.Intent | ||
import android.os.Bundle | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.health.connect.client.PermissionController | ||
import kotlin.coroutines.resume | ||
import kotlinx.coroutines.CancellableContinuation | ||
import kotlinx.coroutines.suspendCancellableCoroutine | ||
|
||
/** | ||
* Health Connect permissions dialog not shown on subsequent requests | ||
* | ||
* https://issuetracker.google.com/issues/233239418 | ||
*/ | ||
internal class HealthConnectPermissionActivity : AppCompatActivity() { | ||
|
||
companion object { | ||
private const val KEY_READ_PERMISSIONS = "KEY_READ_PERMISSIONS" | ||
private const val KEY_WRITE_PERMISSIONS = "KEY_WRITE_PERMISSIONS" | ||
|
||
private var continuation: CancellableContinuation<Result<Boolean>>? = null | ||
|
||
suspend fun request( | ||
context: Context, | ||
readPermissions: Set<String>, | ||
writePermissions: Set<String>, | ||
): Result<Boolean> = suspendCancellableCoroutine { | ||
continuation?.cancel() | ||
continuation = it | ||
|
||
context.startActivity( | ||
Intent(context, HealthConnectPermissionActivity::class.java) | ||
.putExtra(KEY_READ_PERMISSIONS, readPermissions.toTypedArray()) | ||
.putExtra(KEY_WRITE_PERMISSIONS, writePermissions.toTypedArray()) | ||
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK), | ||
) | ||
} | ||
} | ||
|
||
private val permissions: Set<String> by lazy { | ||
val readPermissions = intent.getStringArrayExtra(KEY_READ_PERMISSIONS).orEmpty().toSet() | ||
val writePermissions = intent.getStringArrayExtra(KEY_WRITE_PERMISSIONS).orEmpty().toSet() | ||
readPermissions + writePermissions | ||
} | ||
|
||
private val contract = PermissionController.createRequestPermissionResultContract() | ||
private val requestPermissions = registerForActivityResult(contract) { grantedPermissions -> | ||
val granted = grantedPermissions.containsAll(permissions) | ||
continuation?.resume(Result.success(granted)) | ||
continuation = null | ||
finish() | ||
} | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
val readPermissions = intent.getStringArrayExtra(KEY_READ_PERMISSIONS).orEmpty().toSet() | ||
val writePermissions = intent.getStringArrayExtra(KEY_WRITE_PERMISSIONS).orEmpty().toSet() | ||
val permissions = readPermissions + writePermissions | ||
requestPermissions.launch(permissions) | ||
} | ||
|
||
override fun onDestroy() { | ||
super.onDestroy() | ||
continuation?.cancel() | ||
continuation = null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<resources> | ||
<!-- Transparent and no animation --> | ||
<style name="HealthConnectPermissionTheme" parent="Theme.AppCompat.NoActionBar"> | ||
<item name="android:windowAnimationStyle">@null</item> | ||
<item name="android:windowIsTranslucent">true</item> | ||
<item name="android:windowBackground">@android:color/transparent</item> | ||
<item name="android:windowContentOverlay">@null</item> | ||
<item name="android:windowNoTitle">true</item> | ||
<item name="android:windowIsFloating">true</item> | ||
<item name="android:backgroundDimEnabled">false</item> | ||
</style> | ||
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
core/src/commonMain/kotlin/com/vitoksmile/kmm/health/HealthDataType.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.vitoksmile.kmm.health | ||
|
||
enum class HealthDataType { | ||
STEPS, | ||
WEIGHT, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
core/src/iosMain/kotlin/com/vitoksmile/kmm/health/HealthKitDataType.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.vitoksmile.kmm.health | ||
|
||
import platform.HealthKit.HKQuantityType | ||
import platform.HealthKit.HKQuantityTypeIdentifierBodyMass | ||
import platform.HealthKit.HKQuantityTypeIdentifierStepCount | ||
import platform.HealthKit.HKSampleType | ||
|
||
internal fun HealthDataType.toHKSampleType(): HKSampleType? = when (this) { | ||
HealthDataType.STEPS -> | ||
HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount) | ||
|
||
HealthDataType.WEIGHT -> | ||
HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMass) | ||
} |
Oops, something went wrong.