HealthKitService.swift
6.59 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
219
220
221
222
223
224
225
226
import Foundation
import HealthKit
/// Native Apple Health service for the Flutter host API.
///
/// Responsibilities:
/// - request/read HealthKit permissions
/// - read the health data types used by the original SwiftUI app
/// - register background observers so HealthKit changes refresh local state
final class HealthKitService {
static let shared = HealthKitService()
private let healthStore = HKHealthStore()
private lazy var reader = HealthKitQueryReader(healthStore: healthStore)
private lazy var anchoredReader = SharedHealthAnchoredQueryReader(healthStore: healthStore)
@MainActor private lazy var observerController = SharedHealthAnchoredObserverController(
healthStore: healthStore,
observedTypes: NativeHealthTypeCatalog.observedTypes,
isLoggedIn: { AppShared.shared.isLogin },
isAuthorized: { [weak self] in
await self?.authorizationRequestStatus() == .unnecessary
},
onChanges: { [weak self] identifiers in
await self?.handleObservedChanges(sampleTypeIdentifiers: identifiers)
},
log: { DebugLogger.debugLog("[ArchUploader] \($0)") }
)
private init() {}
var isHealthDataAvailable: Bool {
HKHealthStore.isHealthDataAvailable()
}
func requestAuthorization(completion: @escaping (Bool, Error?) -> Void) {
guard isHealthDataAvailable else {
completion(false, NativeHealthKitError.healthDataUnavailable)
return
}
healthStore.requestAuthorization(
toShare: [],
read: NativeHealthTypeCatalog.readTypes
) { success, error in
completion(success, error)
}
}
func authorizationRequestStatus() async -> HKAuthorizationRequestStatus? {
do {
return try await healthStore.statusForAuthorizationRequest(
toShare: [],
read: NativeHealthTypeCatalog.readTypes
)
} catch {
return nil
}
}
func shouldRequestAuthorization() async -> Bool {
guard isHealthDataAvailable else { return false }
return await authorizationRequestStatus() != .unnecessary
}
func hasAnyReadableData(startingAt requestedStartDate: Date? = nil) async -> Bool {
guard isHealthDataAvailable else { return false }
let endDate = Date()
let startDate = requestedStartDate
?? Calendar.current.date(byAdding: .year, value: -1, to: endDate)
?? Date(timeInterval: -1 * 365 * 24 * 60 * 60, since: endDate)
if let sleepType = NativeHealthTypeCatalog.category(.sleepAnalysis),
await hasAnyReadableSample(
of: [sleepType],
startDate: startDate,
endDate: endDate
) {
return true
}
return false
}
private func hasAnyReadableSample(
of sampleTypes: [HKSampleType],
startDate: Date,
endDate: Date
) async -> Bool {
guard !sampleTypes.isEmpty else { return false }
let predicate = HKQuery.predicateForSamples(
withStart: startDate,
end: endDate,
options: []
)
let state = HealthReadableDataProbeState()
return await withCheckedContinuation { continuation in
let group = DispatchGroup()
for sampleType in sampleTypes {
group.enter()
let query = HKSampleQuery(
sampleType: sampleType,
predicate: predicate,
limit: 1,
sortDescriptors: nil
) { _, samples, _ in
if samples?.isEmpty == false {
state.markDataFound()
}
group.leave()
}
healthStore.execute(query)
}
group.notify(queue: .global(qos: .utility)) {
continuation.resume(returning: state.hasData)
}
}
}
func startBackgroundObserversIfNeeded() {
Task { @MainActor [weak self] in
self?.observerController.startIfNeeded()
}
}
func restartBackgroundObserversAfterAuthorization() {
Task { @MainActor [weak self] in
self?.observerController.restartAfterAuthorization()
}
}
func stopBackgroundObservers() {
Task { @MainActor [weak self] in
self?.observerController.stop()
}
}
func fetchSleepData(startDate: Date, endDate: Date) async throws -> [NativeSleepInterval] {
return try await reader.fetchSleepData(startDate: startDate, endDate: endDate)
}
func fetchActivityTargetDataList(startDate: Date, endDate: Date) async throws -> [NativeActivityTarget] {
return try await reader.fetchActivityTargetDataList(startDate: startDate, endDate: endDate)
}
func fetchWorkoutDataList(startDate: Date, endDate: Date) async throws -> [NativeWorkoutInterval] {
return try await reader.fetchWorkoutDataList(startDate: startDate, endDate: endDate)
}
func fetchRawData(
for dataType: NativeHealthDataType,
startDate: Date,
endDate: Date
) async throws -> [NativeHealthRawDataPoint] {
return try await reader.fetchRawData(for: dataType, startDate: startDate, endDate: endDate)
}
struct AnchoredChanges {
let samples: [HKSample]
let deletedObjectCount: Int
let newAnchor: HKQueryAnchor
let sourceIdentifier: String
}
func fetchAnchoredChanges(
anchor: HKQueryAnchor?,
initialStartDate: Date
) async throws -> AnchoredChanges {
guard let sampleType = NativeHealthTypeCatalog.category(.sleepAnalysis) else {
throw NativeHealthKitError.invalidType(HKCategoryTypeIdentifier.sleepAnalysis.rawValue)
}
let changes = try await anchoredReader.fetchChanges(
sampleType: sampleType,
anchor: anchor,
initialStartDate: initialStartDate
)
return AnchoredChanges(
samples: changes.samples,
deletedObjectCount: changes.deletedObjectCount,
newAnchor: changes.newAnchor,
sourceIdentifier: sampleType.identifier
)
}
private func handleObservedChanges(sampleTypeIdentifiers: Set<String>) async {
guard AppShared.shared.isLogin else { return }
let dataTypes = NativeHealthDataType.mappedTypes(for: sampleTypeIdentifiers)
.map(\.rawValue)
.sorted()
await MainActor.run {
NotificationCenter.default.post(
name: .nativeHealthDataDidUpdate,
object: nil,
userInfo: ["dataTypes": dataTypes]
)
}
await AnchoredHealthDataUploader.shared.uploadAllAfterObservedChange(
sampleTypeIdentifiers: sampleTypeIdentifiers
)
}
func uploadHealthDataAfterForeground() async {
guard AppShared.shared.token?.isEmpty == false else { return }
_ = await AnchoredHealthDataUploader.shared.uploadAll()
}
}
private final class HealthReadableDataProbeState: @unchecked Sendable {
private let lock = NSLock()
private var dataFound = false
func markDataFound() {
lock.lock()
dataFound = true
lock.unlock()
}
var hasData: Bool {
lock.lock()
defer { lock.unlock() }
return dataFound
}
}