The code sample defines a DailyQueryBuilder struct that implements the HealthStoreQueryBuildable protocol. It sets properties like quantityType, anchorDate, and intervalComponents. The builder can then be used to construct an HKStatisticsCollectionQuery to query health data from the HealthKit store.
22. import UIKit
import HealthStore
class ViewController: UIViewController {
private let healthStore = HealthStore()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
}
22
23. guard let healthStore = healthStore else { return }
guard let type = HKQuantityType.quantityType(forIdentifier: .stepCount) else { return }
let builder = DailyQueryBuilder(quantityType: type)
healthStore.requestSources(from: Date.startOfToday(), to: Date(), builder: builder) { (sources: [StepSource]?) in
if let sources = sources {
print("sources: (sources)”)
} else {
print("step sources are not found.”)
}
}
23
24. guard let healthStore = healthStore else { return }
guard let type = HKQuantityType.quantityType(forIdentifier: .stepCount) else { return }
let builder = DailyQueryBuilder(quantityType: type)
healthStore.requestSources(from: Date.startOfToday(), to: Date(), builder: builder) { (sources: [StepSource]?) in
if let sources = sources {
print("sources: (sources)”)
} else {
print("step sources are not found.”)
}
}
24
25. guard let healthStore = healthStore else { return }
guard let type = HKQuantityType.quantityType(forIdentifier: .stepCount) else { return }
let builder = DailyQueryBuilder(quantityType: type)
healthStore.requestSources(to: Date(), builder: builder) { (sources: [StepSource]?) in
if let sources = sources {
print("sources: (sources)”)
} else {
print("step sources are not found.”)
}
}
25
26. guard let healthStore = healthStore else { return }
guard let type = HKQuantityType.quantityType(forIdentifier: .stepCount) else { return }
let builder = DailyQueryBuilder(quantityType: type)
healthStore.observeSourceUpdates(from: Date(), builder: builder, handler: { (sources: [StepSource]?) in
if let sources = sources {
print("sources: (sources)")
} else {
print("step sources are not found.")
}
})
26
27. guard let healthStore = healthStore else { return }
guard let type = HKQuantityType.quantityType(forIdentifier: .stepCount) else { return }
let builder = RestrictedSourceQueryBuilder(quantityType: type)
healthStore.requestSources(from: Date.startOfToday(), to: Date(), builder: builder) { (sources: [StepSource]?) in
if let sources = sources {
print("sources: (sources)”)
} else {
print("step sources are not found.”)
}
}
27
29. import HealthKit
public protocol HealthStoreQueryBuildable {
var quantityType: HKQuantityType { get }
var typesToWrite: Set<HKSampleType>? { get }
var typesToRead: Set<HKObjectType>? { get }
var options: HKStatisticsOptions { get }
var anchorDate: Date { get }
var intervalComponents: DateComponents { get }
var predicates: [NSPredicate] { get }
func build() -> HKStatisticsCollectionQuery?
func build(completion: @escaping (HKStatisticsCollectionQuery?) -> Void)
}
29
31. public struct DailyQueryBuilder: HealthStoreQueryBuildable {
public let quantityType: HKQuantityType
public var typesToRead: Set<HKObjectType>? {
return [quantityType]
}
public let options: HKStatisticsOptions = .cumulativeSum
public let anchorDate: Date
public let intervalComponents: DateComponents = DateComponents(day: 1)
public let predicates: [NSPredicate] = []
public init(quantityType: HKQuantityType, anchorDate: Date = Date.startOfToday()) {
self.quantityType = quantityType
self.anchorDate = anchorDate
}
}
31
32. public class RestrictedSourceQueryBuilder: HealthStoreQueryBuildable {
public func build(completion: @escaping (HKStatisticsCollectionQuery?) -> Void) {
guard let provider = HealthSourceProvider() else {
completion(nil)
return
}
provider.appleSources(sampleType: quantityType) { (sources, error) in
if let error = error {
print(error)
}
guard let sources = sources else {
completion(nil)
return
}
// update predicates
let metadataPredicate = HKQuery.predicateForObjects(withMetadataKey: HKMetadataKeyWasUserEntered,
operatorType: .notEqualTo,
value: true)
let sourcePredicate = HKQuery.predicateForObjects(from: sources)
self.predicates = [metadataPredicate, sourcePredicate]
// build query
completion(self.build())
}
}
}
32