Skip to content

Commit

Permalink
ECG with voltage measurements
Browse files Browse the repository at this point in the history
  • Loading branch information
kvs-coder committed Apr 18, 2022
1 parent ee2005b commit a7e099a
Show file tree
Hide file tree
Showing 16 changed files with 371 additions and 165 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [1.6.4] - 17.04.2022.

* ECG with Voltage measurements in one query

## [1.6.3] - 16.04.2022.

* add from dictionary static factory for WorkoutRoute
Expand Down
6 changes: 3 additions & 3 deletions Example/HealthKitReporter.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
607FACEB1AFB9204008FA782 /* Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Tests.swift; sourceTree = "<group>"; };
7003F1C7C75718FEDF8637AF /* Pods-HealthKitReporter_Tests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-HealthKitReporter_Tests.release.xcconfig"; path = "Target Support Files/Pods-HealthKitReporter_Tests/Pods-HealthKitReporter_Tests.release.xcconfig"; sourceTree = "<group>"; };
784CB4452F8A563A8EC84C82 /* README.md */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = net.daringfireball.markdown; name = README.md; path = ../README.md; sourceTree = "<group>"; };
8274ECC55EA5E895980E76E1 /* HealthKitReporter.podspec */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text; name = HealthKitReporter.podspec; path = ../HealthKitReporter.podspec; sourceTree = "<group>"; };
8274ECC55EA5E895980E76E1 /* HealthKitReporter.podspec */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text; name = HealthKitReporter.podspec; path = ../HealthKitReporter.podspec; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.ruby; };
9E025C132111D8252E831EEC /* Pods_HealthKitReporter_Example.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_HealthKitReporter_Example.framework; sourceTree = BUILT_PRODUCTS_DIR; };
E99BA72C15D1884C0B1E5444 /* LICENSE */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text; name = LICENSE; path = ../LICENSE; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -474,7 +474,7 @@
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
MARKETING_VERSION = 1.6.3;
MARKETING_VERSION = 1.6.4;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
Expand Down Expand Up @@ -525,7 +525,7 @@
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
MARKETING_VERSION = 1.6.3;
MARKETING_VERSION = 1.6.4;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
Expand Down
54 changes: 44 additions & 10 deletions Example/HealthKitReporter/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,21 @@ class ViewController: UIViewController {
@IBOutlet weak var writeButton: UIButton!

private var reporter: HealthKitReporter?
private let typesToRead: [ObjectType] = [
QuantityType.stepCount,
QuantityType.heartRate,
CategoryType.sleepAnalysis,
QuantityType.heartRateVariabilitySDNN,
SeriesType.heartbeatSeries,
WorkoutType.workoutType,
SeriesType.workoutRoute
]
private var typesToRead: [ObjectType] {
var types: [ObjectType] = [
QuantityType.stepCount,
QuantityType.heartRate,
CategoryType.sleepAnalysis,
QuantityType.heartRateVariabilitySDNN,
SeriesType.heartbeatSeries,
WorkoutType.workoutType,
SeriesType.workoutRoute,
]
if #available(iOS 14.0, *) {
types.append(ElectrocardiogramType.electrocardiogramType)
}
return types
}
private let typesToWrite: [QuantityType] = [
.stepCount
]
Expand Down Expand Up @@ -97,7 +103,35 @@ class ViewController: UIViewController {
}
}
@IBAction func seriesButtonTapped(_ sender: UIButton) {
readRoutes()
readEcgs()
}

private func readEcgs() {
let manager = reporter?.manager
let reader = reporter?.reader
do {
if #available(iOS 14.0, *) {
if let seriesQuery = try reader?.electrocardiogramQuery(
predicate: predicate,
resultsHandler: { samples, error in
if error == nil {
do {
print(try samples.encoded())
} catch {
print(error)
}
} else {
print(error ?? "error")
}
}) {
manager?.executeQuery(seriesQuery)
}
} else {
print("ecg is not available")
}
} catch {
print(error)
}
}

private func readHrSeries() {
Expand Down
6 changes: 3 additions & 3 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
PODS:
- HealthKitReporter (1.6.3)
- HealthKitReporter/Tests (1.6.3)
- HealthKitReporter (1.6.4)
- HealthKitReporter/Tests (1.6.4)

DEPENDENCIES:
- HealthKitReporter (from `../`)
Expand All @@ -11,7 +11,7 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
HealthKitReporter: 42ebaa50989c4e4f6171b2caa3388c53519bea33
HealthKitReporter: 313a822918d705beb27bdeeb5794c1afa4fe88bb

PODFILE CHECKSUM: 42dc208a4788fd9b7d8eebf0c62dd84606fddf4d

Expand Down
4 changes: 2 additions & 2 deletions Example/Pods/Local Podspecs/HealthKitReporter.podspec.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Example/Pods/Manifest.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.3'
s.version = '1.6.4'
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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ pod 'HealthKitReporter'
or

```ruby
pod 'HealthKitReporter', '~> 1.6.3'
pod 'HealthKitReporter', '~> 1.6.4'
```

### Swift Package Manager
Expand All @@ -315,7 +315,7 @@ To install it, simply add the following lines to your Package.swift file

```swift
dependencies: [
.package(url: "https://github.com/VictorKachalov/HealthKitReporter.git", from: "1.6.3")
.package(url: "https://github.com/VictorKachalov/HealthKitReporter.git", from: "1.6.4")
]
```

Expand All @@ -324,7 +324,7 @@ dependencies: [
Add the line in your cartfile

```ruby
github "VictorKachalov/HealthKitReporter" "1.6.3"
github "VictorKachalov/HealthKitReporter" "1.6.4"
```

## Author
Expand Down
54 changes: 48 additions & 6 deletions Sources/Decorator/Extensions+HKElectrocardiogram.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import HealthKit

@available(iOS 14.0, *)
extension HKElectrocardiogram: Harmonizable {
extension HKElectrocardiogram {
typealias Harmonized = Electrocardiogram.Harmonized

func harmonize() throws -> Harmonized {
func harmonize(voltageMeasurements: [Electrocardiogram.VoltageMeasurement]) throws -> Harmonized {
let averageHeartRateUnit = HKUnit.count().unitDivided(by: HKUnit.minute())
guard
let averageHeartRate = averageHeartRate?.doubleValue(for: averageHeartRateUnit)
Expand All @@ -28,15 +28,15 @@ extension HKElectrocardiogram: Harmonizable {
"Invalid samplingFrequency value for HKElectrocardiogram"
)
}
let classification = String(describing: self.classification)
let symptomsStatus = String(describing: self.symptomsStatus)
return Harmonized(
averageHeartRate: averageHeartRate,
averageHeartRateUnit: averageHeartRateUnit.unitString,
samplingFrequency: samplingFrequency,
samplingFrequencyUnit: samplingFrequencyUnit.unitString,
classification: classification,
symptomsStatus: symptomsStatus,
classification: classification.description,
symptomsStatus: symptomsStatus.description,
count: numberOfVoltageMeasurements,
voltageMeasurements: voltageMeasurements,
metadata: metadata?.compactMapValues { String(describing: $0 )}
)
}
Expand All @@ -59,3 +59,45 @@ extension HKElectrocardiogram.VoltageMeasurement: Harmonizable {
return Harmonized(value: voltage, unit: unit.unitString)
}
}
// MARK: - CustomStringConvertible
@available(iOS 14.0, *)
extension HKElectrocardiogram.Classification: CustomStringConvertible {
public var description: String {
switch self {
case .notSet:
return "na"
case .sinusRhythm:
return "Sinus rhytm"
case .atrialFibrillation:
return "Atrial fibrillation"
case .inconclusiveLowHeartRate:
return "Inconclusive low heart rate"
case .inconclusiveHighHeartRate:
return "Inconclusive high heart rate"
case .inconclusivePoorReading:
return "Inconclusive poor reading"
case .inconclusiveOther:
return "Inconclusive other"
case .unrecognized:
return "Unrecognized"
@unknown default:
fatalError()
}
}
}
// MARK: - CustomStringConvertible
@available(iOS 14.0, *)
extension HKElectrocardiogram.SymptomsStatus: CustomStringConvertible {
public var description: String {
switch self {
case .notSet:
return "na"
case .none:
return "None"
case .present:
return "Present"
@unknown default:
fatalError()
}
}
}
2 changes: 1 addition & 1 deletion Sources/Decorator/Extensions+HKSample.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ extension HKSample {
}
if #available(iOS 14.0, *) {
if let electrocardiogram = self as? HKElectrocardiogram {
return try Electrocardiogram(electrocardiogram: electrocardiogram)
return try Electrocardiogram(electrocardiogram: electrocardiogram, voltageMeasurements: [])
}
}
throw HealthKitError.parsingFailed("HKSample could not be parsed")
Expand Down
10 changes: 2 additions & 8 deletions Sources/HealthKitReporter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -183,25 +183,19 @@ public typealias PreferredUnitsCompeltion = (
) -> Void
/**
- Parameters:
- samples: electrocardiogram sample array. Empty by default
- ecgs: electrocardiogram sample array
- error: error (optional)
*/
@available(iOS 14.0, *)
public typealias ElectrocardiogramResultsHandler = (
_ samples: [Electrocardiogram],
_ ecgs: [Electrocardiogram],
_ error: Error?
) -> Void
/**
- Parameters:
- samples: electrocardiogram voltage measurements sample array. Empty by default
- error: error (optional)
*/
@available(iOS 14.0, *)
public typealias ElectrocardiogramVoltageMeasurementDataHandler = (
_ measurement: Electrocardiogram.VoltageMeasurement?,
_ done: Bool,
_ error: Error?
) -> Void

/// **HealthKitReporter** class for HK easy integration
public class HealthKitReporter {
Expand Down
Loading

0 comments on commit a7e099a

Please sign in to comment.