Skip to content

Commit

Permalink
Revisions for async/await
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewTurk247 committed Feb 12, 2024
1 parent 177cdfb commit 0a4d901
Showing 1 changed file with 61 additions and 64 deletions.
125 changes: 61 additions & 64 deletions PAWS/ECGRecordings/ECGRecording.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@

import HealthKit
import SwiftUI
import Foundation


struct ECGRecording: View {
let hkElectrocardiogram: HKElectrocardiogram
@State var symptoms: HKElectrocardiogram.Symptoms = [:]
@State var precedingPulseRates: [HKSample] = []


var body: some View {
PAWSCard {
Expand All @@ -31,24 +30,13 @@ struct ECGRecording: View {
Text("Recorded \(symptoms.count) symptoms.")
}
}

ForEach(precedingPulseRates) { sample in
//Text(sample.sampleType.identifier)
if let sample = sample as? HKQuantitySample {
Text("\(60 * sample.quantity.doubleValue(for: .hertz())) beats per minute")
}
}
}
.task {
guard let symptoms = try? await hkElectrocardiogram.symptoms(from: HKHealthStore()) else {
return
}

self.symptoms = symptoms
hkElectrocardiogram.precedingPulseRates { samples in
print(samples)
precedingPulseRates = samples
}
}
}
}
Expand All @@ -62,74 +50,83 @@ extension HKElectrocardiogram {
HKQuery.predicateForSamples(withStart: self.fiveMinutesBefore, end: self.startDate, options: .strictStartDate)
}

Check warning on line 51 in PAWS/ECGRecordings/ECGRecording.swift

View check run for this annotation

Codecov / codecov/patch

PAWS/ECGRecordings/ECGRecording.swift#L49-L51

Added lines #L49 - L51 were not covered by tests

/// To actually get the heart rate (BPM) from one of the samples:
/// ```
/// let heartRate = sample.quantity.doubleValue(for: HKUnit(from: "count/min"))
/// ```
func precedingPulseRates(completion: @escaping (_ samples: [HKSample]) -> Void) {
precedingSamples(forType: HKQuantityType(.heartRate)) { samples in
completion(samples)
var precedingPulseRates: [HKSample] {
get async throws {
try await precedingSamples(forType: HKQuantityType(.heartRate))
}

Check warning on line 56 in PAWS/ECGRecordings/ECGRecording.swift

View check run for this annotation

Codecov / codecov/patch

PAWS/ECGRecordings/ECGRecording.swift#L54-L56

Added lines #L54 - L56 were not covered by tests
}

// var precedingVo2Max: HKSample? {
// precedingSamples(forType: HKQuantityType(.vo2Max), limit: 1, ascending: false).first
// }
//
// var precedingPhysicalEffort: HKQuantity? {
// fiveMinuteSum(forType: HKQuantityType(.physicalEffort))
// }
//
// var precedingStepCount: HKQuantity? {
// fiveMinuteSum(forType: HKQuantityType(.stepCount))
// }
//
// var precedingActiveEnergy: HKQuantity? {
// fiveMinuteSum(forType: HKQuantityType(.activeEnergyBurned))
// }
var precedingVo2Max: HKSample? {
get async {
try? await precedingSamples(forType: HKQuantityType(.vo2Max), limit: 1, ascending: false).first
}

Check warning on line 62 in PAWS/ECGRecordings/ECGRecording.swift

View check run for this annotation

Codecov / codecov/patch

PAWS/ECGRecordings/ECGRecording.swift#L60-L62

Added lines #L60 - L62 were not covered by tests
}

var precedingPhysicalEffort: HKQuantity? {
get async {
try? await fiveMinuteSum(forType: HKQuantityType(.physicalEffort))
}

Check warning on line 68 in PAWS/ECGRecordings/ECGRecording.swift

View check run for this annotation

Codecov / codecov/patch

PAWS/ECGRecordings/ECGRecording.swift#L66-L68

Added lines #L66 - L68 were not covered by tests
}

var precedingStepCount: HKQuantity? {
get async {
try? await fiveMinuteSum(forType: HKQuantityType(.stepCount))
}

Check warning on line 74 in PAWS/ECGRecordings/ECGRecording.swift

View check run for this annotation

Codecov / codecov/patch

PAWS/ECGRecordings/ECGRecording.swift#L72-L74

Added lines #L72 - L74 were not covered by tests
}

private func precedingSamples(
var precedingActiveEnergy: HKQuantity? {
get async {
try? await fiveMinuteSum(forType: HKQuantityType(.activeEnergyBurned))
}

Check warning on line 80 in PAWS/ECGRecordings/ECGRecording.swift

View check run for this annotation

Codecov / codecov/patch

PAWS/ECGRecordings/ECGRecording.swift#L78-L80

Added lines #L78 - L80 were not covered by tests
}

/// To actually get the heart rate (BPM) from one of the samples:
/// ```
/// let heartRate = sample.quantity.doubleValue(for: HKUnit(from: "count/min"))
/// ```
func precedingSamples(
forType type: HKSampleType,
limit: Int? = nil,
ascending: Bool = true,
completion: @escaping (_ samples: [HKSample]) -> Void
) -> [HKSample] {
ascending: Bool = true
) async throws -> [HKSample] {
let healthStore = HKHealthStore()
let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierEndDate, ascending: ascending)
let queryLimit = limit ?? HKObjectQueryNoLimit
var result: [HKSample] = []

let query = HKSampleQuery(
sampleType: type,
predicate: self.fiveMinutePredicate,
limit: queryLimit,
sortDescriptors: [sortDescriptor]
) { _, samples, error in
if error == nil, let samples {
completion(samples)
return try await withCheckedThrowingContinuation { continuation in
let query = HKSampleQuery(
sampleType: type,
predicate: self.fiveMinutePredicate,
limit: queryLimit,
sortDescriptors: [sortDescriptor]
) { _, samples, error in
if let error {
continuation.resume(throwing: error)
} else {
continuation.resume(returning: samples ?? [])
}
}

healthStore.execute(query)
}

healthStore.execute(query)

return result
}

Check warning on line 112 in PAWS/ECGRecordings/ECGRecording.swift

View check run for this annotation

Codecov / codecov/patch

PAWS/ECGRecordings/ECGRecording.swift#L91-L112

Added lines #L91 - L112 were not covered by tests

private func fiveMinuteSum(forType type: HKQuantityType) -> HKQuantity? {
private func fiveMinuteSum(forType type: HKQuantityType) async throws -> HKQuantity? {
let healthStore = HKHealthStore()
var result: HKQuantity?

let query = HKStatisticsQuery(
quantityType: type,
quantitySamplePredicate: self.fiveMinutePredicate
) { _, statistics, error in
if error != nil {
result = statistics?.sumQuantity()
return try await withCheckedThrowingContinuation { continuation in
let query = HKStatisticsQuery(
quantityType: type,
quantitySamplePredicate: self.fiveMinutePredicate
) { _, statistics, error in
if let error {
continuation.resume(throwing: error)
} else {
continuation.resume(returning: statistics?.sumQuantity())
}
}

healthStore.execute(query)
}

healthStore.execute(query)

return result
}

Check warning on line 131 in PAWS/ECGRecordings/ECGRecording.swift

View check run for this annotation

Codecov / codecov/patch

PAWS/ECGRecordings/ECGRecording.swift#L114-L131

Added lines #L114 - L131 were not covered by tests
}

0 comments on commit 0a4d901

Please sign in to comment.