@@ -15,6 +15,9 @@ import com.rafapps.taskerhealthconnect.Serializer
1515import com.rafapps.taskerhealthconnect.healthConnectRecordsPackage
1616import kotlinx.coroutines.runBlocking
1717import java.time.Instant
18+ import kotlin.reflect.full.companionObject
19+ import kotlin.reflect.full.companionObjectInstance
20+ import kotlin.reflect.full.memberProperties
1821
1922class ReadAggregatedDataActionRunner (
2023 private val repositoryProvider : HealthConnectRepositoryProvider = { HealthConnectRepository (it) },
@@ -37,9 +40,11 @@ class ReadAggregatedDataActionRunner(
3740 val startTime = Instant .ofEpochMilli(input.regular.startTime.toLong())
3841 val endTime = Instant .ofEpochMilli(input.regular.endTime.toLong())
3942 val split = input.regular.aggregateMetric.split(" ." )
40- val clazz = Class .forName(" $healthConnectRecordsPackage .${split[0 ]} " )
41- val field = clazz.getField(split[1 ])
42- val metric = field.get(null ) as AggregateMetric <* >
43+ val kClass = Class .forName(" $healthConnectRecordsPackage .${split[0 ]} " ).kotlin
44+ val companionObject = kClass.companionObject
45+ val companionInstance = kClass.companionObjectInstance
46+ val property = companionObject?.memberProperties?.find { it.name == split[1 ] }
47+ val metric = property?.call(companionInstance) as AggregateMetric <* >
4348 val data = runBlocking { repository.readAggregatedData(metric, startTime, endTime) }
4449 val serializedResult = serializer.toString(data)
4550 Log .d(tag, " runner complete, result size: ${serializedResult.length} " )
0 commit comments