Skip to content

Commit

Permalink
Conform workout to sample protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
christophhagen committed Mar 22, 2024
1 parent 08af720 commit 4684ddf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 37 deletions.
6 changes: 4 additions & 2 deletions Sources/HKDatabase/Database/HKDatabaseStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -809,8 +809,10 @@ public final class HKDatabaseStore {
try workoutActivities.insert(activity, isPrimaryActivity: false, dataId: dataId, in: database)
}

for (key, value) in workout.metadata {
try insert(value, for: key, of: dataId)
if let metadata = workout.metadata {
for (key, value) in metadata {
try insert(value, for: key, of: dataId)
}
}
}

Expand Down
53 changes: 18 additions & 35 deletions Sources/HKDatabase/Model/Workouts/Workout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ private let df: DateFormatter = {
return df
}()

public struct Workout {
public struct Workout: HKSampleProtocol {

public let dataId: Int

Expand All @@ -19,46 +19,29 @@ public struct Workout {

public let goal: HKQuantity?

public let workoutEvents: [HKWorkoutEvent]

public let workoutActivities: [HKWorkoutActivity]

/// Information about how and when the workout samples were condensed to quantity series
public let condensed: CondenserInfo?

// MARK: HKSampleProtocol

public let startDate: Date

public let endDate: Date

public let device: HKDevice?

public let metadata: [String : Any]
public var sampleType: HKSampleType { .workoutType() }

public let uuid: UUID
// MARK: HKObjectProtocol

public let workoutEvents: [HKWorkoutEvent]
public let device: HKDevice?

public let workoutActivities: [HKWorkoutActivity]
public let metadata: [String : Any]?

/// Information about how and when the workout samples were condensed to quantity series
public let condensed: CondenserInfo?
public let uuid: UUID

var firstActivityDate: Date? {
workoutActivities.map { $0.startDate }.min()
}

var firstEventDate: Date? {
workoutEvents.map { $0.dateInterval.start }.min()
}

var firstAvailableDate: Date? {
[firstEventDate, firstActivityDate].compactMap { $0 }.min()
}

var dateString: String {
guard let firstAvailableDate else {
return "No date"
}
return df.string(from: firstAvailableDate)
}

var typeString: String {
workoutActivities.first?.workoutConfiguration.activityType.description ?? "Unknown activity"
}

public init(dataId: Int, startDate: Date, endDate: Date, totalDistance: Double? = nil, goalType: Int? = nil, goal: Double? = nil, events: [HKWorkoutEvent] = [], activities: [HKWorkoutActivity] = [], condensed: CondenserInfo? = nil, uuid: UUID? = nil, metadata: [String : Any] = [:], device: HKDevice? = nil) {
self.dataId = dataId
self.startDate = startDate
Expand Down Expand Up @@ -93,7 +76,7 @@ public struct Workout {
samples: samples,
route: route)
}
let metadata = metadata.removingPrivateFields()
let metadata = metadata?.removingPrivateFields()
let events = workoutEvents.map {
HKWorkoutEvent(type: $0.type, dateInterval: $0.dateInterval, metadata: $0.metadata?.removingPrivateFields())
}
Expand All @@ -114,7 +97,7 @@ public struct Workout {
route: route)
}

private func insert(into store: HKHealthStore, activities: [HKWorkoutActivity], events: [HKWorkoutEvent], metadata: [String : Any], samples: [HKSample], route: [CLLocation]) async throws -> HKWorkout {
private func insert(into store: HKHealthStore, activities: [HKWorkoutActivity], events: [HKWorkoutEvent], metadata: [String : Any]?, samples: [HKSample], route: [CLLocation]) async throws -> HKWorkout {
guard let configuration = activities.first?.workoutConfiguration else {
throw WorkoutInsertionError.noWorkoutActivity
}
Expand All @@ -128,7 +111,7 @@ public struct Workout {
if !events.isEmpty {
try await builder.addWorkoutEvents(events)
}
if !metadata.isEmpty {
if let metadata, !metadata.isEmpty {
try await builder.addMetadata(metadata)
}
if !samples.isEmpty {
Expand Down

0 comments on commit 4684ddf

Please sign in to comment.