Decode lap length

This commit is contained in:
Christoph Hagen 2024-01-31 11:24:23 +01:00
parent 5daf92c5bb
commit 3029d94031
2 changed files with 18 additions and 7 deletions

View File

@ -1,5 +1,6 @@
import Foundation
import SQLite
import HealthKit
extension WorkoutActivity {
@ -33,7 +34,7 @@ extension WorkoutActivity {
try database.prepare(table).map(from)
}
private static func from(row: Row) -> WorkoutActivity {
private static func from(row: Row) throws -> WorkoutActivity {
.init(
id: row[columnId],
uuid: row[columnUUID],
@ -41,7 +42,7 @@ extension WorkoutActivity {
activityType: .init(rawValue: UInt(row[columnActivityType]))!,
locationType: .init(rawValue: row[columnLocationType])!,
swimmingLocationType: .init(rawValue: row[columnSwimmingLocationType])!,
lapLength: row[columnLapLength],
lapLength: try row[columnLapLength].map(lapLength),
startDate: Date(timeIntervalSinceReferenceDate: row[columnStartDate]),
endDate: Date(timeIntervalSinceReferenceDate: row[columnEndDate]),
duration: row[columnDuration],
@ -82,12 +83,22 @@ extension WorkoutActivity {
columnActivityType <- Int(element.activityType.rawValue),
columnLocationType <- element.locationType.rawValue,
columnSwimmingLocationType <- element.swimmingLocationType.rawValue,
columnLapLength <- element.lapLength,
columnLapLength <- try element.lapLengthData(),
columnStartDate <- element.startDate.timeIntervalSinceReferenceDate,
columnEndDate <- element.endDate.timeIntervalSinceReferenceDate,
columnDuration <- element.duration,
columnMetadata <- element.metadata)
)
}
}
private extension WorkoutActivity {
func lapLengthData() throws -> Data? {
try lapLength.map { try NSKeyedArchiver.archivedData(withRootObject: $0, requiringSecureCoding: false) }
}
static func lapLength(from data: Data) throws -> HKQuantity? {
try NSKeyedUnarchiver.unarchivedObject(ofClass: HKQuantity.self, from: data)
}
}

View File

@ -15,7 +15,7 @@ struct WorkoutActivity {
let swimmingLocationType: HKWorkoutSwimmingLocationType
let lapLength: Data?
let lapLength: HKQuantity?
#warning("Fix timezone for dates")
let startDate: Date
@ -26,7 +26,7 @@ struct WorkoutActivity {
let metadata: Data?
init(id: Int, uuid: Data, isPrimaryActivity: Bool, activityType: HKWorkoutActivityType, locationType: HKWorkoutSessionLocationType, swimmingLocationType: HKWorkoutSwimmingLocationType, lapLength: Data?, startDate: Date, endDate: Date, duration: TimeInterval, metadata: Data?) {
init(id: Int, uuid: Data, isPrimaryActivity: Bool, activityType: HKWorkoutActivityType, locationType: HKWorkoutSessionLocationType, swimmingLocationType: HKWorkoutSwimmingLocationType, lapLength: HKQuantity?, startDate: Date, endDate: Date, duration: TimeInterval, metadata: Data?) {
self.id = id
self.uuid = uuid
self.isPrimaryActivity = isPrimaryActivity