HealthKitRawDataHostApiImpl.swift
6.05 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
import Foundation
final class HealthKitRawDataHostApiImpl: HealthKitRawDataHostApi {
private let service: HealthKitService
init(service: HealthKitService = .shared) {
self.service = service
}
func hasHealthData(completion: @escaping (Result<Bool, Error>) -> Void) {
Task {
let hasData = await service.hasAnyReadableData()
completion(.success(hasData))
}
}
func performHealthDataUpload(completion: @escaping (Result<Bool, any Error>) -> Void) {
Task {
let summary = await AnchoredHealthDataUploader.shared.uploadAll()
completion(.success(true))
}
}
func performHRDataUpload(sqliteFilePath: String, completion: @escaping (Result<Int64, any Error>) -> Void) {
}
func performHRVDataUpload(sqliteFilePath: String, completion: @escaping (Result<Int64, any Error>) -> Void) {
}
func performAvgRealtimeStressDataUpload(sqliteFilePath: String, completion: @escaping (Result<Bool, any Error>) -> Void) {
}
func getHealthKitRawData(
dataType: Int64,
startTime: Int64,
endTime: Int64,
completion: @escaping (Result<[HealthKitRawDataPoint], Error>) -> Void
) {
guard let nativeDataType = NativeHealthDataType(rawValue: Int(dataType)) else {
completion(.failure(PigeonError(
code: "invalid_data_type",
message: "无效的健康数据类型:\(dataType)",
details: nil
)))
return
}
guard nativeDataType != .sleep else {
completion(.failure(PigeonError(
code: "unsupported_data_type",
message: "睡眠数据请使用 getHealthKitRawSleepData 读取",
details: nil
)))
return
}
let startDate = Date(timeIntervalSince1970: TimeInterval(startTime))
let endDate = Date(timeIntervalSince1970: TimeInterval(endTime))
Task {
do {
let points = try await service.fetchRawData(
for: nativeDataType,
startDate: startDate,
endDate: endDate
)
completion(.success(points.map { Self.rawDataPoint(from: $0, dataType: nativeDataType) }))
} catch {
completion(.failure(error))
}
}
}
func getHealthKitRawSleepData(
startTime: Int64,
endTime: Int64,
completion: @escaping (Result<[HealthKitRawSleepDataPoint], Error>) -> Void
) {
let startDate = Date(timeIntervalSince1970: TimeInterval(startTime))
let endDate = Date(timeIntervalSince1970: TimeInterval(endTime))
Task {
do {
let intervals = try await service.fetchSleepData(
startDate: startDate,
endDate: endDate
)
let points = intervals.map(Self.sleepDataPoint)
guard !points.isEmpty else {
completion(.success([]))
return
}
completion(.success([
HealthKitRawSleepDataPoint(
dataType: Int64(NativeHealthDataType.sleep.rawValue),
sleepDataPoints: points
)
]))
} catch {
completion(.failure(error))
}
}
}
func getHealthKitRawActivityData(
startTime: Int64,
endTime: Int64,
completion: @escaping (Result<[HealthKitRawActivityDataPoint], Error>) -> Void
) {
let startDate = Date(timeIntervalSince1970: TimeInterval(startTime))
let endDate = Date(timeIntervalSince1970: TimeInterval(endTime))
Task {
do {
let targets = try await service.fetchActivityTargetDataList(
startDate: startDate,
endDate: endDate
)
completion(.success(targets.compactMap(Self.activityDataPoint)))
} catch {
completion(.failure(error))
}
}
}
func getHealthKitRawWorkoutData(
startTime: Int64,
endTime: Int64,
completion: @escaping (Result<[HealthKitRawWorkoutDataPoint], Error>) -> Void
) {
let startDate = Date(timeIntervalSince1970: TimeInterval(startTime))
let endDate = Date(timeIntervalSince1970: TimeInterval(endTime))
Task {
do {
let workouts = try await service.fetchWorkoutDataList(
startDate: startDate,
endDate: endDate
)
completion(.success(workouts.map(Self.workoutDataPoint)))
} catch {
completion(.failure(error))
}
}
}
func performSleepDataUpload(completion: @escaping (Result<Bool, Error>) -> Void) {
Task {
let summary = await AnchoredHealthDataUploader.shared.uploadAll()
completion(.success(summary.sleepUploadSuccess))
}
}
private static func rawDataPoint(
from point: NativeHealthRawDataPoint,
dataType: NativeHealthDataType
) -> HealthKitRawDataPoint {
HealthKitRawDataPoint(
dataType: Int64(dataType.rawValue),
startTime: Int64(point.startTime.rounded()),
endTime: Int64(point.endTime.rounded()),
value: point.value
)
}
private static func sleepDataPoint(from interval: NativeSleepInterval) -> HealthKitRawDataPoint {
HealthKitRawDataPoint(
dataType: Int64(interval.dataType),
startTime: Int64(interval.fromTime.rounded()),
endTime: Int64(interval.toTime.rounded()),
value: nil
)
}
private static func activityDataPoint(from target: NativeActivityTarget) -> HealthKitRawActivityDataPoint? {
guard let endTime = target.healthValueTimestamp else {
return nil
}
return HealthKitRawActivityDataPoint(
endTime: Int64(endTime.rounded()),
activeEnergyBurned: target.activeEnergyBurned,
activeEnergyBurnedGoal: target.activeEnergyBurnedGoal,
appleMoveTime: target.appleMoveTime,
appleMoveTimeGoal: target.appleMoveTimeGoal,
appleExerciseTime: target.appleExerciseTime,
exerciseTimeGoal: target.exerciseTimeGoal,
appleStandHours: target.appleStandHours,
standHoursGoal: target.standHoursGoal
)
}
private static func workoutDataPoint(from workout: NativeWorkoutInterval) -> HealthKitRawWorkoutDataPoint {
HealthKitRawWorkoutDataPoint(
workoutType: Int64(workout.workoutType),
startTime: Int64(workout.startTime.rounded()),
endTime: Int64(workout.endTime.rounded())
)
}
}