HealthKitHostApiImpl.swift
8.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import Foundation
final class HealthKitHostApiImpl: HealthKitHostApi {
private let service: HealthKitService
init(service: HealthKitService = .shared) {
self.service = service
}
func checkHealthAppAuthorization(completion: @escaping (Result<Bool, Error>) -> Void) {
Task {
let shouldAuth = await service.shouldRequestAuthorization()
let isAuthorized = service.isHealthDataAvailable && !shouldAuth
completion(.success(isAuthorized))
}
}
func getHealthServerAuthUrl(completion: @escaping (Result<String, Error>) -> Void) {
// Apple Health authorization is system-managed, not URL based.
completion(.success(""))
}
func requestHealthClientAuthorization() throws -> Bool {
let result = runAuthorizationRequest()
if result.success {
service.startBackgroundObserversIfNeeded()
Task {
await service.refreshSharedWatchValues()
}
}
if let error = result.error {
throw error
}
return result.success
}
func cancelHealthAppAuthorization() throws -> Bool {
// iOS does not let apps revoke HealthKit permission programmatically.
// Users must revoke access in Settings > Health > Data Access & Devices.
false
}
func performHealthUpload() throws -> HealthUploadResult {
let summary = runBlocking {
await self.service.performLocalSync()
}
return HealthUploadResult(
commonUploadSuccess: summary.commonCount >= 0,
sleepUploadSuccess: summary.sleepCount >= 0,
errorMessage: nil
)
}
func fetchHrvData(startTime: Int64, endTime: Int64, completion: @escaping (Result<[HealthUploadDataPoint], Error>) -> Void) {
fetchCommon(startTime: startTime, endTime: endTime, service.fetchHrvData, completion: completion)
}
func fetchHeartRateData(startTime: Int64, endTime: Int64, completion: @escaping (Result<[HealthUploadDataPoint], Error>) -> Void) {
fetchCommon(startTime: startTime, endTime: endTime, service.fetchHeartRateData, completion: completion)
}
func fetchWalkingHeartRateData(startTime: Int64, endTime: Int64, completion: @escaping (Result<[HealthUploadDataPoint], Error>) -> Void) {
fetchCommon(startTime: startTime, endTime: endTime, service.fetchWalkingHeartRateData, completion: completion)
}
func fetchRestingHeartRateData(startTime: Int64, endTime: Int64, completion: @escaping (Result<[HealthUploadDataPoint], Error>) -> Void) {
fetchCommon(startTime: startTime, endTime: endTime, service.fetchRestingHeartRateData, completion: completion)
}
func fetchSleepingHeartRateData(startTime: Int64, endTime: Int64, completion: @escaping (Result<[HealthUploadDataPoint], Error>) -> Void) {
fetchCommon(startTime: startTime, endTime: endTime, service.fetchSleepingHeartRateData, completion: completion)
}
func fetchOxygenSaturationData(startTime: Int64, endTime: Int64, completion: @escaping (Result<[HealthUploadDataPoint], Error>) -> Void) {
fetchCommon(startTime: startTime, endTime: endTime, service.fetchOxygenSaturationData, completion: completion)
}
func fetchActiveEnergyData(startTime: Int64, endTime: Int64, completion: @escaping (Result<[HealthUploadDataPoint], Error>) -> Void) {
fetchCommon(startTime: startTime, endTime: endTime, service.fetchActiveEnergyData, completion: completion)
}
func fetchExerciseData(startTime: Int64, endTime: Int64, completion: @escaping (Result<[HealthUploadDataPoint], Error>) -> Void) {
fetchCommon(startTime: startTime, endTime: endTime, service.fetchExerciseData, completion: completion)
}
func fetchStandData(startTime: Int64, endTime: Int64, completion: @escaping (Result<[HealthUploadDataPoint], Error>) -> Void) {
fetchCommon(startTime: startTime, endTime: endTime, service.fetchStandData, completion: completion)
}
func fetchStepCountData(startTime: Int64, endTime: Int64, completion: @escaping (Result<[HealthUploadDataPoint], Error>) -> Void) {
fetchCommon(startTime: startTime, endTime: endTime, service.fetchStepCountData, completion: completion)
}
func fetchSleepData(startTime: Int64, endTime: Int64, completion: @escaping (Result<[HealthSleepUploadDataPoint], Error>) -> Void) {
let range = makeDateRange(startTime: startTime, endTime: endTime)
Task {
do {
let intervals = try await service.fetchSleepData(startDate: range.startDate, endDate: range.endDate)
completion(.success(intervals.map { interval in
HealthSleepUploadDataPoint(
dataType: Int64(interval.dataType),
fromTime: Int64(interval.fromTime.rounded()),
toTime: Int64(interval.toTime.rounded())
)
}))
} catch {
completion(.failure(error))
}
}
}
func fetchSleepingWristTemperatureData(startTime: Int64, endTime: Int64, completion: @escaping (Result<[HealthUploadDataPoint], Error>) -> Void) {
fetchCommon(startTime: startTime, endTime: endTime, service.fetchSleepingWristTemperatureData, completion: completion)
}
func fetchRespiratoryRateData(startTime: Int64, endTime: Int64, completion: @escaping (Result<[HealthUploadDataPoint], Error>) -> Void) {
fetchCommon(startTime: startTime, endTime: endTime, service.fetchRespiratoryRateData, completion: completion)
}
func fetchIrregularHeartRhythmData(startTime: Int64, endTime: Int64, completion: @escaping (Result<[HealthUploadDataPoint], Error>) -> Void) {
fetchCommon(startTime: startTime, endTime: endTime, service.fetchIrregularHeartRhythmData, completion: completion)
}
func fetchActivityTargetData(startTime: Int64, endTime: Int64, completion: @escaping (Result<HealthActivityTargetData?, Error>) -> Void) {
let range = makeDateRange(startTime: startTime, endTime: endTime)
Task {
do {
let target = try await service.fetchActivityTargetData(startDate: range.startDate, endDate: range.endDate)
completion(.success(target.map {
HealthActivityTargetData(
move: $0.move.map(Int64.init),
stand: $0.stand.map(Int64.init)
)
}))
} catch {
completion(.failure(error))
}
}
}
private func fetchCommon(
startTime: Int64,
endTime: Int64,
_ fetch: @escaping (Date, Date) async throws -> [NativeHealthDataPoint],
completion: @escaping (Result<[HealthUploadDataPoint], Error>) -> Void
) {
let range = makeDateRange(startTime: startTime, endTime: endTime)
Task {
do {
let points = try await fetch(range.startDate, range.endDate)
completion(.success(points.map { point in
HealthUploadDataPoint(
dataType: Int64(point.dataType.rawValue),
time: Int64(point.time.rounded()),
value: point.value
)
}))
} catch {
completion(.failure(error))
}
}
}
private func makeDateRange(startTime: Int64, endTime: Int64) -> (startDate: Date, endDate: Date) {
(
Date(timeIntervalSince1970: TimeInterval(startTime)),
Date(timeIntervalSince1970: TimeInterval(endTime))
)
}
private func runAuthorizationRequest() -> (success: Bool, error: Error?) {
var result: (Bool, Error?) = (false, nil)
let semaphore = DispatchSemaphore(value: 0)
service.requestAuthorization { success, error in
result = (success, error)
semaphore.signal()
}
waitForSemaphore(semaphore)
return result
}
}
private func runBlocking<T>(_ operation: @escaping () async -> T) -> T {
let semaphore = DispatchSemaphore(value: 0)
var result: T?
Task {
result = await operation()
semaphore.signal()
}
waitForSemaphore(semaphore)
return result!
}
private func runBlockingThrows<T>(_ operation: @escaping () async throws -> T) throws -> T {
let semaphore = DispatchSemaphore(value: 0)
var result: Result<T, Error>?
Task {
do {
result = .success(try await operation())
} catch {
result = .failure(error)
}
semaphore.signal()
}
waitForSemaphore(semaphore)
return try result!.get()
}
private func waitForSemaphore(_ semaphore: DispatchSemaphore) {
if Thread.isMainThread {
while semaphore.wait(timeout: .now() + 0.05) == .timedOut {
RunLoop.main.run(mode: .default, before: Date(timeIntervalSinceNow: 0.05))
}
} else {
semaphore.wait()
}
}