From c8e76cce5c6324cbe76600cae66c64ebdaffd89c Mon Sep 17 00:00:00 2001 From: Apostolof Date: Sun, 13 Jan 2019 03:16:55 +0200 Subject: [PATCH] Add API connection for diet following, ingredient prohibition and item ratings --- .../flavours/activities/AddDietActivity.java | 82 +++++++++++++++-- .../activities/AddIngredientActivity.java | 54 +++++++++++ .../flavours/activities/AddItemActivity.java | 2 - .../activities/AddRestaurantActivity.java | 5 +- .../flavours/activities/LoginActivity.java | 6 +- .../flavours/activities/RateItemActivity.java | 89 +++++++++++++++++-- .../activities/RateRestaurantActivity.java | 7 +- .../flavours/activities/SignUpActivity.java | 4 +- .../activities/diets/DietsActivity.java | 35 +------- .../activities/drink/DrinkActivity.java | 5 +- .../activities/food/FoodActivity.java | 5 +- .../activities/main/MainActivity.java | 5 +- .../fragments/ProfileInfoFragment.java | 12 ++- .../restaurant/RestaurantActivity.java | 1 - .../flavours/session/SessionManager.java | 5 +- .../flavours/utils/FollowDietStateTask.java | 45 ++++++++++ .../app/src/main/res/values/strings.xml | 2 + 17 files changed, 288 insertions(+), 76 deletions(-) create mode 100644 UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/utils/FollowDietStateTask.java diff --git a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/AddDietActivity.java b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/AddDietActivity.java index 921c39a..00f8437 100644 --- a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/AddDietActivity.java +++ b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/AddDietActivity.java @@ -1,5 +1,6 @@ package gr.auth.databases.flavours.activities; +import android.os.AsyncTask; import android.os.Bundle; import android.view.KeyEvent; import android.view.MenuItem; @@ -8,6 +9,7 @@ import android.view.inputmethod.EditorInfo; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.TextView; +import android.widget.Toast; import java.util.ArrayList; @@ -17,10 +19,15 @@ import androidx.appcompat.widget.AppCompatImageButton; import androidx.appcompat.widget.Toolbar; import gr.auth.databases.flavours.R; import gr.auth.databases.flavours.base.BaseActivity; -import gr.auth.databases.flavours.model.IngredientSummary; +import gr.auth.databases.flavours.model.Ingredient; +import okhttp3.FormBody; +import okhttp3.Request; +import okhttp3.RequestBody; + +import static gr.auth.databases.flavours.session.SessionManager.addDietUrl; public class AddDietActivity extends BaseActivity { - private ArrayList ingredients = new ArrayList<>(); + private ArrayList ingredients = new ArrayList<>(); private EditText dietNameInput, dietDescriptionInput; @Override @@ -37,7 +44,8 @@ public class AddDietActivity extends BaseActivity { } dietNameInput = findViewById(R.id.add_diet_name); - AppCompatButton addItemButton = findViewById(R.id.add_diet_add_btn); + dietDescriptionInput = findViewById(R.id.add_diet_description); + AppCompatButton addDietButton = findViewById(R.id.add_diet_add_btn); final TextView ingredientsTitle = findViewById(R.id.add_diet_add_ingredient_title); final EditText addIngredientInput = findViewById(R.id.add_diet_add_ingredient); final LinearLayout ingredientsList = findViewById(R.id.add_diet_ingredients_list); @@ -46,13 +54,21 @@ public class AddDietActivity extends BaseActivity { @Override public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) { if (actionId == EditorInfo.IME_ACTION_DONE) { - ingredients.add(new IngredientSummary( - addIngredientInput.getText().toString())); + final Ingredient newIngredient = new Ingredient(addIngredientInput.getText().toString(), false); + ingredients.add(newIngredient); View ingredientRow = getLayoutInflater().inflate(R.layout.add_item_ingredient_row, ingredientsList, false); TextView ingredientName = ingredientRow.findViewById(R.id.add_item_ingredient_name); ingredientName.setText(ingredients.get(ingredients.size() - 1).getName()); + ingredientRow.findViewById(R.id.add_item_ingredient_has_alcohol).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + ingredients.get(ingredients.indexOf(newIngredient)). + setHasAlcohol(!ingredients.get(ingredients.indexOf(newIngredient)).hasAlcohol()); + } + }); + AppCompatImageButton removeIngredientBtn = ingredientRow.findViewById(R.id.add_item_remove_ingredient); removeIngredientBtn.setOnClickListener(new View.OnClickListener() { @Override @@ -74,10 +90,11 @@ public class AddDietActivity extends BaseActivity { } }); - addItemButton.setOnClickListener(new View.OnClickListener() { + addDietButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - //TODO + AddDietTask addDietTask = new AddDietTask(); + addDietTask.execute(); } }); @@ -105,4 +122,55 @@ public class AddDietActivity extends BaseActivity { return super.onOptionsItemSelected(item); } + + private class AddDietTask extends AsyncTask { + private static final String REQ_DIET_NAME = "diet_name"; + private static final String REQ_DIET_DESCRIPTION = "diet_description"; + + private static final String REQ_DIET_PROHIBITS_INGREDIENTS_LIST = "ingredients"; + + @Override + protected void onPreExecute() { + } + + @Override + protected Integer doInBackground(Void... params) { + StringBuilder ingredientsJson = new StringBuilder("["); + for (int ingredientIndex = 0; ingredientIndex < ingredients.size(); ++ingredientIndex) { + Ingredient ingredient = ingredients.get(ingredientIndex); + ingredientsJson.append("{\"ingredient_name\": \"").append(ingredient.getName()) + .append("\", \"ingredient_has_alcohol\": \"").append(ingredient.hasAlcohol()) + .append("\"}"); + if (ingredientIndex < ingredients.size() - 1) { + ingredientsJson.append(","); + } + } + ingredientsJson.append("]"); + + //Builds the request + RequestBody formBody = new FormBody.Builder() + .add(REQ_DIET_NAME, dietNameInput.getText().toString()) + .add(REQ_DIET_DESCRIPTION, dietDescriptionInput.getText().toString()) + .add(REQ_DIET_PROHIBITS_INGREDIENTS_LIST, ingredientsJson.toString()) + .build(); + Request request = new Request.Builder() + .url(addDietUrl) + .post(formBody) + .build(); + + try { + client.newCall(request).execute(); + return 0; + } catch (Exception e) { + return 2; + } + } + + @Override + protected void onPostExecute(Integer result) { + Toast.makeText(AddDietActivity.this, + "Diet was added and awaits approval!", Toast.LENGTH_SHORT).show(); + AddDietActivity.this.finish(); + } + } } \ No newline at end of file diff --git a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/AddIngredientActivity.java b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/AddIngredientActivity.java index 84e3785..4ba93e1 100644 --- a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/AddIngredientActivity.java +++ b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/AddIngredientActivity.java @@ -2,6 +2,7 @@ package gr.auth.databases.flavours.activities; import android.app.Activity; import android.content.Intent; +import android.os.AsyncTask; import android.os.Bundle; import android.view.MenuItem; import android.view.View; @@ -15,6 +16,11 @@ import androidx.appcompat.widget.Toolbar; import gr.auth.databases.flavours.R; import gr.auth.databases.flavours.base.BaseActivity; import gr.auth.databases.flavours.model.Ingredient; +import okhttp3.FormBody; +import okhttp3.Request; +import okhttp3.RequestBody; + +import static gr.auth.databases.flavours.session.SessionManager.addIngredientUrl; public class AddIngredientActivity extends BaseActivity { public static final String INGREDIENT_ADD_RESULT = "INGREDIENT_ADD_RESULT"; @@ -57,6 +63,9 @@ public class AddIngredientActivity extends BaseActivity { return; } + AddIngredientTask addIngredientTask = new AddIngredientTask(); + addIngredientTask.execute(); + Intent returnIntent = new Intent(); returnIntent.putExtra(INGREDIENT_ADD_RESULT, new Ingredient(ingredientNameInput.getText().toString(), hasAlcoholResult == 1)); @@ -89,4 +98,49 @@ public class AddIngredientActivity extends BaseActivity { return super.onOptionsItemSelected(item); } + + private class AddIngredientTask extends AsyncTask { + private static final String REQ_INGREDIENT_NAME = "ingredient_name"; + private static final String REQ_INGREDIENT_HAS_ALCOHOL = "ingredient_has_alcohol"; + + @Override + protected void onPreExecute() { + } + + @Override + protected Integer doInBackground(Void... params) { + int hasAlcoholResult = -1; + switch (ingredientHasAlcoholInput.getCheckedRadioButtonId()) { + case R.id.add_ingredient_has_alcohol_yes: + hasAlcoholResult = 1; + break; + case R.id.add_ingredient_has_alcohol_no: + hasAlcoholResult = 0; + break; + } + + //Builds the request + RequestBody formBody = new FormBody.Builder() + .add(REQ_INGREDIENT_NAME, ingredientNameInput.getText().toString()) + .add(REQ_INGREDIENT_HAS_ALCOHOL, hasAlcoholResult == 1 ? "True" : "False") + .build(); + Request request = new Request.Builder() + .url(addIngredientUrl) + .post(formBody) + .build(); + + try { + client.newCall(request).execute(); + return 0; + } catch (Exception e) { + return 2; + } + } + + @Override + protected void onPostExecute(Integer result) { + Toast.makeText(AddIngredientActivity.this, + "Ingredient was added!", Toast.LENGTH_SHORT).show(); + } + } } \ No newline at end of file diff --git a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/AddItemActivity.java b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/AddItemActivity.java index 23f2823..c5a2265 100644 --- a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/AddItemActivity.java +++ b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/AddItemActivity.java @@ -51,7 +51,6 @@ public class AddItemActivity extends BaseActivity { itemType = (ItemSummary.ItemType) extras.getSerializable(BUNDLE_ADD_ITEM_ITEM_TYPE); restaurantId = extras.getInt(BUNDLE_ADD_ITEM_ITEM_RESTAURANT_ID); - Toolbar toolbar = findViewById(R.id.add_item_toolbar); if (itemType == ItemSummary.ItemType.FOOD) { toolbar.setTitle(getString(R.string.add_item_toolbar_title_food)); @@ -214,7 +213,6 @@ public class AddItemActivity extends BaseActivity { //Makes request & handles response client.newCall(request).execute(); - return 0; } catch (Exception e) { diff --git a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/AddRestaurantActivity.java b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/AddRestaurantActivity.java index c1f45bb..cb57982 100644 --- a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/AddRestaurantActivity.java +++ b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/AddRestaurantActivity.java @@ -260,7 +260,7 @@ public class AddRestaurantActivity extends BaseActivity implements AdapterView.O private static final String REQ_RESTAURANT_CLOSING = "restaurant_closing"; @Override - protected void onPreExecute() { //Show a progress dialog until done + protected void onPreExecute() { } @Override @@ -280,7 +280,6 @@ public class AddRestaurantActivity extends BaseActivity implements AdapterView.O .build(); try { - //Makes request & handles response Response response = client.newCall(request).execute(); if (response.code() == 201) { @@ -295,7 +294,7 @@ public class AddRestaurantActivity extends BaseActivity implements AdapterView.O } @Override - protected void onPostExecute(Integer result) { //Handle attempt result + protected void onPostExecute(Integer result) { } } } diff --git a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/LoginActivity.java b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/LoginActivity.java index fda8fc5..978a085 100644 --- a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/LoginActivity.java +++ b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/LoginActivity.java @@ -53,7 +53,7 @@ public class LoginActivity extends BaseActivity { private class LoginTask extends AsyncTask { @Override - protected void onPreExecute() { //Show a progress dialog until done + protected void onPreExecute() { loginButton.setEnabled(false); View view = getCurrentFocus(); @@ -69,7 +69,7 @@ public class LoginActivity extends BaseActivity { } @Override - protected void onPostExecute(Integer result) { //Handle attempt result + protected void onPostExecute(Integer result) { Toast.makeText(getApplicationContext(), "Welcome, " + sessionManager.getUsername() + "!", Toast.LENGTH_LONG) .show(); @@ -82,7 +82,7 @@ public class LoginActivity extends BaseActivity { @Override protected void onCancelled() { super.onCancelled(); - loginButton.setEnabled(true); //Re-enable login button + loginButton.setEnabled(true); //Re-enables login button } } diff --git a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/RateItemActivity.java b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/RateItemActivity.java index b1face4..7a9b24f 100644 --- a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/RateItemActivity.java +++ b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/RateItemActivity.java @@ -1,5 +1,6 @@ package gr.auth.databases.flavours.activities; +import android.os.AsyncTask; import android.os.Bundle; import android.view.MenuItem; import android.view.View; @@ -7,6 +8,7 @@ import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.Spinner; +import android.widget.Toast; import androidx.appcompat.app.ActionBar; import androidx.appcompat.widget.AppCompatButton; @@ -15,11 +17,17 @@ import gr.auth.databases.flavours.R; import gr.auth.databases.flavours.base.BaseActivity; import gr.auth.databases.flavours.model.ItemRating; import gr.auth.databases.flavours.model.ItemSummary; +import okhttp3.FormBody; +import okhttp3.Request; +import okhttp3.RequestBody; + +import static gr.auth.databases.flavours.session.SessionManager.rateDrinkUrl; +import static gr.auth.databases.flavours.session.SessionManager.rateFoodUrl; public class RateItemActivity extends BaseActivity implements AdapterView.OnItemSelectedListener { - public static final String BUNDLE_RATE_ITEM_TYPE = "BUNDLE_RATE_ITEM_TYPE"; + public static final String BUNDLE_RATE_ITEM = "BUNDLE_RATE_ITEM_TYPE"; - private ItemSummary.ItemType itemType; + private ItemSummary item; private EditText ratingText; private int ratingGrade; private ItemRating.PortionSize ratingPortionSize; @@ -30,12 +38,17 @@ public class RateItemActivity extends BaseActivity implements AdapterView.OnItem setContentView(R.layout.activity_rate_item); Bundle extras = getIntent().getExtras(); - if (extras != null) { - itemType = (ItemSummary.ItemType) extras.getSerializable(BUNDLE_RATE_ITEM_TYPE); - } + assert extras != null; + item = extras.getParcelable(BUNDLE_RATE_ITEM); Toolbar toolbar = findViewById(R.id.rate_item_toolbar); setSupportActionBar(toolbar); + if (item.getType() == ItemSummary.ItemType.FOOD) { + toolbar.setTitle(getString(R.string.rate_item_toolbar_title_food)); + } else if (item.getType() == ItemSummary.ItemType.DRINK) { + toolbar.setTitle(getString(R.string.rate_item_toolbar_title_drink)); + } + ActionBar actionbar = getSupportActionBar(); if (actionbar != null) { actionbar.setDisplayHomeAsUpEnabled(true); @@ -62,7 +75,8 @@ public class RateItemActivity extends BaseActivity implements AdapterView.OnItem rateButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - //TODO + RateItemTask rateItemTask = new RateItemTask(); + rateItemTask.execute(); } }); @@ -102,4 +116,67 @@ public class RateItemActivity extends BaseActivity implements AdapterView.OnItem @Override public void onNothingSelected(AdapterView adapterView) { } + + private class RateItemTask extends AsyncTask { + private static final String REQ_RATE_FOOD_ID = "food"; + + private static final String REQ_RATE_DRINK_ID = "drink"; + + private static final String REQ_RATE_ITEM_GRADE = "rating_grade"; + private static final String REQ_RATE_ITEM_DESCRIPTION = "rating_text"; + private static final String REQ_RATE_ITEM_PORTION_SIZE = "rating_pοrtion_size"; + + @Override + protected void onPreExecute() { + } + + @Override + protected Integer doInBackground(Void... params) { + Request request; + FormBody.Builder builder = new FormBody.Builder(); + + if (ratingPortionSize != null) { + builder.add(REQ_RATE_ITEM_PORTION_SIZE, + "" + ItemRating.PortionSize.getWorkingTypeFromId(ratingPortionSize.getId())); + } + + if (item.getType() == ItemSummary.ItemType.FOOD) { + builder.add(REQ_RATE_FOOD_ID, "" + item.getId()) + .add(REQ_RATE_ITEM_GRADE, "" + ratingGrade) + .add(REQ_RATE_ITEM_DESCRIPTION, "" + ratingText.getText().toString()) + .add("user", "1") + .build(); + RequestBody formBody = builder.build(); + request = new Request.Builder() + .url(rateFoodUrl) + .post(formBody) + .build(); + } else { + builder.add(REQ_RATE_DRINK_ID, "" + item.getId()) + .add(REQ_RATE_ITEM_GRADE, "" + ratingGrade) + .add(REQ_RATE_ITEM_DESCRIPTION, "" + ratingText.getText().toString()) + .add("user", "1") + .build(); + RequestBody formBody = builder.build(); + request = new Request.Builder() + .url(rateDrinkUrl) + .post(formBody) + .build(); + } + + try { + client.newCall(request).execute(); + + return 0; + } catch (Exception e) { + return 2; + } + } + + @Override + protected void onPostExecute(Integer result) { + Toast.makeText(RateItemActivity.this, "Rating added!", Toast.LENGTH_SHORT).show(); + RateItemActivity.this.finish(); + } + } } diff --git a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/RateRestaurantActivity.java b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/RateRestaurantActivity.java index f776c8d..289e03c 100644 --- a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/RateRestaurantActivity.java +++ b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/RateRestaurantActivity.java @@ -149,7 +149,6 @@ public class RateRestaurantActivity extends BaseActivity implements AdapterView. .build(); try { - //Makes request & handles response Response response = client.newCall(request).execute(); ResponseBody responseBody = response.body(); @@ -172,7 +171,7 @@ public class RateRestaurantActivity extends BaseActivity implements AdapterView. } @Override - protected void onPostExecute(Integer result) { //Handle attempt result + protected void onPostExecute(Integer result) { Spinner dietSpinner = findViewById(R.id.rate_restaurant_diet); ArrayAdapter dietAdapter = new ArrayAdapter<>(RateRestaurantActivity.this, android.R.layout.simple_spinner_item, spinnerArray); @@ -196,7 +195,6 @@ public class RateRestaurantActivity extends BaseActivity implements AdapterView. @Override protected Integer doInBackground(Void... params) { - //Builds the signup request FormBody.Builder builder = new FormBody.Builder() .add(REQ_RATE_RESTAURANT_ID, "" + restaurant.getId()) .add(REQ_RATE_RESTAURANT_GRADE, "" + ratingGrade) @@ -218,7 +216,6 @@ public class RateRestaurantActivity extends BaseActivity implements AdapterView. .build(); try { - //Makes request & handles response client.newCall(request).execute(); return 0; @@ -228,7 +225,7 @@ public class RateRestaurantActivity extends BaseActivity implements AdapterView. } @Override - protected void onPostExecute(Integer result) { //Handle attempt result + protected void onPostExecute(Integer result) { Toast.makeText(RateRestaurantActivity.this, "Rating added!", Toast.LENGTH_SHORT).show(); RateRestaurantActivity.this.finish(); } diff --git a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/SignUpActivity.java b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/SignUpActivity.java index a45223e..eb17128 100644 --- a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/SignUpActivity.java +++ b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/SignUpActivity.java @@ -94,7 +94,7 @@ public class SignUpActivity extends BaseActivity { private class SignupTask extends AsyncTask { @Override - protected void onPreExecute() { //Show a progress dialog until done + protected void onPreExecute() { signupButton.setEnabled(false); View view = getCurrentFocus(); @@ -110,7 +110,7 @@ public class SignUpActivity extends BaseActivity { } @Override - protected void onPostExecute(Integer result) { //Handle attempt result + protected void onPostExecute(Integer result) { Toast.makeText(getApplicationContext(), "Welcome, " + sessionManager.getUsername() + "!", Toast.LENGTH_LONG) .show(); 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 1208dc4..48e07fe 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 @@ -21,13 +21,12 @@ 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 gr.auth.databases.flavours.utils.FollowDietStateTask; 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 implements DietsAdapter.SubscribeDietsAdapterInteractionListener { private RecyclerView recyclerView; @@ -91,7 +90,7 @@ public class DietsActivity extends BaseActivity implements DietsAdapter.Subscrib @Override public void onSubscribeDietsAdapterInteraction(Diet diet) { - FollowDietStateTask followDietStateTask = new FollowDietStateTask(); + FollowDietStateTask followDietStateTask = new FollowDietStateTask(client); followDietStateTask.execute(diet.getId()); } @@ -153,34 +152,4 @@ public class DietsActivity extends BaseActivity implements DietsAdapter.Subscrib 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/drink/DrinkActivity.java b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/drink/DrinkActivity.java index b8214ef..f71279f 100644 --- a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/drink/DrinkActivity.java +++ b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/drink/DrinkActivity.java @@ -39,7 +39,7 @@ import okhttp3.Request; import okhttp3.Response; import okhttp3.ResponseBody; -import static gr.auth.databases.flavours.activities.RateItemActivity.BUNDLE_RATE_ITEM_TYPE; +import static gr.auth.databases.flavours.activities.RateItemActivity.BUNDLE_RATE_ITEM; import static gr.auth.databases.flavours.activities.ingredients.IngredientsActivity.INGREDIENT_PICK_RESULT; import static gr.auth.databases.flavours.session.SessionManager.drinksUserViewUrl; @@ -103,7 +103,7 @@ public class DrinkActivity extends BaseActivity { startActivityForResult(intent, ADD_INGREDIENT_REQUEST); } else if (viewPager.getCurrentItem() == 2) { Intent intent = new Intent(view.getContext(), RateItemActivity.class); - intent.putExtra(BUNDLE_RATE_ITEM_TYPE, ItemSummary.ItemType.DRINK); + intent.putExtra(BUNDLE_RATE_ITEM, mDrink); startActivity(intent); } } @@ -207,7 +207,6 @@ public class DrinkActivity extends BaseActivity { private static final String JSON_TAG_DRINK_RATING_DATE = "rating_date"; private static final String JSON_TAG_DRINK_RATING_PORTION_SIZE = "rating_pοrtion_size"; - @Override protected void onPreExecute() { } diff --git a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/food/FoodActivity.java b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/food/FoodActivity.java index 7c19a52..6f006d3 100644 --- a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/food/FoodActivity.java +++ b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/food/FoodActivity.java @@ -39,7 +39,7 @@ import okhttp3.Request; import okhttp3.Response; import okhttp3.ResponseBody; -import static gr.auth.databases.flavours.activities.RateItemActivity.BUNDLE_RATE_ITEM_TYPE; +import static gr.auth.databases.flavours.activities.RateItemActivity.BUNDLE_RATE_ITEM; import static gr.auth.databases.flavours.activities.ingredients.IngredientsActivity.INGREDIENT_PICK_RESULT; import static gr.auth.databases.flavours.session.SessionManager.foodsUserViewUrl; @@ -103,7 +103,7 @@ public class FoodActivity extends BaseActivity { startActivityForResult(intent, ADD_INGREDIENT_REQUEST); } else if (viewPager.getCurrentItem() == 2) { Intent intent = new Intent(view.getContext(), RateItemActivity.class); - intent.putExtra(BUNDLE_RATE_ITEM_TYPE, ItemSummary.ItemType.FOOD); + intent.putExtra(BUNDLE_RATE_ITEM, mFood); startActivity(intent); } } @@ -207,7 +207,6 @@ public class FoodActivity extends BaseActivity { private static final String JSON_TAG_FOOD_RATING_DATE = "rating_date"; private static final String JSON_TAG_FOOD_RATING_PORTION_SIZE = "rating_pοrtion_size"; - @Override protected void onPreExecute() { } diff --git a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/main/MainActivity.java b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/main/MainActivity.java index 523cbf7..121025e 100644 --- a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/main/MainActivity.java +++ b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/main/MainActivity.java @@ -301,7 +301,7 @@ public class MainActivity extends BaseActivity { private static final String JSON_TAG_RESTAURANT_CLOSING = "restaurant_closing"; @Override - protected void onPreExecute() { //Show a progress dialog until done + protected void onPreExecute() { restaurants.clear(); } @@ -354,7 +354,6 @@ public class MainActivity extends BaseActivity { .build(); try { - //Makes request & handles response Response response = client.newCall(request).execute(); ResponseBody responseBody = response.body(); @@ -380,7 +379,7 @@ public class MainActivity extends BaseActivity { } @Override - protected void onPostExecute(Integer result) { //Handle attempt result + protected void onPostExecute(Integer result) { viewPager.setAdapter(null); PagerAdapter pagerAdapter = new MainPagerAdapter(getSupportFragmentManager()); viewPager.setAdapter(pagerAdapter); diff --git a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/profile/fragments/ProfileInfoFragment.java b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/profile/fragments/ProfileInfoFragment.java index abd123f..1268294 100644 --- a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/profile/fragments/ProfileInfoFragment.java +++ b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/profile/fragments/ProfileInfoFragment.java @@ -11,8 +11,10 @@ import androidx.annotation.NonNull; import androidx.appcompat.widget.AppCompatImageButton; import androidx.fragment.app.Fragment; import gr.auth.databases.flavours.R; +import gr.auth.databases.flavours.base.BaseApplication; import gr.auth.databases.flavours.model.DietSummary; import gr.auth.databases.flavours.model.Profile; +import gr.auth.databases.flavours.utils.FollowDietStateTask; public class ProfileInfoFragment extends Fragment { @@ -76,11 +78,11 @@ public class ProfileInfoFragment extends Fragment { if (!profileUserView.getDietsFollowed().isEmpty()) { (rootView.findViewById(R.id.profile_diets_list_title)).setVisibility(View.VISIBLE); - LinearLayout dietsList = rootView.findViewById(R.id.profile_diets_list); + final LinearLayout dietsList = rootView.findViewById(R.id.profile_diets_list); dietsList.setVisibility(View.VISIBLE); - for (DietSummary dietFollowed : profileUserView.getDietsFollowed()) { - View userDietRow = getLayoutInflater().inflate(R.layout.profile_diet_row, dietsList, false); + for (final DietSummary dietFollowed : profileUserView.getDietsFollowed()) { + final View userDietRow = getLayoutInflater().inflate(R.layout.profile_diet_row, dietsList, false); TextView dietName = userDietRow.findViewById(R.id.profile_diet_name); dietName.setText(dietFollowed.getName()); @@ -90,7 +92,9 @@ public class ProfileInfoFragment extends Fragment { removeDiet.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - //TODO + FollowDietStateTask followDietStateTask = new FollowDietStateTask(BaseApplication.getInstance().getClient()); + followDietStateTask.execute(dietFollowed.getId()); + dietsList.removeView(userDietRow); } }); diff --git a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/restaurant/RestaurantActivity.java b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/restaurant/RestaurantActivity.java index e063657..74fc178 100644 --- a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/restaurant/RestaurantActivity.java +++ b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/restaurant/RestaurantActivity.java @@ -63,7 +63,6 @@ public class RestaurantActivity extends BaseActivity private TabLayout tabLayout; private FloatingActionButton FAB; - @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/session/SessionManager.java b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/session/SessionManager.java index 10cc636..dccb39e 100644 --- a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/session/SessionManager.java +++ b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/session/SessionManager.java @@ -56,6 +56,8 @@ public class SessionManager { public static final String restaurantsUrl = baseServerUrl + "api/restaurant/"; public static final String restaurantsUserViewUrl = baseServerUrl + "api/restaurantUserView/"; public static final String getUserDietsUrl = baseServerUrl + "api/userDiets/"; + public static final String rateDrinkUrl = baseServerUrl + "api/userratesdrink/"; + public static final String rateFoodUrl = baseServerUrl + "api/userratesfood/"; public static final String rateRestaurantUrl = baseServerUrl + "api/userratesrestaurant/"; public static final String foodsUserViewUrl = baseServerUrl + "api/foodUserView/"; public static final String drinksUserViewUrl = baseServerUrl + "api/drinkUserView/"; @@ -66,6 +68,8 @@ public class SessionManager { public static final String ingredientsUserViewUrl = baseServerUrl + "api/ingredientUserView/"; public static final String prohibitIngredientUrl = baseServerUrl + "api/prohibitIngredient/"; public static final String followDietUrl = baseServerUrl + "api/followDiet/"; + public static final String addIngredientUrl = baseServerUrl + "api/ingredient/"; + public static final String addDietUrl = baseServerUrl + "api/addDiet/"; // Client & Cookies private final OkHttpClient client; @@ -104,7 +108,6 @@ public class SessionManager { .post(formBody) .build(); - try { //Makes request & handles response Response response = client.newCall(request).execute(); diff --git a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/utils/FollowDietStateTask.java b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/utils/FollowDietStateTask.java new file mode 100644 index 0000000..4a85f78 --- /dev/null +++ b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/utils/FollowDietStateTask.java @@ -0,0 +1,45 @@ +package gr.auth.databases.flavours.utils; + +import android.os.AsyncTask; + +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; + +import static gr.auth.databases.flavours.session.SessionManager.followDietUrl; + +public class FollowDietStateTask extends AsyncTask { + private OkHttpClient client; + + public FollowDietStateTask(OkHttpClient client) { + this.client = client; + } + + @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/res/values/strings.xml b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/res/values/strings.xml index 828a2c0..4b8cdbe 100644 --- a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/res/values/strings.xml +++ b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/res/values/strings.xml @@ -160,6 +160,8 @@ Rate Item + Rate Food + Rate Drink Grade: Portion size: Add some text to your rating