|
|
@ -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(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |