Skip to content

Commit

Permalink
Better handling for allergens downloading
Browse files Browse the repository at this point in the history
  • Loading branch information
itchix committed May 28, 2016
1 parent 1a604cf commit 279a49e
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 59 deletions.
Binary file modified app/app-release.apk
Binary file not shown.
4 changes: 2 additions & 2 deletions app/build.gradle
Expand Up @@ -8,8 +8,8 @@ android {
applicationId "openfoodfacts.github.scrachx.openfood"
minSdkVersion 16
targetSdkVersion 23
versionCode 20
versionName "0.2.2"
versionCode 21
versionName "0.2.3"
}

dexOptions {
Expand Down
@@ -1,15 +1,23 @@
package openfoodfacts.github.scrachx.openfood.fragments;

import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.github.clans.fab.FloatingActionButton;

import net.steamcrafted.loadtoast.LoadToast;

import org.apache.commons.collections.IteratorUtils;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -19,6 +27,7 @@
import butterknife.OnClick;
import openfoodfacts.github.scrachx.openfood.R;
import openfoodfacts.github.scrachx.openfood.models.Allergen;
import openfoodfacts.github.scrachx.openfood.models.FoodAPIRestClientUsage;
import openfoodfacts.github.scrachx.openfood.views.adapters.AllergensAdapter;

public class AlertUserFragment extends BaseFragment {
Expand Down Expand Up @@ -72,26 +81,73 @@ protected void onAddAllergens() {
if(a.getIdAllergen().contains("en:")) allS.add(a.getName().substring(a.getName().indexOf(":")+1));
}
}
new MaterialDialog.Builder(mView.getContext())
.title(R.string.title_dialog_alert)
.items(allS)
.itemsCallback(new MaterialDialog.ListCallback() {
@Override
public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
all.get(which).setEnable("true");
all.get(which).save();
boolean canAdd = true;
for(Allergen a : mAllergens) {
if(a.getName().equals(all.get(which).getName())) canAdd = false;
if(allS.size() > 0) {
new MaterialDialog.Builder(mView.getContext())
.title(R.string.title_dialog_alert)
.items(allS)
.itemsCallback(new MaterialDialog.ListCallback() {
@Override
public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
all.get(which).setEnable("true");
all.get(which).save();
boolean canAdd = true;
for(Allergen a : mAllergens) {
if(a.getName().equals(all.get(which).getName())) canAdd = false;
}
if(canAdd) {
mAllergens.add(all.get(which));
mAdapter.notifyItemInserted(mAllergens.size() - 1);
mRvAllergens.scrollToPosition(mAdapter.getItemCount() - 1);
}
}
if(canAdd) {
mAllergens.add(all.get(which));
mAdapter.notifyItemInserted(mAllergens.size() - 1);
mRvAllergens.scrollToPosition(mAdapter.getItemCount() - 1);
}
}
})
.show();
})
.show();
} else {
ConnectivityManager cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
if(isConnected) {
final LoadToast lt = new LoadToast(getContext());
lt.setText(getContext().getString(R.string.toast_retrieving));
lt.setBackgroundColor(getContext().getResources().getColor(R.color.indigo_600));
lt.setTextColor(getContext().getResources().getColor(R.color.white));
lt.show();
new MaterialDialog.Builder(mView.getContext())
.title(R.string.title_dialog_alert)
.content(R.string.info_download_data)
.positiveText(R.string.txtYes)
.negativeText(R.string.txtNo)
.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull final MaterialDialog dialog, @NonNull DialogAction which) {
final SharedPreferences.Editor editor = mSettings.edit();
FoodAPIRestClientUsage api = new FoodAPIRestClientUsage();
api.getAllergens(new FoodAPIRestClientUsage.OnAllergensCallback() {
@Override
public void onAllergensResponse(boolean value) {
if (!value) {
editor.putBoolean("errorAllergens", true);
editor.apply();
} else {
editor.putBoolean("errorAllergens", false);
editor.apply();
}
lt.success();
dialog.hide();
}
});
}
})
.show();
} else {
new MaterialDialog.Builder(mView.getContext())
.title(R.string.title_dialog_alert)
.content(R.string.info_download_data_connection)
.neutralText(R.string.txtOk)
.show();
}
}

}

}
Expand Up @@ -264,8 +264,12 @@ public void onRetry(int retryNo) {
});
}

public void getAllergens() {
FoodAPIRestClient.getSync("/allergens.json", null, new AsyncHttpResponseHandler() {
public interface OnAllergensCallback {
public void onAllergensResponse(boolean value);
}

public void getAllergens(final OnAllergensCallback onAllergensCallback) {
FoodAPIRestClient.getAsync("/allergens.json", null, new AsyncHttpResponseHandler() {

@Override
public void onStart() {
Expand All @@ -283,11 +287,14 @@ public void onSuccess(int statusCode, cz.msebera.android.httpclient.Header[] hea
Allergen al = new Allergen(a.getUrl(),a.getName(),a.getProducts(),a.getIdAllergen());
al.save();
}
onAllergensCallback.onAllergensResponse(true);
} catch (InvalidFormatException e) {
e.printStackTrace();
onAllergensCallback.onAllergensResponse(false);
}
} catch (IOException e) {
e.printStackTrace();
onAllergensCallback.onAllergensResponse(false);
}
}

Expand Down
Expand Up @@ -3,6 +3,8 @@
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;

Expand All @@ -26,8 +28,16 @@ public void onCreate(Bundle icicle) {
super.onCreate(icicle);

settings = getSharedPreferences("prefs", 0);
SharedPreferences.Editor editor = settings.edit();
boolean firstRun = settings.getBoolean("firstRun", true);
if (!firstRun) {
boolean errorAdditives = settings.getBoolean("errorAdditives", true);
boolean errorAllergens = settings.getBoolean("errorAllergens", true);
if(!errorAdditives && ! errorAllergens) {
editor.putBoolean("firstRun", false);
editor.apply();
firstRun = false;
}
if (!firstRun ) {
Intent mainIntent = new Intent(SplashActivity.this, MainActivity.class);
SplashActivity.this.startActivity(mainIntent);
SplashActivity.this.finish();
Expand Down Expand Up @@ -62,33 +72,34 @@ protected Boolean 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<Additive> la = objectMapper.readValue(json, new TypeReference<List<Additive>>() {});
for (Additive a : la) {
Additive ta = new Additive(a.getCode(), a.getName(), a.getRisk());
ta.save();
boolean errorAdditives = settings.getBoolean("errorAdditives", true);
if(errorAdditives) {
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<Additive> la = objectMapper.readValue(json, new TypeReference<List<Additive>>() {});
for (Additive a : la) {
Additive ta = new Additive(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<Additive> la1 = objectMapper1.readValue(json1, new TypeReference<List<Additive>>() {});
for (Additive a : la1) {
Additive ta = new Additive(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<Additive> la1 = objectMapper1.readValue(json1, new TypeReference<List<Additive>>() {});
for (Additive a : la1) {
Additive ta = new Additive(a.getCode(), a.getName(), a.getRisk());
ta.save();
}
FoodAPIRestClientUsage api = new FoodAPIRestClientUsage();
api.getAllergens();
return true;
} catch (IOException ex) {
ex.printStackTrace();
Expand All @@ -97,16 +108,51 @@ protected Boolean doInBackground(Void... arg0) {
}

@Override
protected void onPostExecute(Boolean result) {
protected void onPostExecute(final Boolean result) {
super.onPostExecute(result);
if (result) lt.success();
else lt.error();
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("firstRun", false);
editor.apply();
Intent mainIntent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(mainIntent);
finish();
final SharedPreferences.Editor editor = settings.edit();
boolean errorAllergens = settings.getBoolean("errorAllergens", true);
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
if(isConnected) {
if(errorAllergens) {
FoodAPIRestClientUsage api = new FoodAPIRestClientUsage();
api.getAllergens(new FoodAPIRestClientUsage.OnAllergensCallback() {
@Override
public void onAllergensResponse(boolean value) {
if (result && value) {
lt.success();
editor.putBoolean("firstRun", false);
editor.apply();
}
if(!value){
lt.error();
editor.putBoolean("errorAllergens", true);
editor.apply();
} else {
editor.putBoolean("errorAllergens", false);
editor.apply();
}
Intent mainIntent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(mainIntent);
finish();
}
});
}
} else {
if(!result){
lt.error();
editor.putBoolean("errorAdditives", true);
editor.apply();
} else {
editor.putBoolean("errorAdditives", false);
editor.apply();
}
Intent mainIntent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(mainIntent);
finish();
}
}
}
}
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Expand Up @@ -152,6 +152,8 @@
<string name="permission_title">Permission information</string>
<string name="permission_denied">Please activate permission in the settings</string>
<string name="permission_storage">Storage is needed to take or choose pictures</string>
<string name="info_download_data">You need to download some data to get this functionality working, please press yes to download it.</string>
<string name="info_download_data_connection">You need to download some data to get this functionality working. Please activate internet connection.</string>

<style name="DefaultButtonText">
<item name="android:layout_width">fill_parent</item>
Expand Down

0 comments on commit 279a49e

Please sign in to comment.