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 Foundation
import SQLite import SQLite
import HealthKit
extension WorkoutActivity { extension WorkoutActivity {
@ -33,7 +34,7 @@ extension WorkoutActivity {
try database.prepare(table).map(from) try database.prepare(table).map(from)
} }
private static func from(row: Row) -> WorkoutActivity { private static func from(row: Row) throws -> WorkoutActivity {
.init( .init(
id: row[columnId], id: row[columnId],
uuid: row[columnUUID], uuid: row[columnUUID],
@ -41,7 +42,7 @@ extension WorkoutActivity {
activityType: .init(rawValue: UInt(row[columnActivityType]))!, activityType: .init(rawValue: UInt(row[columnActivityType]))!,
locationType: .init(rawValue: row[columnLocationType])!, locationType: .init(rawValue: row[columnLocationType])!,
swimmingLocationType: .init(rawValue: row[columnSwimmingLocationType])!, swimmingLocationType: .init(rawValue: row[columnSwimmingLocationType])!,
lapLength: row[columnLapLength], lapLength: try row[columnLapLength].map(lapLength),
startDate: Date(timeIntervalSinceReferenceDate: row[columnStartDate]), startDate: Date(timeIntervalSinceReferenceDate: row[columnStartDate]),
endDate: Date(timeIntervalSinceReferenceDate: row[columnEndDate]), endDate: Date(timeIntervalSinceReferenceDate: row[columnEndDate]),
duration: row[columnDuration], duration: row[columnDuration],
@ -82,12 +83,22 @@ extension WorkoutActivity {
columnActivityType <- Int(element.activityType.rawValue), columnActivityType <- Int(element.activityType.rawValue),
columnLocationType <- element.locationType.rawValue, columnLocationType <- element.locationType.rawValue,
columnSwimmingLocationType <- element.swimmingLocationType.rawValue, columnSwimmingLocationType <- element.swimmingLocationType.rawValue,
columnLapLength <- element.lapLength, columnLapLength <- try element.lapLengthData(),
columnStartDate <- element.startDate.timeIntervalSinceReferenceDate, columnStartDate <- element.startDate.timeIntervalSinceReferenceDate,
columnEndDate <- element.endDate.timeIntervalSinceReferenceDate, columnEndDate <- element.endDate.timeIntervalSinceReferenceDate,
columnDuration <- element.duration, columnDuration <- element.duration,
columnMetadata <- element.metadata) 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 swimmingLocationType: HKWorkoutSwimmingLocationType
let lapLength: Data? let lapLength: HKQuantity?
#warning("Fix timezone for dates") #warning("Fix timezone for dates")
let startDate: Date let startDate: Date
@ -26,7 +26,7 @@ struct WorkoutActivity {
let metadata: Data? 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.id = id
self.uuid = uuid self.uuid = uuid
self.isPrimaryActivity = isPrimaryActivity self.isPrimaryActivity = isPrimaryActivity