Skip to content

Commit

Permalink
feat: add aggregateGroupByDuration API (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
taisuke-j authored Oct 28, 2024
1 parent 781cfb9 commit 60efc22
Show file tree
Hide file tree
Showing 49 changed files with 848 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,24 @@ class HealthConnectManager(private val applicationContext: ReactApplicationConte
}
}

fun aggregateGroupByDuration(record: ReadableMap, promise: Promise) {
throwUnlessClientIsAvailable(promise) {
coroutineScope.launch {
try {
val recordType = record.getString("recordType") ?: ""
val response = healthConnectClient.aggregateGroupByDuration(
ReactHealthRecord.getAggregateGroupByDurationRequest(
recordType, record
)
)
promise.resolve(ReactHealthRecord.parseAggregationResultGroupedByDuration(recordType, response))
} catch (e: Exception) {
promise.rejectWithException(e)
}
}
}
}

fun aggregateGroupByPeriod(record: ReadableMap, promise: Promise) {
throwUnlessClientIsAvailable(promise) {
coroutineScope.launch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ class HealthConnectModule internal constructor(context: ReactApplicationContext)
return manager.aggregateRecord(record, promise)
}

@ReactMethod
override fun aggregateGroupByDuration(record: ReadableMap, promise: Promise) {
return manager.aggregateGroupByDuration(record, promise)
}

@ReactMethod
override fun aggregateGroupByPeriod(record: ReadableMap, promise: Promise) {
return manager.aggregateGroupByPeriod(record, promise)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package dev.matinzd.healthconnect.records

import androidx.health.connect.client.aggregate.AggregationResult
import androidx.health.connect.client.aggregate.AggregationResultGroupedByDuration
import androidx.health.connect.client.aggregate.AggregationResultGroupedByPeriod
import androidx.health.connect.client.records.ActiveCaloriesBurnedRecord
import androidx.health.connect.client.request.AggregateGroupByDurationRequest
import androidx.health.connect.client.request.AggregateRequest
import androidx.health.connect.client.request.AggregateGroupByPeriodRequest
import com.facebook.react.bridge.ReadableArray
Expand All @@ -17,6 +19,7 @@ import dev.matinzd.healthconnect.utils.energyToJsMap
import dev.matinzd.healthconnect.utils.getEnergyFromJsMap
import dev.matinzd.healthconnect.utils.getTimeRangeFilter
import dev.matinzd.healthconnect.utils.toMapList
import dev.matinzd.healthconnect.utils.mapJsDurationToDuration
import dev.matinzd.healthconnect.utils.mapJsPeriodToPeriod
import java.time.Instant

Expand Down Expand Up @@ -53,6 +56,15 @@ class ReactActiveCaloriesBurnedRecord : ReactHealthRecordImpl<ActiveCaloriesBurn
)
}

override fun getAggregateGroupByDurationRequest(record: ReadableMap): AggregateGroupByDurationRequest {
return AggregateGroupByDurationRequest(
metrics = aggregateMetrics,
timeRangeFilter = record.getTimeRangeFilter("timeRangeFilter"),
timeRangeSlicer = mapJsDurationToDuration(record.getMap("timeRangeSlicer")),
dataOriginFilter = convertJsToDataOriginSet(record.getArray("dataOriginFilter"))
)
}

override fun getAggregateGroupByPeriodRequest(record: ReadableMap): AggregateGroupByPeriodRequest {
return AggregateGroupByPeriodRequest(
metrics = aggregateMetrics,
Expand Down Expand Up @@ -87,6 +99,20 @@ class ReactActiveCaloriesBurnedRecord : ReactHealthRecordImpl<ActiveCaloriesBurn
}
}

override fun parseAggregationResultGroupedByDuration(record: List<AggregationResultGroupedByDuration>): WritableNativeArray {
return WritableNativeArray().apply {
record.forEach {
val map = WritableNativeMap().apply {
putMap("result", parseAggregationResult(it.result))
putString("startTime", it.startTime.toString())
putString("endTime", it.endTime.toString())
putString("zoneOffset", it.zoneOffset.toString())
}
pushMap(map)
}
}
}

override fun parseAggregationResultGroupedByPeriod(record: List<AggregationResultGroupedByPeriod>): WritableNativeArray {
return WritableNativeArray().apply {
record.forEach {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package dev.matinzd.healthconnect.records

import androidx.health.connect.client.aggregate.AggregationResult
import androidx.health.connect.client.aggregate.AggregationResultGroupedByDuration
import androidx.health.connect.client.aggregate.AggregationResultGroupedByPeriod
import androidx.health.connect.client.records.BasalBodyTemperatureRecord
import androidx.health.connect.client.records.BodyTemperatureMeasurementLocation
import androidx.health.connect.client.request.AggregateGroupByDurationRequest
import androidx.health.connect.client.request.AggregateGroupByPeriodRequest
import androidx.health.connect.client.request.AggregateRequest
import androidx.health.connect.client.units.Temperature
Expand Down Expand Up @@ -48,6 +50,10 @@ class ReactBasalBodyTemperatureRecord : ReactHealthRecordImpl<BasalBodyTemperatu
throw AggregationNotSupported()
}

override fun getAggregateGroupByDurationRequest(record: ReadableMap): AggregateGroupByDurationRequest {
throw AggregationNotSupported()
}

override fun getAggregateGroupByPeriodRequest(record: ReadableMap): AggregateGroupByPeriodRequest {
throw AggregationNotSupported()
}
Expand All @@ -56,6 +62,10 @@ class ReactBasalBodyTemperatureRecord : ReactHealthRecordImpl<BasalBodyTemperatu
throw AggregationNotSupported()
}

override fun parseAggregationResultGroupedByDuration(record: List<AggregationResultGroupedByDuration>): WritableNativeArray {
throw AggregationNotSupported()
}

override fun parseAggregationResultGroupedByPeriod(record: List<AggregationResultGroupedByPeriod>): WritableNativeArray {
throw AggregationNotSupported()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package dev.matinzd.healthconnect.records

import androidx.health.connect.client.aggregate.AggregationResult
import androidx.health.connect.client.aggregate.AggregationResultGroupedByDuration
import androidx.health.connect.client.aggregate.AggregationResultGroupedByPeriod
import androidx.health.connect.client.records.BasalMetabolicRateRecord
import androidx.health.connect.client.request.AggregateGroupByDurationRequest
import androidx.health.connect.client.request.AggregateGroupByPeriodRequest
import androidx.health.connect.client.request.AggregateRequest
import androidx.health.connect.client.units.Power
Expand All @@ -16,6 +18,7 @@ import dev.matinzd.healthconnect.utils.convertJsToDataOriginSet
import dev.matinzd.healthconnect.utils.convertMetadataFromJSMap
import dev.matinzd.healthconnect.utils.convertMetadataToJSMap
import dev.matinzd.healthconnect.utils.getTimeRangeFilter
import dev.matinzd.healthconnect.utils.mapJsDurationToDuration
import dev.matinzd.healthconnect.utils.mapJsPeriodToPeriod
import dev.matinzd.healthconnect.utils.toMapList
import java.time.Instant
Expand Down Expand Up @@ -50,6 +53,15 @@ class ReactBasalMetabolicRateRecord : ReactHealthRecordImpl<BasalMetabolicRateRe
)
}

override fun getAggregateGroupByDurationRequest(record: ReadableMap): AggregateGroupByDurationRequest {
return AggregateGroupByDurationRequest(
metrics = aggregateMetrics,
timeRangeFilter = record.getTimeRangeFilter("timeRangeFilter"),
timeRangeSlicer = mapJsDurationToDuration(record.getMap("timeRangeSlicer")),
dataOriginFilter = convertJsToDataOriginSet(record.getArray("dataOriginFilter"))
)
}

override fun getAggregateGroupByPeriodRequest(record: ReadableMap): AggregateGroupByPeriodRequest {
return AggregateGroupByPeriodRequest(
metrics = aggregateMetrics,
Expand All @@ -59,6 +71,20 @@ class ReactBasalMetabolicRateRecord : ReactHealthRecordImpl<BasalMetabolicRateRe
)
}

override fun parseAggregationResultGroupedByDuration(record: List<AggregationResultGroupedByDuration>): WritableNativeArray {
return WritableNativeArray().apply {
record.forEach {
val map = WritableNativeMap().apply {
putMap("result", parseAggregationResult(it.result))
putString("startTime", it.startTime.toString())
putString("endTime", it.endTime.toString())
putString("zoneOffset", it.zoneOffset.toString())
}
pushMap(map)
}
}
}

override fun parseAggregationResultGroupedByPeriod(record: List<AggregationResultGroupedByPeriod>): WritableNativeArray {
return WritableNativeArray().apply {
record.forEach {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package dev.matinzd.healthconnect.records

import androidx.health.connect.client.aggregate.AggregationResult
import androidx.health.connect.client.aggregate.AggregationResultGroupedByDuration
import androidx.health.connect.client.aggregate.AggregationResultGroupedByPeriod
import androidx.health.connect.client.records.BloodGlucoseRecord
import androidx.health.connect.client.records.MealType.MEAL_TYPE_UNKNOWN
import androidx.health.connect.client.request.AggregateGroupByDurationRequest
import androidx.health.connect.client.request.AggregateGroupByPeriodRequest
import androidx.health.connect.client.request.AggregateRequest
import androidx.health.connect.client.units.BloodGlucose
Expand Down Expand Up @@ -53,10 +55,18 @@ class ReactBloodGlucoseRecord : ReactHealthRecordImpl<BloodGlucoseRecord> {
throw AggregationNotSupported()
}

override fun getAggregateGroupByDurationRequest(record: ReadableMap): AggregateGroupByDurationRequest {
throw AggregationNotSupported()
}

override fun getAggregateGroupByPeriodRequest(record: ReadableMap): AggregateGroupByPeriodRequest {
throw AggregationNotSupported()
}

override fun parseAggregationResultGroupedByDuration(record: List<AggregationResultGroupedByDuration>): WritableNativeArray {
throw AggregationNotSupported()
}

override fun parseAggregationResult(record: AggregationResult): WritableNativeMap {
throw AggregationNotSupported()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package dev.matinzd.healthconnect.records

import androidx.health.connect.client.aggregate.AggregationResult
import androidx.health.connect.client.aggregate.AggregationResultGroupedByDuration
import androidx.health.connect.client.aggregate.AggregationResultGroupedByPeriod
import androidx.health.connect.client.records.BloodPressureRecord
import androidx.health.connect.client.request.AggregateGroupByDurationRequest
import androidx.health.connect.client.request.AggregateGroupByPeriodRequest
import androidx.health.connect.client.request.AggregateRequest
import androidx.health.connect.client.units.Pressure
Expand Down Expand Up @@ -58,6 +60,15 @@ class ReactBloodPressureRecord : ReactHealthRecordImpl<BloodPressureRecord> {
)
}

override fun getAggregateGroupByDurationRequest(record: ReadableMap): AggregateGroupByDurationRequest {
return AggregateGroupByDurationRequest(
metrics = aggregateMetrics,
timeRangeFilter = record.getTimeRangeFilter("timeRangeFilter"),
timeRangeSlicer = mapJsDurationToDuration(record.getMap("timeRangeSlicer")),
dataOriginFilter = convertJsToDataOriginSet(record.getArray("dataOriginFilter"))
)
}

override fun getAggregateGroupByPeriodRequest(record: ReadableMap): AggregateGroupByPeriodRequest {
return AggregateGroupByPeriodRequest(
metrics = aggregateMetrics,
Expand Down Expand Up @@ -94,6 +105,20 @@ class ReactBloodPressureRecord : ReactHealthRecordImpl<BloodPressureRecord> {
}
}

override fun parseAggregationResultGroupedByDuration(record: List<AggregationResultGroupedByDuration>): WritableNativeArray {
return WritableNativeArray().apply {
record.forEach {
val map = WritableNativeMap().apply {
putMap("result", parseAggregationResult(it.result))
putString("startTime", it.startTime.toString())
putString("endTime", it.endTime.toString())
putString("zoneOffset", it.zoneOffset.toString())
}
pushMap(map)
}
}
}

override fun parseAggregationResultGroupedByPeriod(record: List<AggregationResultGroupedByPeriod>): WritableNativeArray {
return WritableNativeArray().apply {
record.forEach {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package dev.matinzd.healthconnect.records

import androidx.health.connect.client.aggregate.AggregationResult
import androidx.health.connect.client.aggregate.AggregationResultGroupedByDuration
import androidx.health.connect.client.aggregate.AggregationResultGroupedByPeriod
import androidx.health.connect.client.records.BodyFatRecord
import androidx.health.connect.client.request.AggregateGroupByDurationRequest
import androidx.health.connect.client.request.AggregateGroupByPeriodRequest
import androidx.health.connect.client.request.AggregateRequest
import androidx.health.connect.client.units.Percentage
Expand Down Expand Up @@ -40,10 +42,18 @@ class ReactBodyFatRecord : ReactHealthRecordImpl<BodyFatRecord> {
throw AggregationNotSupported()
}

override fun getAggregateGroupByDurationRequest(record: ReadableMap): AggregateGroupByDurationRequest {
throw AggregationNotSupported()
}

override fun getAggregateGroupByPeriodRequest(record: ReadableMap): AggregateGroupByPeriodRequest {
throw AggregationNotSupported()
}

override fun parseAggregationResultGroupedByDuration(record: List<AggregationResultGroupedByDuration>): WritableNativeArray {
throw AggregationNotSupported()
}

override fun parseAggregationResultGroupedByPeriod(record: List<AggregationResultGroupedByPeriod>): WritableNativeArray {
throw AggregationNotSupported()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package dev.matinzd.healthconnect.records

import androidx.health.connect.client.aggregate.AggregationResult
import androidx.health.connect.client.aggregate.AggregationResultGroupedByDuration
import androidx.health.connect.client.aggregate.AggregationResultGroupedByPeriod
import androidx.health.connect.client.records.BodyTemperatureMeasurementLocation
import androidx.health.connect.client.records.BodyTemperatureRecord
import androidx.health.connect.client.request.AggregateGroupByDurationRequest
import androidx.health.connect.client.request.AggregateGroupByPeriodRequest
import androidx.health.connect.client.request.AggregateRequest
import androidx.health.connect.client.units.Temperature
Expand Down Expand Up @@ -48,6 +50,10 @@ class ReactBodyTemperatureRecord : ReactHealthRecordImpl<BodyTemperatureRecord>
throw AggregationNotSupported()
}

override fun getAggregateGroupByDurationRequest(record: ReadableMap): AggregateGroupByDurationRequest {
throw AggregationNotSupported()
}

override fun getAggregateGroupByPeriodRequest(record: ReadableMap): AggregateGroupByPeriodRequest {
throw AggregationNotSupported()
}
Expand All @@ -56,6 +62,10 @@ class ReactBodyTemperatureRecord : ReactHealthRecordImpl<BodyTemperatureRecord>
throw AggregationNotSupported()
}

override fun parseAggregationResultGroupedByDuration(record: List<AggregationResultGroupedByDuration>): WritableNativeArray {
throw AggregationNotSupported()
}

override fun parseAggregationResultGroupedByPeriod(record: List<AggregationResultGroupedByPeriod>): WritableNativeArray {
throw AggregationNotSupported()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package dev.matinzd.healthconnect.records

import androidx.health.connect.client.aggregate.AggregationResult
import androidx.health.connect.client.aggregate.AggregationResultGroupedByDuration
import androidx.health.connect.client.aggregate.AggregationResultGroupedByPeriod
import androidx.health.connect.client.records.BodyWaterMassRecord
import androidx.health.connect.client.request.AggregateGroupByDurationRequest
import androidx.health.connect.client.request.AggregateGroupByPeriodRequest
import androidx.health.connect.client.request.AggregateRequest
import com.facebook.react.bridge.ReadableArray
Expand Down Expand Up @@ -36,6 +38,10 @@ class ReactBodyWaterMassRecord : ReactHealthRecordImpl<BodyWaterMassRecord> {
throw AggregationNotSupported()
}

override fun getAggregateGroupByDurationRequest(record: ReadableMap): AggregateGroupByDurationRequest {
throw AggregationNotSupported()
}

override fun getAggregateGroupByPeriodRequest(record: ReadableMap): AggregateGroupByPeriodRequest {
throw AggregationNotSupported()
}
Expand All @@ -44,6 +50,10 @@ class ReactBodyWaterMassRecord : ReactHealthRecordImpl<BodyWaterMassRecord> {
throw AggregationNotSupported()
}

override fun parseAggregationResultGroupedByDuration(record: List<AggregationResultGroupedByDuration>): WritableNativeArray {
throw AggregationNotSupported()
}

override fun parseAggregationResultGroupedByPeriod(record: List<AggregationResultGroupedByPeriod>): WritableNativeArray {
throw AggregationNotSupported()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package dev.matinzd.healthconnect.records

import androidx.health.connect.client.aggregate.AggregationResult
import androidx.health.connect.client.aggregate.AggregationResultGroupedByDuration
import androidx.health.connect.client.aggregate.AggregationResultGroupedByPeriod
import androidx.health.connect.client.records.BoneMassRecord
import androidx.health.connect.client.request.AggregateGroupByDurationRequest
import androidx.health.connect.client.request.AggregateGroupByPeriodRequest
import androidx.health.connect.client.request.AggregateRequest
import com.facebook.react.bridge.ReadableArray
Expand Down Expand Up @@ -41,6 +43,10 @@ class ReactBoneMassRecord : ReactHealthRecordImpl<BoneMassRecord> {
throw AggregationNotSupported()
}

override fun getAggregateGroupByDurationRequest(record: ReadableMap): AggregateGroupByDurationRequest {
throw AggregationNotSupported()
}

override fun getAggregateGroupByPeriodRequest(record: ReadableMap): AggregateGroupByPeriodRequest {
throw AggregationNotSupported()
}
Expand All @@ -49,6 +55,10 @@ class ReactBoneMassRecord : ReactHealthRecordImpl<BoneMassRecord> {
throw AggregationNotSupported()
}

override fun parseAggregationResultGroupedByDuration(record: List<AggregationResultGroupedByDuration>): WritableNativeArray {
throw AggregationNotSupported()
}

override fun parseAggregationResultGroupedByPeriod(record: List<AggregationResultGroupedByPeriod>): WritableNativeArray {
throw AggregationNotSupported()
}
Expand Down
Loading

0 comments on commit 60efc22

Please sign in to comment.