Skip to content

Make averageHeartRate optional #16

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## [1.7.1] - 27.06.2022.

* Add raw keys for electrocardiogram classification and symptom status

## [1.7.0] - 23.06.2022.

* Make `averageHeartRate` optional as a `HKElectrocardiogram.Classification.inconclusivePoorReading` may not have a value for this field

## [1.6.9] - 27.05.2022.

* Add copyWith methods to Correlation
Expand Down
2 changes: 1 addition & 1 deletion HealthKitReporter.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'HealthKitReporter'
s.version = '1.6.9'
s.version = '1.7.1'
s.summary = 'HealthKitReporter. A wrapper for HealthKit framework.'
s.swift_versions = '5.3'
s.description = 'Helps to write or read data from Apple Health via HealthKit framework.'
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.2
// swift-tools-version:5.3
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
Expand Down
46 changes: 39 additions & 7 deletions Sources/Decorator/Extensions+HKElectrocardiogram.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,8 @@ extension HKElectrocardiogram {

func harmonize(voltageMeasurements: [Electrocardiogram.VoltageMeasurement]) throws -> Harmonized {
let averageHeartRateUnit = HKUnit.count().unitDivided(by: HKUnit.minute())
guard
let averageHeartRate = averageHeartRate?.doubleValue(for: averageHeartRateUnit)
else {
throw HealthKitError.invalidValue(
"Invalid averageHeartRate value for HKElectrocardiogram"
)
}
let averageHeartRate = averageHeartRate?.doubleValue(for: averageHeartRateUnit)

let samplingFrequencyUnit = HKUnit.hertz()
guard
let samplingFrequency = samplingFrequency?.doubleValue(for: samplingFrequencyUnit)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it appears that the samplingFrequency can also be nullable, could please introduce the optionality here the same way you did for the averageHeartRate? For reference

Expand All @@ -34,7 +29,9 @@ extension HKElectrocardiogram {
samplingFrequency: samplingFrequency,
samplingFrequencyUnit: samplingFrequencyUnit.unitString,
classification: classification.description,
classificationKey: classification.key,
symptomsStatus: symptomsStatus.description,
symptomsStatusKey: symptomsStatus.key,
count: numberOfVoltageMeasurements,
voltageMeasurements: voltageMeasurements,
metadata: metadata?.compactMapValues { String(describing: $0 )}
Expand Down Expand Up @@ -62,6 +59,29 @@ extension HKElectrocardiogram.VoltageMeasurement: Harmonizable {
// MARK: - CustomStringConvertible
@available(iOS 14.0, *)
extension HKElectrocardiogram.Classification: CustomStringConvertible {
public var key: String {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather not introduce another field for the JSON payload, but change the values in description here

switch self {
case .notSet:
return "notSet"
case .sinusRhythm:
return "sinusRhythm"
case .atrialFibrillation:
return "atrialFibrillation"
case .inconclusiveLowHeartRate:
return "inconclusiveLowHeartRate"
case .inconclusiveHighHeartRate:
return "inconclusiveHighHeartRate"
case .inconclusivePoorReading:
return "inconclusivePoorReading"
case .inconclusiveOther:
return "inconclusiveOther"
case .unrecognized:
return "unrecognized"
@unknown default:
fatalError()
}
}

public var description: String {
switch self {
case .notSet:
Expand All @@ -88,6 +108,18 @@ extension HKElectrocardiogram.Classification: CustomStringConvertible {
// MARK: - CustomStringConvertible
@available(iOS 14.0, *)
extension HKElectrocardiogram.SymptomsStatus: CustomStringConvertible {
public var key: String {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the same as in the comment

switch self {
case .notSet:
return "notSet"
case .none:
return "none"
case .present:
return "present"
@unknown default:
fatalError()
}
}
public var description: String {
switch self {
case .notSet:
Expand Down
14 changes: 12 additions & 2 deletions Sources/Model/Payload/Electrocardiogram.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,27 @@ import HealthKit
@available(iOS 14.0, *)
public struct Electrocardiogram: Identifiable, Sample {
public struct Harmonized: Codable {
public let averageHeartRate: Double
public let averageHeartRate: Double?
public let averageHeartRateUnit: String
public let samplingFrequency: Double
public let samplingFrequencyUnit: String
public let classification: String
public let classificationKey: String
public let symptomsStatus: String
public let symptomsStatusKey: String
public let count: Int
public let voltageMeasurements: [VoltageMeasurement]
public let metadata: [String: String]?

init(
averageHeartRate: Double,
averageHeartRate: Double?,
averageHeartRateUnit: String,
samplingFrequency: Double,
samplingFrequencyUnit: String,
classification: String,
classificationKey: String,
symptomsStatus: String,
symptomsStatusKey: String,
count: Int,
voltageMeasurements: [VoltageMeasurement],
metadata: [String: String]?
Expand All @@ -36,7 +40,9 @@ public struct Electrocardiogram: Identifiable, Sample {
self.samplingFrequency = samplingFrequency
self.samplingFrequencyUnit = samplingFrequencyUnit
self.classification = classification
self.classificationKey = classificationKey
self.symptomsStatus = symptomsStatus
self.symptomsStatusKey = symptomsStatusKey
self.count = count
self.voltageMeasurements = voltageMeasurements
self.metadata = metadata
Expand Down Expand Up @@ -117,7 +123,9 @@ extension Electrocardiogram.Harmonized: Payload {
let samplingFrequency = dictionary["samplingFrequency"] as? NSNumber,
let samplingFrequencyUnit = dictionary["samplingFrequencyUnit"] as? String,
let classification = dictionary["classification"] as? String,
let classificationKey = dictionary["classificationKey"] as? String,
let symptomsStatus = dictionary["symptomsStatus"] as? String,
let symptomsStatusKey = dictionary["symptomsStatusKey"] as? String,
let count = dictionary["count"] as? Int
else {
throw HealthKitError.invalidValue("Invalid dictionary: \(dictionary)")
Expand All @@ -130,7 +138,9 @@ extension Electrocardiogram.Harmonized: Payload {
samplingFrequency: Double(truncating: samplingFrequency),
samplingFrequencyUnit: samplingFrequencyUnit,
classification: classification,
classificationKey: classificationKey,
symptomsStatus: symptomsStatus,
symptomsStatusKey: symptomsStatusKey,
count: count,
voltageMeasurements: voltageMeasurements != nil
? try Electrocardiogram.VoltageMeasurement.collect(from: voltageMeasurements!)
Expand Down