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 94251e3..23f2823 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 @@ -1,6 +1,7 @@ package gr.auth.databases.flavours.activities; import android.annotation.SuppressLint; +import android.os.AsyncTask; import android.os.Bundle; import android.view.KeyEvent; import android.view.MenuItem; @@ -9,6 +10,7 @@ import android.view.inputmethod.EditorInfo; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.TextView; +import android.widget.Toast; import com.google.android.material.textfield.TextInputLayout; @@ -20,13 +22,21 @@ 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 gr.auth.databases.flavours.model.ItemSummary; +import okhttp3.FormBody; +import okhttp3.Request; +import okhttp3.RequestBody; + +import static gr.auth.databases.flavours.session.SessionManager.addDrinkUrl; +import static gr.auth.databases.flavours.session.SessionManager.addFoodUrl; public class AddItemActivity extends BaseActivity { public static final String BUNDLE_ADD_ITEM_ITEM_TYPE = "BUNDLE_ADD_ITEM_ITEM_TYPE"; + public static final String BUNDLE_ADD_ITEM_ITEM_RESTAURANT_ID = "BUNDLE_ADD_ITEM_ITEM_RESTAURANT_ID"; - private ArrayList ingredients = new ArrayList<>(); + private int restaurantId; + private ArrayList ingredients = new ArrayList<>(); private ItemSummary.ItemType itemType; private EditText itemNameInput, itemDescriptionInput; @@ -37,9 +47,10 @@ public class AddItemActivity extends BaseActivity { setContentView(R.layout.activity_add_item); Bundle extras = getIntent().getExtras(); - if (extras != null) { - itemType = (ItemSummary.ItemType) extras.getSerializable(BUNDLE_ADD_ITEM_ITEM_TYPE); - } + assert extras != null; + 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) { @@ -56,6 +67,7 @@ public class AddItemActivity extends BaseActivity { } itemNameInput = findViewById(R.id.add_item_name); + itemDescriptionInput = findViewById(R.id.add_item_description); AppCompatButton addItemButton = findViewById(R.id.add_item_add_btn); final TextView ingredientsTitle = findViewById(R.id.add_item_add_ingredient_title); final EditText addIngredientInput = findViewById(R.id.add_item_add_ingredient); @@ -73,13 +85,21 @@ public class AddItemActivity 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 @@ -104,7 +124,8 @@ public class AddItemActivity extends BaseActivity { addItemButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - //TODO + AddItemTask addItemTask = new AddItemTask(); + addItemTask.execute(); } }); @@ -132,4 +153,80 @@ public class AddItemActivity extends BaseActivity { return super.onOptionsItemSelected(item); } + + private class AddItemTask extends AsyncTask { + private static final String REQ_FOOD_NAME = "food_name"; + private static final String REQ_FOOD_DESCRIPTION = "food_description"; + + private static final String REQ_DRINK_NAME = "drink_name"; + private static final String REQ_DRINK_DESCRIPTION = "drink_description"; + + private static final String REQ_ITEM_RESTAURANT = "restaurant"; + private static final String REQ_ITEM_INGREDIENTS = "ingredients"; + + @Override + protected void onPreExecute() { + } + + @Override + protected Integer doInBackground(Void... params) { + Request request; + + 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("]"); + + if (itemType == ItemSummary.ItemType.FOOD) { + //Builds the request + RequestBody formBody = new FormBody.Builder() + .add(REQ_FOOD_NAME, itemNameInput.getText().toString()) + .add(REQ_FOOD_DESCRIPTION, itemDescriptionInput.getText().toString()) + .add(REQ_ITEM_RESTAURANT, "" + restaurantId) + .add(REQ_ITEM_INGREDIENTS, ingredientsJson.toString()) + .build(); + request = new Request.Builder() + .url(addFoodUrl) + .post(formBody) + .build(); + } else { + //Builds the request + RequestBody formBody = new FormBody.Builder() + .add(REQ_DRINK_NAME, itemNameInput.getText().toString()) + .add(REQ_DRINK_DESCRIPTION, itemDescriptionInput.getText().toString()) + .add(REQ_ITEM_RESTAURANT, "" + restaurantId) + .add(REQ_ITEM_INGREDIENTS, ingredientsJson.toString()) + .build(); + request = new Request.Builder() + .url(addDrinkUrl) + .post(formBody) + .build(); + } + + try { + //Makes request & handles response + client.newCall(request).execute(); + + + return 0; + + } catch (Exception e) { + return 2; + } + } + + @Override + protected void onPostExecute(Integer result) { + Toast.makeText(AddItemActivity.this, (itemType == ItemSummary.ItemType.FOOD ? "Food" : "Drink") + + " was added!", Toast.LENGTH_SHORT).show(); + AddItemActivity.this.finish(); + } + } } \ No newline at end of file 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 bb2d335..e063657 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 @@ -41,6 +41,7 @@ import okhttp3.Request; import okhttp3.Response; import okhttp3.ResponseBody; +import static gr.auth.databases.flavours.activities.AddItemActivity.BUNDLE_ADD_ITEM_ITEM_RESTAURANT_ID; import static gr.auth.databases.flavours.activities.AddItemActivity.BUNDLE_ADD_ITEM_ITEM_TYPE; import static gr.auth.databases.flavours.activities.RateRestaurantActivity.BUNDLE_RATE_RESTAURANT; import static gr.auth.databases.flavours.activities.drink.DrinkActivity.BUNDLE_ARG_DRINK; @@ -92,10 +93,12 @@ public class RestaurantActivity extends BaseActivity if (viewPager.getCurrentItem() == 1) { Intent intent = new Intent(view.getContext(), AddItemActivity.class); intent.putExtra(BUNDLE_ADD_ITEM_ITEM_TYPE, ItemSummary.ItemType.FOOD); + intent.putExtra(BUNDLE_ADD_ITEM_ITEM_RESTAURANT_ID, mRestaurant.getId()); startActivity(intent); } else if (viewPager.getCurrentItem() == 2) { Intent intent = new Intent(view.getContext(), AddItemActivity.class); intent.putExtra(BUNDLE_ADD_ITEM_ITEM_TYPE, ItemSummary.ItemType.DRINK); + intent.putExtra(BUNDLE_ADD_ITEM_ITEM_RESTAURANT_ID, mRestaurant.getId()); startActivity(intent); } else if (viewPager.getCurrentItem() == 3) { Intent intent = new Intent(view.getContext(), RateRestaurantActivity.class); diff --git a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/model/Ingredient.java b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/model/Ingredient.java index 2d7c9e6..8147025 100644 --- a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/model/Ingredient.java +++ b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/model/Ingredient.java @@ -20,6 +20,10 @@ public class Ingredient implements Parcelable { return hasAlcohol; } + public void setHasAlcohol(boolean hasAlcohol) { + this.hasAlcohol = hasAlcohol; + } + @Override public int describeContents() { return 0; 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 7ac4d3f..0e41138 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 @@ -60,6 +60,9 @@ public class SessionManager { public static final String foodsUserViewUrl = baseServerUrl + "api/foodUserView/"; public static final String drinksUserViewUrl = baseServerUrl + "api/drinkUserView/"; public static final String profileUserViewUrl = baseServerUrl + "api/profileUserView/"; + public static final String addFoodUrl = baseServerUrl + "api/addFood/"; + public static final String addDrinkUrl = baseServerUrl + "api/addDrink/"; + // Client & Cookies private final OkHttpClient client; diff --git a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/res/layout/add_item_ingredient_row.xml b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/res/layout/add_item_ingredient_row.xml index 3d33a13..4199fd1 100644 --- a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/res/layout/add_item_ingredient_row.xml +++ b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/res/layout/add_item_ingredient_row.xml @@ -4,13 +4,34 @@ android:layout_height="wrap_content" android:orientation="horizontal"> - + android:orientation="vertical"> + + + + + + + + + + ADD ITEM ADD FOOD ADD DRINK + Ingredient has alcohol: Add Diet