Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The method 'getSample' not working #81

Closed
rodolfobarretoweb opened this issue Mar 8, 2019 · 7 comments
Closed

The method 'getSample' not working #81

rodolfobarretoweb opened this issue Mar 8, 2019 · 7 comments

Comments

@rodolfobarretoweb
Copy link

I search in many places where the code to put to work but I can't find

@EJohnF
Copy link
Contributor

EJohnF commented Mar 9, 2019

Can you share your code please?
Generally you need to do it after initialisation.
Plus, be aware that this function is in the master, but not in npm (you need to user link to github in order to use it)

@rodolfobarretoweb
Copy link
Author

This is my class responsible to put the data the way I need.

import AppleHealthKit from 'rn-apple-healthkit';
import * as math from 'mathjs';
import { convertMetersToKm } from 'app/utils/number';

class AppleWatch {
  static name = 'AppleWatch';
  static options = {
    permissions: {
      read: [
        "Height",
        "Weight",
        "StepCount",
        "DateOfBirth",
        "HeartRate",
        "ActiveEnergyBurned",
        "DistanceCycling",
        "DistanceWalkingRunning"
      ]
    }
  };

  start() {
    return new Promise((resolve, reject) => {
      AppleHealthKit.initHealthKit(AppleWatch.options, error => {
        if(error) {
          reject(error);
        } else {
          resolve();
        }
      });
    })
  }

  getStepCount(date) {
    return new Promise(resolve => {
      AppleHealthKit.getStepCount({ date }, (error, results) => {
        resolve(!error ? this.clearValue(results.value) : 0);
      });
    });
  }

  getEnergyBurned(startDate, endDate) {
    return new Promise(resolve => {
      AppleHealthKit.getActiveEnergyBurned({ startDate, endDate }, (error, results) => {
        resolve(!error ? this.calculateValues(results) : 0);
      });
    });
  }

  getHeartBeats(startDate, endDate) {
    return new Promise(resolve => {
      AppleHealthKit.getHeartRateSamples({ startDate, endDate }, (error, results) => {
        resolve(!error ? results.map(item => item.value) : []);
      });
    });
  }

  getDistanceWalkingRunning(date) {
    return new Promise(resolve => {
      AppleHealthKit.getDistanceWalkingRunning({ unit: 'meter', date }, (error, results) => {
        resolve(error ? 0 : convertMetersToKm(results.value));
      });
    });
  }

  getActivities(startDate, endDate) {
    return new Promise(resolve => {
      AppleHealthKit. getSamples({ startDate, endDate }, (error, results) => {
        resolve(error ? {} : results);
      });
    });
  }

  calculateValues(data) {
    let value = data.reduce((acc, item) => {
      acc = math.add(math.bignumber(acc), math.bignumber(item.value));
      return acc;
    }, 0);

    value = math.floor(value);
    value = value.valueOf();

    return value;
  }

  clearValue(value) {
    value = math.floor(math.bignumber(value));
    return value.valueOf();
  }
}

export default AppleWatch;

My action:

export const getActivities = async (device, startDate, endDate) => {
  const reference = await getApiReference(device);
  return reference.getActivities(startDate, endDate);
};

function getApiReference(device) {
  return new Promise(async resolve => {
    switch(device) {
      case AppleWatchApi.name:
        const api = new AppleWatchApi();
        await api.start();
        resolve(api);
      break;
    }
  })
}

The erro is: "getSamples" is not a function and yes I try to using the master branch.

@EJohnF
Copy link
Contributor

EJohnF commented Mar 9, 2019

for getSamples in options you need to use also type field.
Check docs here https://github.com/terrillo/rn-apple-healthkit/blob/master/docs/getSamples().md

However the error is strange... Only getSamples is not working?
Maybe the world went crazy and the problem is in a space after dot AppleHealthKit. getSamples ?

small advice, better to use constants, somelike this:

const PERMS = AppleHealthKit.Constants.Permissions;
const healthKitOptions = {
  permissions: {
    read: [
      PERMS.ActiveEnergyBurned,
      PERMS.DistanceCycling,
      PERMS.DateOfBirth,
      PERMS.FlightsClimbed,
      PERMS.Height,
      PERMS.BiologicalSex,
      PERMS.StepCount,
      PERMS.Steps,
      PERMS.DistanceWalkingRunning,
      PERMS.Weight,
      PERMS.Workout,
    ],
    write: [
      PERMS.Weight,
    ],
  }
};

@rodolfobarretoweb
Copy link
Author

I will try put it to work considering your observations, but this error only shows up when I try to use this method.

About permissions constants: Yes, I see that constants a few days ago, thanks for the tip.

@EJohnF
Copy link
Contributor

EJohnF commented Mar 9, 2019

Also sometimes I had problems with package manager, that when changing package.json and call install command - packages didn't change.
So, also pls check node_modules/rn-apple-healthkit/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m

is content the same as https://github.com/terrillo/rn-apple-healthkit/blob/715a0ebe9e50fb867f8a07a4bd9d2e89a2ebd425/RCTAppleHealthKit/RCTAppleHealthKit%2BQueries.m#L110 ?

@rodolfobarretoweb
Copy link
Author

Ok, I will check this.
And John, thanks for everthing.

@rodolfobarretoweb
Copy link
Author

Hi, I found the problem: I forget to rebuild in xcode, I just update the package and put to run, now works well.

Thanks for your tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants