GithubHelp home page GithubHelp logo

Comments (12)

MaskyS avatar MaskyS commented on August 18, 2024 3

I'm planning to do a write-up about it on my blog soon, but roughly, here's how I did it:
You'll need the following vars:

  • savedStepCount to save previous days' step count (e.g using shared_preferences). If you already save and display/use/show previous days' step count, you cannot use that var for this purpose, as this savedStepCount will reset intermittently
  • lastDaySaved, either an int or a DateTime to save the last time you "reset" your pedometer. this will become apparent shortly. persist this as well.
  • value is the count received from the pedometer stream
  • todayStepCount
      if (value < savedStepCount) {
        // Upon device reboot, pedometer resets. When this happens, the saved counter must be reset as well.
        savedStepCount = 0;
        // {persist this value using a package of your choice here}
      }

      var lastDaySaved;
      // {load the saved value using a package of your choice here}

      if (lastDaySaved < todayDayNo) { // whether you use int or DateTime, this should only return true once every 24 hours, otherwise your value will reset too frequently.
        
        lastDaySaved = todayDayNo
        // {save lastDaySaved here}
        savedStepCount = value;
        // {save savedStepCount here}
      }

      todayStepCount = value - savedStepCount;
      return todayStepCount; // this is your daily steps value.

Hope this helps. If anyone finds a neater solution, please drop it here!

from flutter-plugins.

MaskyS avatar MaskyS commented on August 18, 2024

Migrate to androidX: https://github.com/cph-cachet/flutter-plugins/pull/40/files

from flutter-plugins.

k3v1n-uncle avatar k3v1n-uncle commented on August 18, 2024

from flutter-plugins.

MaskyS avatar MaskyS commented on August 18, 2024

from flutter-plugins.

MaskyS avatar MaskyS commented on August 18, 2024

@xuwankang steps are cumulative but reset whenever device is rebooted.

from flutter-plugins.

k3v1n-uncle avatar k3v1n-uncle commented on August 18, 2024

from flutter-plugins.

smallstepman avatar smallstepman commented on August 18, 2024

To fix this, I had to

git clone https://github.com/cph-cachet/flutter-pluginsthis 
mkdir my_flutter_project_dir/packages
cp flutter-plugins/packages/pedometer my_flutter_project_dir/packages

then in my_flutter_project_dir/packages/pedometer/android/build.gradle I changed

dependencies {
        classpath 'com.android.tools.build:gradle:3.5.2'  // <- bump
    }
    

android {
    compileSdkVersion 28 // <- bump

    defaultConfig {
        minSdkVersion 16
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 
//                                 ^ bump to androidX
    }
    lintOptions {
        disable 'InvalidPackage'
    }

Finally in pubspac.yaml, I changed

dependencies:
  pedometer: ^1.0.0

to

dependencies:
  pedometer:
    path: packages/pedometer

and built my project

from flutter-plugins.

smallstepman avatar smallstepman commented on August 18, 2024

to extend on @MaskyS wonderful post, here is my implementation of his idea, with a bonus of being able to preserve today steps count over reboot
value is the value passed directly from steps counter stream

Future<int> getTodaySteps(int value) async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  int todayDayNo = DateTime.now().day;
  int savedStepCount = prefs.getInt('savedStepCount') ?? 0;
  int lastDaySaved = prefs.getInt('lastDaySaved') ?? 0;
  int todaySteps = prefs.getInt('todaySteps') ?? 0;
  int lastValue = prefs.getInt('stepsLastValue') ?? 0;
  prefs.setInt('stepsLastValue', value);

  // device reboot
  if (savedStepCount > value) {
    prefs.setInt('savedStepCount', 0);
    savedStepCount = 0;
  }

  // next day
  if (todayDayNo > lastDaySaved) {
    prefs.setInt('lastDaySaved', todayDayNo);
    prefs.setInt('savedStepCount', value);
    prefs.setInt('todaySteps', 0);
    savedStepCount = value;
    todaySteps = 0;
  }

  if (savedStepCount == 0 && todaySteps > value) {
    if (value < 10) {
      prefs.setInt('todaySteps', value + todaySteps);
    } else {
      prefs.setInt('todaySteps', value - lastValue + todaySteps);
    }
  } else {
    prefs.setInt('todaySteps', value - savedStepCount);
  }

  todaySteps = prefs.getInt('todaySteps') ?? 0;

  if (todaySteps > 0) {
    return todaySteps; 
  } else {
    return 0;
  }
}
  • if (todaySteps > 0) is an overkill to make sure I never run into some sticky situation where I display negative step count in UI
  • if (value < 10) is to make sure there is ten step buffor for a sensor to catch up after reboot, otherwise, it would substract lastValue recorded before the reboot, from value which would be equal to 0 after reboot

from flutter-plugins.

thomasnilsson avatar thomasnilsson commented on August 18, 2024

Hi, the latest version of the Pedometer plugin (1.2.0) has the same behaviour on both platforms, i.e. reporting the step count since last reboot.

The idea of keeping track of the step count between boots is great but our philosophy on the plugins is that they have to be quite simple and 'lean' since there is always a wide array of use cases, for each case a different implementation would be preferred.

This means you will have to store these things yourselves, perhaps make a package (not a plugin) which uses the pedometer plugin?

from flutter-plugins.

MaskyS avatar MaskyS commented on August 18, 2024

I finally got around to writing down a full-blown code sample for tracking daily steps. Here's the blog post and the code.

from flutter-plugins.

deepakneelakandan avatar deepakneelakandan commented on August 18, 2024

I finally got around to writing down a full-blown code sample for tracking daily steps. Here's the blog post and the code.

Great @MaskyS but when I kill the app, the value resets, is there any solution for that

from flutter-plugins.

yogeshButani avatar yogeshButani commented on August 18, 2024

to extend on @MaskyS wonderful post, here is my implementation of his idea, with a bonus of being able to preserve today steps count over reboot value is the value passed directly from steps counter stream

Future<int> getTodaySteps(int value) async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  int todayDayNo = DateTime.now().day;
  int savedStepCount = prefs.getInt('savedStepCount') ?? 0;
  int lastDaySaved = prefs.getInt('lastDaySaved') ?? 0;
  int todaySteps = prefs.getInt('todaySteps') ?? 0;
  int lastValue = prefs.getInt('stepsLastValue') ?? 0;
  prefs.setInt('stepsLastValue', value);

  // device reboot
  if (savedStepCount > value) {
    prefs.setInt('savedStepCount', 0);
    savedStepCount = 0;
  }

  // next day
  if (todayDayNo > lastDaySaved) {
    prefs.setInt('lastDaySaved', todayDayNo);
    prefs.setInt('savedStepCount', value);
    prefs.setInt('todaySteps', 0);
    savedStepCount = value;
    todaySteps = 0;
  }

  if (savedStepCount == 0 && todaySteps > value) {
    if (value < 10) {
      prefs.setInt('todaySteps', value + todaySteps);
    } else {
      prefs.setInt('todaySteps', value - lastValue + todaySteps);
    }
  } else {
    prefs.setInt('todaySteps', value - savedStepCount);
  }

  todaySteps = prefs.getInt('todaySteps') ?? 0;

  if (todaySteps > 0) {
    return todaySteps; 
  } else {
    return 0;
  }
}
  • if (todaySteps > 0) is an overkill to make sure I never run into some sticky situation where I display negative step count in UI
  • if (value < 10) is to make sure there is ten step buffor for a sensor to catch up after reboot, otherwise, it would substract lastValue recorded before the reboot, from value which would be equal to 0 after reboot

This code works fine, but want to know that ho to reset steps to 0 daily? for example, when a new day starts, the steps count should be automatically set to 0 even if i don't open the app on that day

for example after 12am, new day have started and i have taken the steps, so when i open the app, i need to see updated step count for that day...it means we have to run above function in backgroud and it should be automatically hit without opening the app, whenver new day starts.

from flutter-plugins.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.