0

I would like to reset dailyTasks field to 0 every day at midnight. I'm persisting the field using Get Storage plugin. The data is persisting but not resetting.

Code is as follow:

class DataController with GetxController {

   @override
   void onInit() {
    getDailyTask();
    super.onInit();
    } 

     int dailyTasks;
    
      // DateTime
      DateTime now = DateTime.now();
    
      void dailyTaskIncrement() {
        dailyReads++;
        dataBox.write('dailyTasks', dailyTasks);
        update();
      }
    
      void getDailyTask() {
        dailyTasks = dataBox.read('dailyTasks');
        if (now == 12) {
          dailyTasks = 0;
        }
        update();
      }

}

// For User Loren

 int dailyTasks;

  // DateTime
  DateTime now = DateTime.now();

  //
  void dailyTasksIncrement() {
    dailyTasks++;
    dataBox.write('dailyTasks', dailyTasks);
    update();
  }

  // Stores time to storage
  void _storeAppStartupTime() => dataBox.write('startupTime', now.toString());

  //
  void getDailyTasks() {
    final previousStartupTime = DateTime.parse(dataBox.read('startupTime'));
    final nextMidnight = DateTime(
        previousStartupTime.year,
        previousStartupTime.month,
        previousStartupTime.day + 1); // returns exactly the next midnight

    if (now.isAfter(nextMidnight)) {
      dailyTasks = dataBox.read('dailyTasks');
      dailyTasks = 0;
    } else {
      dailyTasks = dataBox.read('dailyTasks');
      dailyTasks == null ? dailyTasks = 0 : dailyTasks;
    }
    update();
  }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Raj A
  • 544
  • 9
  • 21

2 Answers2

0

Your problem is the logic here:

   if (now == 12) {
      dailyTasks = 0;
    }

now is a DateTime object and you're comparing it to an int. So even though the compiler for some reason lets you get away with that, that will never be true.

However even if you fixed that to

 if (now.hour == 0) { // 0 is midnight
      dailyTasks = 0;
    }

That's only gonna reset if they happen to open the app between 12am and 1am.

You can store the startup time every time the user opens the app.

This function will now reset your dailyTask if now is after the closest following midnight to the last time the user opened the app. This should work assuming this controller only gets initialized once when the user starts the app. If not, store the startup time somewhere where it only happens once, but AFTER you run this function. Because this function relies on the stored start up time of the last time user opened the app, so it can't get stored again until after you reset dailyTasks.

  void getDailyTask() {
    final previousStartupTime = DateTime.parse(dataBox.read('startup_time')) ?? DateTime.now();
    final nextMidnight = DateTime(
        previousStartupTime.year,
        previousStartupTime.month,
        previousStartupTime.day + 1); // returns exactly the next midnight

    if (now.isAfter(nextMidnight)) {
      dailyTasks = 0;
    }
    update();
   dataBox.write('startup_time', now.toString());
  }
Loren.A
  • 4,872
  • 1
  • 10
  • 17
  • Greetings Loren, The value is not resetting. I have updated the code please review it. – Raj A Jun 03 '21 at 20:05
  • Are you calling `_storeAppStartupTime` in your `onInit`? I don't see it there. – Loren.A Jun 03 '21 at 22:27
  • Yess Loren, I'm calling `_storeAppStartupTime` as well as `getDailyTasks()` in the `onInit` method. The data is persisting but not resetting at midnight. – Raj A Jun 04 '21 at 08:29
  • Whoops I see my mistake. I was storing the time right before comparing it to now, sorry. Couldn't test it without waiting until the next day. Startup time needs to be stored after `dailyTasks` is reset. See updated answer. You can remove `_storeAppStartupTime` entirely and store it at the end of your `getDailyTasks` function and not in `onInit`. – Loren.A Jun 04 '21 at 11:49
-1

Try converting your dailyTasks into an Observable type from int to RxInt, like this,

class DataController with GetxController {

   @override
   void onInit() {
     getDailyTask();
     super.onInit();
   } 

   var dailyTasks = 0.obs;
    
   // DateTime
   DateTime now = DateTime.now();
    
   void dailyTaskIncrement() {
     dailyReads++;
     dataBox.write('dailyTasks', dailyTasks.value);
     update();
   }
    
   void getDailyTask() {
     dailyTasks = dataBox.read('dailyTasks');
     if (now == 12) {
       dailyTasks = 0.obs;
     }
     update();
   }

}
Nisanth Reddy
  • 5,967
  • 1
  • 11
  • 29