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-daily-step-counter

v0.0.12

Published

get daily step count using CMPedometer and activity sensor

Downloads

26

Readme

react-native-daily-step-counter

WIP. Definitely not production-ready. 🚧

It collects step counts and lets you query the data per date without accessing healthkit or google fitness api.

Abstract

Basically it's a CMPedometer implemented in React Native.

On ios, there's a RNReactNativeDailyStepCounter.m which is a mere implementation of CMPedometer.

On android, PedometerImple.java is as android implementation of CMPedometer. PedometerImple is actually a SenserEventListener which listens to step counter event and save daily step count to shared preferences.

Not knowing both android and ios, I'm not 100% sure if it works properly and is good to go. Any idea and opinios are welcome.

Usage

| Installing package

yarn add react-native-daily-step-counter

| Getting Permission

( not implemented yet.)

sepcify permissions needed.

<!-- android/app/src/main/AndroidManifest.xml -->
<manifest>
	...
    <!-- belog API ver 28 -->
    <uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
    <!-- above API ver 29 -->
    <uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />
	...
	<application />

</manifest>
<!-- ios/your_build_target/info.plist -->
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	...
	<key>NSMotionUsageDescription</key>
	<string>Need Motion Permission</string>
	...
</dict>

check and ask for permission. ( recommend using react-native-permission ). FYI, it seems to me that calling CMPedometer.queryPedometerDataFromDate for the first time force ios to request for the permission. Below is how I dealt with permission.

class Pedometer {
	...
  private askPermissionIOS(): Promise<boolean> {
    return new Promise(res => {
      this.queryPedometerDataBetweenDates(
        new Date().getDate(),
        new Date().getDate(),
        (error, data) => {
          if (data?.numberOfSteps === null) return res(false);
          res(true);
        }
      );
    });
  }

  public async checkPermission() {
    if (Platform.OS === 'android') {
      const permission = await check(PERMISSIONS.ANDROID.ACTIVITY_RECOGNITION);
      return permission === 'granted';
    }
    if (Platform.OS === 'ios') {
      const permission = await this.ios_authorizationStatus();
      return permission === 'authorized';
    }
    return false;
  }

  public async requestPermission(): Promise<boolean> {
    if (Platform.OS === 'android') {
      const permission = await request(
        PERMISSIONS.ANDROID.ACTIVITY_RECOGNITION
      );
      return permission === 'granted';
    }
    if (Platform.OS === 'ios') {
      const granted = await this.askPermissionIOS();
      return granted;
    }
    return false;
  }
  ...
}

| Getting step count

you can get step count in 2 ways. By imperatively query for it (queryPedometerDataBetweenDates) or by listening to step count change event ( startPedometerUpdatesFromDate )

const Screen = () => {
  const [stepCount, setStepCount] = useState<number>(0);
  const [streaming, setStreaming] = useState<boolean>(false);

  const gettingStepCount = async () => {
    const startDate = new Date();
    startDate.setDate(startDate.getDate() - 1); // getting date from yesterday
    startDate.setHours(0, 0, 0);
    const endDate = new Date();

    StepCount.queryPedometerDataBetweenDates(
      startDate.getTime(), // react native can't pass Date object, so you should pass timestamp.
      endDate.getTime(),
      (error, data) => {
        setStepCount(data?.numberOfSteps ?? 0);
      }
    );
  };

  const startStreaming = () => {
	setStreaming(true);
  }

  // android need initial listner register process.
  useEffect(() => {
    // on android, you should call StepCounter.startPedometerUpdatesFromDate once to start collecting step count.
    // make sure you get permission before you call this.
    // don't call stopPedometerUpdates unless you want to stop collecting step count.
    if (Platform.OS === "android") StepCounter.startPedometerUpdatesFromDate();
  }, []);

  useEffect(() => {
    if (streaming) {
      const startDate = new Date();
      date.setHours(0, 0, 0); // ios collects hours, minutes, seconds. but android only works for date. step counts are saved per date YYYY-MM-DD.
      StepCounter.startPedometerUpdatesFromDate(
        startDate.getTime(),
        (pedometerData) => {
          setStepCount(pedometerData.numberOfSteps);
        }
      );
    }
  }, [streaming]);

  return (
    <View>
      {stepCount}
      <TouchableOpacity onPress={gettingStepCount}>
        <Text>fetch data</Text>
      </TouchableOpacity>
      <TouchableOpacity onPress={startStreaming}>
        <Text>start data streaming</Text>
      </TouchableOpacity>
    </View>
  );
};