Created
May 18, 2021 10:52
-
-
Save fakeheal/4c1a73931694e10bf58c50aea9541b77 to your computer and use it in GitHub Desktop.
Reproductive Health Permissions + Fetch Data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState } from 'react' | |
import { SafeAreaView, ScrollView, StatusBar, StyleSheet, Text, View } from 'react-native' | |
import { Colors } from 'react-native/Libraries/NewAppScreen' | |
import AppleHealthKit, { HealthKitPermissions, HealthValue } from 'react-native-health' | |
/* Permission options */ | |
const permissions = { | |
permissions: { | |
read: [ | |
AppleHealthKit.Constants.Permissions.MenstrualFlow, | |
'CervicalMucusQuality', | |
'Contraceptive', | |
'IntermenstrualBleeding', | |
'Lactation', | |
'OvulationTestResult', | |
'Pregnancy', | |
'SexualActivity', | |
'MenstrualFlow', | |
], | |
write: [ | |
'BasalBodyTemperature', | |
'CervicalMucusQuality', | |
'Contraceptive', | |
'IntermenstrualBleeding', | |
'Lactation', | |
'OvulationTestResult', | |
'Pregnancy', | |
'SexualActivity', | |
'MenstrualFlow', | |
], | |
}, | |
} as HealthKitPermissions | |
AppleHealthKit.initHealthKit(permissions, (error) => { | |
/* Called after we receive a response from the system */ | |
AppleHealthKit.getAuthStatus(permissions, (err, results) => { | |
console.log(err, results, 'AUTH STATUS') | |
}) | |
if (error) { | |
console.log('[ERROR] Cannot grant permissions!') | |
} | |
}) | |
export default function App() { | |
const [authStatus, setAuthStatus] = useState<any>({}) | |
const handleGetDataPressed = () => { | |
let options = { | |
startDate: new Date(2021, 0, 0).toISOString(), // required | |
endDate: new Date().toISOString(), // optional; default now | |
limit: 10, // optional; default no limit | |
} | |
AppleHealthKit.getMenstrualFlowSamples(options, (err: Object, results: Array<HealthValue>) => { | |
if (err) { | |
return | |
} | |
console.log(results) | |
}) | |
} | |
return ( | |
<> | |
<StatusBar barStyle='dark-content' /> | |
<SafeAreaView> | |
<ScrollView | |
contentInsetAdjustmentBehavior='automatic' | |
style={styles.scrollView}> | |
<View style={styles.body}> | |
<View style={styles.sectionContainer}> | |
<Text style={styles.sectionTitle}> | |
React Native Health Example | |
</Text> | |
<Text onPress={handleGetDataPressed}> | |
Press me to get data | |
</Text> | |
<Text style={styles.sectionDescription}> | |
{JSON.stringify(authStatus, null, 2)} | |
</Text> | |
</View> | |
</View> | |
</ScrollView> | |
</SafeAreaView> | |
</> | |
) | |
} | |
const styles = StyleSheet.create({ | |
scrollView: { | |
backgroundColor: Colors.lighter, | |
}, | |
engine: { | |
position: 'absolute', | |
right: 0, | |
}, | |
body: { | |
backgroundColor: Colors.white, | |
}, | |
sectionContainer: { | |
marginTop: 32, | |
paddingHorizontal: 24, | |
}, | |
sectionTitle: { | |
fontSize: 24, | |
fontWeight: '600', | |
color: Colors.black, | |
}, | |
sectionDescription: { | |
marginTop: 8, | |
fontSize: 18, | |
fontWeight: '400', | |
color: Colors.dark, | |
}, | |
highlight: { | |
fontWeight: '700', | |
}, | |
footer: { | |
color: Colors.dark, | |
fontSize: 12, | |
fontWeight: '600', | |
padding: 4, | |
paddingRight: 12, | |
textAlign: 'right', | |
}, | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment