Skip to content

Commit e1bf37e

Browse files
committed
Add getFiberSamples
1 parent 03a20fa commit e1bf37e

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,
@@ -478,10 +483,10 @@ declare module 'react-native-health' {
478483
}
479484

480485
export interface HeartbeatSeriesSampleValue extends BaseValue {
481-
heartbeatSeries: ({
486+
heartbeatSeries: {
482487
timeSinceSeriesStart: number
483488
precededByGap: boolean
484-
})[]
489+
}[]
485490
}
486491

487492
export interface HealthUnitOptions {
@@ -526,15 +531,13 @@ declare module 'react-native-health' {
526531
end: string
527532
}
528533

529-
530-
531534
export interface ElectrocardiogramSampleValue extends BaseValue {
532-
classification: ElectrocardiogramClassification,
533-
averageHeartRate: number,
534-
samplingFrequency: number,
535-
device: string,
536-
algorithmVersion: number,
537-
voltageMeasurements: (number[])[]
535+
classification: ElectrocardiogramClassification
536+
averageHeartRate: number
537+
samplingFrequency: number
538+
device: string
539+
algorithmVersion: number
540+
voltageMeasurements: number[][]
538541
}
539542

540543
export interface HealthValueOptions extends HealthUnitOptions {
@@ -578,14 +581,14 @@ declare module 'react-native-health' {
578581
LabResultRecord = 'LabResultRecord',
579582
MedicationRecord = 'MedicationRecord',
580583
ProcedureRecord = 'ProcedureRecord',
581-
VitalSignRecord = 'VitalSignRecord'
584+
VitalSignRecord = 'VitalSignRecord',
582585
}
583586

584587
export interface HealthClinicalRecord extends BaseValue {
585-
sourceName: string,
586-
sourceId: string,
587-
displayName: string,
588-
fhirData: any,
588+
sourceName: string
589+
sourceId: string
590+
displayName: string
591+
fhirData: any
589592
}
590593

591594
/* Health Constants */
@@ -760,7 +763,7 @@ declare module 'react-native-health' {
760763
WalkingHeartRateAverage = 'WalkingHeartRateAverage',
761764
Weight = 'Weight',
762765
Workout = 'Workout',
763-
WorkoutRoute = 'WorkoutRoute'
766+
WorkoutRoute = 'WorkoutRoute',
764767
}
765768

766769
export enum HealthUnit {

0 commit comments

Comments
 (0)