@flomentumsolutions/capacitor-health-extended 0.6.0 → 0.6.2

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
- healthStore.requestAuthorization(toShare: nil, read: Set(types)) { success, error in
84
- if success {
85
- //we don't know which actual permissions were granted, so we assume all
86
- var result: [String: Bool] = [:]
87
- permissions.forEach{ result[$0] = true }
88
- call.resolve(["permissions": result])
89
- } else if let error = error {
90
- call.reject("Authorization failed: \(error.localizedDescription)")
91
- } else {
92
- //assume no permissions were granted. We can ask user to adjust them manually
93
- var result: [String: Bool] = [:]
94
- permissions.forEach{ result[$0] = false }
95
- call.resolve(["permissions": result])
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
- var result: [String: Any] = [:]
102
-
103
- func setValue(_ key: String, _ value: String?) {
104
- result[key] = value ?? NSNull()
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
- if let biologicalSexObject = try? healthStore.biologicalSex() {
108
- setValue("biologicalSex", mapBiologicalSex(biologicalSexObject.biologicalSex))
109
- } else {
110
- setValue("biologicalSex", nil)
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
- if let bloodTypeObject = try? healthStore.bloodType() {
114
- setValue("bloodType", mapBloodType(bloodTypeObject.bloodType))
115
- } else {
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
- if let dateComponents = try? healthStore.dateOfBirthComponents() {
120
- setValue("dateOfBirth", isoBirthDateString(from: dateComponents))
121
- } else {
122
- setValue("dateOfBirth", nil)
123
- }
126
+ func resolveCharacteristics() {
127
+ var result: [String: Any] = [:]
124
128
 
125
- if let fitzpatrickObject = try? healthStore.fitzpatrickSkinType() {
126
- setValue("fitzpatrickSkinType", mapFitzpatrickSkinType(fitzpatrickObject.skinType))
127
- } else {
128
- setValue("fitzpatrickSkinType", nil)
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
- if let wheelchairUseObject = try? healthStore.wheelchairUse() {
132
- setValue("wheelchairUse", mapWheelchairUse(wheelchairUseObject.wheelchairUse))
133
- } else {
134
- setValue("wheelchairUse", nil)
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
- result["platformSupported"] = true
138
- call.resolve(result)
184
+ healthStore.getRequestStatusForAuthorization(toShare: nil, 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) {
@@ -629,17 +689,17 @@ public class HealthPlugin: CAPPlugin, CAPBridgedPlugin {
629
689
 
630
690
  private func mapFitzpatrickSkinType(_ skinType: HKFitzpatrickSkinType) -> String {
631
691
  switch skinType {
632
- case .typeI:
692
+ case .I:
633
693
  return "type1"
634
- case .typeII:
694
+ case .II:
635
695
  return "type2"
636
- case .typeIII:
696
+ case .III:
637
697
  return "type3"
638
- case .typeIV:
698
+ case .IV:
639
699
  return "type4"
640
- case .typeV:
700
+ case .V:
641
701
  return "type5"
642
- case .typeVI:
702
+ case .VI:
643
703
  return "type6"
644
704
  case .notSet:
645
705
  return "not_set"
@@ -650,9 +710,9 @@ public class HealthPlugin: CAPPlugin, CAPBridgedPlugin {
650
710
 
651
711
  private func mapWheelchairUse(_ wheelchairUse: HKWheelchairUse) -> String {
652
712
  switch wheelchairUse {
653
- case .wheelchairUser:
713
+ case .yes:
654
714
  return "wheelchair_user"
655
- case .notWheelchairUser:
715
+ case .no:
656
716
  return "not_wheelchair_user"
657
717
  case .notSet:
658
718
  return "not_set"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flomentumsolutions/capacitor-health-extended",
3
- "version": "0.6.0",
3
+ "version": "0.6.2",
4
4
  "description": "Capacitor plugin for Apple HealthKit and Google Health Connect Platform",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",