diff --git a/HealthImport/Model/WorkoutActivity+SQLite.swift b/HealthImport/Model/WorkoutActivity+SQLite.swift index 8f3e3b8..28fffc3 100644 --- a/HealthImport/Model/WorkoutActivity+SQLite.swift +++ b/HealthImport/Model/WorkoutActivity+SQLite.swift @@ -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) + } } diff --git a/HealthImport/Model/WorkoutActivity.swift b/HealthImport/Model/WorkoutActivity.swift index 6623431..37be2af 100644 --- a/HealthImport/Model/WorkoutActivity.swift +++ b/HealthImport/Model/WorkoutActivity.swift @@ -15,8 +15,8 @@ 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