Add PromiseKit dependency

- Added PromiseKit dependency
This commit is contained in:
2018-11-15 22:08:00 -04:00
parent 2689d86c18
commit be7b6b5881
541 changed files with 46282 additions and 0 deletions

View File

@@ -0,0 +1,71 @@
import HealthKit
#if !PMKCocoaPods
import PromiseKit
#endif
public extension HKHealthStore {
func requestAuthorization(toShare typesToShare: Set<HKSampleType>?, read typesToRead: Set<HKObjectType>?) -> Promise<Bool> {
return Promise { seal in
requestAuthorization(toShare: typesToShare, read: typesToRead, completion: seal.resolve)
}
}
#if os(iOS)
func enableBackgroundDelivery(for type: HKObjectType, frequency: HKUpdateFrequency) -> Promise<Bool> {
return Promise { seal in
enableBackgroundDelivery(for: type, frequency: frequency, withCompletion: seal.resolve)
}
}
#endif
}
public extension HKStatisticsQuery {
static func promise(quantityType: HKQuantityType, quantitySamplePredicate: NSPredicate? = nil, options: HKStatisticsOptions = [], healthStore: HKHealthStore = .init()) -> Promise<HKStatistics> {
return Promise { seal in
let query = HKStatisticsQuery(quantityType: quantityType, quantitySamplePredicate: quantitySamplePredicate, options: options) {
seal.resolve($1, $2)
}
healthStore.execute(query)
}
}
}
public extension HKAnchoredObjectQuery {
static func promise(type: HKSampleType, predicate: NSPredicate? = nil, anchor: HKQueryAnchor? = nil, limit: Int = HKObjectQueryNoLimit, healthStore: HKHealthStore = .init()) -> Promise<([HKSample], [HKDeletedObject], HKQueryAnchor)> {
return Promise { seal in
let query = HKAnchoredObjectQuery(type: type, predicate: predicate, anchor: anchor, limit: limit) {
if let a = $1, let b = $2, let c = $3 {
seal.fulfill((a, b, c))
} else if let e = $4 {
seal.reject(e)
} else {
seal.reject(PMKError.invalidCallingConvention)
}
}
healthStore.execute(query)
}
}
}
public extension HKStatisticsCollectionQuery {
func promise(healthStore: HKHealthStore = .init()) -> Promise<HKStatisticsCollection> {
return Promise { seal in
initialResultsHandler = {
seal.resolve($1, $2)
}
healthStore.execute(self)
}
}
}
public extension HKSampleQuery {
static func promise(sampleType: HKSampleType, predicate: NSPredicate? = nil, limit: Int = HKObjectQueryNoLimit, sortDescriptors: [NSSortDescriptor]? = nil, healthStore: HKHealthStore = .init()) -> Promise<[HKSample]> {
return Promise { seal in
let query = HKSampleQuery(sampleType: sampleType, predicate: predicate, limit: limit, sortDescriptors: sortDescriptors) {
seal.resolve($1, $2)
}
healthStore.execute(query)
}
}
}