From 5ec72e4c4eb6b36d6eead593243c0fab249cca9c Mon Sep 17 00:00:00 2001 From: matinzd <24797481+matinzd@users.noreply.github.com> Date: Tue, 23 Apr 2024 14:36:51 +0200 Subject: [PATCH] fix: empty exercise route #85 --- .../records/ReactExerciseSessionRecord.kt | 35 ++++++++++++------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/android/src/main/java/dev/matinzd/healthconnect/records/ReactExerciseSessionRecord.kt b/android/src/main/java/dev/matinzd/healthconnect/records/ReactExerciseSessionRecord.kt index 266d55f..39a1d42 100644 --- a/android/src/main/java/dev/matinzd/healthconnect/records/ReactExerciseSessionRecord.kt +++ b/android/src/main/java/dev/matinzd/healthconnect/records/ReactExerciseSessionRecord.kt @@ -18,6 +18,17 @@ import java.time.Instant class ReactExerciseSessionRecord : ReactHealthRecordImpl { override fun parseWriteRecord(records: ReadableArray): List { return records.toMapList().map { + val routeList = it.getMap("exerciseRoute")?.getArray("route")?.toMapList()?.map { sample -> + ExerciseRoute.Location( + time = Instant.parse(sample.getString("time")), + latitude = sample.getDouble("latitude"), + longitude = sample.getDouble("longitude"), + horizontalAccuracy = getLengthFromJsMap(sample.getMap("horizontalAccuracy")), + verticalAccuracy = getLengthFromJsMap(sample.getMap("verticalAccuracy")), + altitude = getLengthFromJsMap(sample.getMap("altitude")), + ) + } ?: emptyList() + ExerciseSessionRecord( startTime = Instant.parse(it.getString("startTime")), endTime = Instant.parse(it.getString("endTime")), @@ -45,18 +56,13 @@ class ReactExerciseSessionRecord : ReactHealthRecordImpl repetitions = sample.getSafeInt("repetitions", 0), ) } ?: emptyList(), - exerciseRoute = ExerciseRoute( - route = it.getMap("exerciseRoute")?.getArray("route")?.toMapList()?.map { sample -> - ExerciseRoute.Location( - time = Instant.parse(sample.getString("time")), - latitude = sample.getDouble("latitude"), - longitude = sample.getDouble("longitude"), - horizontalAccuracy = getLengthFromJsMap(sample.getMap("horizontalAccuracy")), - verticalAccuracy = getLengthFromJsMap(sample.getMap("verticalAccuracy")), - altitude = getLengthFromJsMap(sample.getMap("altitude")), - ) - } ?: emptyList(), - ) + exerciseRoute = if (routeList.isNotEmpty()) { + ExerciseRoute( + route = routeList, + ) + } else { + null + }, ) } } @@ -89,7 +95,7 @@ class ReactExerciseSessionRecord : ReactHealthRecordImpl }) - when(record.exerciseRouteResult) { + when (record.exerciseRouteResult) { is ExerciseRouteResult.Data -> { val exerciseRouteMap = WritableNativeMap() exerciseRouteMap.putArray("route", WritableNativeArray().apply { @@ -106,12 +112,15 @@ class ReactExerciseSessionRecord : ReactHealthRecordImpl }) putMap("exerciseRoute", exerciseRouteMap) } + is ExerciseRouteResult.NoData -> { putMap("exerciseRoute", WritableNativeMap()) } + is ExerciseRouteResult.ConsentRequired -> { throw Exception("Consent required") } + else -> { putMap("exerciseRoute", WritableNativeMap()) }