Repository: openfoodfacts/openfoodfacts-androidapp
Issue: add a new product from scan fragment 

From rhe scan fragment, when the product doesn't exist a popup appears and when i want to add the product the application crash 

Changed files:
app/src/main/java/openfoodfacts/github/scrachx/openfood/network/OpenFoodAPIClient.java
app/src/main/java/openfoodfacts/github/scrachx/openfood/views/SaveProductOfflineActivity.java

diff --git a/app/src/main/java/openfoodfacts/github/scrachx/openfood/network/OpenFoodAPIClient.java b/app/src/main/java/openfoodfacts/github/scrachx/openfood/network/OpenFoodAPIClient.java
index 70d56b508..9306d75c3 100644
--- a/app/src/main/java/openfoodfacts/github/scrachx/openfood/network/OpenFoodAPIClient.java
+++ b/app/src/main/java/openfoodfacts/github/scrachx/openfood/network/OpenFoodAPIClient.java
@@ -13,6 +13,8 @@ import com.orm.SugarRecord;
 
 import net.steamcrafted.loadtoast.LoadToast;
 
+import org.apache.commons.lang3.StringUtils;
+
 import java.io.File;
 import java.util.Date;
 import java.util.List;
@@ -25,7 +27,6 @@ import openfoodfacts.github.scrachx.openfood.models.AllergenRestResponse;
 import openfoodfacts.github.scrachx.openfood.models.HistoryProduct;
 import openfoodfacts.github.scrachx.openfood.models.Product;
 import openfoodfacts.github.scrachx.openfood.models.ProductImage;
-import openfoodfacts.github.scrachx.openfood.models.ProductImageField;
 import openfoodfacts.github.scrachx.openfood.models.Search;
 import openfoodfacts.github.scrachx.openfood.models.SendProduct;
 import openfoodfacts.github.scrachx.openfood.models.State;
@@ -37,6 +38,10 @@ import retrofit2.Response;
 import retrofit2.Retrofit;
 import retrofit2.converter.jackson.JacksonConverterFactory;
 
+import static openfoodfacts.github.scrachx.openfood.models.ProductImageField.FRONT;
+import static openfoodfacts.github.scrachx.openfood.models.ProductImageField.INGREDIENT;
+import static openfoodfacts.github.scrachx.openfood.models.ProductImageField.NUTRITION;
+
 public class OpenFoodAPIClient {
 
     private static final JacksonConverterFactory jacksonConverterFactory = JacksonConverterFactory.create();
@@ -293,44 +298,34 @@ public class OpenFoodAPIClient {
         apiService.saveProduct(product.getBarcode(), product.getName(), product.getStores(), product.getUserId(), product.getPassword()).enqueue(new Callback<State>() {
             @Override
             public void onResponse(Call<State> call, Response<State> response) {
-                if (!response.isSuccess()) {
+                if (!response.isSuccess() || response.body().getStatus() == 0) {
+                    lt.error();
                     productSentCallback.onProductSentResponse(false);
                     return;
                 }
 
-                long status = response.body().getStatus();
-                if (status == 0) {
-                    String imguploadFront = product.getImgupload_front();
-                    if (!imguploadFront.isEmpty()) {
-                        ProductImage image = new ProductImage(product.getBarcode(), ProductImageField.FRONT, new File(imguploadFront));
-                        postImg(activity, image);
-                    }
-
-                    String imguploadIngredients = product.getImgupload_ingredients();
-                    if (!imguploadIngredients.isEmpty()) {
-                        ProductImage image = new ProductImage(product.getBarcode(), ProductImageField.INGREDIENT, new File(imguploadIngredients));
-                        postImg(activity, image);
-                    }
+                String imguploadFront = product.getImgupload_front();
+                ProductImage image = new ProductImage(product.getBarcode(), FRONT, new File(imguploadFront));
+                postImg(activity, image);
 
-                    String imguploadNutrition = product.getImgupload_nutrition();
-                    if (!imguploadNutrition.isEmpty()) {
-                        ProductImage image = new ProductImage(product.getBarcode(), ProductImageField.NUTRITION, new File(imguploadNutrition));
-                        postImg(activity, image);
-                    }
+                String imguploadIngredients = product.getImgupload_ingredients();
+                if (StringUtils.isNotEmpty(imguploadIngredients)) {
+                    postImg(activity, new ProductImage(product.getBarcode(), INGREDIENT, new File(imguploadIngredients)));
+                }
 
-                    lt.error();
-                    productSentCallback.onProductSentResponse(false);
-                } else {
-                    lt.success();
-                    productSentCallback.onProductSentResponse(true);
+                String imguploadNutrition = product.getImgupload_nutrition();
+                if (StringUtils.isNotBlank(imguploadNutrition)) {
+                    postImg(activity, new ProductImage(product.getBarcode(), NUTRITION, new File(imguploadNutrition)));
                 }
 
+                lt.success();
+                productSentCallback.onProductSentResponse(true);
             }
 
             @Override
             public void onFailure(Call<State> call, Throwable t) {
-                productSentCallback.onProductSentResponse(false);
                 lt.error();
+                productSentCallback.onProductSentResponse(false);
             }
         });
     }
diff --git a/app/src/main/java/openfoodfacts/github/scrachx/openfood/views/SaveProductOfflineActivity.java b/app/src/main/java/openfoodfacts/github/scrachx/openfood/views/SaveProductOfflineActivity.java
index 2c9796cc1..48e5d2506 100644
--- a/app/src/main/java/openfoodfacts/github/scrachx/openfood/views/SaveProductOfflineActivity.java
+++ b/app/src/main/java/openfoodfacts/github/scrachx/openfood/views/SaveProductOfflineActivity.java
@@ -25,6 +25,8 @@ import com.afollestad.materialdialogs.DialogAction;
 import com.afollestad.materialdialogs.MaterialDialog;
 import com.squareup.picasso.Picasso;
 
+import org.apache.commons.lang3.StringUtils;
+
 import java.io.File;
 import java.util.List;
 
@@ -116,6 +118,9 @@ public class SaveProductOfflineActivity extends BaseActivity {
             int spinnerPosition = unitAdapter.getPosition(mProduct.getWeight_unit());
             spinnerW.setSelection(spinnerPosition);
             spinnerI.setSelection(0);
+        } else {
+            mProduct = new SendProduct();
+            mProduct.setBarcode(mBarcode);
         }
     }
     @OnItemSelected(value = R.id.spinnerUnitWeight, callback = OnItemSelected.Callback.ITEM_SELECTED)
@@ -128,7 +133,7 @@ public class SaveProductOfflineActivity extends BaseActivity {
         mImage[0] = spinnerI.getItemAtPosition(pos).toString();
 
         if(pos == 0) {
-            if(!mProduct.getImgupload_front().isEmpty()) {
+            if(StringUtils.isNotBlank(mProduct.getImgupload_front())) {
                 imgSave.setVisibility(View.VISIBLE);
                 Picasso.with(this)
                         .load(new File(mProduct.getImgupload_front()))
@@ -139,7 +144,7 @@ public class SaveProductOfflineActivity extends BaseActivity {
                 imgSave.setVisibility(View.GONE);
             }
         } else if(pos == 1) {
-            if(!mProduct.getImgupload_nutrition().isEmpty()) {
+            if(StringUtils.isNotBlank(mProduct.getImgupload_nutrition())) {
                 imgSave.setVisibility(View.VISIBLE);
                 Picasso.with(this)
                         .load(new File(mProduct.getImgupload_nutrition()))
@@ -150,7 +155,7 @@ public class SaveProductOfflineActivity extends BaseActivity {
                 imgSave.setVisibility(View.GONE);
             }
         } else {
-            if(!mProduct.getImgupload_ingredients().isEmpty()) {
+            if(StringUtils.isNotBlank(mProduct.getImgupload_ingredients())) {
                 imgSave.setVisibility(View.VISIBLE);
                 Picasso.with(this)
                         .load(new File(mProduct.getImgupload_ingredients()))
@@ -171,60 +176,62 @@ public class SaveProductOfflineActivity extends BaseActivity {
 
     @OnClick(R.id.buttonSaveProduct)
     protected void onSaveProduct() {
-        if (!mProduct.getImgupload_front().isEmpty()) {
+        if (mProduct.getImgupload_front().isEmpty()) {
+            Toast.makeText(getApplicationContext(), R.string.txtPictureNeeded, Toast.LENGTH_LONG).show();
+            return;
+        }
 
-            mProduct.setBarcode(mBarcode);
-            mProduct.setName(name.getText().toString());
-            mProduct.setWeight(weight.getText().toString());
-            mProduct.setWeight_unit(mUnit[0]);
-            mProduct.setStores(store.getText().toString());
-
-            final SharedPreferences settingsUser = getSharedPreferences("login", 0);
-            String login = settingsUser.getString("user", "");
-            String password = settingsUser.getString("pass", "");
-            if(!login.isEmpty() && !password.isEmpty()) {
-                mProduct.setUserId(login);
-                mProduct.setPassword(password);
-            }
+        mProduct.setBarcode(mBarcode);
+        mProduct.setName(name.getText().toString());
+        mProduct.setWeight(weight.getText().toString());
+        mProduct.setWeight_unit(mUnit[0]);
+        mProduct.setStores(store.getText().toString());
+
+        final SharedPreferences settingsUser = getSharedPreferences("login", 0);
+        String login = settingsUser.getString("user", "");
+        String password = settingsUser.getString("pass", "");
+        if (!login.isEmpty() && !password.isEmpty()) {
+            mProduct.setUserId(login);
+            mProduct.setPassword(password);
+        }
 
+        Utils.compressImage(mProduct.getImgupload_front());
+
+        if (StringUtils.isNotBlank(mProduct.getImgupload_ingredients())) {
             Utils.compressImage(mProduct.getImgupload_ingredients());
+        }
+
+        if(StringUtils.isNotBlank(mProduct.getImgupload_nutrition())) {
             Utils.compressImage(mProduct.getImgupload_nutrition());
-            Utils.compressImage(mProduct.getImgupload_front());
-
-            ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
-            NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
-            boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
-
-            if(isConnected) {
-                api.post(this, mProduct, new OpenFoodAPIClient.OnProductSentCallback() {
-                    @Override
-                    public void onProductSentResponse(boolean value) {
-                        if(!value) {
-                            if (mProduct != null) {
-                                mProduct.save();
-                            }
-                            Toast.makeText(getApplicationContext(), R.string.txtDialogsContentInfoSave, Toast.LENGTH_LONG).show();
-                        } else {
-                            Toast.makeText(getApplicationContext(), R.string.product_sent, Toast.LENGTH_LONG).show();
-                        }
-                        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
-                        intent.putExtra("openOfflineEdit",true);
-                        startActivity(intent);
-                        finish();
+        }
+
+        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
+        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
+        boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
+
+        if (isConnected) {
+            api.post(this, mProduct, new OpenFoodAPIClient.OnProductSentCallback() {
+                @Override
+                public void onProductSentResponse(boolean value) {
+                    if (!value) {
+                        mProduct.save();
+                        Toast.makeText(getApplicationContext(), R.string.txtDialogsContentInfoSave, Toast.LENGTH_LONG).show();
+                    } else {
+                        Toast.makeText(getApplicationContext(), R.string.product_sent, Toast.LENGTH_LONG).show();
                     }
-                });
-            } else {
-                if (mProduct != null) {
-                    mProduct.save();
+                    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
+                    intent.putExtra("openOfflineEdit", true);
+                    startActivity(intent);
+                    finish();
                 }
-                Toast.makeText(getApplicationContext(), R.string.txtDialogsContentInfoSave, Toast.LENGTH_LONG).show();
-                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
-                intent.putExtra("openOfflineEdit",true);
-                startActivity(intent);
-                finish();
-            }
+            });
         } else {
-            Toast.makeText(getApplicationContext(), R.string.txtPictureNeeded, Toast.LENGTH_LONG).show();
+            mProduct.save();
+            Toast.makeText(getApplicationContext(), R.string.txtDialogsContentInfoSave, Toast.LENGTH_LONG).show();
+            Intent intent = new Intent(getApplicationContext(), MainActivity.class);
+            intent.putExtra("openOfflineEdit", true);
+            startActivity(intent);
+            finish();
         }
     }