From d6f3fd04a40184b6b2ad0b648162d3ba33563498 Mon Sep 17 00:00:00 2001 From: Louis Lagrange Date: Thu, 1 Aug 2024 18:04:46 +0200 Subject: [PATCH] refactor: create and use zoneOffsetToJsMap util --- .../records/ReactExerciseSessionRecord.kt | 17 ++--------------- .../healthconnect/utils/HealthConnectUtils.kt | 12 ++++++++++++ 2 files changed, 14 insertions(+), 15 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 10c21a2..21d7407 100644 --- a/android/src/main/java/dev/matinzd/healthconnect/records/ReactExerciseSessionRecord.kt +++ b/android/src/main/java/dev/matinzd/healthconnect/records/ReactExerciseSessionRecord.kt @@ -70,25 +70,12 @@ class ReactExerciseSessionRecord : ReactHealthRecordImpl override fun parseRecord(record: ExerciseSessionRecord): WritableNativeMap { return WritableNativeMap().apply { putString("startTime", record.startTime.toString()) + putMap("startZoneOffset", zoneOffsetToJsMap(record.startZoneOffset)) putString("endTime", record.endTime.toString()) + putMap("endZoneOffset", zoneOffsetToJsMap(record.endZoneOffset)) putString("notes", record.notes) putString("title", record.title) putInt("exerciseType", record.exerciseType) - - if (record.startZoneOffset != null) { - putMap("startZoneOffset", WritableNativeMap().apply { - putString("id", record.startZoneOffset!!.id) - putInt("totalSeconds", record.startZoneOffset!!.totalSeconds) - }) - } - - if (record.endZoneOffset != null) { - putMap("endZoneOffset", WritableNativeMap().apply { - putString("id", record.endZoneOffset!!.id) - putInt("totalSeconds", record.endZoneOffset!!.totalSeconds) - }) - } - putArray("laps", WritableNativeArray().apply { record.laps.map { val map = WritableNativeMap() diff --git a/android/src/main/java/dev/matinzd/healthconnect/utils/HealthConnectUtils.kt b/android/src/main/java/dev/matinzd/healthconnect/utils/HealthConnectUtils.kt index f87b3b7..29a976f 100644 --- a/android/src/main/java/dev/matinzd/healthconnect/utils/HealthConnectUtils.kt +++ b/android/src/main/java/dev/matinzd/healthconnect/utils/HealthConnectUtils.kt @@ -12,6 +12,7 @@ import com.facebook.react.bridge.WritableNativeArray import com.facebook.react.bridge.WritableNativeMap import dev.matinzd.healthconnect.records.* import java.time.Instant +import java.time.ZoneOffset import kotlin.reflect.KClass fun convertReactRequestOptionsFromJS( @@ -258,6 +259,17 @@ fun lengthToJsMap(length: Length?): WritableNativeMap { } } +fun zoneOffsetToJsMap(zoneOffset: ZoneOffset?): WritableNativeMap? { + if (zoneOffset == null) { + return null + } + + return WritableNativeMap().apply { + putString("id", zoneOffset.id) + putInt("totalSeconds", zoneOffset.totalSeconds) + } +} + fun getVolumeFromJsMap(volume: ReadableMap?): Volume { if (volume == null) { throw InvalidLength()