Home > Back-end >  Flutter: counter of series days, counter of days entry
Flutter: counter of series days, counter of days entry

Time:03-26

My application needs a counter that counts a continuous series of user logins each day. For example, he visits 7 days in a row, the application shows this. If the user interrupted the series (did not enter on the 8th day), then the counter is reset.

How can this be implemented?

CodePudding user response:

I can assume that you need to create a Database and write dates there, for example:

23/03/2022 - 1st launch
24/03/2022 - 2nd launch
25/03/2022 - did not open the application
26/03/2022 - 3th launch, but counter reset to 1st.

Just read the Database data the next time you log into the application from the sum of the days that users logged in in a row. You can come up with several methods to optimize this process so as not to clutter up the Database.

Or you can make the application work in the background, but it seems to me that using the Database and counting days when entering the application will be more efficient.

CodePudding user response:

I made some assumptions to create the following code:

  1. that you want a difference in calendar day, not 24 hour periods. That is... if a user logs in 5 minutes before midnight, and five minutes after, that it counts as a separate day. I took care of that formatting in the getJustDate function.

  2. That you are keeping the counter in some non-volatile way, like a database or writing to disk

  3. That the code in main is run as an event that is trigger at login.

Using those assumptions, I created a dartpad.dev gist so that you can play around with the code: https://dartpad.dev/?id=6c74d9a4ad8f561fd2fdd65be8ffc3be

And, just in case you can't open it, here's the code written out:

    import 'package:intl/intl.dart';

void main() { //<-- this would be triggered by the login event
      
    // this gets the last counter from the database
    int getLastCounter(){
      return 3;
    }
      
    // this sets the database counter
    void setCounter(counter) {
      // set database value 
      print('Counter set successfully to value $counter');
    }
      
    // this gets the last date the user logged in from database 
    DateTime loadLastTime(){
      return (DateTime.parse("2022-03-24")); //<-- alter this date to see code change
    }
      
    // This sets new login date
    void setNewTime(date){
      print('Set last login date to: $date');
    }  
    
     DateTime getJustDate(DateTime date){
        final DateFormat formatter = DateFormat('yyyy-MM-dd');
        final  formatted = formatter.format(date);
        return DateTime.parse(formatted);
      }  
      
      
      // run this on login event
      int counter  = getLastCounter();
      print('current login count: $counter');
      final today = getJustDate(DateTime.now());
      final last = getJustDate(loadLastTime());
      if(today.difference(last) == Duration(hours:24)){
        print(counter   1);
        setCounter(counter   1);
      }
      else
      {
        setCounter(0);
      }
      setNewTime(today);
    }
  • Related