OvalMoney / react-native-fitness

A React Native module to interact with Apple Healthkit and Google Fit.
MIT License
341 stars 68 forks source link

In iOS take too much time to return steps data. #114

Open AdilHypeteq opened 1 year ago

AdilHypeteq commented 1 year ago

I have implemented code as below, but it's taken time to return steps data in iOS.

const weeks = 4;
    const interval = 'hour';
    const tonigth = new Date();
    // Set date to midnight tonight:
    tonigth.setHours(0, 0, 0, 0);
    tonigth.setDate(tonigth.getDate() + 1);

    const weekAgo = new Date(
      tonigth.getTime() - weeks * 7 * 24 * 60 * 60 * 1000,
    );

    const startDate = Start ? Start : weekAgo.toString();
    const endDate = End ? End : tonigth.toString();

const fetchSteps = async () => {
      const steps = await Fitness.getSteps({startDate, endDate, interval});
      console.log('steps :', steps);
      return steps.map(step => {
        return {
          ...step,
          startDate: moment(step.startDate).toDate(),
          endDate: moment(step.endDate).toDate(),
        };
      });
    };
    const permissions = [
      {
        kind: Fitness.PermissionKinds.Steps,
        access: Fitness.PermissionAccesses.Read,
      },
    ];
    const authorized = await Fitness.isAuthorized(permissions);
    if (authorized) {
      if (Platform.OS === 'android') {
        return fetchSteps();
      } else {
        return fetchSteps();
      }
    }