forked from nutrisense/rn-apple-healthkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
165 lines (165 loc) · 6.64 KB
/
index.js
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.HKConstants = void 0;
const react_native_1 = require("react-native");
const Constants_1 = require("./Constants");
var AuthorizationStatus;
(function (AuthorizationStatus) {
AuthorizationStatus[AuthorizationStatus["UnavailablePermission"] = -1] = "UnavailablePermission";
AuthorizationStatus[AuthorizationStatus["NotDetermined"] = 0] = "NotDetermined";
AuthorizationStatus[AuthorizationStatus["SharingDenied"] = 1] = "SharingDenied";
AuthorizationStatus[AuthorizationStatus["SharingAuthorized"] = 2] = "SharingAuthorized";
})(AuthorizationStatus || (AuthorizationStatus = {}));
var HealthObserver;
(function (HealthObserver) {
HealthObserver["AllergyRecord"] = "AllergyRecord";
HealthObserver["ConditionRecord"] = "ConditionRecord";
HealthObserver["CoverageRecord"] = "CoverageRecord";
HealthObserver["Cycling"] = "Cycling";
HealthObserver["HeartRate"] = "HeartRate";
HealthObserver["ImmunizationRecord"] = "ImmunizationRecord";
HealthObserver["LabResultRecord"] = "LabResultRecord";
HealthObserver["MedicationRecord"] = "MedicationRecord";
HealthObserver["ProcedureRecord"] = "ProcedureRecord";
HealthObserver["RestingHeartRate"] = "RestingHeartRate";
HealthObserver["Running"] = "Running";
HealthObserver["StairClimbing"] = "StairClimbing";
HealthObserver["VitalSignRecord"] = "VitalSignRecord";
HealthObserver["Walking"] = "Walking";
HealthObserver["Workout"] = "Workout";
})(HealthObserver || (HealthObserver = {}));
const { AppleHealthKit } = react_native_1.NativeModules;
const baseCallback = (appleHealthKit, options) => {
return new Promise((resolve, reject) => {
appleHealthKit(options, (err, results) => {
if (err) {
reject(err);
}
resolve(results);
});
});
};
const handleCallbackWithPromise = (resolve, reject) => (err, results) => {
if (err) {
reject(err);
}
else {
resolve(results);
}
};
const HealthKit = {
isAvailable: () => {
return new Promise((resolve, reject) => {
AppleHealthKit.isAvailable(handleCallbackWithPromise(resolve, reject));
});
},
init: (options) => {
return new Promise((resolve, reject) => {
AppleHealthKit.initHealthKit({ permissions: options }, handleCallbackWithPromise(resolve, reject));
});
},
saveMindfulSession: (options) => {
return new Promise((resolve, reject) => {
const { startDate, endDate } = options;
AppleHealthKit.saveMindfulSession({
startDate: startDate.toISOString(),
endDate: endDate.toISOString(),
}, handleCallbackWithPromise(resolve, reject));
});
},
saveWorkout: (options) => {
return new Promise((resolve, reject) => {
const { startDate, endDate, energyBurned = { unit: Constants_1.Units.calorie, value: -1 }, distance = { unit: Constants_1.Units.mile, value: -1 }, ...rest } = options;
AppleHealthKit.saveWorkout({
startDate: startDate.toISOString(),
endDate: startDate.toISOString(),
energyBurned: energyBurned.value,
energyBurnedUnit: energyBurned.unit,
distance: distance.value,
distanceUnit: distance.unit,
...rest,
}, handleCallbackWithPromise(resolve, reject));
});
},
authorizationStatus: (types) => {
return new Promise((resolve, reject) => {
AppleHealthKit.authorizationStatus(types, handleCallbackWithPromise(resolve, reject));
});
},
getAuthStatus: (options) => {
return new Promise((resolve, reject) => {
AppleHealthKit.initHealthKit({ permissions: options }, (err, result) => {
if (err) {
reject(err);
}
else {
resolve(result?.permissions);
}
});
});
},
getHeartRate: (options) => {
const { startDate, endDate = new Date(), ...rest } = options;
return new Promise((resolve, reject) => {
AppleHealthKit.getHeartRateSamples({
startDate: startDate.toISOString(),
endDate: endDate.toISOString(),
...rest,
}, (err, results) => {
if (err) {
reject(err);
}
resolve(results);
});
});
},
getStepCount: (options) => {
const { includeManuallyAdded = true, date, ...rest } = options;
const optionDate = { date, includeManuallyAdded };
return baseCallback(AppleHealthKit.getStepCount, { ...optionDate, ...rest });
},
getStepCounts: (options) => {
const { includeManuallyAdded = true, startDate = new Date().toISOString(), endDate = new Date().toISOString(), ...rest } = options;
const optionDate = { startDate, endDate, includeManuallyAdded };
return baseCallback(AppleHealthKit.getDailyStepCountSamples, { ...optionDate, ...rest });
},
getLatestWeight: (options) => {
const { unit = Constants_1.Units.kg } = options;
return baseCallback(AppleHealthKit.getLatestWeight, { unit });
},
checkPermission: (type) => {
return new Promise((resolve, reject) => {
AppleHealthKit.authorizationStatusForType(type)
.then((res) => resolve(res))
.catch((err) => reject(err));
});
},
saveWeight: (value) => {
return baseCallback(AppleHealthKit.saveWeight, { value });
},
saveStep: (value, startDate, endDate) => {
return baseCallback(AppleHealthKit.saveSteps, { value, startDate, endDate });
},
getWeight: (options) => {
const { unit = Constants_1.Units.kg, startDate, endDate } = options;
return baseCallback(AppleHealthKit.getWeightSamples, { unit, startDate, endDate });
},
getHeight: (options) => {
const { unit = Constants_1.Units.meter, startDate, endDate } = options;
return baseCallback(AppleHealthKit.getHeightSamples, { unit, startDate, endDate });
},
saveHeight: (value) => {
return baseCallback(AppleHealthKit.saveHeight, { value });
},
lastHeight: (options) => {
const { unit = Constants_1.Units.meter } = options;
return baseCallback(AppleHealthKit.getLatestHeight, { unit });
},
};
exports.HKConstants = {
Permissions: Constants_1.Permissions,
Units: Constants_1.Units,
Activities: Constants_1.Activities,
};
exports.default = HealthKit;
//# sourceMappingURL=index.js.map