package openfoodfacts.github.scrachx.openfood.views;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import openfoodfacts.github.scrachx.openfood.R;
import openfoodfacts.github.scrachx.openfood.models.Additives;

/**
 * Created by scotscriven on 08/05/15.
 */
public class SplashActivity extends Activity {

    /** Duration of wait **/
    private final int SPLASH_DISPLAY_LENGTH = 1000;
    private SharedPreferences settings;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.splash);

        settings = getSharedPreferences("prefs",0);
        boolean firstRun = settings.getBoolean("firstRun",true);
        if(!firstRun) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    Intent mainIntent = new Intent(SplashActivity.this, MainActivity.class);
                    SplashActivity.this.startActivity(mainIntent);
                    SplashActivity.this.finish();
                }
            }, SPLASH_DISPLAY_LENGTH);

        } else {
            new AdditivesJson().execute();
        }
    }

    private class AdditivesJson extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... arg0) {

            String json = null;
            String json1 = null;
            try {
                InputStream is = getAssets().open("additives_fr.json");
                int size = is.available();
                byte[] buffer = new byte[size];
                is.read(buffer);
                is.close();
                json = new String(buffer, "UTF-8");
                ObjectMapper objectMapper = new ObjectMapper();
                List<Additives> la = objectMapper.readValue(json, new TypeReference<List<Additives>>(){});
                for (Additives a: la) {
                    Additives ta = new Additives(a.getCode(),a.getName(),a.getRisk());
                    ta.save();
                }

                InputStream is1 = getAssets().open("additives_en.json");
                int size1 = is1.available();
                byte[] buffer1 = new byte[size1];
                is1.read(buffer1);
                is1.close();
                json1 = new String(buffer1, "UTF-8");
                ObjectMapper objectMapper1 = new ObjectMapper();
                List<Additives> la1 = objectMapper1.readValue(json1, new TypeReference<List<Additives>>(){});
                for (Additives a: la1) {
                    Additives ta = new Additives(a.getCode(),a.getName(),a.getRisk());
                    ta.save();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            SharedPreferences.Editor editor=settings.edit();
            editor.putBoolean("firstRun",false);
            editor.apply();
            Intent mainIntent = new Intent(SplashActivity.this, MainActivity.class);
            startActivity(mainIntent);
            finish();
        }

    }
}
@@ -76,16 +76,48 @@
 tivity;%0A
+import android.content.Context;%0A
 import a
@@ -396,16 +396,62 @@
 apper;%0A%0A
+import net.steamcrafted.loadtoast.LoadToast;%0A%0A
 import j
@@ -1608,16 +1608,20 @@
 vesJson(
+this
 ).execut
@@ -1702,111 +1702,559 @@
 id, 
-Void, Void%3E %7B%0A%0A        @Override%0A        protected void onPreExecute() %7B%0A            super.onPreExecute
+Boolean, Boolean%3E %7B%0A%0A        private Context context;%0A        private LoadToast lt;%0A%0A        public AdditivesJson(Context ctx) %7B%0A            context = ctx;%0A            lt = new LoadToast(ctx);%0A        %7D%0A%0A%0A        @Override%0A        protected void onPreExecute() %7B%0A            super.onPreExecute();%0A            lt.setText(context.getString(R.string.toast_retrieving));%0A            lt.setBackgroundColor(context.getResources().getColor(R.color.indigo_600));%0A            lt.setTextColor(context.getResources().getColor(R.color.white));%0A            lt.show
 ();%0A
@@ -2300,20 +2300,23 @@
 otected 
-Void
+Boolean
  doInBac
@@ -3677,32 +3677,61 @@
               %7D%0A
+                return true;%0A
             %7D ca
@@ -3807,35 +3807,39 @@
     
-%7D%0A%0A            return null;
+    return false;%0A            %7D
 %0A   
@@ -3901,20 +3901,23 @@
 Execute(
-Void
+Boolean
  result)
@@ -3956,24 +3956,90 @@
 te(result);%0A
+            if(result) lt.success();%0A            else lt.error();%0A
             

Add loading toast on splash screen