Skip to content

Commit 7d2d8b2

Browse files
author
Ivan Mikhailovskii
committed
feat: check authorization status
1 parent 6fe519f commit 7d2d8b2

7 files changed

Lines changed: 68 additions & 3 deletions

File tree

ios/SQFitnessStatistic.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,9 @@ @interface RCT_EXTERN_MODULE (SQFitnessStatistic, NSObject)
2020
(RCTPromiseResolveBlock)resolve
2121
reject:(RCTPromiseRejectBlock)reject)
2222

23+
RCT_EXTERN_METHOD(authorizationStatus:
24+
(NSString*)type
25+
resolve:(RCTPromiseResolveBlock)resolve
26+
reject:(RCTPromiseRejectBlock)reject)
27+
2328
@end

ios/SQFitnessStatistic.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@ public class SQFitnessStatistic: NSObject {
3131
)
3232
}
3333

34+
@objc
35+
public func authorizationStatus(
36+
_ type: String,
37+
resolve: @escaping RCTPromiseResolveBlock,
38+
reject: @escaping RCTPromiseRejectBlock
39+
) {
40+
guard let type = HKTypeData(rawValue: type) else {
41+
resolve(false)
42+
return
43+
}
44+
45+
resolve(self.healthKitManager.authorizationStatus(for: type))
46+
}
47+
3448
@objc
3549
public func getWorkoutData(
3650
_ type: String,
@@ -51,6 +65,10 @@ public class SQFitnessStatistic: NSObject {
5165
return
5266
}
5367

68+
if !self.healthKitManager.authorizationStatus(for: .workout) {
69+
return
70+
}
71+
5472
self.healthKitManager.queryWorkouts(
5573
type,
5674
start: startDate,

ios/SQHealthKitManager.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ class SQHealthKitManager {
3737
}
3838
}
3939

40+
public func authorizationStatus(for readType: HKTypeData) -> Bool {
41+
guard let objectType = self.transformDataKeyToHKSampleType(readType.rawValue) else { return false }
42+
let status = self.healthStore.authorizationStatus(for: objectType)
43+
44+
return status == .sharingAuthorized
45+
}
46+
4047
public func queryWorkouts(
4148
_ workoutDataType: WorkoutDataType,
4249
start: Date,
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { NativeModules } from 'react-native';
2+
import FitnessDataType from "react-native-sq-core-motion/src/entity/FitnessDataType"
3+
4+
/** @internal */
5+
const { SQFitnessStatistic } = NativeModules;
6+
7+
export const authorizationStatus = async (
8+
type: FitnessDataType
9+
): Promise<boolean> => {
10+
return SQFitnessStatistic.authorizationStatus(type) ?? false
11+
}

src/api/SQFitnessStatistic/getFitnessData.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const { SQFitnessStatistic } = NativeModules;
77
export const getFitnessData = async (): Promise<{ [key: string]: any }> => {
88
const startDate = "2024-08-17T09:45:00Z"
99
const endDate = "2024-09-19T12:45:00Z"
10-
const type = FitnessDataType.StepCount
10+
const type = FitnessDataType.STEP_COUNT
1111

1212
return SQFitnessStatistic.getFitnessData(type, startDate, endDate);
1313
};

src/api/SQFitnessStatistic/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ export * from './workout';
22

33
export * from './authorize'
44

5+
export * from './authorizationStatus'
6+
57
export * from './getFitnessData'

src/entity/FitnessDataType.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
11
enum FitnessDataType {
2-
StepCount = 'HKQuantityTypeIdentifierStepCount',
3-
DistanceWalkingRunning = 'HKQuantityTypeIdentifierDistanceWalkingRunning'
2+
WORKOUT = "Workout",
3+
STEP_COUNT = "HKQuantityTypeIdentifierStepCount",
4+
DISTANCE_WALKING_RUNNING = "HKQuantityTypeIdentifierDistanceWalkingRunning",
5+
RUNNING_SPEED = "HKQuantityTypeIdentifierRunningSpeed",
6+
RUNNING_STRIDE_LENGTH = "HKQuantityTypeIdentifierRunningStrideLength",
7+
RUNNING_POWER = "HKQuantityTypeIdentifierRunningPower",
8+
RUNNING_GROUND_CONTACT_TIME = "HKQuantityTypeIdentifierRunningGroundContactTime",
9+
RUNNING_VERTICAL_OSCILLATION = "HKQuantityTypeIdentifierRunningVerticalOscillation",
10+
DISTANCE_CYCLING = "HKQuantityTypeIdentifierDistanceCycling",
11+
PUSH_COUNT = "HKQuantityTypeIdentifierPushCount",
12+
DISTANCE_WHEEL_CHAIR = "HKQuantityTypeIdentifierDistanceWheelchair",
13+
SWIMMING_STROKE_COUNT = "HKQuantityTypeIdentifierSwimmingStrokeCount",
14+
DISTANCE_SWIMMING = "HKQuantityTypeIdentifierDistanceSwimming",
15+
DISTANCE_DOWNHILL_SNOW_SPORTS = "HKQuantityTypeIdentifierDistanceDownhillSnowSports",
16+
BASAL_ENERGY_BURNED = "HKQuantityTypeIdentifierBasalEnergyBurned",
17+
ACTIVE_ENERGY_BURNED = "HKQuantityTypeIdentifierActiveEnergyBurned",
18+
FLIGHT_SCLIMBED = "HKQuantityTypeIdentifierFlightsClimbed",
19+
NIKE_FUEL = "HKQuantityTypeIdentifierNikeFuel",
20+
APPLE_EXERCISE_TIME = "HKQuantityTypeIdentifierAppleExerciseTime",
21+
APPLE_MOVE_TIME = "HKQuantityTypeIdentifierAppleMoveTime",
22+
APPLE_STAND_HOUR = "HKCategoryTypeIdentifierAppleStandHour",
23+
APPLE_STAND_TIME = "HKQuantityTypeIdentifierAppleStandTime",
24+
VO2_MAX = "HKQuantityTypeIdentifierVO2Max",
25+
LOW_CARDIO_FITNES_SEVENT = "HKCategoryTypeIdentifierLowCardioFitnessEvent"
426
}
527

628
export default FitnessDataType

0 commit comments

Comments
 (0)