Skip to content

Commit

Permalink
๐Ÿ’‰:: [#28] ๊ฑธ์Œ ์ˆ˜ ์—…๋ฐ์ดํŠธ ๊ธฐ๋Šฅ ๊ฐœ๋ฐœ
Browse files Browse the repository at this point in the history
  • Loading branch information
jjunhaa0211 committed Mar 8, 2024
1 parent 62ae48c commit 52148f6
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions MindGymKit/HealthKitStepCountProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import RxSwift
// MARK: - MindGymKit
public protocol StepCountProvider {
func fetchTodayStepCount(completion: @escaping (Double?) -> Void)
func observeStepCountUpdates(completion: @escaping (Double?) -> Void)
}

open class HealthKitStepCountProvider: StepCountProvider {
Expand Down Expand Up @@ -46,4 +47,23 @@ open class HealthKitStepCountProvider: StepCountProvider {
}
}
}

public func observeStepCountUpdates(completion: @escaping (Double?) -> Void) {
let stepType = HKObjectType.quantityType(forIdentifier: .stepCount)!
let query = HKObserverQuery(sampleType: stepType, predicate: nil) { query, completionHandler, error in
if let error = error {
print("Error: \(error.localizedDescription)")
} else {
self.fetchTodayStepCount(completion: completion)
}
completionHandler()
}

healthStore.execute(query)
healthStore.enableBackgroundDelivery(for: stepType, frequency: .immediate) { success, error in
if let error = error {
print("Failed to enable background delivery: \(error.localizedDescription)")
}
}
}
}

0 comments on commit 52148f6

Please sign in to comment.