package openfoodfacts.github.scrachx.openfood.views.splash;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import openfoodfacts.github.scrachx.openfood.BuildConfig;
import openfoodfacts.github.scrachx.openfood.utils.Utils;
import openfoodfacts.github.scrachx.openfood.views.LoadTaxonomiesService;

/**
 * Created by Lobster on 03.03.18.
 */

public class SplashPresenter implements ISplashPresenter.Actions {

    /**
     * Mutiplied by 6*30* to reduce the issue. TODO: fix https://github.com/openfoodfacts/openfoodfacts-androidapp/issues/1616
     */
    private final Long REFRESH_PERIOD = 6 * 30 * 24 * 60 * 60 * 1000L;

    private ISplashPresenter.View view;
    private SharedPreferences settings;
    Context context;

    public SplashPresenter(SharedPreferences settings, ISplashPresenter.View view, Context context) {
        this.view = view;
        this.settings = settings;
        this.context = context;
    }

    @Override
    public void refreshData() {
            boolean firstRun = settings.getBoolean("firstRun", true);
            if (firstRun) {
                settings.edit()
                        .putBoolean("firstRun", false)
                        .apply();
                //Set to 1 the lastDownload for the taxonomy to be loaded :
                //This two are needed for all flavors :
                settings.edit().putLong("lastDownloadcategories", Long.valueOf(1)).apply();
                settings.edit().putLong("lastDownloadtags", Long.valueOf(1)).apply();
                if (BuildConfig.FLAVOR.equals("off") || BuildConfig.FLAVOR.equals("obf")) {
                    settings.edit().putLong("lastDownloadadditives", Long.valueOf(1)).apply();
                    settings.edit().putLong("lastDownloadcountries", Long.valueOf(1)).apply();
                    settings.edit().putLong("lastDownloadlabels", Long.valueOf(1)).apply();
                    //For the moment, ingredients are only need if Diet is use.
                    settings.edit().putLong("lastDownloadingredients", Long.valueOf(0)).apply();
                }
                if (BuildConfig.FLAVOR.equals("off")) {
                    settings.edit().putLong("lastDownloadallergens", Long.valueOf(1)).apply();
                }
            }
            if (isNeedToRefresh()) { //true if data was refreshed more than 1 day ago
                Intent intent = new Intent(context, LoadTaxonomiesService.class);
                context.startService(intent);
            }
            if (firstRun) {
                new Handler().postDelayed(() -> view.navigateToMainActivity(), 6000);
            } else {
                view.navigateToMainActivity();
            }
    }

    /*
     * This method checks if data was refreshed more than 1 day ago
     */
    private Boolean isNeedToRefresh() {
        return System.currentTimeMillis() - settings.getLong(Utils.LAST_REFRESH_DATE, 0) > REFRESH_PERIOD;
    }
}

@@ -1424,83 +1424,1194 @@
     
-settings.edit().putLong(%22lastDownloadcategories%22, Long.valueOf(1)).apply();
+ if(!settings.contains(%22lastDownloadcategories%22))%7B%0A            settings.edit().putLong(%22lastDownloadcategories%22, Long.valueOf(ProductRepository.TAXONOMY_UP_TO_DATE)).apply();%0A            settings.edit().putLong(%22lastDownloadtags%22, Long.valueOf(ProductRepository.TAXONOMY_UP_TO_DATE)).apply();%0A            if (BuildConfig.FLAVOR.equals(%22off%22) %7C%7C BuildConfig.FLAVOR.equals(%22obf%22)) %7B%0A                settings.edit().putLong(%22lastDownloadadditives%22, Long.valueOf(ProductRepository.TAXONOMY_UP_TO_DATE)).apply();%0A                settings.edit().putLong(%22lastDownloadcountries%22, Long.valueOf(ProductRepository.TAXONOMY_UP_TO_DATE)).apply();%0A                settings.edit().putLong(%22lastDownloadlabels%22, Long.valueOf(ProductRepository.TAXONOMY_UP_TO_DATE)).apply();%0A                //For the moment, ingredients are only need if Diet is use.%0A                settings.edit().putLong(%22lastDownloadingredients%22, Long.valueOf(ProductRepository.TAXONOMY_NOT_TO_BE_LOADED)).apply();%0A            %7D%0A            if (BuildConfig.FLAVOR.equals(%22off%22)) %7B%0A                settings.edit().putLong(%22lastDownloadallergens%22, Long.valueOf(ProductRepository.TAXONOMY_UP_TO_DATE)).apply();%0A            %7D%0A        %7D
 %0A   

Update app/src/main/java/openfoodfacts/github/scrachx/openfood/views/splash/SplashPresenter.java