Home > front end >  How to implement a daily streak counter in android java?
How to implement a daily streak counter in android java?

Time:12-24

I want to implement a daily streak counter for the user. The streak count should increase by 1 if the user return t the app the next day, and the streak count should be again return to 1 if the user has not returned to the app the next consecutive day.

So far, i have tried implementing this using the given method in the image, but my daily streak always show 1. I think this is because it is never going to the 'else' part.

Can you guys help me solve this thing.Any type of help is really appreciated.

enter image description here

CodePudding user response:

savedInstanceState will return null on app launch you have to check first time user with like below.

     if preferences.getInt(STREAKS) == null{ 
    
    }else {}

CodePudding user response:

If the user logged in for the first time

  • initialize last login date and reset streak

else

  • if the user logged in on the same day do nothing
  • if the user logged in consecutive days increase the streak and update the last login date
  • if the last login date is more than a day reset the streak and last login date

Below is a pseudo implementation

class ConsecutiveDayChecker {

    /**
     * Call this method when user login
     */
    public void onUserLogin() {
        DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
        Date date = new Date();
        String today = dateFormat.format(date);
        String lastLoginDay = getLastLoginDate();

        String yesterday = getYesterdayDate(dateFormat, date);

        if (lastLoginDay == null) {
            // user logged in for the first time
            updateLastLoginDate(today);
            incrementDays();
        } else {
            if (lastLoginDay.equals(today)) {
                // User logged in the same day , do nothing
            } else if (lastLoginDay.equals(yesterday)) {
                // User logged in consecutive days , add 1
                updateLastLoginDate(today);
                incrementDays();
            } else {
                // It's been more than a day user logged in, reset the counter to 1
                updateLastLoginDate(today);
                resetDays();
            }
        }
    }

    private String getYesterdayDate(DateFormat simpleDateFormat, Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, -1);
        return simpleDateFormat.format(calendar.getTime());
    }

    /**
     * Update last login date into the storage
     * @param date
     */
    private void updateLastLoginDate(String date) {
        // SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
        // SharedPreferences.Editor editor = sharedPref.edit();
        // editor.putString("last_login_day", date);
        // editor.apply();
    }

    /**
     * Get last login date 
     * @return
     */
    private String getLastLoginDate() {
        // String lastLogin = null;
        // SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
        // lastLogin = sharedPref.getString("last_login_day", null);
        // return lastLogin;
    }

    private int getConsecutiveDays() {
        // int days = 0;
        // SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
        // days = sharedPref.getInt("num_consecutive_days", 0);
        // return days;
    }

    private void incrementDays() {
        // int days = getConsecutiveDays()   1;
        // SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
        // SharedPreferences.Editor editor = sharedPref.edit();
        // editor.putInt("num_consecutive_days", days);
        // editor.apply();
    }

    private void resetDays() {
        // int days = 1;
        // SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
        // SharedPreferences.Editor editor = sharedPref.edit();
        // editor.putInt("num_consecutive_days", days);
        // editor.apply();
    }

    public int getStreak() {
        return getConsecutiveDays();
    }
}
  • Related