Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HealthKitConfig.metrics #527

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions BeeKit/GoalExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ extension Goal {
if self.autodata == "ifttt" { return "IFTTT" }
if self.autodata == "api" { return "API" }
if self.autodata == "apple" {
let metric = HealthKitConfig.shared.metrics.first(where: { (metric) -> Bool in
metric.databaseString == self.healthKitMetric
})
let metric = HealthKitConfig.metrics.first(where: { $0.databaseString == self.healthKitMetric })
return self.healthKitMetric == nil ? "Apple" : metric?.humanText
}
if let autodata = self.autodata, autodata.count > 0 { return autodata.capitalized }
Expand Down
15 changes: 5 additions & 10 deletions BeeKit/HeathKit/HealthKitConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@
import Foundation
import HealthKit


public class HealthKitConfig : NSObject {
public static let shared = HealthKitConfig()

public let metrics : [HealthKitMetric] = {
var allMetrics : [HealthKitMetric] = [
public enum HealthKitConfig {
public static var metrics: [HealthKitMetric] {
[
// Activity
QuantityHealthKitMetric(humanText: "Active energy", databaseString: "activeEnergy", category: .Activity, hkQuantityTypeIdentifier: .activeEnergyBurned, precision: [HKUnit.largeCalorie(): 0]),
QuantityHealthKitMetric(humanText: "Cycling distance", databaseString: "cyclingDistance", category: .Activity, hkQuantityTypeIdentifier: .distanceCycling),
Expand Down Expand Up @@ -57,11 +54,9 @@ public class HealthKitConfig : NSObject {
// Sleep
TimeInBedHealthKitMetric(humanText: "Time in bed", databaseString: "timeInBed", category: .Sleep),
TimeAsleepHealthKitMetric(humanText: "Time asleep", databaseString: "timeAsleep", category: .Sleep),

// Other
QuantityHealthKitMetric(humanText: "Time in Daylight", databaseString: "timeInDaylight", category: .Other, hkQuantityTypeIdentifier: .timeInDaylight),
]

return allMetrics
}()
}
}
12 changes: 6 additions & 6 deletions BeeKit/Managers/HealthStoreManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,9 @@ public actor HealthStoreManager {

for metricName in metricNames {
if monitors[metricName] == nil {
guard let metric = HealthKitConfig.shared.metrics.first(where: { (metric) -> Bool in
metric.databaseString == metricName
}) else {
guard
let metric = HealthKitConfig.metrics.first(where: { $0.databaseString == metricName })
else {
logger.error("No metric found for \(metricName, privacy: .public)")
continue
}
Expand Down Expand Up @@ -195,9 +195,9 @@ public actor HealthStoreManager {
}

private func updateWithRecentData(goal: Goal, days: Int) async throws {
guard let metric = HealthKitConfig.shared.metrics.first(where: { (metric) -> Bool in
metric.databaseString == goal.healthKitMetric
}) else {
guard
let metric = HealthKitConfig.metrics.first(where: { $0.databaseString == goal.healthKitMetric })
else {
throw HealthKitError("No metric found for goal \(goal.slug) with metric \(goal.healthKitMetric ?? "nil")")
}
let newDataPoints = try await metric.recentDataPoints(days: days, deadline: goal.deadline, healthStore: healthStore)
Expand Down
4 changes: 1 addition & 3 deletions BeeSwift/Settings/ChooseHKMetricViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ class ChooseHKMetricViewController: UIViewController {

extension ChooseHKMetricViewController : UITableViewDelegate, UITableViewDataSource {
var sortedHKMetrics: [HealthKitMetric] {
HealthKitConfig.shared.metrics.sorted { (lhs, rhs) -> Bool in
lhs.humanText < rhs.humanText
}
HealthKitConfig.metrics.sorted(using: SortDescriptor(\.humanText))
}

var sortedMetricsByCategory: Dictionary<HealthKitCategory, [HealthKitMetric]> {
Expand Down
Loading