Skip to content

Commit b1aaf5f

Browse files
authored
Merge pull request #299 from inmycorner/master
Add getFiberSamples
2 parents 2fa62bf + e1bf37e commit b1aaf5f

6 files changed

+105
-18
lines changed

RCTAppleHealthKit/RCTAppleHealthKit+Methods_Dietary.h

+1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@
1818

1919
- (void)dietary_getEnergyConsumedSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
2020
- (void)dietary_getProteinSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
21+
- (void)dietary_getFiberSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
2122
- (void)dietary_getTotalFatSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
2223
@end

RCTAppleHealthKit/RCTAppleHealthKit+Methods_Dietary.m

+31
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,37 @@ - (void)dietary_getProteinSamples:(NSDictionary *)input callback:(RCTResponseSen
108108
}];
109109
}
110110

111+
- (void)dietary_getFiberSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
112+
{
113+
HKQuantityType *fiberType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryFiber];
114+
HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input key:@"unit" withDefault:[HKUnit gramUnit]];
115+
NSUInteger limit = [RCTAppleHealthKit uintFromOptions:input key:@"limit" withDefault:HKObjectQueryNoLimit];
116+
BOOL ascending = [RCTAppleHealthKit boolFromOptions:input key:@"ascending" withDefault:false];
117+
NSDate *startDate = [RCTAppleHealthKit dateFromOptions:input key:@"startDate" withDefault:nil];
118+
NSDate *endDate = [RCTAppleHealthKit dateFromOptions:input key:@"endDate" withDefault:[NSDate date]];
119+
if(startDate == nil){
120+
callback(@[RCTMakeError(@"startDate is required in options", nil, nil)]);
121+
return;
122+
}
123+
NSPredicate * predicate = [RCTAppleHealthKit predicateForSamplesBetweenDates:startDate endDate:endDate];
124+
125+
[self fetchQuantitySamplesOfType:fiberType
126+
unit:unit
127+
predicate:predicate
128+
ascending:ascending
129+
limit:limit
130+
completion:^(NSArray *results, NSError *error) {
131+
if(results){
132+
callback(@[[NSNull null], results]);
133+
return;
134+
} else {
135+
NSLog(@"An error occured while retrieving the fiber sample %@. The error was: ", error);
136+
callback(@[RCTMakeError(@"An error occured while retrieving the fiber sample", error, nil)]);
137+
return;
138+
}
139+
}];
140+
}
141+
111142
- (void)saveFood:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
112143
{
113144
NSString *foodNameValue = [RCTAppleHealthKit stringFromOptions:input key:@"foodName" withDefault:nil];

RCTAppleHealthKit/RCTAppleHealthKit.m

+5
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,11 @@ + (BOOL)requiresMainQueueSetup
335335
[self dietary_getProteinSamples:input callback:callback];
336336
}
337337

338+
RCT_EXPORT_METHOD(getFiberSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
339+
{
340+
[self dietary_getFiberSamples:input callback:callback];
341+
}
342+
338343
RCT_EXPORT_METHOD(getTotalFatSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
339344
{
340345
[self dietary_getTotalFatSamples:input callback:callback];

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ This package allows access to health & fitness data exposed by Apple Healthkit.
88

99
If you are looking for a more robust solution providing normalized data, insights and recommendations based on user's biometric data, data from multiple sources (Fitbit, Oura), or a hosted solution, please check out the [Point SDK](https://www.areyouonpoint.co/) developed by our frens.
1010

11-
1211
# Discord
12+
1313
<a href="https://discord.gg/d24g5XDePr"><img src="https://img.shields.io/badge/Discord-join%20chat-738bd7.svg" alt="React Native Health official Discord"></a>
1414

1515
## Getting Started
@@ -200,6 +200,7 @@ All the documentation is under the [docs](/docs) folder. They are split into the
200200
201201
- [getEnergyConsumedSamples](/docs/getEnergyConsumedSamples.md)
202202
- [getProteinSamples](/docs/getProteinSamples.md)
203+
- [getFiberSamples](/docs/getFiberSamples.md)
203204
- [getTotalFatSamples](/docs/getTotalFatSamples.md)
204205
- [saveFood](/docs/saveFood.md)
205206
- [saveWater](/docs/saveWater.md)

docs/getFiberSamples.md

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# getFiberSamples
2+
3+
A quantity sample type that measures the amount of fiber consumed.
4+
5+
Permission required:
6+
7+
- `AppleHealthKit.Constants.Permissions.Fiber`
8+
9+
Example input options:
10+
11+
```javascript
12+
let options = {
13+
startDate: new Date(2021, 0, 0).toISOString(), // required
14+
endDate: new Date().toISOString(), // optional; default now
15+
}
16+
```
17+
18+
Call the method:
19+
20+
```javascript
21+
AppleHealthKit.getFiberSamples(
22+
(options: HealthInputOptions),
23+
(err: Object, results: HealthValue) => {
24+
if (err) {
25+
return
26+
}
27+
console.log(results)
28+
},
29+
)
30+
```
31+
32+
Example output:
33+
34+
```json
35+
[
36+
{
37+
"id": "5013eca7-4aee-45af-83c1-dbe3696b2e51", // The universally unique identifier (UUID) for this HealthKit object.
38+
"endDate": "2021-04-01T22:00:00.000+0300",
39+
"startDate": "2021-04-01T22:00:00.000+0300",
40+
"value": 39,
41+
"metadata": {
42+
"HKWasUserEntered": true
43+
}
44+
}
45+
]
46+
```

index.d.ts

+20-17
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ declare module 'react-native-health' {
1414
}
1515

1616
export interface HKErrorResponse {
17-
message?: string;
17+
message?: string
1818
}
1919

2020
export interface AppleHealthKit {
@@ -210,6 +210,11 @@ declare module 'react-native-health' {
210210
callback: (err: string, results: Array<HealthValue>) => void,
211211
): void
212212

213+
getFiberSamples(
214+
options: HealthInputOptions,
215+
callback: (err: string, results: Array<HealthValue>) => void,
216+
): void
217+
213218
getTotalFatSamples(
214219
options: HealthInputOptions,
215220
callback: (err: string, results: Array<HealthValue>) => void,
@@ -495,10 +500,10 @@ declare module 'react-native-health' {
495500
}
496501

497502
export interface HeartbeatSeriesSampleValue extends BaseValue {
498-
heartbeatSeries: ({
503+
heartbeatSeries: {
499504
timeSinceSeriesStart: number
500505
precededByGap: boolean
501-
})[]
506+
}[]
502507
}
503508

504509
export interface HealthUnitOptions {
@@ -543,15 +548,13 @@ declare module 'react-native-health' {
543548
end: string
544549
}
545550

546-
547-
548551
export interface ElectrocardiogramSampleValue extends BaseValue {
549-
classification: ElectrocardiogramClassification,
550-
averageHeartRate: number,
551-
samplingFrequency: number,
552-
device: string,
553-
algorithmVersion: number,
554-
voltageMeasurements: (number[])[]
552+
classification: ElectrocardiogramClassification
553+
averageHeartRate: number
554+
samplingFrequency: number
555+
device: string
556+
algorithmVersion: number
557+
voltageMeasurements: number[][]
555558
}
556559

557560
export interface HealthValueOptions extends HealthUnitOptions {
@@ -595,14 +598,14 @@ declare module 'react-native-health' {
595598
LabResultRecord = 'LabResultRecord',
596599
MedicationRecord = 'MedicationRecord',
597600
ProcedureRecord = 'ProcedureRecord',
598-
VitalSignRecord = 'VitalSignRecord'
601+
VitalSignRecord = 'VitalSignRecord',
599602
}
600603

601604
export interface HealthClinicalRecord extends BaseValue {
602-
sourceName: string,
603-
sourceId: string,
604-
displayName: string,
605-
fhirData: any,
605+
sourceName: string
606+
sourceId: string
607+
displayName: string
608+
fhirData: any
606609
}
607610

608611
/* Health Constants */
@@ -778,7 +781,7 @@ declare module 'react-native-health' {
778781
WalkingHeartRateAverage = 'WalkingHeartRateAverage',
779782
Weight = 'Weight',
780783
Workout = 'Workout',
781-
WorkoutRoute = 'WorkoutRoute'
784+
WorkoutRoute = 'WorkoutRoute',
782785
}
783786

784787
export enum HealthUnit {

0 commit comments

Comments
 (0)