@kingstinct/react-native-healthkit 7.2.0 → 7.3.1
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.
- package/ios/Constants.swift +1 -1
- package/ios/ReactNativeHealthkit.m +2 -1
- package/ios/ReactNativeHealthkit.swift +108 -54
- package/lib/commonjs/hooks/useHealthkitAuthorization.js +5 -0
- package/lib/commonjs/hooks/useHealthkitAuthorization.js.map +1 -1
- package/lib/commonjs/hooks/useMostRecentCategorySample.js +3 -0
- package/lib/commonjs/hooks/useMostRecentCategorySample.js.map +1 -1
- package/lib/commonjs/hooks/useMostRecentQuantitySample.js +3 -0
- package/lib/commonjs/hooks/useMostRecentQuantitySample.js.map +1 -1
- package/lib/commonjs/hooks/useMostRecentWorkout.js +3 -0
- package/lib/commonjs/hooks/useMostRecentWorkout.js.map +1 -1
- package/lib/commonjs/index.js +475 -82
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/index.native.js +251 -0
- package/lib/commonjs/index.native.js.map +1 -0
- package/lib/commonjs/index.web.js +17 -0
- package/lib/commonjs/index.web.js.map +1 -0
- package/lib/commonjs/jest.setup.js +2 -1
- package/lib/commonjs/jest.setup.js.map +1 -1
- package/lib/commonjs/native-types.js +5 -5
- package/lib/commonjs/native-types.js.map +1 -1
- package/lib/commonjs/utils/ensureTotals.js +12 -0
- package/lib/commonjs/utils/ensureTotals.js.map +1 -0
- package/lib/commonjs/utils/getWorkoutPlanById.js +14 -0
- package/lib/commonjs/utils/getWorkoutPlanById.js.map +1 -0
- package/lib/commonjs/utils/saveCategorySample.js +4 -0
- package/lib/commonjs/utils/saveCategorySample.js.map +1 -1
- package/lib/commonjs/utils/saveWorkoutSample.js +2 -1
- package/lib/commonjs/utils/saveWorkoutSample.js.map +1 -1
- package/lib/module/hooks/useHealthkitAuthorization.js +5 -0
- package/lib/module/hooks/useHealthkitAuthorization.js.map +1 -1
- package/lib/module/hooks/useMostRecentCategorySample.js +3 -0
- package/lib/module/hooks/useMostRecentCategorySample.js.map +1 -1
- package/lib/module/hooks/useMostRecentQuantitySample.js +3 -0
- package/lib/module/hooks/useMostRecentQuantitySample.js.map +1 -1
- package/lib/module/hooks/useMostRecentWorkout.js +3 -0
- package/lib/module/hooks/useMostRecentWorkout.js.map +1 -1
- package/lib/module/index.js +195 -80
- package/lib/module/index.js.map +1 -1
- package/lib/module/index.native.js +136 -0
- package/lib/module/index.native.js.map +1 -0
- package/lib/module/index.web.js +2 -0
- package/lib/module/index.web.js.map +1 -0
- package/lib/module/jest.setup.js +2 -1
- package/lib/module/jest.setup.js.map +1 -1
- package/lib/module/native-types.js +3 -3
- package/lib/module/native-types.js.map +1 -1
- package/lib/module/utils/ensureTotals.js +5 -0
- package/lib/module/utils/ensureTotals.js.map +1 -0
- package/lib/module/utils/getWorkoutPlanById.js +6 -0
- package/lib/module/utils/getWorkoutPlanById.js.map +1 -0
- package/lib/module/utils/saveCategorySample.js +4 -0
- package/lib/module/utils/saveCategorySample.js.map +1 -1
- package/lib/module/utils/saveWorkoutSample.js +2 -1
- package/lib/module/utils/saveWorkoutSample.js.map +1 -1
- package/lib/typescript/src/hooks/useHealthkitAuthorization.d.ts +5 -0
- package/lib/typescript/src/hooks/useMostRecentCategorySample.d.ts +3 -0
- package/lib/typescript/src/hooks/useMostRecentQuantitySample.d.ts +3 -0
- package/lib/typescript/src/hooks/useMostRecentWorkout.d.ts +3 -0
- package/lib/typescript/src/index.d.ts +155 -27
- package/lib/typescript/src/index.native.d.ts +29 -0
- package/lib/typescript/src/index.web.d.ts +1 -0
- package/lib/typescript/src/native-types.d.ts +82 -75
- package/lib/typescript/src/utils/ensureTotals.d.ts +2 -0
- package/lib/typescript/src/utils/getWorkoutPlanById.d.ts +5 -0
- package/lib/typescript/src/utils/saveCategorySample.d.ts +4 -0
- package/lib/typescript/src/utils/saveWorkoutSample.d.ts +4 -0
- package/package.json +1 -1
- package/src/hooks/useHealthkitAuthorization.ts +5 -0
- package/src/hooks/useMostRecentCategorySample.ts +3 -0
- package/src/hooks/useMostRecentQuantitySample.ts +3 -0
- package/src/hooks/useMostRecentWorkout.ts +3 -0
- package/src/index.native.tsx +201 -0
- package/src/index.tsx +269 -91
- package/src/index.web.tsx +1 -0
- package/src/jest.setup.ts +2 -1
- package/src/native-types.ts +143 -116
- package/src/utils/ensureTotals.ts +5 -0
- package/src/utils/getWorkoutPlanById.ts +7 -0
- package/src/utils/saveCategorySample.ts +4 -0
- package/src/utils/saveWorkoutSample.ts +6 -0
- package/lib/commonjs/index.ios.js +0 -213
- package/lib/commonjs/index.ios.js.map +0 -1
- package/lib/module/index.ios.js +0 -191
- package/lib/module/index.ios.js.map +0 -1
- package/lib/typescript/src/index.ios.d.ts +0 -141
- package/src/index.ios.tsx +0 -217
package/ios/Constants.swift
CHANGED
|
@@ -25,4 +25,4 @@ let HKDataTypeIdentifierHeartbeatSeries = "HKDataTypeIdentifierHeartbeatSeries"
|
|
|
25
25
|
|
|
26
26
|
let SpeedUnit = HKUnit(from: "m/s") // HKUnit.meter().unitDivided(by: HKUnit.second())
|
|
27
27
|
// Support for MET data: HKAverageMETs 8.24046 kcal/hr·kg
|
|
28
|
-
let METUnit = HKUnit(from: "kcal/hr·kg")
|
|
28
|
+
let METUnit = HKUnit(from: "kcal/hr·kg")
|
|
@@ -146,6 +146,7 @@ RCT_EXTERN_METHOD(saveWorkoutSample:(NSInteger)typeIdentifier
|
|
|
146
146
|
quantities:(NSArray)quantities
|
|
147
147
|
start:(NSDate)start
|
|
148
148
|
end:(NSDate)end
|
|
149
|
+
totals:(NSDictionary)totals
|
|
149
150
|
metadata:(NSDictionary)metadata
|
|
150
151
|
resolve:(RCTPromiseResolveBlock)resolve
|
|
151
152
|
reject:(RCTPromiseRejectBlock)reject
|
|
@@ -211,7 +212,7 @@ RCT_EXTERN_METHOD(getWorkoutRoutes:(NSString)workoutUUID
|
|
|
211
212
|
reject:(RCTPromiseRejectBlock)reject
|
|
212
213
|
)
|
|
213
214
|
|
|
214
|
-
RCT_EXTERN_METHOD(
|
|
215
|
+
RCT_EXTERN_METHOD(getWorkoutPlanById:(NSString)workoutUUID
|
|
215
216
|
resolve:(RCTPromiseResolveBlock)resolve
|
|
216
217
|
reject:(RCTPromiseRejectBlock)reject
|
|
217
218
|
)
|
|
@@ -17,7 +17,6 @@ class ReactNativeHealthkit: RCTEventEmitter {
|
|
|
17
17
|
self._runningQueries = [String: HKQuery]()
|
|
18
18
|
self._dateFormatter = ISO8601DateFormatter()
|
|
19
19
|
self._dateFormatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
|
|
20
|
-
|
|
21
20
|
if HKHealthStore.isHealthDataAvailable() {
|
|
22
21
|
self._store = HKHealthStore.init()
|
|
23
22
|
}
|
|
@@ -297,7 +296,6 @@ class ReactNativeHealthkit: RCTEventEmitter {
|
|
|
297
296
|
if let type = HKSampleType.categoryType(forIdentifier: typeId) {
|
|
298
297
|
let value = sample["value"] as! Int
|
|
299
298
|
let metadata = sample["metadata"] as? [String: Any]
|
|
300
|
-
|
|
301
299
|
let categorySample = HKCategorySample.init(type: type, value: value, start: start, end: end, metadata: metadata)
|
|
302
300
|
initializedSamples.insert(categorySample)
|
|
303
301
|
}
|
|
@@ -315,8 +313,8 @@ class ReactNativeHealthkit: RCTEventEmitter {
|
|
|
315
313
|
}
|
|
316
314
|
}
|
|
317
315
|
|
|
318
|
-
@objc(saveWorkoutSample:quantities:start:end:metadata:resolve:reject:)
|
|
319
|
-
func saveWorkoutSample(typeIdentifier: UInt, quantities: [[String: Any]], start: Date, end: Date, metadata: [String: Any], resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
|
|
316
|
+
@objc(saveWorkoutSample:quantities:start:end:totals:metadata:resolve:reject:)
|
|
317
|
+
func saveWorkoutSample(typeIdentifier: UInt, quantities: [[String: Any]], start: Date, end: Date, totals: [String: Any], metadata: [String: Any], resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
|
|
320
318
|
guard let store = _store else {
|
|
321
319
|
return reject(INIT_ERROR, INIT_ERROR_MESSAGE, nil)
|
|
322
320
|
}
|
|
@@ -337,7 +335,6 @@ class ReactNativeHealthkit: RCTEventEmitter {
|
|
|
337
335
|
var totalDistance: HKQuantity?
|
|
338
336
|
var totalSwimmingStrokeCount: HKQuantity?
|
|
339
337
|
var totalFlightsClimbed: HKQuantity?
|
|
340
|
-
|
|
341
338
|
// generating quantity samples
|
|
342
339
|
for quantity in quantities {
|
|
343
340
|
let typeId = HKQuantityTypeIdentifier.init(rawValue: quantity["quantityType"] as! String)
|
|
@@ -362,7 +359,6 @@ class ReactNativeHealthkit: RCTEventEmitter {
|
|
|
362
359
|
if typeId == HKQuantityTypeIdentifier.flightsClimbed {
|
|
363
360
|
totalFlightsClimbed = quantity
|
|
364
361
|
}
|
|
365
|
-
|
|
366
362
|
if let quantityStart, let quantityEnd {
|
|
367
363
|
let quantityStartDate = self._dateFormatter.date(from: quantityStart) ?? start
|
|
368
364
|
let quantityEndDate = self._dateFormatter.date(from: quantityEnd) ?? end
|
|
@@ -376,6 +372,17 @@ class ReactNativeHealthkit: RCTEventEmitter {
|
|
|
376
372
|
}
|
|
377
373
|
}
|
|
378
374
|
|
|
375
|
+
// if totals are provided override samples
|
|
376
|
+
let rawTotalDistance = totals["distance"] as? Double ?? 0.0
|
|
377
|
+
let rawTotalEnergy = totals["energyBurned"] as? Double ?? 0.0
|
|
378
|
+
|
|
379
|
+
if rawTotalDistance != 0.0 {
|
|
380
|
+
totalDistance = HKQuantity(unit: .meter(), doubleValue: rawTotalDistance)
|
|
381
|
+
}
|
|
382
|
+
if rawTotalEnergy != 0.0 {
|
|
383
|
+
totalEnergyBurned = HKQuantity(unit: .kilocalorie(), doubleValue: rawTotalEnergy)
|
|
384
|
+
}
|
|
385
|
+
|
|
379
386
|
// creating workout
|
|
380
387
|
var workout: HKWorkout?
|
|
381
388
|
|
|
@@ -405,6 +412,10 @@ class ReactNativeHealthkit: RCTEventEmitter {
|
|
|
405
412
|
return
|
|
406
413
|
}
|
|
407
414
|
|
|
415
|
+
if initializedSamples.isEmpty {
|
|
416
|
+
return resolve(workout.uuid.uuidString)
|
|
417
|
+
}
|
|
418
|
+
|
|
408
419
|
store.add(initializedSamples, to: workout) { (_, error: Error?) in
|
|
409
420
|
guard error == nil else {
|
|
410
421
|
reject(GENERIC_ERROR, error!.localizedDescription, error)
|
|
@@ -454,7 +465,6 @@ class ReactNativeHealthkit: RCTEventEmitter {
|
|
|
454
465
|
if clLocations.isEmpty {
|
|
455
466
|
return reject(GENERIC_ERROR, "No locations provided", nil)
|
|
456
467
|
}
|
|
457
|
-
|
|
458
468
|
// create route
|
|
459
469
|
let routeBuilder = HKWorkoutRouteBuilder(healthStore: store, device: nil)
|
|
460
470
|
try await routeBuilder.insertRouteData(clLocations)
|
|
@@ -663,42 +673,45 @@ class ReactNativeHealthkit: RCTEventEmitter {
|
|
|
663
673
|
}
|
|
664
674
|
|
|
665
675
|
let query = HKStatisticsQuery.init(quantityType: quantityType, quantitySamplePredicate: predicate, options: opts) { (_, stats: HKStatistics?, _: Error?) in
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
676
|
+
var dic = [String: [String: Any]?]()
|
|
677
|
+
|
|
678
|
+
guard let gottenStats = stats else {
|
|
679
|
+
return resolve(dic)
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
let unit = HKUnit.init(from: unitString)
|
|
683
|
+
if let averageQuantity = gottenStats.averageQuantity() {
|
|
684
|
+
dic.updateValue(serializeQuantity(unit: unit, quantity: averageQuantity), forKey: "averageQuantity")
|
|
685
|
+
}
|
|
686
|
+
if let maximumQuantity = gottenStats.maximumQuantity() {
|
|
687
|
+
dic.updateValue(serializeQuantity(unit: unit, quantity: maximumQuantity), forKey: "maximumQuantity")
|
|
688
|
+
}
|
|
689
|
+
if let minimumQuantity = gottenStats.minimumQuantity() {
|
|
690
|
+
dic.updateValue(serializeQuantity(unit: unit, quantity: minimumQuantity), forKey: "minimumQuantity")
|
|
691
|
+
}
|
|
692
|
+
if let sumQuantity = gottenStats.sumQuantity() {
|
|
693
|
+
dic.updateValue(serializeQuantity(unit: unit, quantity: sumQuantity), forKey: "sumQuantity")
|
|
694
|
+
}
|
|
695
|
+
if #available(iOS 12, *) {
|
|
696
|
+
if let mostRecent = gottenStats.mostRecentQuantity() {
|
|
697
|
+
dic.updateValue(serializeQuantity(unit: unit, quantity: mostRecent), forKey: "mostRecentQuantity")
|
|
680
698
|
}
|
|
681
|
-
if #available(iOS 12, *) {
|
|
682
|
-
if let mostRecent = gottenStats.mostRecentQuantity() {
|
|
683
|
-
dic.updateValue(serializeQuantity(unit: unit, quantity: mostRecent), forKey: "mostRecentQuantity")
|
|
684
|
-
}
|
|
685
699
|
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
}
|
|
700
|
+
if let mostRecentDateInterval = gottenStats.mostRecentQuantityDateInterval() {
|
|
701
|
+
dic.updateValue([
|
|
702
|
+
"start": self._dateFormatter.string(from: mostRecentDateInterval.start),
|
|
703
|
+
"end": self._dateFormatter.string(from: mostRecentDateInterval.end)
|
|
704
|
+
], forKey: "mostRecentQuantityDateInterval")
|
|
692
705
|
}
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
706
|
+
}
|
|
707
|
+
if #available(iOS 13, *) {
|
|
708
|
+
let durationUnit = HKUnit.second()
|
|
709
|
+
if let duration = gottenStats.duration() {
|
|
710
|
+
dic.updateValue(serializeQuantity(unit: durationUnit, quantity: duration), forKey: "duration")
|
|
698
711
|
}
|
|
699
|
-
|
|
700
|
-
resolve(dic)
|
|
701
712
|
}
|
|
713
|
+
|
|
714
|
+
resolve(dic)
|
|
702
715
|
}
|
|
703
716
|
|
|
704
717
|
store.execute(query)
|
|
@@ -706,7 +719,6 @@ class ReactNativeHealthkit: RCTEventEmitter {
|
|
|
706
719
|
|
|
707
720
|
@objc(queryWorkoutSamples:distanceUnitString:from:to:limit:ascending:resolve:reject:)
|
|
708
721
|
func queryWorkoutSamples(energyUnitString: String, distanceUnitString: String, from: Date, to: Date, limit: Int, ascending: Bool, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
|
|
709
|
-
|
|
710
722
|
guard let store = _store else {
|
|
711
723
|
return reject(INIT_ERROR, INIT_ERROR_MESSAGE, nil)
|
|
712
724
|
}
|
|
@@ -797,21 +809,6 @@ class ReactNativeHealthkit: RCTEventEmitter {
|
|
|
797
809
|
dict.setValue(serializeQuantity(unit: HKUnit.count(), quantity: workout.totalFlightsClimbed), forKey: "totalFlightsClimbed")
|
|
798
810
|
}
|
|
799
811
|
|
|
800
|
-
#if canImport(WorkoutKit)
|
|
801
|
-
if #available(iOS 17.0, *) {
|
|
802
|
-
Task {
|
|
803
|
-
do {
|
|
804
|
-
let workoutplan = try await workout.workoutPlan
|
|
805
|
-
if let workoutplanId = workoutplan?.id {
|
|
806
|
-
dict["workoutPlanId"] = workoutplanId.uuidString
|
|
807
|
-
}
|
|
808
|
-
} catch {
|
|
809
|
-
// handle error
|
|
810
|
-
}
|
|
811
|
-
}
|
|
812
|
-
}
|
|
813
|
-
#endif
|
|
814
|
-
|
|
815
812
|
arr.add(dict)
|
|
816
813
|
}
|
|
817
814
|
}
|
|
@@ -1263,6 +1260,63 @@ class ReactNativeHealthkit: RCTEventEmitter {
|
|
|
1263
1260
|
return allRoutes
|
|
1264
1261
|
}
|
|
1265
1262
|
|
|
1263
|
+
@available(iOS 17.0.0, *)
|
|
1264
|
+
func getWorkoutPlan(workout: HKWorkout) async -> [String: Any]? {
|
|
1265
|
+
#if canImport(WorkoutKit)
|
|
1266
|
+
do {
|
|
1267
|
+
let workoutPlan = try await workout.workoutPlan
|
|
1268
|
+
|
|
1269
|
+
var dict = [String: Any]()
|
|
1270
|
+
if (workoutPlan?.id) != nil {
|
|
1271
|
+
dict["id"] = workoutPlan?.id.uuidString
|
|
1272
|
+
|
|
1273
|
+
}
|
|
1274
|
+
if (workoutPlan?.workout.activity) != nil {
|
|
1275
|
+
dict["activityType"] = workoutPlan?.workout.activity.rawValue
|
|
1276
|
+
}
|
|
1277
|
+
|
|
1278
|
+
if dict.isEmpty {
|
|
1279
|
+
return nil
|
|
1280
|
+
}
|
|
1281
|
+
return dict
|
|
1282
|
+
} catch {
|
|
1283
|
+
return nil
|
|
1284
|
+
}
|
|
1285
|
+
#else
|
|
1286
|
+
return nil
|
|
1287
|
+
#endif
|
|
1288
|
+
}
|
|
1289
|
+
|
|
1290
|
+
@objc(getWorkoutPlanById:resolve:reject:)
|
|
1291
|
+
func getWorkoutPlanById(workoutUUID: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
|
|
1292
|
+
if #available(iOS 17.0, *) {
|
|
1293
|
+
#if canImport(WorkoutKit)
|
|
1294
|
+
guard let store = _store else {
|
|
1295
|
+
return reject(INIT_ERROR, INIT_ERROR_MESSAGE, nil)
|
|
1296
|
+
}
|
|
1297
|
+
|
|
1298
|
+
Task {
|
|
1299
|
+
if let uuid = UUID(uuidString: workoutUUID) {
|
|
1300
|
+
let workout = await self.getWorkoutByID(store: store, workoutUUID: uuid)
|
|
1301
|
+
if let workout {
|
|
1302
|
+
let workoutPlan = await self.getWorkoutPlan(workout: workout)
|
|
1303
|
+
|
|
1304
|
+
return resolve(workoutPlan)
|
|
1305
|
+
} else {
|
|
1306
|
+
return reject(GENERIC_ERROR, "No workout found", nil)
|
|
1307
|
+
}
|
|
1308
|
+
} else {
|
|
1309
|
+
return reject(GENERIC_ERROR, "Invalid UUID", nil)
|
|
1310
|
+
}
|
|
1311
|
+
}
|
|
1312
|
+
#else
|
|
1313
|
+
return resolve(nil)
|
|
1314
|
+
#endif
|
|
1315
|
+
} else {
|
|
1316
|
+
return resolve(nil)
|
|
1317
|
+
}
|
|
1318
|
+
}
|
|
1319
|
+
|
|
1266
1320
|
func serializeLocation(location: CLLocation, previousLocation: CLLocation?) -> [String: Any] {
|
|
1267
1321
|
var distance: CLLocationDistance?
|
|
1268
1322
|
if let previousLocation = previousLocation {
|
|
@@ -8,6 +8,11 @@ var _react = require("react");
|
|
|
8
8
|
var _getRequestStatusForAuthorization = _interopRequireDefault(require("../utils/getRequestStatusForAuthorization"));
|
|
9
9
|
var _requestAuthorization = _interopRequireDefault(require("../utils/requestAuthorization"));
|
|
10
10
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
|
+
/**
|
|
12
|
+
* @description Hook to retrieve the current authorization status for the given types, and request authorization if needed.
|
|
13
|
+
* @see {@link https://developer.apple.com/documentation/healthkit/hkhealthstore/1614152-requestauthorization Apple Docs - requestAuthorization}
|
|
14
|
+
* @see {@link https://developer.apple.com/documentation/healthkit/authorizing_access_to_health_data Apple Docs - Authorizing access to health data}
|
|
15
|
+
*/
|
|
11
16
|
const useHealthkitAuthorization = (read, write) => {
|
|
12
17
|
const [status, setStatus] = (0, _react.useState)(null);
|
|
13
18
|
const readMemo = (0, _react.useRef)(read);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["_react","require","_getRequestStatusForAuthorization","_interopRequireDefault","_requestAuthorization","obj","__esModule","default","useHealthkitAuthorization","read","write","status","setStatus","useState","readMemo","useRef","writeMemo","useEffect","current","refreshAuthStatus","useCallback","auth","getRequestStatusForAuthorization","request","requestAuthorization","_default","exports"],"sources":["useHealthkitAuthorization.ts"],"sourcesContent":["import {\n useCallback, useEffect, useRef, useState,\n} from 'react'\n\nimport getRequestStatusForAuthorization from '../utils/getRequestStatusForAuthorization'\nimport requestAuthorization from '../utils/requestAuthorization'\n\nimport type { HealthkitReadAuthorization, HealthkitWriteAuthorization, HKAuthorizationRequestStatus } from '../native-types'\n\nconst useHealthkitAuthorization = (read: readonly HealthkitReadAuthorization[], write?: readonly HealthkitWriteAuthorization[]) => {\n const [status, setStatus] = useState<HKAuthorizationRequestStatus | null>(null)\n\n const readMemo = useRef(read)\n const writeMemo = useRef(write)\n\n useEffect(() => {\n readMemo.current = read\n writeMemo.current = write\n }, [read, write])\n\n const refreshAuthStatus = useCallback(async () => {\n const auth = await getRequestStatusForAuthorization(readMemo.current, writeMemo.current)\n\n setStatus(auth)\n return auth\n }, [])\n\n const request = useCallback(async () => {\n await requestAuthorization(readMemo.current, writeMemo.current)\n return refreshAuthStatus()\n }, [refreshAuthStatus])\n\n useEffect(() => {\n void refreshAuthStatus()\n }, [refreshAuthStatus])\n\n return [status, request] as const\n}\n\nexport default useHealthkitAuthorization\n"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AAIA,IAAAC,iCAAA,GAAAC,sBAAA,CAAAF,OAAA;AACA,IAAAG,qBAAA,GAAAD,sBAAA,CAAAF,OAAA;AAAgE,SAAAE,uBAAAE,GAAA,WAAAA,GAAA,IAAAA,GAAA,CAAAC,UAAA,GAAAD,GAAA,KAAAE,OAAA,EAAAF,GAAA;AAIhE,MAAMG,yBAAyB,GAAGA,CAACC,IAA2C,EAAEC,KAA8C,KAAK;EACjI,MAAM,CAACC,MAAM,EAAEC,SAAS,CAAC,GAAG,IAAAC,eAAQ,EAAsC,IAAI,CAAC;EAE/E,MAAMC,QAAQ,GAAG,IAAAC,aAAM,EAACN,IAAI,CAAC;EAC7B,MAAMO,SAAS,GAAG,IAAAD,aAAM,EAACL,KAAK,CAAC;EAE/B,IAAAO,gBAAS,EAAC,MAAM;IACdH,QAAQ,CAACI,OAAO,GAAGT,IAAI;IACvBO,SAAS,CAACE,OAAO,GAAGR,KAAK;EAC3B,CAAC,EAAE,CAACD,IAAI,EAAEC,KAAK,CAAC,CAAC;EAEjB,MAAMS,iBAAiB,GAAG,IAAAC,kBAAW,EAAC,YAAY;IAChD,MAAMC,IAAI,GAAG,MAAM,IAAAC,yCAAgC,EAACR,QAAQ,CAACI,OAAO,EAAEF,SAAS,CAACE,OAAO,CAAC;IAExFN,SAAS,CAACS,IAAI,CAAC;IACf,OAAOA,IAAI;EACb,CAAC,EAAE,EAAE,CAAC;EAEN,MAAME,OAAO,GAAG,IAAAH,kBAAW,EAAC,YAAY;IACtC,MAAM,IAAAI,6BAAoB,EAACV,QAAQ,CAACI,OAAO,EAAEF,SAAS,CAACE,OAAO,CAAC;IAC/D,OAAOC,iBAAiB,CAAC,CAAC;EAC5B,CAAC,EAAE,CAACA,iBAAiB,CAAC,CAAC;EAEvB,IAAAF,gBAAS,EAAC,MAAM;IACd,KAAKE,iBAAiB,CAAC,CAAC;EAC1B,CAAC,EAAE,CAACA,iBAAiB,CAAC,CAAC;EAEvB,OAAO,CAACR,MAAM,EAAEY,OAAO,CAAC;AAC1B,CAAC;AAAA,IAAAE,QAAA,GAEcjB,yBAAyB;AAAAkB,OAAA,CAAAnB,OAAA,GAAAkB,QAAA"}
|
|
1
|
+
{"version":3,"names":["_react","require","_getRequestStatusForAuthorization","_interopRequireDefault","_requestAuthorization","obj","__esModule","default","useHealthkitAuthorization","read","write","status","setStatus","useState","readMemo","useRef","writeMemo","useEffect","current","refreshAuthStatus","useCallback","auth","getRequestStatusForAuthorization","request","requestAuthorization","_default","exports"],"sources":["useHealthkitAuthorization.ts"],"sourcesContent":["import {\n useCallback, useEffect, useRef, useState,\n} from 'react'\n\nimport getRequestStatusForAuthorization from '../utils/getRequestStatusForAuthorization'\nimport requestAuthorization from '../utils/requestAuthorization'\n\nimport type { HealthkitReadAuthorization, HealthkitWriteAuthorization, HKAuthorizationRequestStatus } from '../native-types'\n\n/**\n * @description Hook to retrieve the current authorization status for the given types, and request authorization if needed.\n * @see {@link https://developer.apple.com/documentation/healthkit/hkhealthstore/1614152-requestauthorization Apple Docs - requestAuthorization}\n * @see {@link https://developer.apple.com/documentation/healthkit/authorizing_access_to_health_data Apple Docs - Authorizing access to health data}\n */\nconst useHealthkitAuthorization = (read: readonly HealthkitReadAuthorization[], write?: readonly HealthkitWriteAuthorization[]) => {\n const [status, setStatus] = useState<HKAuthorizationRequestStatus | null>(null)\n\n const readMemo = useRef(read)\n const writeMemo = useRef(write)\n\n useEffect(() => {\n readMemo.current = read\n writeMemo.current = write\n }, [read, write])\n\n const refreshAuthStatus = useCallback(async () => {\n const auth = await getRequestStatusForAuthorization(readMemo.current, writeMemo.current)\n\n setStatus(auth)\n return auth\n }, [])\n\n const request = useCallback(async () => {\n await requestAuthorization(readMemo.current, writeMemo.current)\n return refreshAuthStatus()\n }, [refreshAuthStatus])\n\n useEffect(() => {\n void refreshAuthStatus()\n }, [refreshAuthStatus])\n\n return [status, request] as const\n}\n\nexport default useHealthkitAuthorization\n"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AAIA,IAAAC,iCAAA,GAAAC,sBAAA,CAAAF,OAAA;AACA,IAAAG,qBAAA,GAAAD,sBAAA,CAAAF,OAAA;AAAgE,SAAAE,uBAAAE,GAAA,WAAAA,GAAA,IAAAA,GAAA,CAAAC,UAAA,GAAAD,GAAA,KAAAE,OAAA,EAAAF,GAAA;AAIhE;AACA;AACA;AACA;AACA;AACA,MAAMG,yBAAyB,GAAGA,CAACC,IAA2C,EAAEC,KAA8C,KAAK;EACjI,MAAM,CAACC,MAAM,EAAEC,SAAS,CAAC,GAAG,IAAAC,eAAQ,EAAsC,IAAI,CAAC;EAE/E,MAAMC,QAAQ,GAAG,IAAAC,aAAM,EAACN,IAAI,CAAC;EAC7B,MAAMO,SAAS,GAAG,IAAAD,aAAM,EAACL,KAAK,CAAC;EAE/B,IAAAO,gBAAS,EAAC,MAAM;IACdH,QAAQ,CAACI,OAAO,GAAGT,IAAI;IACvBO,SAAS,CAACE,OAAO,GAAGR,KAAK;EAC3B,CAAC,EAAE,CAACD,IAAI,EAAEC,KAAK,CAAC,CAAC;EAEjB,MAAMS,iBAAiB,GAAG,IAAAC,kBAAW,EAAC,YAAY;IAChD,MAAMC,IAAI,GAAG,MAAM,IAAAC,yCAAgC,EAACR,QAAQ,CAACI,OAAO,EAAEF,SAAS,CAACE,OAAO,CAAC;IAExFN,SAAS,CAACS,IAAI,CAAC;IACf,OAAOA,IAAI;EACb,CAAC,EAAE,EAAE,CAAC;EAEN,MAAME,OAAO,GAAG,IAAAH,kBAAW,EAAC,YAAY;IACtC,MAAM,IAAAI,6BAAoB,EAACV,QAAQ,CAACI,OAAO,EAAEF,SAAS,CAACE,OAAO,CAAC;IAC/D,OAAOC,iBAAiB,CAAC,CAAC;EAC5B,CAAC,EAAE,CAACA,iBAAiB,CAAC,CAAC;EAEvB,IAAAF,gBAAS,EAAC,MAAM;IACd,KAAKE,iBAAiB,CAAC,CAAC;EAC1B,CAAC,EAAE,CAACA,iBAAiB,CAAC,CAAC;EAEvB,OAAO,CAACR,MAAM,EAAEY,OAAO,CAAC;AAC1B,CAAC;AAAA,IAAAE,QAAA,GAEcjB,yBAAyB;AAAAkB,OAAA,CAAAnB,OAAA,GAAAkB,QAAA"}
|
|
@@ -8,6 +8,9 @@ var _react = require("react");
|
|
|
8
8
|
var _useSubscribeToChanges = _interopRequireDefault(require("./useSubscribeToChanges"));
|
|
9
9
|
var _getMostRecentCategorySample = _interopRequireDefault(require("../utils/getMostRecentCategorySample"));
|
|
10
10
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
|
+
/**
|
|
12
|
+
* @returns the most recent sample for the given category type.
|
|
13
|
+
*/
|
|
11
14
|
function useMostRecentCategorySample(identifier) {
|
|
12
15
|
const [category, setCategory] = (0, _react.useState)(null);
|
|
13
16
|
const updater = (0, _react.useCallback)(() => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["_react","require","_useSubscribeToChanges","_interopRequireDefault","_getMostRecentCategorySample","obj","__esModule","default","useMostRecentCategorySample","identifier","category","setCategory","useState","updater","useCallback","getMostRecentCategorySample","then","useSubscribeToChanges","_default","exports"],"sources":["useMostRecentCategorySample.ts"],"sourcesContent":["import { useCallback, useState } from 'react'\n\nimport useSubscribeToChanges from './useSubscribeToChanges'\nimport getMostRecentCategorySample from '../utils/getMostRecentCategorySample'\n\nimport type { HKCategoryTypeIdentifier } from '../native-types'\nimport type { HKCategorySample } from '../types'\n\nfunction useMostRecentCategorySample<\n TCategory extends HKCategoryTypeIdentifier\n>(identifier: TCategory) {\n const [category, setCategory] = useState<HKCategorySample<TCategory> | null>(\n null,\n )\n const updater = useCallback(() => {\n void getMostRecentCategorySample(identifier).then(setCategory)\n }, [identifier])\n\n useSubscribeToChanges(identifier, updater)\n\n return category\n}\n\nexport default useMostRecentCategorySample\n"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AAEA,IAAAC,sBAAA,GAAAC,sBAAA,CAAAF,OAAA;AACA,IAAAG,4BAAA,GAAAD,sBAAA,CAAAF,OAAA;AAA8E,SAAAE,uBAAAE,GAAA,WAAAA,GAAA,IAAAA,GAAA,CAAAC,UAAA,GAAAD,GAAA,KAAAE,OAAA,EAAAF,GAAA;AAK9E,SAASG,2BAA2BA,CAElCC,UAAqB,EAAE;EACvB,MAAM,CAACC,QAAQ,EAAEC,WAAW,CAAC,GAAG,IAAAC,eAAQ,EACtC,IACF,CAAC;EACD,MAAMC,OAAO,GAAG,IAAAC,kBAAW,EAAC,MAAM;IAChC,KAAK,IAAAC,oCAA2B,EAACN,UAAU,CAAC,CAACO,IAAI,CAACL,WAAW,CAAC;EAChE,CAAC,EAAE,CAACF,UAAU,CAAC,CAAC;EAEhB,IAAAQ,8BAAqB,EAACR,UAAU,EAAEI,OAAO,CAAC;EAE1C,OAAOH,QAAQ;AACjB;AAAC,IAAAQ,QAAA,GAEcV,2BAA2B;AAAAW,OAAA,CAAAZ,OAAA,GAAAW,QAAA"}
|
|
1
|
+
{"version":3,"names":["_react","require","_useSubscribeToChanges","_interopRequireDefault","_getMostRecentCategorySample","obj","__esModule","default","useMostRecentCategorySample","identifier","category","setCategory","useState","updater","useCallback","getMostRecentCategorySample","then","useSubscribeToChanges","_default","exports"],"sources":["useMostRecentCategorySample.ts"],"sourcesContent":["import { useCallback, useState } from 'react'\n\nimport useSubscribeToChanges from './useSubscribeToChanges'\nimport getMostRecentCategorySample from '../utils/getMostRecentCategorySample'\n\nimport type { HKCategoryTypeIdentifier } from '../native-types'\nimport type { HKCategorySample } from '../types'\n\n/**\n * @returns the most recent sample for the given category type.\n */\nfunction useMostRecentCategorySample<\n TCategory extends HKCategoryTypeIdentifier\n>(identifier: TCategory) {\n const [category, setCategory] = useState<HKCategorySample<TCategory> | null>(\n null,\n )\n const updater = useCallback(() => {\n void getMostRecentCategorySample(identifier).then(setCategory)\n }, [identifier])\n\n useSubscribeToChanges(identifier, updater)\n\n return category\n}\n\nexport default useMostRecentCategorySample\n"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AAEA,IAAAC,sBAAA,GAAAC,sBAAA,CAAAF,OAAA;AACA,IAAAG,4BAAA,GAAAD,sBAAA,CAAAF,OAAA;AAA8E,SAAAE,uBAAAE,GAAA,WAAAA,GAAA,IAAAA,GAAA,CAAAC,UAAA,GAAAD,GAAA,KAAAE,OAAA,EAAAF,GAAA;AAK9E;AACA;AACA;AACA,SAASG,2BAA2BA,CAElCC,UAAqB,EAAE;EACvB,MAAM,CAACC,QAAQ,EAAEC,WAAW,CAAC,GAAG,IAAAC,eAAQ,EACtC,IACF,CAAC;EACD,MAAMC,OAAO,GAAG,IAAAC,kBAAW,EAAC,MAAM;IAChC,KAAK,IAAAC,oCAA2B,EAACN,UAAU,CAAC,CAACO,IAAI,CAACL,WAAW,CAAC;EAChE,CAAC,EAAE,CAACF,UAAU,CAAC,CAAC;EAEhB,IAAAQ,8BAAqB,EAACR,UAAU,EAAEI,OAAO,CAAC;EAE1C,OAAOH,QAAQ;AACjB;AAAC,IAAAQ,QAAA,GAEcV,2BAA2B;AAAAW,OAAA,CAAAZ,OAAA,GAAAW,QAAA"}
|
|
@@ -9,6 +9,9 @@ var _ensureUnit = _interopRequireDefault(require("../utils/ensureUnit"));
|
|
|
9
9
|
var _getMostRecentQuantitySample = _interopRequireDefault(require("../utils/getMostRecentQuantitySample"));
|
|
10
10
|
var _subscribeToChanges = _interopRequireDefault(require("../utils/subscribeToChanges"));
|
|
11
11
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
12
|
+
/**
|
|
13
|
+
* @returns the most recent sample for the given quantity type.
|
|
14
|
+
*/
|
|
12
15
|
function useMostRecentQuantitySample(identifier, unit) {
|
|
13
16
|
const [lastSample, setLastSample] = (0, _react.useState)(null);
|
|
14
17
|
(0, _react.useEffect)(() => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["_react","require","_ensureUnit","_interopRequireDefault","_getMostRecentQuantitySample","_subscribeToChanges","obj","__esModule","default","useMostRecentQuantitySample","identifier","unit","lastSample","setLastSample","useState","useEffect","cancelSubscription","init","actualUnit","ensureUnit","subscribeToChanges","value","getMostRecentQuantitySample","_cancelSubscription","_default","exports"],"sources":["useMostRecentQuantitySample.ts"],"sourcesContent":["import { useEffect, useState } from 'react'\n\nimport ensureUnit from '../utils/ensureUnit'\nimport getMostRecentQuantitySample from '../utils/getMostRecentQuantitySample'\nimport subscribeToChanges from '../utils/subscribeToChanges'\n\nimport type { HKQuantityTypeIdentifier, UnitForIdentifier } from '../native-types'\nimport type { HKQuantitySample } from '../types'\n\nfunction useMostRecentQuantitySample<\n TIdentifier extends HKQuantityTypeIdentifier,\n TUnit extends UnitForIdentifier<TIdentifier>\n>(identifier: TIdentifier, unit?: TUnit) {\n const [lastSample, setLastSample] = useState<HKQuantitySample<\n TIdentifier\n > | null>(null)\n\n useEffect(() => {\n let cancelSubscription: (() => Promise<boolean>) | undefined\n\n const init = async () => {\n const actualUnit = await ensureUnit(identifier, unit)\n\n cancelSubscription = await subscribeToChanges(identifier, async () => {\n const value = await getMostRecentQuantitySample(identifier, actualUnit)\n setLastSample(value)\n })\n }\n void init()\n\n return () => {\n void cancelSubscription?.()\n }\n }, [identifier, unit])\n\n return lastSample\n}\n\nexport default useMostRecentQuantitySample\n"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AAEA,IAAAC,WAAA,GAAAC,sBAAA,CAAAF,OAAA;AACA,IAAAG,4BAAA,GAAAD,sBAAA,CAAAF,OAAA;AACA,IAAAI,mBAAA,GAAAF,sBAAA,CAAAF,OAAA;AAA4D,SAAAE,uBAAAG,GAAA,WAAAA,GAAA,IAAAA,GAAA,CAAAC,UAAA,GAAAD,GAAA,KAAAE,OAAA,EAAAF,GAAA;AAK5D,SAASG,2BAA2BA,CAGlCC,UAAuB,EAAEC,IAAY,EAAE;EACvC,MAAM,CAACC,UAAU,EAAEC,aAAa,CAAC,GAAG,IAAAC,eAAQ,EAElC,IAAI,CAAC;EAEf,IAAAC,gBAAS,EAAC,MAAM;IACd,IAAIC,kBAAwD;IAE5D,MAAMC,IAAI,GAAG,MAAAA,CAAA,KAAY;MACvB,MAAMC,UAAU,GAAG,MAAM,IAAAC,mBAAU,EAACT,UAAU,EAAEC,IAAI,CAAC;MAErDK,kBAAkB,GAAG,MAAM,IAAAI,2BAAkB,EAACV,UAAU,EAAE,YAAY;QACpE,MAAMW,KAAK,GAAG,MAAM,IAAAC,oCAA2B,EAACZ,UAAU,EAAEQ,UAAU,CAAC;QACvEL,aAAa,CAACQ,KAAK,CAAC;MACtB,CAAC,CAAC;IACJ,CAAC;IACD,KAAKJ,IAAI,CAAC,CAAC;IAEX,OAAO,MAAM;MAAA,IAAAM,mBAAA;MACX,OAAAA,mBAAA,GAAKP,kBAAkB,cAAAO,mBAAA,uBAAlBA,mBAAA,CAAqB,CAAC;IAC7B,CAAC;EACH,CAAC,EAAE,CAACb,UAAU,EAAEC,IAAI,CAAC,CAAC;EAEtB,OAAOC,UAAU;AACnB;AAAC,IAAAY,QAAA,GAEcf,2BAA2B;AAAAgB,OAAA,CAAAjB,OAAA,GAAAgB,QAAA"}
|
|
1
|
+
{"version":3,"names":["_react","require","_ensureUnit","_interopRequireDefault","_getMostRecentQuantitySample","_subscribeToChanges","obj","__esModule","default","useMostRecentQuantitySample","identifier","unit","lastSample","setLastSample","useState","useEffect","cancelSubscription","init","actualUnit","ensureUnit","subscribeToChanges","value","getMostRecentQuantitySample","_cancelSubscription","_default","exports"],"sources":["useMostRecentQuantitySample.ts"],"sourcesContent":["import { useEffect, useState } from 'react'\n\nimport ensureUnit from '../utils/ensureUnit'\nimport getMostRecentQuantitySample from '../utils/getMostRecentQuantitySample'\nimport subscribeToChanges from '../utils/subscribeToChanges'\n\nimport type { HKQuantityTypeIdentifier, UnitForIdentifier } from '../native-types'\nimport type { HKQuantitySample } from '../types'\n\n/**\n * @returns the most recent sample for the given quantity type.\n */\nfunction useMostRecentQuantitySample<\n TIdentifier extends HKQuantityTypeIdentifier,\n TUnit extends UnitForIdentifier<TIdentifier>\n>(identifier: TIdentifier, unit?: TUnit) {\n const [lastSample, setLastSample] = useState<HKQuantitySample<\n TIdentifier\n > | null>(null)\n\n useEffect(() => {\n let cancelSubscription: (() => Promise<boolean>) | undefined\n\n const init = async () => {\n const actualUnit = await ensureUnit(identifier, unit)\n\n cancelSubscription = await subscribeToChanges(identifier, async () => {\n const value = await getMostRecentQuantitySample(identifier, actualUnit)\n setLastSample(value)\n })\n }\n void init()\n\n return () => {\n void cancelSubscription?.()\n }\n }, [identifier, unit])\n\n return lastSample\n}\n\nexport default useMostRecentQuantitySample\n"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AAEA,IAAAC,WAAA,GAAAC,sBAAA,CAAAF,OAAA;AACA,IAAAG,4BAAA,GAAAD,sBAAA,CAAAF,OAAA;AACA,IAAAI,mBAAA,GAAAF,sBAAA,CAAAF,OAAA;AAA4D,SAAAE,uBAAAG,GAAA,WAAAA,GAAA,IAAAA,GAAA,CAAAC,UAAA,GAAAD,GAAA,KAAAE,OAAA,EAAAF,GAAA;AAK5D;AACA;AACA;AACA,SAASG,2BAA2BA,CAGlCC,UAAuB,EAAEC,IAAY,EAAE;EACvC,MAAM,CAACC,UAAU,EAAEC,aAAa,CAAC,GAAG,IAAAC,eAAQ,EAElC,IAAI,CAAC;EAEf,IAAAC,gBAAS,EAAC,MAAM;IACd,IAAIC,kBAAwD;IAE5D,MAAMC,IAAI,GAAG,MAAAA,CAAA,KAAY;MACvB,MAAMC,UAAU,GAAG,MAAM,IAAAC,mBAAU,EAACT,UAAU,EAAEC,IAAI,CAAC;MAErDK,kBAAkB,GAAG,MAAM,IAAAI,2BAAkB,EAACV,UAAU,EAAE,YAAY;QACpE,MAAMW,KAAK,GAAG,MAAM,IAAAC,oCAA2B,EAACZ,UAAU,EAAEQ,UAAU,CAAC;QACvEL,aAAa,CAACQ,KAAK,CAAC;MACtB,CAAC,CAAC;IACJ,CAAC;IACD,KAAKJ,IAAI,CAAC,CAAC;IAEX,OAAO,MAAM;MAAA,IAAAM,mBAAA;MACX,OAAAA,mBAAA,GAAKP,kBAAkB,cAAAO,mBAAA,uBAAlBA,mBAAA,CAAqB,CAAC;IAC7B,CAAC;EACH,CAAC,EAAE,CAACb,UAAU,EAAEC,IAAI,CAAC,CAAC;EAEtB,OAAOC,UAAU;AACnB;AAAC,IAAAY,QAAA,GAEcf,2BAA2B;AAAAgB,OAAA,CAAAjB,OAAA,GAAAgB,QAAA"}
|
|
@@ -9,6 +9,9 @@ var _getMostRecentWorkout = _interopRequireDefault(require("../utils/getMostRece
|
|
|
9
9
|
var _getPreferredUnitsTyped = _interopRequireDefault(require("../utils/getPreferredUnitsTyped"));
|
|
10
10
|
var _subscribeToChanges = _interopRequireDefault(require("../utils/subscribeToChanges"));
|
|
11
11
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
12
|
+
/**
|
|
13
|
+
* @returns the most recent workout sample.
|
|
14
|
+
*/
|
|
12
15
|
function useMostRecentWorkout(options) {
|
|
13
16
|
const [workout, setWorkout] = (0, _react.useState)(null);
|
|
14
17
|
const optionsRef = (0, _react.useRef)(options);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["_react","require","_getMostRecentWorkout","_interopRequireDefault","_getPreferredUnitsTyped","_subscribeToChanges","obj","__esModule","default","useMostRecentWorkout","options","workout","setWorkout","useState","optionsRef","useRef","useEffect","current","update","useCallback","energyUnit","distanceUnit","getPreferredUnitsTyped","getMostRecentWorkout","cancelSubscription","init","subscribeToChanges","_cancelSubscription","_default","exports"],"sources":["useMostRecentWorkout.ts"],"sourcesContent":["import {\n useEffect, useState, useRef, useCallback,\n} from 'react'\n\nimport getMostRecentWorkout from '../utils/getMostRecentWorkout'\nimport getPreferredUnitsTyped from '../utils/getPreferredUnitsTyped'\nimport subscribeToChanges from '../utils/subscribeToChanges'\n\nimport type { EnergyUnit, LengthUnit } from '../native-types'\nimport type { HKWorkout } from '../types'\n\nfunction useMostRecentWorkout<\n TEnergy extends EnergyUnit,\n TDistance extends LengthUnit\n>(options?: { readonly energyUnit?: TEnergy; readonly distanceUnit?: TDistance }) {\n const [workout, setWorkout] = useState<HKWorkout<TEnergy, TDistance> | null>(null)\n\n const optionsRef = useRef(options)\n\n useEffect(() => {\n optionsRef.current = options\n }, [options])\n\n const update = useCallback(async () => {\n const { energyUnit, distanceUnit } = await getPreferredUnitsTyped(\n optionsRef.current,\n )\n\n setWorkout(await getMostRecentWorkout({\n energyUnit,\n distanceUnit,\n }))\n }, [])\n\n useEffect(() => {\n void update()\n }, [update])\n\n useEffect(() => {\n let cancelSubscription: (() => Promise<boolean>) | undefined\n\n const init = async () => {\n cancelSubscription = await subscribeToChanges(\n 'HKWorkoutTypeIdentifier',\n update,\n )\n }\n void init()\n\n return () => {\n void cancelSubscription?.()\n }\n }, [update])\n\n return workout\n}\n\nexport default useMostRecentWorkout\n"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AAIA,IAAAC,qBAAA,GAAAC,sBAAA,CAAAF,OAAA;AACA,IAAAG,uBAAA,GAAAD,sBAAA,CAAAF,OAAA;AACA,IAAAI,mBAAA,GAAAF,sBAAA,CAAAF,OAAA;AAA4D,SAAAE,uBAAAG,GAAA,WAAAA,GAAA,IAAAA,GAAA,CAAAC,UAAA,GAAAD,GAAA,KAAAE,OAAA,EAAAF,GAAA;AAK5D,SAASG,oBAAoBA,CAG3BC,OAA8E,EAAE;EAChF,MAAM,CAACC,OAAO,EAAEC,UAAU,CAAC,GAAG,IAAAC,eAAQ,EAAuC,IAAI,CAAC;EAElF,MAAMC,UAAU,GAAG,IAAAC,aAAM,EAACL,OAAO,CAAC;EAElC,IAAAM,gBAAS,EAAC,MAAM;IACdF,UAAU,CAACG,OAAO,GAAGP,OAAO;EAC9B,CAAC,EAAE,CAACA,OAAO,CAAC,CAAC;EAEb,MAAMQ,MAAM,GAAG,IAAAC,kBAAW,EAAC,YAAY;IACrC,MAAM;MAAEC,UAAU;MAAEC;IAAa,CAAC,GAAG,MAAM,IAAAC,+BAAsB,EAC/DR,UAAU,CAACG,OACb,CAAC;IAEDL,UAAU,CAAC,MAAM,IAAAW,6BAAoB,EAAC;MACpCH,UAAU;MACVC;IACF,CAAC,CAAC,CAAC;EACL,CAAC,EAAE,EAAE,CAAC;EAEN,IAAAL,gBAAS,EAAC,MAAM;IACd,KAAKE,MAAM,CAAC,CAAC;EACf,CAAC,EAAE,CAACA,MAAM,CAAC,CAAC;EAEZ,IAAAF,gBAAS,EAAC,MAAM;IACd,IAAIQ,kBAAwD;IAE5D,MAAMC,IAAI,GAAG,MAAAA,CAAA,KAAY;MACvBD,kBAAkB,GAAG,MAAM,IAAAE,2BAAkB,EAC3C,yBAAyB,EACzBR,MACF,CAAC;IACH,CAAC;IACD,KAAKO,IAAI,CAAC,CAAC;IAEX,OAAO,MAAM;MAAA,IAAAE,mBAAA;MACX,OAAAA,mBAAA,GAAKH,kBAAkB,cAAAG,mBAAA,uBAAlBA,mBAAA,CAAqB,CAAC;IAC7B,CAAC;EACH,CAAC,EAAE,CAACT,MAAM,CAAC,CAAC;EAEZ,OAAOP,OAAO;AAChB;AAAC,IAAAiB,QAAA,GAEcnB,oBAAoB;AAAAoB,OAAA,CAAArB,OAAA,GAAAoB,QAAA"}
|
|
1
|
+
{"version":3,"names":["_react","require","_getMostRecentWorkout","_interopRequireDefault","_getPreferredUnitsTyped","_subscribeToChanges","obj","__esModule","default","useMostRecentWorkout","options","workout","setWorkout","useState","optionsRef","useRef","useEffect","current","update","useCallback","energyUnit","distanceUnit","getPreferredUnitsTyped","getMostRecentWorkout","cancelSubscription","init","subscribeToChanges","_cancelSubscription","_default","exports"],"sources":["useMostRecentWorkout.ts"],"sourcesContent":["import {\n useEffect, useState, useRef, useCallback,\n} from 'react'\n\nimport getMostRecentWorkout from '../utils/getMostRecentWorkout'\nimport getPreferredUnitsTyped from '../utils/getPreferredUnitsTyped'\nimport subscribeToChanges from '../utils/subscribeToChanges'\n\nimport type { EnergyUnit, LengthUnit } from '../native-types'\nimport type { HKWorkout } from '../types'\n\n/**\n * @returns the most recent workout sample.\n */\nfunction useMostRecentWorkout<\n TEnergy extends EnergyUnit,\n TDistance extends LengthUnit\n>(options?: { readonly energyUnit?: TEnergy; readonly distanceUnit?: TDistance }) {\n const [workout, setWorkout] = useState<HKWorkout<TEnergy, TDistance> | null>(null)\n\n const optionsRef = useRef(options)\n\n useEffect(() => {\n optionsRef.current = options\n }, [options])\n\n const update = useCallback(async () => {\n const { energyUnit, distanceUnit } = await getPreferredUnitsTyped(\n optionsRef.current,\n )\n\n setWorkout(await getMostRecentWorkout({\n energyUnit,\n distanceUnit,\n }))\n }, [])\n\n useEffect(() => {\n void update()\n }, [update])\n\n useEffect(() => {\n let cancelSubscription: (() => Promise<boolean>) | undefined\n\n const init = async () => {\n cancelSubscription = await subscribeToChanges(\n 'HKWorkoutTypeIdentifier',\n update,\n )\n }\n void init()\n\n return () => {\n void cancelSubscription?.()\n }\n }, [update])\n\n return workout\n}\n\nexport default useMostRecentWorkout\n"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AAIA,IAAAC,qBAAA,GAAAC,sBAAA,CAAAF,OAAA;AACA,IAAAG,uBAAA,GAAAD,sBAAA,CAAAF,OAAA;AACA,IAAAI,mBAAA,GAAAF,sBAAA,CAAAF,OAAA;AAA4D,SAAAE,uBAAAG,GAAA,WAAAA,GAAA,IAAAA,GAAA,CAAAC,UAAA,GAAAD,GAAA,KAAAE,OAAA,EAAAF,GAAA;AAK5D;AACA;AACA;AACA,SAASG,oBAAoBA,CAG3BC,OAA8E,EAAE;EAChF,MAAM,CAACC,OAAO,EAAEC,UAAU,CAAC,GAAG,IAAAC,eAAQ,EAAuC,IAAI,CAAC;EAElF,MAAMC,UAAU,GAAG,IAAAC,aAAM,EAACL,OAAO,CAAC;EAElC,IAAAM,gBAAS,EAAC,MAAM;IACdF,UAAU,CAACG,OAAO,GAAGP,OAAO;EAC9B,CAAC,EAAE,CAACA,OAAO,CAAC,CAAC;EAEb,MAAMQ,MAAM,GAAG,IAAAC,kBAAW,EAAC,YAAY;IACrC,MAAM;MAAEC,UAAU;MAAEC;IAAa,CAAC,GAAG,MAAM,IAAAC,+BAAsB,EAC/DR,UAAU,CAACG,OACb,CAAC;IAEDL,UAAU,CAAC,MAAM,IAAAW,6BAAoB,EAAC;MACpCH,UAAU;MACVC;IACF,CAAC,CAAC,CAAC;EACL,CAAC,EAAE,EAAE,CAAC;EAEN,IAAAL,gBAAS,EAAC,MAAM;IACd,KAAKE,MAAM,CAAC,CAAC;EACf,CAAC,EAAE,CAACA,MAAM,CAAC,CAAC;EAEZ,IAAAF,gBAAS,EAAC,MAAM;IACd,IAAIQ,kBAAwD;IAE5D,MAAMC,IAAI,GAAG,MAAAA,CAAA,KAAY;MACvBD,kBAAkB,GAAG,MAAM,IAAAE,2BAAkB,EAC3C,yBAAyB,EACzBR,MACF,CAAC;IACH,CAAC;IACD,KAAKO,IAAI,CAAC,CAAC;IAEX,OAAO,MAAM;MAAA,IAAAE,mBAAA;MACX,OAAAA,mBAAA,GAAKH,kBAAkB,cAAAG,mBAAA,uBAAlBA,mBAAA,CAAqB,CAAC;IAC7B,CAAC;EACH,CAAC,EAAE,CAACT,MAAM,CAAC,CAAC;EAEZ,OAAOP,OAAO;AAChB;AAAC,IAAAiB,QAAA,GAEcnB,oBAAoB;AAAAoB,OAAA,CAAArB,OAAA,GAAAoB,QAAA"}
|