Skip to content

Commit

Permalink
Change HealthDataType to sealed interface
Browse files Browse the repository at this point in the history
  • Loading branch information
vitoksmile committed Sep 8, 2023
1 parent 09e8951 commit e6ae267
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 13 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ The library supports:
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
- Steps
- Weight

## Requesting permission

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import androidx.health.connect.client.permission.HealthPermission
import androidx.health.connect.client.records.Record
import androidx.health.connect.client.records.StepsRecord
import androidx.health.connect.client.records.WeightRecord
import com.vitoksmile.kmp.health.HealthDataType.Steps
import com.vitoksmile.kmp.health.HealthDataType.Weight
import kotlin.reflect.KClass

internal fun HealthDataType.toRecordType(): KClass<out Record> = when (this) {
HealthDataType.STEPS -> StepsRecord::class
HealthDataType.WEIGHT -> WeightRecord::class
Steps -> StepsRecord::class

Weight -> WeightRecord::class
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.vitoksmile.kmp.health

enum class HealthDataType {
STEPS,
WEIGHT,
sealed interface HealthDataType {
data object Steps : HealthDataType

data object Weight : HealthDataType
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package com.vitoksmile.kmp.health

import com.vitoksmile.kmp.health.HealthDataType.Steps
import com.vitoksmile.kmp.health.HealthDataType.Weight
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 ->
Steps ->
HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)

HealthDataType.WEIGHT ->
Weight ->
HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMass)
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp
import com.vitoksmile.kmp.health.HealthDataType
import com.vitoksmile.kmp.health.HealthDataType.Steps
import com.vitoksmile.kmp.health.HealthDataType.Weight
import com.vitoksmile.kmp.health.HealthManagerFactory
import com.vitoksmile.kmp.health.HealthRecord
import com.vitoksmile.kmp.health.records.StepsRecord
Expand All @@ -45,14 +47,14 @@ fun SampleApp() {

val readTypes = remember {
listOf(
HealthDataType.STEPS,
HealthDataType.WEIGHT,
Steps,
Weight,
)
}
val writeTypes = remember {
listOf(
HealthDataType.STEPS,
HealthDataType.WEIGHT,
Steps,
Weight,
)
}

Expand Down

0 comments on commit e6ae267

Please sign in to comment.