@flomentumsolutions/capacitor-health-extended 0.6.1 → 0.6.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -80,62 +80,122 @@ public class HealthPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
80
80
|
return
|
|
81
81
|
}
|
|
82
82
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
83
|
+
DispatchQueue.main.async {
|
|
84
|
+
self.healthStore.requestAuthorization(toShare: nil, read: Set(types)) { success, error in
|
|
85
|
+
DispatchQueue.main.async {
|
|
86
|
+
if success {
|
|
87
|
+
//we don't know which actual permissions were granted, so we assume all
|
|
88
|
+
var result: [String: Bool] = [:]
|
|
89
|
+
permissions.forEach{ result[$0] = true }
|
|
90
|
+
call.resolve(["permissions": result])
|
|
91
|
+
} else if let error = error {
|
|
92
|
+
call.reject("Authorization failed: \(error.localizedDescription)")
|
|
93
|
+
} else {
|
|
94
|
+
//assume no permissions were granted. We can ask user to adjust them manually
|
|
95
|
+
var result: [String: Bool] = [:]
|
|
96
|
+
permissions.forEach{ result[$0] = false }
|
|
97
|
+
call.resolve(["permissions": result])
|
|
98
|
+
}
|
|
99
|
+
}
|
|
96
100
|
}
|
|
97
101
|
}
|
|
98
102
|
}
|
|
99
103
|
|
|
100
104
|
@objc func getCharacteristics(_ call: CAPPluginCall) {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
+
guard HKHealthStore.isHealthDataAvailable() else {
|
|
106
|
+
call.resolve([
|
|
107
|
+
"platformSupported": false,
|
|
108
|
+
"platformMessage": "Health data is unavailable on this device."
|
|
109
|
+
])
|
|
110
|
+
return
|
|
105
111
|
}
|
|
106
112
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
113
|
+
let characteristicTypes: [HKObjectType] = [
|
|
114
|
+
HKObjectType.characteristicType(forIdentifier: .biologicalSex),
|
|
115
|
+
HKObjectType.characteristicType(forIdentifier: .bloodType),
|
|
116
|
+
HKObjectType.characteristicType(forIdentifier: .dateOfBirth),
|
|
117
|
+
HKObjectType.characteristicType(forIdentifier: .fitzpatrickSkinType),
|
|
118
|
+
HKObjectType.characteristicType(forIdentifier: .wheelchairUse)
|
|
119
|
+
].compactMap { $0 }
|
|
112
120
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
setValue("bloodType", nil)
|
|
121
|
+
guard !characteristicTypes.isEmpty else {
|
|
122
|
+
call.reject("HealthKit characteristics are unavailable on this device.")
|
|
123
|
+
return
|
|
117
124
|
}
|
|
118
125
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
} else {
|
|
122
|
-
setValue("dateOfBirth", nil)
|
|
123
|
-
}
|
|
126
|
+
func resolveCharacteristics() {
|
|
127
|
+
var result: [String: Any] = [:]
|
|
124
128
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
+
func setValue(_ key: String, _ value: String?) {
|
|
130
|
+
result[key] = value ?? NSNull()
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if let biologicalSexObject = try? healthStore.biologicalSex() {
|
|
134
|
+
setValue("biologicalSex", mapBiologicalSex(biologicalSexObject.biologicalSex))
|
|
135
|
+
} else {
|
|
136
|
+
setValue("biologicalSex", nil)
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if let bloodTypeObject = try? healthStore.bloodType() {
|
|
140
|
+
setValue("bloodType", mapBloodType(bloodTypeObject.bloodType))
|
|
141
|
+
} else {
|
|
142
|
+
setValue("bloodType", nil)
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if let dateComponents = try? healthStore.dateOfBirthComponents() {
|
|
146
|
+
setValue("dateOfBirth", isoBirthDateString(from: dateComponents))
|
|
147
|
+
} else {
|
|
148
|
+
setValue("dateOfBirth", nil)
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if let fitzpatrickObject = try? healthStore.fitzpatrickSkinType() {
|
|
152
|
+
setValue("fitzpatrickSkinType", mapFitzpatrickSkinType(fitzpatrickObject.skinType))
|
|
153
|
+
} else {
|
|
154
|
+
setValue("fitzpatrickSkinType", nil)
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if let wheelchairUseObject = try? healthStore.wheelchairUse() {
|
|
158
|
+
setValue("wheelchairUse", mapWheelchairUse(wheelchairUseObject.wheelchairUse))
|
|
159
|
+
} else {
|
|
160
|
+
setValue("wheelchairUse", nil)
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
result["platformSupported"] = true
|
|
164
|
+
call.resolve(result)
|
|
129
165
|
}
|
|
130
166
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
167
|
+
func requestCharacteristicsAccess() {
|
|
168
|
+
self.healthStore.requestAuthorization(toShare: nil, read: Set(characteristicTypes)) { success, error in
|
|
169
|
+
DispatchQueue.main.async {
|
|
170
|
+
if success {
|
|
171
|
+
resolveCharacteristics()
|
|
172
|
+
} else if let error = error {
|
|
173
|
+
call.reject("Authorization failed: \(error.localizedDescription)")
|
|
174
|
+
} else {
|
|
175
|
+
call.resolve([
|
|
176
|
+
"platformSupported": true,
|
|
177
|
+
"platformMessage": "Characteristics access was not granted. Update permissions in the Health app."
|
|
178
|
+
])
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
135
182
|
}
|
|
136
183
|
|
|
137
|
-
|
|
138
|
-
|
|
184
|
+
healthStore.getRequestStatusForAuthorization(toShare: Set<HKSampleType>(), read: Set(characteristicTypes)) { status, error in
|
|
185
|
+
DispatchQueue.main.async {
|
|
186
|
+
if let error = error {
|
|
187
|
+
call.reject("Failed to determine HealthKit authorization status: \(error.localizedDescription)")
|
|
188
|
+
return
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
switch status {
|
|
192
|
+
case .shouldRequest, .unknown:
|
|
193
|
+
requestCharacteristicsAccess()
|
|
194
|
+
default:
|
|
195
|
+
resolveCharacteristics()
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
139
199
|
}
|
|
140
200
|
|
|
141
201
|
@objc func queryLatestSample(_ call: CAPPluginCall) {
|
package/package.json
CHANGED