Skip to content

Commit

Permalink
Get stored workout types and frequencies
Browse files Browse the repository at this point in the history
  • Loading branch information
christophhagen committed Mar 19, 2024
1 parent bbbf72d commit 50d572b
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions Sources/HKDatabase/Database/HKDatabaseStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,39 @@ public final class HKDatabaseStore {

// MARK: Workouts

/**
Get a list of all workout activity types in the database.
*/
public func storedWorkoutTypes() throws -> [HKWorkoutActivityType] {
let query = workoutActivities.table
.select(workoutActivities.activityType.distinct)
return try database.prepare(query).compactMap { row in
let raw = row[workoutActivities.activityType.distinct]
return HKWorkoutActivityType(rawValue: UInt(raw))
}
}

/**
Get a list of all workout activity types in the database including their count
*/
public func workoutTypeFrequencies() throws -> [HKWorkoutActivityType: Int] {
let query = workoutActivities.table
.select(workoutActivities.activityType)
return try database.prepare(query).reduce(into: [:]) { result, row in
let raw = row[workoutActivities.activityType]
guard raw >= 0 else {
print("Found unknown activity type \(raw)")
return
}
guard let type = HKWorkoutActivityType(rawValue: UInt(raw)) else {
print("Found unknown activity type \(raw)")
return
}
let oldCount = result[type] ?? 0
result[type] = oldCount + 1
}
}

/**
All workouts in the database, regardless of type.
- Parameter start: The start of the date range of interest
Expand Down

0 comments on commit 50d572b

Please sign in to comment.