From 5a82bbd6731323877a5a5cdf2dda0db5f5acc01b Mon Sep 17 00:00:00 2001 From: Apostolof Date: Sat, 12 Jan 2019 22:09:08 +0200 Subject: [PATCH] API connection for diets and ingredients addition --- .../activities/diets/DietsActivity.java | 118 ++++++++++++++- .../activities/diets/DietsAdapter.java | 29 +++- .../ingredients/IngredientsActivity.java | 134 ++++++++++++++++-- .../ingredients/IngredientsAdapter.java | 44 +++++- .../auth/databases/flavours/model/Diet.java | 4 + .../flavours/session/SessionManager.java | 5 +- .../src/main/res/layout/ingredients_row.xml | 7 + 7 files changed, 316 insertions(+), 25 deletions(-) diff --git a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/diets/DietsActivity.java b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/diets/DietsActivity.java index ff68b7b..1208dc4 100644 --- a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/diets/DietsActivity.java +++ b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/diets/DietsActivity.java @@ -1,12 +1,16 @@ package gr.auth.databases.flavours.activities.diets; import android.content.Intent; +import android.os.AsyncTask; import android.os.Bundle; import android.view.MenuItem; import android.view.View; import com.google.android.material.floatingactionbutton.FloatingActionButton; +import org.json.JSONArray; +import org.json.JSONObject; + import java.util.ArrayList; import androidx.appcompat.app.ActionBar; @@ -17,8 +21,16 @@ import gr.auth.databases.flavours.R; import gr.auth.databases.flavours.activities.AddDietActivity; import gr.auth.databases.flavours.base.BaseActivity; import gr.auth.databases.flavours.model.Diet; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; +import okhttp3.ResponseBody; + +import static gr.auth.databases.flavours.session.SessionManager.dietsUserViewUrl; +import static gr.auth.databases.flavours.session.SessionManager.followDietUrl; -public class DietsActivity extends BaseActivity { +public class DietsActivity extends BaseActivity implements DietsAdapter.SubscribeDietsAdapterInteractionListener { + private RecyclerView recyclerView; private FloatingActionButton FAB; private ArrayList diets = new ArrayList<>(); @@ -47,16 +59,13 @@ public class DietsActivity extends BaseActivity { } }); - RecyclerView recyclerView = findViewById(R.id.diets_list); + recyclerView = findViewById(R.id.diets_list); recyclerView.setHasFixedSize(true); LinearLayoutManager layoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(layoutManager); - diets.add(new Diet(1, "Μόνο κρέας", "Τρώς πολύ κρέας.", false)); - diets.add(new Diet(2, "Σχεδόν μόνο κρέας", "Τρώς αρκετό κρέας.", true)); - - DietsAdapter dietsAdapter = new DietsAdapter(diets); - recyclerView.setAdapter(dietsAdapter); + DietsTask dietsTask = new DietsTask(); + dietsTask.execute(); } @Override @@ -79,4 +88,99 @@ public class DietsActivity extends BaseActivity { return super.onOptionsItemSelected(item); } + + @Override + public void onSubscribeDietsAdapterInteraction(Diet diet) { + FollowDietStateTask followDietStateTask = new FollowDietStateTask(); + followDietStateTask.execute(diet.getId()); + } + + private class DietsTask extends AsyncTask { + private static final String JSON_TAG_DIETS_LIST = "diets"; + private static final String JSON_TAG_DIET_ID = "diet_id"; + private static final String JSON_TAG_DIET_NAME = "diet_name"; + private static final String JSON_TAG_DIET_DESCRIPTION = "diet_description"; + //private static final String JSON_TAG_DIET_IS_APPROVED = "food_calories"; + + private static final String JSON_TAG_USER_FOLLOWS_DIET_LIST = "followed"; + private static final String JSON_TAG_USER_FOLLOWS_DIET_DIET = "diet"; + + @Override + protected void onPreExecute() { + } + + @Override + protected Integer doInBackground(Void... params) { + //Builds the request + Request request = new Request.Builder() + .url(dietsUserViewUrl) + .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); + + ArrayList userDiets = new ArrayList<>(); + JSONArray jsonUserDiets = jsonResponse.getJSONArray(JSON_TAG_USER_FOLLOWS_DIET_LIST); + for (int dietIndex = 0; dietIndex < jsonUserDiets.length(); ++dietIndex) { + JSONObject diet = jsonUserDiets.getJSONObject(dietIndex); + userDiets.add(diet.getInt(JSON_TAG_USER_FOLLOWS_DIET_DIET)); + } + + JSONArray jsonDietsList = jsonResponse.getJSONArray(JSON_TAG_DIETS_LIST); + for (int dietIndex = 0; dietIndex < jsonDietsList.length(); ++dietIndex) { + JSONObject diet = jsonDietsList.getJSONObject(dietIndex); + diets.add(new Diet(diet.getInt(JSON_TAG_DIET_ID), + diet.getString(JSON_TAG_DIET_NAME), + diet.getString(JSON_TAG_DIET_DESCRIPTION), + userDiets.contains(diet.getInt(JSON_TAG_DIET_ID)))); + } + return 1; + } catch (Exception e) { + e.printStackTrace(); + return 2; + } + } + + @Override + protected void onPostExecute(Integer result) { + DietsAdapter dietsAdapter = new DietsAdapter(diets, DietsActivity.this); + recyclerView.setAdapter(dietsAdapter); + } + } + + private class FollowDietStateTask extends AsyncTask { + @Override + protected void onPreExecute() { + } + + @Override + protected Integer doInBackground(Integer... params) { + RequestBody requestBody = RequestBody.create(null, new byte[]{}); + + //Builds the request + Request request = new Request.Builder() + .patch(requestBody) + .url(followDietUrl + 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) { + } + } } diff --git a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/diets/DietsAdapter.java b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/diets/DietsAdapter.java index a3a1dd9..18579ca 100644 --- a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/diets/DietsAdapter.java +++ b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/diets/DietsAdapter.java @@ -15,9 +15,11 @@ import gr.auth.databases.flavours.model.Diet; public class DietsAdapter extends RecyclerView.Adapter { private ArrayList diets; + private SubscribeDietsAdapterInteractionListener subscribeDietsAdapterInteractionListener; - DietsAdapter(ArrayList diets) { + DietsAdapter(ArrayList diets, SubscribeDietsAdapterInteractionListener subscribeDietsAdapterInteractionListener) { this.diets = diets; + this.subscribeDietsAdapterInteractionListener = subscribeDietsAdapterInteractionListener; } @NonNull @@ -29,12 +31,31 @@ public class DietsAdapter extends RecyclerView.Adapter userProhibitsIngredients; private boolean isCalledForResult; private FloatingActionButton FAB; private ArrayList 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 { + 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 { + @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) { + } + } +} diff --git a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/ingredients/IngredientsAdapter.java b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/ingredients/IngredientsAdapter.java index 05a09cc..b3a4795 100644 --- a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/ingredients/IngredientsAdapter.java +++ b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/ingredients/IngredientsAdapter.java @@ -9,6 +9,7 @@ import android.widget.TextView; import java.util.ArrayList; import androidx.annotation.NonNull; +import androidx.appcompat.widget.AppCompatImageButton; import androidx.cardview.widget.CardView; import androidx.recyclerview.widget.RecyclerView; import gr.auth.databases.flavours.R; @@ -17,12 +18,17 @@ import gr.auth.databases.flavours.model.Ingredient; public class IngredientsAdapter extends RecyclerView.Adapter { private Context context; private ArrayList ingredients; + private ArrayList userProhibitsIngredients; + private SubscribeIngredientsAdapterInteractionListener subscribeIngredientsAdapterInteractionListener; private IngredientsAdapterInteractionListener ingredientsAdapterInteractionListener; - IngredientsAdapter(Context context, ArrayList ingredients, + IngredientsAdapter(Context context, ArrayList ingredients, ArrayList userProhibitsIngredients, + SubscribeIngredientsAdapterInteractionListener subscribeIngredientsAdapterInteractionListener, IngredientsAdapterInteractionListener ingredientsAdapterInteractionListener) { this.context = context; this.ingredients = ingredients; + this.userProhibitsIngredients = userProhibitsIngredients; + this.subscribeIngredientsAdapterInteractionListener = subscribeIngredientsAdapterInteractionListener; this.ingredientsAdapterInteractionListener = ingredientsAdapterInteractionListener; } @@ -35,11 +41,39 @@ public class IngredientsAdapter extends RecyclerView.Adapter + + \ No newline at end of file