|
|
@ -2,6 +2,7 @@ package gr.auth.databases.flavours.activities.ingredients; |
|
|
|
|
|
|
|
import android.app.Activity; |
|
|
|
import android.content.Intent; |
|
|
|
import android.os.AsyncTask; |
|
|
|
import android.os.Bundle; |
|
|
|
import android.view.MenuItem; |
|
|
|
import android.view.View; |
|
|
@ -9,6 +10,9 @@ import android.widget.Toast; |
|
|
|
|
|
|
|
import com.google.android.material.floatingactionbutton.FloatingActionButton; |
|
|
|
|
|
|
|
import org.json.JSONArray; |
|
|
|
import org.json.JSONObject; |
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
|
|
|
import androidx.appcompat.app.ActionBar; |
|
|
@ -19,13 +23,23 @@ import gr.auth.databases.flavours.R; |
|
|
|
import gr.auth.databases.flavours.activities.AddIngredientActivity; |
|
|
|
import gr.auth.databases.flavours.base.BaseActivity; |
|
|
|
import gr.auth.databases.flavours.model.Ingredient; |
|
|
|
import okhttp3.Request; |
|
|
|
import okhttp3.RequestBody; |
|
|
|
import okhttp3.Response; |
|
|
|
import okhttp3.ResponseBody; |
|
|
|
|
|
|
|
import static gr.auth.databases.flavours.activities.AddIngredientActivity.INGREDIENT_ADD_RESULT; |
|
|
|
import static gr.auth.databases.flavours.session.SessionManager.ingredientsUserViewUrl; |
|
|
|
import static gr.auth.databases.flavours.session.SessionManager.prohibitIngredientUrl; |
|
|
|
|
|
|
|
public class IngredientsActivity extends BaseActivity implements IngredientsAdapter.IngredientsAdapterInteractionListener { |
|
|
|
public class IngredientsActivity extends BaseActivity |
|
|
|
implements IngredientsAdapter.SubscribeIngredientsAdapterInteractionListener, |
|
|
|
IngredientsAdapter.IngredientsAdapterInteractionListener { |
|
|
|
public static final String INGREDIENT_PICK_RESULT = "INGREDIENT_PICK_RESULT"; |
|
|
|
private static final int ADD_NEW_INGREDIENT_REQUEST = 2000; |
|
|
|
|
|
|
|
private RecyclerView recyclerView; |
|
|
|
private ArrayList<String> userProhibitsIngredients; |
|
|
|
private boolean isCalledForResult; |
|
|
|
private FloatingActionButton FAB; |
|
|
|
private ArrayList<Ingredient> ingredients = new ArrayList<>(); |
|
|
@ -57,21 +71,13 @@ public class IngredientsActivity extends BaseActivity implements IngredientsAdap |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
RecyclerView recyclerView = findViewById(R.id.ingredients_list); |
|
|
|
recyclerView = findViewById(R.id.ingredients_list); |
|
|
|
recyclerView.setHasFixedSize(true); |
|
|
|
LinearLayoutManager layoutManager = new LinearLayoutManager(this); |
|
|
|
recyclerView.setLayoutManager(layoutManager); |
|
|
|
|
|
|
|
ingredients.add(new Ingredient("Μόνο κρέας", false)); |
|
|
|
ingredients.add(new Ingredient("Σχεδόν μόνο κρέας", true)); |
|
|
|
|
|
|
|
if (isCalledForResult) { |
|
|
|
ingredientsAdapter = new IngredientsAdapter(this, ingredients, this); |
|
|
|
} else { |
|
|
|
ingredientsAdapter = new IngredientsAdapter(this, ingredients, null); |
|
|
|
} |
|
|
|
|
|
|
|
recyclerView.setAdapter(ingredientsAdapter); |
|
|
|
IngredientsTask ingredientsTask = new IngredientsTask(); |
|
|
|
ingredientsTask.execute(); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
@ -106,6 +112,12 @@ public class IngredientsActivity extends BaseActivity implements IngredientsAdap |
|
|
|
super.onDestroy(); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onSubscribeIngredientsAdapterInteraction(Ingredient ingredient) { |
|
|
|
ProhibitIngredientStateTask prohibitIngredientStateTask = new ProhibitIngredientStateTask(); |
|
|
|
prohibitIngredientStateTask.execute(ingredient.getName()); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onIngredientsAdapterInteraction(Ingredient ingredient) { |
|
|
|
Intent returnIntent = new Intent(); |
|
|
@ -125,5 +137,101 @@ public class IngredientsActivity extends BaseActivity implements IngredientsAdap |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private class IngredientsTask extends AsyncTask<Void, Void, Integer> { |
|
|
|
private static final String JSON_TAG_INGREDIENTS_LIST = "ingredients"; |
|
|
|
private static final String JSON_TAG_INGREDIENT_NAME = "ingredient_name"; |
|
|
|
private static final String JSON_TAG_INGREDIENT_HAS_ALCOHOL = "ingredient_has_alcohol"; |
|
|
|
|
|
|
|
private static final String JSON_TAG_USER_PROHIBITS_INGREDIENT_LIST = "prohibits"; |
|
|
|
private static final String JSON_TAG_USER_PROHIBITS_INGREDIENT = "ingredient_name"; |
|
|
|
|
|
|
|
@Override |
|
|
|
protected void onPreExecute() { |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
protected Integer doInBackground(Void... params) { |
|
|
|
//Builds the request
|
|
|
|
Request request = new Request.Builder() |
|
|
|
.url(ingredientsUserViewUrl) |
|
|
|
.build(); |
|
|
|
|
|
|
|
try { |
|
|
|
//Makes request & handles response
|
|
|
|
Response response = client.newCall(request).execute(); |
|
|
|
|
|
|
|
ResponseBody responseBody = response.body(); |
|
|
|
assert responseBody != null; |
|
|
|
String result = responseBody.string(); |
|
|
|
JSONObject jsonResponse = new JSONObject(result); |
|
|
|
|
|
|
|
userProhibitsIngredients = new ArrayList<>(); |
|
|
|
JSONArray jsonUserProhibitsIngredients = jsonResponse.getJSONArray(JSON_TAG_USER_PROHIBITS_INGREDIENT_LIST); |
|
|
|
for (int ingredientIndex = 0; ingredientIndex < jsonUserProhibitsIngredients.length(); ++ingredientIndex) { |
|
|
|
JSONObject ingredient = jsonUserProhibitsIngredients.getJSONObject(ingredientIndex); |
|
|
|
userProhibitsIngredients.add(ingredient.getString(JSON_TAG_USER_PROHIBITS_INGREDIENT)); |
|
|
|
} |
|
|
|
|
|
|
|
JSONArray jsonIngredientsList = jsonResponse.getJSONArray(JSON_TAG_INGREDIENTS_LIST); |
|
|
|
for (int dietIndex = 0; dietIndex < jsonIngredientsList.length(); ++dietIndex) { |
|
|
|
JSONObject ingredient = jsonIngredientsList.getJSONObject(dietIndex); |
|
|
|
ingredients.add(new Ingredient(ingredient.getString(JSON_TAG_INGREDIENT_NAME), |
|
|
|
ingredient.getBoolean(JSON_TAG_INGREDIENT_HAS_ALCOHOL))); |
|
|
|
} |
|
|
|
return 1; |
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
return 2; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
protected void onPostExecute(Integer result) { |
|
|
|
if (isCalledForResult) { |
|
|
|
ingredientsAdapter = new IngredientsAdapter(IngredientsActivity.this, |
|
|
|
ingredients, |
|
|
|
userProhibitsIngredients, |
|
|
|
null, |
|
|
|
IngredientsActivity.this); |
|
|
|
} else { |
|
|
|
ingredientsAdapter = new IngredientsAdapter(IngredientsActivity.this, |
|
|
|
ingredients, |
|
|
|
userProhibitsIngredients, |
|
|
|
IngredientsActivity.this, |
|
|
|
null); |
|
|
|
} |
|
|
|
recyclerView.setAdapter(ingredientsAdapter); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private class ProhibitIngredientStateTask extends AsyncTask<String, Void, Integer> { |
|
|
|
@Override |
|
|
|
protected void onPreExecute() { |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
protected Integer doInBackground(String... params) { |
|
|
|
RequestBody requestBody = RequestBody.create(null, new byte[]{}); |
|
|
|
|
|
|
|
//Builds the request
|
|
|
|
Request request = new Request.Builder() |
|
|
|
.patch(requestBody) |
|
|
|
.url(prohibitIngredientUrl + params[0] + "/") |
|
|
|
.build(); |
|
|
|
|
|
|
|
try { |
|
|
|
//Makes request & handles response
|
|
|
|
client.newCall(request).execute(); |
|
|
|
return 0; |
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
return 2; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
protected void onPostExecute(Integer result) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|