@solutionforest/android-health-connect 0.0.0-stage → 0.3.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AndroidHealthConnect.podspec +20 -0
- package/CHANGELOG.md +211 -0
- package/LICENSE +20 -0
- package/README.md +849 -2
- package/SECURITY.md +99 -0
- package/android/build.gradle +104 -0
- package/android/src/main/AndroidManifest.xml +4 -0
- package/android/src/main/java/com/androidhealthconnect/AggregateMapper.kt +521 -0
- package/android/src/main/java/com/androidhealthconnect/AndroidHealthConnectModule.kt +637 -0
- package/android/src/main/java/com/androidhealthconnect/AndroidHealthConnectPackage.kt +31 -0
- package/android/src/main/java/com/androidhealthconnect/AvailabilitySupport.kt +149 -0
- package/android/src/main/java/com/androidhealthconnect/ChangesMapper.kt +105 -0
- package/android/src/main/java/com/androidhealthconnect/ExtendedRecordMapper.kt +665 -0
- package/android/src/main/java/com/androidhealthconnect/FeatureSupport.kt +88 -0
- package/android/src/main/java/com/androidhealthconnect/PhrMapper.kt +187 -0
- package/android/src/main/java/com/androidhealthconnect/PlannedExerciseMapper.kt +242 -0
- package/android/src/main/java/com/androidhealthconnect/RecordMapper.kt +1222 -0
- package/app.plugin.js +1 -0
- package/docs/assets/logo.jpg +0 -0
- package/ios/AndroidHealthConnect.h +5 -0
- package/ios/AndroidHealthConnect.mm +21 -0
- package/lib/module/NativeAndroidHealthConnect.js +5 -0
- package/lib/module/NativeAndroidHealthConnect.js.map +1 -0
- package/lib/module/foreground.js +117 -0
- package/lib/module/foreground.js.map +1 -0
- package/lib/module/helpers.js +280 -0
- package/lib/module/helpers.js.map +1 -0
- package/lib/module/index.js +354 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/module/types.js +392 -0
- package/lib/module/types.js.map +1 -0
- package/lib/module/upgrade.js +101 -0
- package/lib/module/upgrade.js.map +1 -0
- package/lib/typescript/package.json +1 -0
- package/lib/typescript/plugin/src/index.d.ts +14 -0
- package/lib/typescript/plugin/src/index.d.ts.map +1 -0
- package/lib/typescript/src/NativeAndroidHealthConnect.d.ts +95 -0
- package/lib/typescript/src/NativeAndroidHealthConnect.d.ts.map +1 -0
- package/lib/typescript/src/foreground.d.ts +43 -0
- package/lib/typescript/src/foreground.d.ts.map +1 -0
- package/lib/typescript/src/helpers.d.ts +108 -0
- package/lib/typescript/src/helpers.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +208 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/lib/typescript/src/types.d.ts +1209 -0
- package/lib/typescript/src/types.d.ts.map +1 -0
- package/lib/typescript/src/upgrade.d.ts +26 -0
- package/lib/typescript/src/upgrade.d.ts.map +1 -0
- package/package.json +255 -4
- package/plugin/build/index.d.ts +13 -0
- package/plugin/build/index.js +57 -0
- package/plugin/src/index.ts +105 -0
- package/src/NativeAndroidHealthConnect.ts +133 -0
- package/src/foreground.ts +167 -0
- package/src/helpers.ts +373 -0
- package/src/index.tsx +623 -0
- package/src/types.ts +1525 -0
- package/src/upgrade.ts +155 -0
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
package com.androidhealthconnect
|
|
2
|
+
|
|
3
|
+
import androidx.health.connect.client.ExperimentalMatchmakingApi
|
|
4
|
+
import androidx.health.connect.client.HealthConnectClient
|
|
5
|
+
import androidx.health.connect.client.HealthConnectFeatures
|
|
6
|
+
import androidx.health.connect.client.feature.ExperimentalPersonalHealthRecordApi
|
|
7
|
+
import androidx.health.connect.client.records.*
|
|
8
|
+
import kotlin.reflect.KClass
|
|
9
|
+
|
|
10
|
+
@OptIn(ExperimentalMatchmakingApi::class, ExperimentalPersonalHealthRecordApi::class)
|
|
11
|
+
internal object FeatureSupport {
|
|
12
|
+
|
|
13
|
+
fun statusToString(status: Int): String =
|
|
14
|
+
when (status) {
|
|
15
|
+
HealthConnectFeatures.FEATURE_STATUS_AVAILABLE -> "available"
|
|
16
|
+
else -> "unavailable"
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
fun featureFromName(name: String): Int =
|
|
20
|
+
when (name) {
|
|
21
|
+
"activityIntensity" -> HealthConnectFeatures.FEATURE_ACTIVITY_INTENSITY
|
|
22
|
+
"matchmaking" -> HealthConnectFeatures.FEATURE_MATCHMAKING
|
|
23
|
+
"mindfulnessSession" -> HealthConnectFeatures.FEATURE_MINDFULNESS_SESSION
|
|
24
|
+
"personalHealthRecord" -> HealthConnectFeatures.FEATURE_PERSONAL_HEALTH_RECORD
|
|
25
|
+
"plannedExercise" -> HealthConnectFeatures.FEATURE_PLANNED_EXERCISE
|
|
26
|
+
"readHealthDataHistory" -> HealthConnectFeatures.FEATURE_READ_HEALTH_DATA_HISTORY
|
|
27
|
+
"readHealthDataInBackground" ->
|
|
28
|
+
HealthConnectFeatures.FEATURE_READ_HEALTH_DATA_IN_BACKGROUND
|
|
29
|
+
"skinTemperature" -> HealthConnectFeatures.FEATURE_SKIN_TEMPERATURE
|
|
30
|
+
"extendedDeviceTypes" -> HealthConnectFeatures.FEATURE_EXTENDED_DEVICE_TYPES
|
|
31
|
+
"exerciseSessionImprovements" ->
|
|
32
|
+
HealthConnectFeatures.FEATURE_EXERCISE_SESSION_IMPROVEMENTS
|
|
33
|
+
else -> throw IllegalArgumentException("Unknown Health Connect feature: $name")
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
fun getStatus(client: HealthConnectClient, featureName: String): String {
|
|
37
|
+
val feature = featureFromName(featureName)
|
|
38
|
+
return statusToString(client.features.getFeatureStatus(feature))
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Maps JS recordType strings to HC Record KClasses for MatchmakingRequest. */
|
|
42
|
+
fun recordClassForType(recordType: String): KClass<out Record> =
|
|
43
|
+
when (recordType) {
|
|
44
|
+
"Steps" -> StepsRecord::class
|
|
45
|
+
"HeartRate" -> HeartRateRecord::class
|
|
46
|
+
"SleepSession" -> SleepSessionRecord::class
|
|
47
|
+
"ActiveCaloriesBurned" -> ActiveCaloriesBurnedRecord::class
|
|
48
|
+
"TotalCaloriesBurned" -> TotalCaloriesBurnedRecord::class
|
|
49
|
+
"Distance" -> DistanceRecord::class
|
|
50
|
+
"Weight" -> WeightRecord::class
|
|
51
|
+
"Height" -> HeightRecord::class
|
|
52
|
+
"BloodPressure" -> BloodPressureRecord::class
|
|
53
|
+
"BloodGlucose" -> BloodGlucoseRecord::class
|
|
54
|
+
"OxygenSaturation" -> OxygenSaturationRecord::class
|
|
55
|
+
"BodyTemperature" -> BodyTemperatureRecord::class
|
|
56
|
+
"BodyFat" -> BodyFatRecord::class
|
|
57
|
+
"Hydration" -> HydrationRecord::class
|
|
58
|
+
"ExerciseSession" -> ExerciseSessionRecord::class
|
|
59
|
+
"RestingHeartRate" -> RestingHeartRateRecord::class
|
|
60
|
+
"RespiratoryRate" -> RespiratoryRateRecord::class
|
|
61
|
+
"FloorsClimbed" -> FloorsClimbedRecord::class
|
|
62
|
+
"ElevationGained" -> ElevationGainedRecord::class
|
|
63
|
+
"MindfulnessSession" -> MindfulnessSessionRecord::class
|
|
64
|
+
"SkinTemperature" -> SkinTemperatureRecord::class
|
|
65
|
+
"ActivityIntensity" -> ActivityIntensityRecord::class
|
|
66
|
+
"PlannedExerciseSession" -> PlannedExerciseSessionRecord::class
|
|
67
|
+
"Nutrition" -> NutritionRecord::class
|
|
68
|
+
"Vo2Max" -> Vo2MaxRecord::class
|
|
69
|
+
"Speed" -> SpeedRecord::class
|
|
70
|
+
"Power" -> PowerRecord::class
|
|
71
|
+
"StepsCadence" -> StepsCadenceRecord::class
|
|
72
|
+
"CyclingPedalingCadence" -> CyclingPedalingCadenceRecord::class
|
|
73
|
+
"WheelchairPushes" -> WheelchairPushesRecord::class
|
|
74
|
+
"MenstruationFlow" -> MenstruationFlowRecord::class
|
|
75
|
+
"MenstruationPeriod" -> MenstruationPeriodRecord::class
|
|
76
|
+
"OvulationTest" -> OvulationTestRecord::class
|
|
77
|
+
"CervicalMucus" -> CervicalMucusRecord::class
|
|
78
|
+
"IntermenstrualBleeding" -> IntermenstrualBleedingRecord::class
|
|
79
|
+
"SexualActivity" -> SexualActivityRecord::class
|
|
80
|
+
"BasalBodyTemperature" -> BasalBodyTemperatureRecord::class
|
|
81
|
+
"LeanBodyMass" -> LeanBodyMassRecord::class
|
|
82
|
+
"BoneMass" -> BoneMassRecord::class
|
|
83
|
+
"BodyWaterMass" -> BodyWaterMassRecord::class
|
|
84
|
+
"BasalMetabolicRate" -> BasalMetabolicRateRecord::class
|
|
85
|
+
"HeartRateVariabilityRmssd" -> HeartRateVariabilityRmssdRecord::class
|
|
86
|
+
else -> throw IllegalArgumentException("Unsupported record type: $recordType")
|
|
87
|
+
}
|
|
88
|
+
}
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
package com.androidhealthconnect
|
|
2
|
+
|
|
3
|
+
import android.net.Uri
|
|
4
|
+
import androidx.health.connect.client.HealthConnectClient
|
|
5
|
+
import androidx.health.connect.client.feature.ExperimentalPersonalHealthRecordApi
|
|
6
|
+
import androidx.health.connect.client.records.FhirResource
|
|
7
|
+
import androidx.health.connect.client.records.FhirVersion
|
|
8
|
+
import androidx.health.connect.client.records.MedicalDataSource
|
|
9
|
+
import androidx.health.connect.client.records.MedicalResource
|
|
10
|
+
import androidx.health.connect.client.records.MedicalResourceId
|
|
11
|
+
import androidx.health.connect.client.request.CreateMedicalDataSourceRequest
|
|
12
|
+
import androidx.health.connect.client.request.DeleteMedicalResourcesRequest
|
|
13
|
+
import androidx.health.connect.client.request.GetMedicalDataSourcesRequest
|
|
14
|
+
import androidx.health.connect.client.request.ReadMedicalResourcesInitialRequest
|
|
15
|
+
import androidx.health.connect.client.request.ReadMedicalResourcesPageRequest
|
|
16
|
+
import androidx.health.connect.client.request.UpsertMedicalResourceRequest
|
|
17
|
+
import org.json.JSONArray
|
|
18
|
+
import org.json.JSONObject
|
|
19
|
+
|
|
20
|
+
@OptIn(ExperimentalPersonalHealthRecordApi::class)
|
|
21
|
+
internal object PhrMapper {
|
|
22
|
+
|
|
23
|
+
fun fhirVersionFromJson(json: JSONObject): FhirVersion =
|
|
24
|
+
FhirVersion(
|
|
25
|
+
json.getInt("major"),
|
|
26
|
+
json.getInt("minor"),
|
|
27
|
+
json.getInt("patch"),
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
fun fhirVersionToJson(version: FhirVersion): JSONObject =
|
|
31
|
+
JSONObject().apply {
|
|
32
|
+
put("major", version.major)
|
|
33
|
+
put("minor", version.minor)
|
|
34
|
+
put("patch", version.patch)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
fun medicalDataSourceToJson(source: MedicalDataSource): JSONObject =
|
|
38
|
+
JSONObject().apply {
|
|
39
|
+
put("id", source.id)
|
|
40
|
+
put("displayName", source.displayName)
|
|
41
|
+
put("fhirBaseUri", source.fhirBaseUri.toString())
|
|
42
|
+
put("fhirVersion", fhirVersionToJson(source.fhirVersion))
|
|
43
|
+
put("packageName", source.packageName)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
fun medicalResourceIdToJson(id: MedicalResourceId): JSONObject =
|
|
47
|
+
JSONObject().apply {
|
|
48
|
+
put("dataSourceId", id.dataSourceId)
|
|
49
|
+
put("fhirResourceType", id.fhirResourceType)
|
|
50
|
+
put("fhirResourceId", id.fhirResourceId)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
fun medicalResourceIdFromJson(json: JSONObject): MedicalResourceId =
|
|
54
|
+
MedicalResourceId(
|
|
55
|
+
dataSourceId = json.getString("dataSourceId"),
|
|
56
|
+
fhirResourceType = json.getInt("fhirResourceType"),
|
|
57
|
+
fhirResourceId = json.getString("fhirResourceId"),
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
fun fhirResourceToJson(resource: FhirResource): JSONObject =
|
|
61
|
+
JSONObject().apply {
|
|
62
|
+
put("type", resource.type)
|
|
63
|
+
put("id", resource.id)
|
|
64
|
+
put("data", resource.data)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
fun medicalResourceToJson(resource: MedicalResource): JSONObject =
|
|
68
|
+
JSONObject().apply {
|
|
69
|
+
put("type", resource.type)
|
|
70
|
+
put("id", medicalResourceIdToJson(resource.id))
|
|
71
|
+
put("dataSourceId", resource.dataSourceId)
|
|
72
|
+
put("fhirVersion", fhirVersionToJson(resource.fhirVersion))
|
|
73
|
+
put("fhirResource", fhirResourceToJson(resource.fhirResource))
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
fun createRequestFromJson(json: JSONObject): CreateMedicalDataSourceRequest =
|
|
77
|
+
CreateMedicalDataSourceRequest(
|
|
78
|
+
fhirBaseUri = Uri.parse(json.getString("fhirBaseUri")),
|
|
79
|
+
displayName = json.getString("displayName"),
|
|
80
|
+
fhirVersion = fhirVersionFromJson(json.getJSONObject("fhirVersion")),
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
fun upsertRequestsFromJson(arr: JSONArray): List<UpsertMedicalResourceRequest> =
|
|
84
|
+
(0 until arr.length()).map { i ->
|
|
85
|
+
val j = arr.getJSONObject(i)
|
|
86
|
+
UpsertMedicalResourceRequest(
|
|
87
|
+
dataSourceId = j.getString("dataSourceId"),
|
|
88
|
+
fhirVersion = fhirVersionFromJson(j.getJSONObject("fhirVersion")),
|
|
89
|
+
data = j.getString("data"),
|
|
90
|
+
)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
suspend fun getMedicalDataSources(
|
|
94
|
+
client: HealthConnectClient,
|
|
95
|
+
options: JSONObject,
|
|
96
|
+
): JSONArray {
|
|
97
|
+
val idsArr = options.optJSONArray("medicalDataSourceIds")
|
|
98
|
+
val sources =
|
|
99
|
+
if (idsArr != null && idsArr.length() > 0) {
|
|
100
|
+
val ids = (0 until idsArr.length()).map { idsArr.getString(it) }
|
|
101
|
+
client.getMedicalDataSources(ids)
|
|
102
|
+
} else {
|
|
103
|
+
val packagesArr = options.optJSONArray("packageNames")
|
|
104
|
+
val packageNames =
|
|
105
|
+
if (packagesArr != null) {
|
|
106
|
+
(0 until packagesArr.length()).map { packagesArr.getString(it) }
|
|
107
|
+
} else {
|
|
108
|
+
emptyList()
|
|
109
|
+
}
|
|
110
|
+
client.getMedicalDataSources(GetMedicalDataSourcesRequest(packageNames))
|
|
111
|
+
}
|
|
112
|
+
return JSONArray().apply {
|
|
113
|
+
sources.forEach { put(medicalDataSourceToJson(it)) }
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
suspend fun readMedicalResources(
|
|
118
|
+
client: HealthConnectClient,
|
|
119
|
+
options: JSONObject,
|
|
120
|
+
): JSONObject {
|
|
121
|
+
val pageToken = options.optString("pageToken", "").ifEmpty { null }
|
|
122
|
+
val pageSize = options.optInt("pageSize", 100)
|
|
123
|
+
val request =
|
|
124
|
+
if (pageToken != null) {
|
|
125
|
+
ReadMedicalResourcesPageRequest(pageToken = pageToken, pageSize = pageSize)
|
|
126
|
+
} else {
|
|
127
|
+
val type = options.getInt("medicalResourceType")
|
|
128
|
+
val sourceIdsArr = options.optJSONArray("medicalDataSourceIds")
|
|
129
|
+
val sourceIds =
|
|
130
|
+
if (sourceIdsArr != null) {
|
|
131
|
+
(0 until sourceIdsArr.length()).map { sourceIdsArr.getString(it) }.toSet()
|
|
132
|
+
} else {
|
|
133
|
+
emptySet()
|
|
134
|
+
}
|
|
135
|
+
ReadMedicalResourcesInitialRequest(
|
|
136
|
+
medicalResourceType = type,
|
|
137
|
+
medicalDataSourceIds = sourceIds,
|
|
138
|
+
pageSize = pageSize,
|
|
139
|
+
)
|
|
140
|
+
}
|
|
141
|
+
val response = client.readMedicalResources(request)
|
|
142
|
+
return JSONObject().apply {
|
|
143
|
+
put(
|
|
144
|
+
"medicalResources",
|
|
145
|
+
JSONArray().apply {
|
|
146
|
+
response.medicalResources.forEach { put(medicalResourceToJson(it)) }
|
|
147
|
+
},
|
|
148
|
+
)
|
|
149
|
+
put("nextPageToken", response.nextPageToken ?: JSONObject.NULL)
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
suspend fun deleteMedicalResources(
|
|
154
|
+
client: HealthConnectClient,
|
|
155
|
+
options: JSONObject,
|
|
156
|
+
) {
|
|
157
|
+
val idsArr = options.optJSONArray("ids")
|
|
158
|
+
if (idsArr != null && idsArr.length() > 0) {
|
|
159
|
+
val ids =
|
|
160
|
+
(0 until idsArr.length()).map {
|
|
161
|
+
medicalResourceIdFromJson(idsArr.getJSONObject(it))
|
|
162
|
+
}
|
|
163
|
+
client.deleteMedicalResources(ids)
|
|
164
|
+
return
|
|
165
|
+
}
|
|
166
|
+
val dataSourceIdsArr = options.optJSONArray("dataSourceIds")
|
|
167
|
+
val typesArr = options.optJSONArray("medicalResourceTypes")
|
|
168
|
+
val dataSourceIds =
|
|
169
|
+
if (dataSourceIdsArr != null) {
|
|
170
|
+
(0 until dataSourceIdsArr.length()).map { dataSourceIdsArr.getString(it) }.toSet()
|
|
171
|
+
} else {
|
|
172
|
+
emptySet()
|
|
173
|
+
}
|
|
174
|
+
val types =
|
|
175
|
+
if (typesArr != null) {
|
|
176
|
+
(0 until typesArr.length()).map { typesArr.getInt(it) }.toSet()
|
|
177
|
+
} else {
|
|
178
|
+
emptySet()
|
|
179
|
+
}
|
|
180
|
+
client.deleteMedicalResources(
|
|
181
|
+
DeleteMedicalResourcesRequest(
|
|
182
|
+
dataSourceIds = dataSourceIds,
|
|
183
|
+
medicalResourceTypes = types,
|
|
184
|
+
)
|
|
185
|
+
)
|
|
186
|
+
}
|
|
187
|
+
}
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
package com.androidhealthconnect
|
|
2
|
+
|
|
3
|
+
import androidx.health.connect.client.records.ExerciseCompletionGoal
|
|
4
|
+
import androidx.health.connect.client.records.ExercisePerformanceTarget
|
|
5
|
+
import androidx.health.connect.client.records.PlannedExerciseBlock
|
|
6
|
+
import androidx.health.connect.client.records.PlannedExerciseSessionRecord
|
|
7
|
+
import androidx.health.connect.client.records.PlannedExerciseStep
|
|
8
|
+
import androidx.health.connect.client.units.Energy
|
|
9
|
+
import androidx.health.connect.client.units.Length
|
|
10
|
+
import androidx.health.connect.client.units.Mass
|
|
11
|
+
import androidx.health.connect.client.units.Power
|
|
12
|
+
import androidx.health.connect.client.units.Velocity
|
|
13
|
+
import org.json.JSONArray
|
|
14
|
+
import org.json.JSONObject
|
|
15
|
+
import java.time.Duration
|
|
16
|
+
import java.time.Instant
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* JSON ↔ [PlannedExerciseSessionRecord] (FEATURE_PLANNED_EXERCISE).
|
|
20
|
+
* Completion goals / performance targets use a `{ type: ... }` discriminant.
|
|
21
|
+
*/
|
|
22
|
+
internal object PlannedExerciseMapper {
|
|
23
|
+
|
|
24
|
+
fun toJson(r: PlannedExerciseSessionRecord): JSONObject =
|
|
25
|
+
JSONObject().apply {
|
|
26
|
+
put("recordType", "PlannedExerciseSession")
|
|
27
|
+
put("exerciseType", r.exerciseType)
|
|
28
|
+
put("startTime", r.startTime.toString())
|
|
29
|
+
put("endTime", r.endTime.toString())
|
|
30
|
+
put("title", r.title ?: JSONObject.NULL)
|
|
31
|
+
put("notes", r.notes ?: JSONObject.NULL)
|
|
32
|
+
put("hasExplicitTime", r.hasExplicitTime)
|
|
33
|
+
put(
|
|
34
|
+
"completedExerciseSessionId",
|
|
35
|
+
r.completedExerciseSessionId ?: JSONObject.NULL
|
|
36
|
+
)
|
|
37
|
+
val blocks = JSONArray()
|
|
38
|
+
r.blocks.forEach { blocks.put(blockToJson(it)) }
|
|
39
|
+
put("blocks", blocks)
|
|
40
|
+
r.startZoneOffset?.let { put("startZoneOffset", it.toString()) }
|
|
41
|
+
r.endZoneOffset?.let { put("endZoneOffset", it.toString()) }
|
|
42
|
+
put("metadata", RecordMapper.metadataToJson(r.metadata))
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
fun fromJson(j: JSONObject): PlannedExerciseSessionRecord {
|
|
46
|
+
val blocksArr = j.optJSONArray("blocks")
|
|
47
|
+
val blocks =
|
|
48
|
+
if (blocksArr != null) {
|
|
49
|
+
(0 until blocksArr.length()).map { i ->
|
|
50
|
+
blockFromJson(blocksArr.getJSONObject(i))
|
|
51
|
+
}
|
|
52
|
+
} else {
|
|
53
|
+
emptyList()
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return PlannedExerciseSessionRecord(
|
|
57
|
+
startTime = Instant.parse(j.getString("startTime")),
|
|
58
|
+
startZoneOffset = RecordMapper.parseZoneOffset(j.optString("startZoneOffset")),
|
|
59
|
+
endTime = Instant.parse(j.getString("endTime")),
|
|
60
|
+
endZoneOffset = RecordMapper.parseZoneOffset(j.optString("endZoneOffset")),
|
|
61
|
+
metadata = RecordMapper.jsonToMetadata(j.optJSONObject("metadata")),
|
|
62
|
+
blocks = blocks,
|
|
63
|
+
exerciseType = j.getInt("exerciseType"),
|
|
64
|
+
title = j.optString("title").ifEmpty { null },
|
|
65
|
+
notes = j.optString("notes").ifEmpty { null },
|
|
66
|
+
)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
private fun blockToJson(b: PlannedExerciseBlock): JSONObject =
|
|
70
|
+
JSONObject().apply {
|
|
71
|
+
put("repetitions", b.repetitions)
|
|
72
|
+
put("description", b.description ?: JSONObject.NULL)
|
|
73
|
+
val steps = JSONArray()
|
|
74
|
+
b.steps.forEach { steps.put(stepToJson(it)) }
|
|
75
|
+
put("steps", steps)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
private fun blockFromJson(j: JSONObject): PlannedExerciseBlock {
|
|
79
|
+
val stepsArr = j.getJSONArray("steps")
|
|
80
|
+
val steps =
|
|
81
|
+
(0 until stepsArr.length()).map { i ->
|
|
82
|
+
stepFromJson(stepsArr.getJSONObject(i))
|
|
83
|
+
}
|
|
84
|
+
return PlannedExerciseBlock(
|
|
85
|
+
repetitions = j.getInt("repetitions"),
|
|
86
|
+
steps = steps,
|
|
87
|
+
description = j.optString("description").ifEmpty { null },
|
|
88
|
+
)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
private fun stepToJson(s: PlannedExerciseStep): JSONObject =
|
|
92
|
+
JSONObject().apply {
|
|
93
|
+
put("exerciseType", s.exerciseType)
|
|
94
|
+
put("exercisePhase", s.exercisePhase)
|
|
95
|
+
put("description", s.description ?: JSONObject.NULL)
|
|
96
|
+
put("completionGoal", completionGoalToJson(s.completionGoal))
|
|
97
|
+
val targets = JSONArray()
|
|
98
|
+
s.performanceTargets.forEach { targets.put(performanceTargetToJson(it)) }
|
|
99
|
+
put("performanceTargets", targets)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
private fun stepFromJson(j: JSONObject): PlannedExerciseStep {
|
|
103
|
+
val targetsArr = j.optJSONArray("performanceTargets")
|
|
104
|
+
val targets =
|
|
105
|
+
if (targetsArr != null) {
|
|
106
|
+
(0 until targetsArr.length()).map { i ->
|
|
107
|
+
performanceTargetFromJson(targetsArr.getJSONObject(i))
|
|
108
|
+
}
|
|
109
|
+
} else {
|
|
110
|
+
emptyList()
|
|
111
|
+
}
|
|
112
|
+
return PlannedExerciseStep(
|
|
113
|
+
exerciseType = j.getInt("exerciseType"),
|
|
114
|
+
exercisePhase =
|
|
115
|
+
j.optInt("exercisePhase", PlannedExerciseStep.EXERCISE_PHASE_ACTIVE),
|
|
116
|
+
completionGoal = completionGoalFromJson(j.getJSONObject("completionGoal")),
|
|
117
|
+
performanceTargets = targets,
|
|
118
|
+
description = j.optString("description").ifEmpty { null },
|
|
119
|
+
)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
private fun completionGoalToJson(goal: ExerciseCompletionGoal): JSONObject =
|
|
123
|
+
when (goal) {
|
|
124
|
+
is ExerciseCompletionGoal.DistanceGoal ->
|
|
125
|
+
JSONObject()
|
|
126
|
+
.put("type", "Distance")
|
|
127
|
+
.put("distance", goal.distance.inMeters)
|
|
128
|
+
is ExerciseCompletionGoal.DurationGoal ->
|
|
129
|
+
JSONObject()
|
|
130
|
+
.put("type", "Duration")
|
|
131
|
+
.put("durationMillis", goal.duration.toMillis())
|
|
132
|
+
is ExerciseCompletionGoal.RepetitionsGoal ->
|
|
133
|
+
JSONObject()
|
|
134
|
+
.put("type", "Repetitions")
|
|
135
|
+
.put("repetitions", goal.repetitions)
|
|
136
|
+
is ExerciseCompletionGoal.StepsGoal ->
|
|
137
|
+
JSONObject().put("type", "Steps").put("steps", goal.steps)
|
|
138
|
+
is ExerciseCompletionGoal.ActiveCaloriesBurnedGoal ->
|
|
139
|
+
JSONObject()
|
|
140
|
+
.put("type", "ActiveCaloriesBurned")
|
|
141
|
+
.put("energy", goal.activeCalories.inKilocalories)
|
|
142
|
+
is ExerciseCompletionGoal.TotalCaloriesBurnedGoal ->
|
|
143
|
+
JSONObject()
|
|
144
|
+
.put("type", "TotalCaloriesBurned")
|
|
145
|
+
.put("energy", goal.totalCalories.inKilocalories)
|
|
146
|
+
is ExerciseCompletionGoal.DistanceAndDurationGoal ->
|
|
147
|
+
JSONObject()
|
|
148
|
+
.put("type", "DistanceAndDuration")
|
|
149
|
+
.put("distance", goal.distance.inMeters)
|
|
150
|
+
.put("durationMillis", goal.duration.toMillis())
|
|
151
|
+
is ExerciseCompletionGoal.ManualCompletion ->
|
|
152
|
+
JSONObject().put("type", "ManualCompletion")
|
|
153
|
+
else -> JSONObject().put("type", "Unknown")
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
private fun completionGoalFromJson(j: JSONObject): ExerciseCompletionGoal =
|
|
157
|
+
when (j.getString("type")) {
|
|
158
|
+
"Distance" ->
|
|
159
|
+
ExerciseCompletionGoal.DistanceGoal(Length.meters(j.getDouble("distance")))
|
|
160
|
+
"Duration" ->
|
|
161
|
+
ExerciseCompletionGoal.DurationGoal(Duration.ofMillis(j.getLong("durationMillis")))
|
|
162
|
+
"Repetitions" ->
|
|
163
|
+
ExerciseCompletionGoal.RepetitionsGoal(j.getInt("repetitions"))
|
|
164
|
+
"Steps" -> ExerciseCompletionGoal.StepsGoal(j.getInt("steps"))
|
|
165
|
+
"ActiveCaloriesBurned" ->
|
|
166
|
+
ExerciseCompletionGoal.ActiveCaloriesBurnedGoal(
|
|
167
|
+
Energy.kilocalories(j.getDouble("energy"))
|
|
168
|
+
)
|
|
169
|
+
"TotalCaloriesBurned" ->
|
|
170
|
+
ExerciseCompletionGoal.TotalCaloriesBurnedGoal(
|
|
171
|
+
Energy.kilocalories(j.getDouble("energy"))
|
|
172
|
+
)
|
|
173
|
+
"DistanceAndDuration" ->
|
|
174
|
+
ExerciseCompletionGoal.DistanceAndDurationGoal(
|
|
175
|
+
Length.meters(j.getDouble("distance")),
|
|
176
|
+
Duration.ofMillis(j.getLong("durationMillis")),
|
|
177
|
+
)
|
|
178
|
+
"ManualCompletion" -> ExerciseCompletionGoal.ManualCompletion
|
|
179
|
+
else -> ExerciseCompletionGoal.UnknownGoal
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
private fun performanceTargetToJson(t: ExercisePerformanceTarget): JSONObject =
|
|
183
|
+
when (t) {
|
|
184
|
+
is ExercisePerformanceTarget.HeartRateTarget ->
|
|
185
|
+
JSONObject()
|
|
186
|
+
.put("type", "HeartRate")
|
|
187
|
+
.put("minHeartRate", t.minHeartRate)
|
|
188
|
+
.put("maxHeartRate", t.maxHeartRate)
|
|
189
|
+
is ExercisePerformanceTarget.SpeedTarget ->
|
|
190
|
+
JSONObject()
|
|
191
|
+
.put("type", "Speed")
|
|
192
|
+
.put("minMetersPerSecond", t.minSpeed.inMetersPerSecond)
|
|
193
|
+
.put("maxMetersPerSecond", t.maxSpeed.inMetersPerSecond)
|
|
194
|
+
is ExercisePerformanceTarget.PowerTarget ->
|
|
195
|
+
JSONObject()
|
|
196
|
+
.put("type", "Power")
|
|
197
|
+
.put("minWatts", t.minPower.inWatts)
|
|
198
|
+
.put("maxWatts", t.maxPower.inWatts)
|
|
199
|
+
is ExercisePerformanceTarget.CadenceTarget ->
|
|
200
|
+
JSONObject()
|
|
201
|
+
.put("type", "Cadence")
|
|
202
|
+
.put("minRpm", t.minCadence)
|
|
203
|
+
.put("maxRpm", t.maxCadence)
|
|
204
|
+
is ExercisePerformanceTarget.WeightTarget ->
|
|
205
|
+
JSONObject().put("type", "Weight").put("mass", t.mass.inKilograms)
|
|
206
|
+
is ExercisePerformanceTarget.RateOfPerceivedExertionTarget ->
|
|
207
|
+
JSONObject().put("type", "RateOfPerceivedExertion").put("rpe", t.rpe)
|
|
208
|
+
is ExercisePerformanceTarget.AmrapTarget ->
|
|
209
|
+
JSONObject().put("type", "Amrap")
|
|
210
|
+
else -> JSONObject().put("type", "Unknown")
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
private fun performanceTargetFromJson(j: JSONObject): ExercisePerformanceTarget =
|
|
214
|
+
when (j.getString("type")) {
|
|
215
|
+
"HeartRate" ->
|
|
216
|
+
ExercisePerformanceTarget.HeartRateTarget(
|
|
217
|
+
j.getDouble("minHeartRate"),
|
|
218
|
+
j.getDouble("maxHeartRate"),
|
|
219
|
+
)
|
|
220
|
+
"Speed" ->
|
|
221
|
+
ExercisePerformanceTarget.SpeedTarget(
|
|
222
|
+
Velocity.metersPerSecond(j.getDouble("minMetersPerSecond")),
|
|
223
|
+
Velocity.metersPerSecond(j.getDouble("maxMetersPerSecond")),
|
|
224
|
+
)
|
|
225
|
+
"Power" ->
|
|
226
|
+
ExercisePerformanceTarget.PowerTarget(
|
|
227
|
+
Power.watts(j.getDouble("minWatts")),
|
|
228
|
+
Power.watts(j.getDouble("maxWatts")),
|
|
229
|
+
)
|
|
230
|
+
"Cadence" ->
|
|
231
|
+
ExercisePerformanceTarget.CadenceTarget(
|
|
232
|
+
j.getDouble("minRpm"),
|
|
233
|
+
j.getDouble("maxRpm"),
|
|
234
|
+
)
|
|
235
|
+
"Weight" ->
|
|
236
|
+
ExercisePerformanceTarget.WeightTarget(Mass.kilograms(j.getDouble("mass")))
|
|
237
|
+
"RateOfPerceivedExertion" ->
|
|
238
|
+
ExercisePerformanceTarget.RateOfPerceivedExertionTarget(j.getInt("rpe"))
|
|
239
|
+
"Amrap" -> ExercisePerformanceTarget.AmrapTarget
|
|
240
|
+
else -> ExercisePerformanceTarget.UnknownTarget
|
|
241
|
+
}
|
|
242
|
+
}
|