Browse Source

API connection for food and drink addition

master
Apostolos Fanakis 6 years ago
parent
commit
eea7db8e3d
  1. 111
      UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/AddItemActivity.java
  2. 3
      UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/restaurant/RestaurantActivity.java
  3. 4
      UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/model/Ingredient.java
  4. 3
      UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/session/SessionManager.java
  5. 25
      UI/AndroidApp/flavoursWithoutBorders/app/src/main/res/layout/add_item_ingredient_row.xml
  6. 1
      UI/AndroidApp/flavoursWithoutBorders/app/src/main/res/values/strings.xml

111
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<IngredientSummary> ingredients = new ArrayList<>();
private int restaurantId;
private ArrayList<Ingredient> 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) {
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<Void, Void, Integer> {
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();
}
}
}

3
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);

4
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;

3
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;

25
UI/AndroidApp/flavoursWithoutBorders/app/src/main/res/layout/add_item_ingredient_row.xml

@ -4,14 +4,35 @@
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/add_item_ingredient_name"
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/add_item_ingredient_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/add_item_ingredient_has_alcohol" />
<androidx.appcompat.widget.AppCompatCheckBox
android:id="@+id/add_item_ingredient_has_alcohol"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<androidx.appcompat.widget.AppCompatImageButton
android:id="@+id/add_item_remove_ingredient"
android:layout_width="wrap_content"

1
UI/AndroidApp/flavoursWithoutBorders/app/src/main/res/values/strings.xml

@ -132,6 +132,7 @@
<string name="add_item_add_btn">ADD ITEM</string>
<string name="add_food_add_btn">ADD FOOD</string>
<string name="add_drink_add_btn">ADD DRINK</string>
<string name="add_item_ingredient_has_alcohol">Ingredient has alcohol: </string>
<!-- Add Diet -->
<string name="add_diet_toolbar_title">Add Diet</string>

Loading…
Cancel
Save