npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-native-use-health-kit

v3.0.0

Published

This package allows app to read and write health data by HealthKit.

Downloads

13

Readme

react-native-use-health-kit

React Native module which allows app to access Health Kit.

Getting started

Installation For Expo

Expo: This package is not available in the Expo Go app. Learn how you can use it with custom dev clients.

Installation - For yarn

$ yarn add react-native-use-health-kit

Installation - For npm

$ npm install react-native-use-health-kit --save

Usage

Permissions & Types for Write Data

You can access below types for both of Read and Write.

'activeEnergyBurned',
'basalEnergyBurned',
'bodyFatPercentage',
'bodyMass',
'bodyMassIndex',
'dietaryEnergyConsumed',
'dietaryWater',
'distanceWalkingRunning',
'flightsClimbed',
'restingHeartRate',
'stepCount',

Unit

You can check more unit in Apple's document.

'cal' // Calories.
'g' // Grams.
'm' // Meters.
'L' or 'l' // Liters.
'count' // Count.
'min' // Minute.
'%' // Percentage.

Example

Check example project for details.

How to run example project.

$ yarn
$ yarn example ios

Example code

Authorization

import {
  isHealthDataAvailable,
  initHealthKit,
  HealthType,
  getActiveEnergyBurned,
  getBasalEnergyBurned,
  getBodyFatPercentage,
  getBodyMass,
  getBodyMassIndex,
  getDietaryEnergyConsumed,
  getDietaryWater,
  getDistanceWalkingRunning,
  getFlightsClimbed,
  getRestingHeartRate,
  getStepCount,
  setQuantityData,
  deleteQuantityData,
  SetOptions,
  DeleteOptions,
} from 'react-native-use-health-kit';

// Authorization
const TYPES: HealthType[] = [
  'activeEnergyBurned',
  'basalEnergyBurned',
  'bodyFatPercentage',
  'bodyMass',
  'bodyMassIndex',
  'dietaryEnergyConsumed',
  'dietaryWater',
  'distanceWalkingRunning',
  'flightsClimbed',
  'restingHeartRate',
  'stepCount',
];
const authorize = useCallback(async () => {
  if (TYPES.length === 0) return;

  const isAvailable = await isHealthDataAvailable();
  if (!isAvailable) throw new Error('HealthKit is not available.');

  const isAuthorized = await initHealthKit(TYPES, TYPES);
  if (!isAuthorized) throw new Error('HealthKit is not authorized.');
}, []);

Read data from Apple-Health

// Read data from Apple-Health
const getData = useCallback(async () => {
  const today = moment().startOf('days');
  const startDate = moment(today).add(-3, 'months').unix();
  const endDate = moment(today).endOf('days').unix();
  const options = { startDate, endDate };

  const functions = [];
  functions.push(getActiveEnergyBurned(options));
  functions.push(getBasalEnergyBurned(options));
  functions.push(getBodyFatPercentage(options));
  functions.push(getBodyMass(options));
  functions.push(getBodyMassIndex(options));
  functions.push(getDietaryEnergyConsumed(options));
  functions.push(getDietaryWater(options));
  functions.push(getDistanceWalkingRunning(options));
  functions.push(getFlightsClimbed(options));
  functions.push(getRestingHeartRate(options));
  functions.push(getStepCount(options));

  return Promise.all(functions);
}, []);

Write data to Apple-Health

// Write data to Apple-Health
const setData = useCallback(async () => {
  const date = moment().startOf('days');
  const twoDaysAgo = moment(date).add(-2, 'days').unix();
  const yesterday = moment(date).add(-1, 'days').unix();
  const today = moment(date).unix();

  const optionsList: SetOptions[] = [
    // Active Energy
    {
      type: 'activeEnergyBurned',
      unit: 'kcal',
      data: [
        { startDate: twoDaysAgo, endDate: twoDaysAgo, value: 1000 },
        { startDate: yesterday, endDate: yesterday, value: 1100 },
        { startDate: today, endDate: today, value: 1200 },
      ],
    },
    // Basal Energy
    {
      type: 'basalEnergyBurned',
      unit: 'kcal',
      data: [
        { startDate: twoDaysAgo, endDate: twoDaysAgo, value: 1000 },
        { startDate: yesterday, endDate: yesterday, value: 1100 },
        { startDate: today, endDate: today, value: 1200 },
      ],
    },
    // Walking + Running Distance
    {
      type: 'distanceWalkingRunning',
      unit: 'm',
      data: [
        { startDate: twoDaysAgo, endDate: twoDaysAgo, value: 1000 },
        { startDate: yesterday, endDate: yesterday, value: 1100 },
        { startDate: today, endDate: today, value: 1200 },
      ],
    },
    // Flights Climbed
    {
      type: 'flightsClimbed',
      unit: 'count',
      data: [
        { startDate: twoDaysAgo, endDate: twoDaysAgo, value: 100 },
        { startDate: yesterday, endDate: yesterday, value: 110 },
        { startDate: today, endDate: today, value: 120 },
      ],
    },
    // Steps
    {
      type: 'stepCount',
      unit: 'count',
      data: [
        { startDate: twoDaysAgo, endDate: twoDaysAgo, value: 1000 },
        { startDate: yesterday, endDate: yesterday, value: 1100 },
        { startDate: today, endDate: today, value: 1200 },
      ],
    },
    // Water
    {
      type: 'dietaryWater',
      unit: 'ml',
      data: [
        { startDate: twoDaysAgo, endDate: twoDaysAgo, value: 1000 },
        { startDate: yesterday, endDate: yesterday, value: 1100 },
        { startDate: today, endDate: today, value: 1200 },
      ],
    },
    // Resting Heart Rate
    {
      type: 'restingHeartRate',
      unit: 'count/min',
      data: [
        { startDate: twoDaysAgo, endDate: twoDaysAgo, value: 65 },
        { startDate: yesterday, endDate: yesterday, value: 66 },
        { startDate: today, endDate: today, value: 67 },
      ],
    },
    // Weight
    {
      type: 'bodyMass',
      unit: 'kg',
      data: [
        { startDate: twoDaysAgo, endDate: twoDaysAgo, value: 70 },
        { startDate: yesterday, endDate: yesterday, value: 70.5 },
        { startDate: today, endDate: today, value: 71 },
      ],
    },
    // Body Mass Index
    {
      type: 'bodyMassIndex',
      unit: 'count',
      data: [
        { startDate: twoDaysAgo, endDate: twoDaysAgo, value: 18.0 },
        { startDate: yesterday, endDate: yesterday, value: 18.0 },
        { startDate: today, endDate: today, value: 18.0 },
      ],
    },
    // Body Fat Percentage
    {
      type: 'bodyFatPercentage',
      unit: '%',
      data: [
        { startDate: twoDaysAgo, endDate: twoDaysAgo, value: 0.18 },
        { startDate: yesterday, endDate: yesterday, value: 0.18 },
        { startDate: today, endDate: today, value: 0.18 },
      ],
    },
  ];

  return await Promise.all(
    optionsList.map((options) => setQuantityData(options))
  );
}, []);

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT


Made with create-react-native-library