From 574ed64d90779805d90ed96ecb7bcc37c433fb35 Mon Sep 17 00:00:00 2001 From: Apostolof Date: Fri, 11 Jan 2019 01:58:57 +0200 Subject: [PATCH] API connection for food and drink views --- .../activities/drink/DrinkActivity.java | 127 ++++++++++++++++-- .../drink/fragments/DrinkInfoFragment.java | 28 ++-- .../fragments/DrinkIngredientsFragment.java | 14 +- .../drink/fragments/DrinkRatingsFragment.java | 19 +-- .../activities/food/FoodActivity.java | 126 ++++++++++++++++- .../food/fragments/FoodInfoFragment.java | 28 ++-- .../fragments/FoodIngredientsFragment.java | 14 +- .../food/fragments/FoodRatingsFragment.java | 19 +-- .../restaurant/RestaurantActivity.java | 5 +- .../fragments/RestaurantItemAdapter.java | 2 +- .../auth/databases/flavours/model/Drink.java | 78 +++++++++++ .../auth/databases/flavours/model/Food.java | 77 +++++++++++ .../databases/flavours/model/ItemRating.java | 13 ++ .../databases/flavours/model/ItemSummary.java | 2 +- .../auth/databases/flavours/model/Rating.java | 8 +- .../flavours/session/SessionManager.java | 2 + .../flavours/utils/ItemRatingsAdapter.java | 7 +- 17 files changed, 482 insertions(+), 87 deletions(-) create mode 100644 UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/model/Drink.java create mode 100644 UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/model/Food.java 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 bdf5114..b8214ef 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 @@ -2,8 +2,8 @@ package gr.auth.databases.flavours.activities.drink; import android.app.Activity; import android.content.Intent; +import android.os.AsyncTask; import android.os.Bundle; -import android.util.Log; import android.view.MenuItem; import android.view.View; import android.widget.Toast; @@ -11,6 +11,9 @@ import android.widget.Toast; import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.android.material.tabs.TabLayout; +import org.json.JSONArray; +import org.json.JSONObject; + import java.util.ArrayList; import java.util.List; @@ -28,25 +31,41 @@ import gr.auth.databases.flavours.activities.drink.fragments.DrinkIngredientsFra import gr.auth.databases.flavours.activities.drink.fragments.DrinkRatingsFragment; import gr.auth.databases.flavours.activities.ingredients.IngredientsActivity; import gr.auth.databases.flavours.base.BaseActivity; +import gr.auth.databases.flavours.model.Drink; import gr.auth.databases.flavours.model.Ingredient; +import gr.auth.databases.flavours.model.ItemRating; import gr.auth.databases.flavours.model.ItemSummary; +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.ingredients.IngredientsActivity.INGREDIENT_PICK_RESULT; +import static gr.auth.databases.flavours.session.SessionManager.drinksUserViewUrl; public class DrinkActivity extends BaseActivity { + public static final String BUNDLE_ARG_DRINK = "BUNDLE_ARG_DRINK"; private static final int ADD_INGREDIENT_REQUEST = 911; + private ItemSummary mDrink; + private Drink drink; + private ArrayList ingredients = new ArrayList<>(); + private ArrayList ratings = new ArrayList<>(); private ViewPager viewPager; private FloatingActionButton FAB; + private TabLayout tabLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_drink); + Bundle extras = getIntent().getExtras(); + assert extras != null; + mDrink = extras.getParcelable(BUNDLE_ARG_DRINK); + Toolbar toolbar = findViewById(R.id.drink_toolbar); - toolbar.setTitle("White Russian"); + toolbar.setTitle(mDrink.getItemName()); setSupportActionBar(toolbar); ActionBar actionbar = getSupportActionBar(); if (actionbar != null) { @@ -90,9 +109,10 @@ public class DrinkActivity extends BaseActivity { } }); - setupViewPager(viewPager); - TabLayout tabLayout = findViewById(R.id.drink_tabs); - tabLayout.setupWithViewPager(viewPager); + tabLayout = findViewById(R.id.drink_tabs); + + DrinkTask drinkTask = new DrinkTask(); + drinkTask.execute(); } @Override @@ -129,9 +149,9 @@ public class DrinkActivity extends BaseActivity { private void setupViewPager(ViewPager viewPager) { RestaurantPagerAdapter adapter = new RestaurantPagerAdapter(getSupportFragmentManager()); - adapter.addFrag(DrinkInfoFragment.newInstance(64), "INFO"); - adapter.addFrag(DrinkIngredientsFragment.newInstance(64), "INGREDIENTS"); - adapter.addFrag(DrinkRatingsFragment.newInstance(64), "RATINGS"); + adapter.addFrag(DrinkInfoFragment.newInstance(drink), "INFO"); + adapter.addFrag(DrinkIngredientsFragment.newInstance(ingredients), "INGREDIENTS"); + adapter.addFrag(DrinkRatingsFragment.newInstance(ratings), "RATINGS"); viewPager.setAdapter(adapter); } @@ -164,4 +184,95 @@ public class DrinkActivity extends BaseActivity { return mFragmentTitleList.get(position); } } + + private class DrinkTask extends AsyncTask { + private static final String JSON_TAG_DRINK_INFO = "drinkInfo"; + private static final String JSON_TAG_DRINK_ID = "drink_id"; + private static final String JSON_TAG_DRINK_NAME = "drink_name"; + private static final String JSON_TAG_DRINK_DESCRIPTION = "drink_description"; + private static final String JSON_TAG_DRINK_AVG_RATING_OBJ = "averageRating"; + private static final String JSON_TAG_DRINK_AVG_RATING = "rating_grade__avg"; + + private static final String JSON_TAG_DRINK_HAS_ALCOHOL = "drinkHasAlcohol"; + private static final String JSON_TAG_DRINK_RESTAURANT_NAME = "restaurantName"; + + private static final String JSON_TAG_DRINK_INGREDIENTS = "ingredients"; + private static final String JSON_TAG_DRINK_INGREDIENT_NAME = "ingredient_name"; + private static final String JSON_TAG_DRINK_INGREDIENT_HAS_ALCOHOL = "ingredient_has_alcohol"; + + private static final String JSON_TAG_DRINK_RATINGS = "ratings"; + private static final String JSON_TAG_DRINK_RATING_GRADE = "rating_grade"; + private static final String JSON_TAG_DRINK_RATING_USERNAME = "username"; + private static final String JSON_TAG_DRINK_RATING_TEXT = "rating_text"; + 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() { + } + + @Override + protected Integer doInBackground(Void... params) { + String requestUrl = drinksUserViewUrl + mDrink.getId() + "/"; + + //Builds the request + Request request = new Request.Builder() + .url(requestUrl) + .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); + + JSONObject jsonFoodInfo = jsonResponse.getJSONObject(JSON_TAG_DRINK_INFO); + + double avgRestaurantRating = -1; + if (!jsonResponse.getJSONObject(JSON_TAG_DRINK_AVG_RATING_OBJ).isNull(JSON_TAG_DRINK_AVG_RATING)) { + avgRestaurantRating = jsonResponse.getJSONObject(JSON_TAG_DRINK_AVG_RATING_OBJ).getDouble(JSON_TAG_DRINK_AVG_RATING); + } + + drink = new Drink(jsonFoodInfo.getInt(JSON_TAG_DRINK_ID), + jsonFoodInfo.getString(JSON_TAG_DRINK_NAME), + jsonResponse.getString(JSON_TAG_DRINK_RESTAURANT_NAME), + jsonFoodInfo.getString(JSON_TAG_DRINK_DESCRIPTION), + jsonResponse.getBoolean(JSON_TAG_DRINK_HAS_ALCOHOL), + avgRestaurantRating); + + JSONArray jsonIngredients = jsonResponse.getJSONArray(JSON_TAG_DRINK_INGREDIENTS); + for (int ingredientIndex = 0; ingredientIndex < jsonIngredients.length(); ++ingredientIndex) { + JSONObject ingredient = jsonIngredients.getJSONObject(ingredientIndex); + ingredients.add(new Ingredient(ingredient.getString(JSON_TAG_DRINK_INGREDIENT_NAME), + ingredient.getBoolean(JSON_TAG_DRINK_INGREDIENT_HAS_ALCOHOL))); + } + + JSONArray jsonRatings = jsonResponse.getJSONArray(JSON_TAG_DRINK_RATINGS); + for (int ratingIndex = 0; ratingIndex < jsonRatings.length(); ++ratingIndex) { + JSONObject rating = jsonRatings.getJSONObject(ratingIndex); + ratings.add(new ItemRating(rating.getInt(JSON_TAG_DRINK_RATING_GRADE), + rating.getString(JSON_TAG_DRINK_RATING_USERNAME), + rating.getString(JSON_TAG_DRINK_RATING_TEXT), + rating.getString(JSON_TAG_DRINK_RATING_DATE), + ItemRating.PortionSize.getEnumTypeFromString( + rating.getString(JSON_TAG_DRINK_RATING_PORTION_SIZE)))); + } + + return 1; + } catch (Exception e) { + e.printStackTrace(); + return 2; + } + } + + @Override + protected void onPostExecute(Integer result) { + setupViewPager(viewPager); + tabLayout.setupWithViewPager(viewPager); + } + } } diff --git a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/drink/fragments/DrinkInfoFragment.java b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/drink/fragments/DrinkInfoFragment.java index 981eea4..f7ddbdb 100644 --- a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/drink/fragments/DrinkInfoFragment.java +++ b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/drink/fragments/DrinkInfoFragment.java @@ -9,6 +9,7 @@ import android.widget.TextView; import androidx.annotation.NonNull; import androidx.fragment.app.Fragment; import gr.auth.databases.flavours.R; +import gr.auth.databases.flavours.model.Drink; public class DrinkInfoFragment extends Fragment { @@ -16,14 +17,14 @@ public class DrinkInfoFragment extends Fragment { // Required empty public constructor } - private static final String DRINK_ID = "DRINK_ID"; + private static final String DRINK_INFO = "DRINK_INFO"; - private int drinkId; + private Drink drink; - public static DrinkInfoFragment newInstance(int drinkId) { + public static DrinkInfoFragment newInstance(Drink drink) { DrinkInfoFragment fragment = new DrinkInfoFragment(); Bundle args = new Bundle(); - args.putInt(DRINK_ID, drinkId); + args.putParcelable(DRINK_INFO, drink); fragment.setArguments(args); return fragment; } @@ -32,7 +33,7 @@ public class DrinkInfoFragment extends Fragment { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); assert getArguments() != null; - drinkId = getArguments().getInt(DRINK_ID); + drink = getArguments().getParcelable(DRINK_INFO); } @Override @@ -40,13 +41,22 @@ public class DrinkInfoFragment extends Fragment { final View rootView = inflater.inflate(R.layout.fragment_drink_info, container, false); TextView restaurantServing = rootView.findViewById(R.id.drink_serving_restaurant); - restaurantServing.setText(getString(R.string.drink_serving_restaurant_placeholder, "Restaurant's name")); + restaurantServing.setText(getString(R.string.drink_serving_restaurant_placeholder, drink.getRestaurantName())); TextView hasAlcohol = rootView.findViewById(R.id.drink_contains_alcohol); - hasAlcohol.setText(getString(R.string.drink_has_alcohol_placeholder, "YES")); + hasAlcohol.setText(getString(R.string.drink_has_alcohol_placeholder, drink.hasAlcohol() ? "YES" : "NO")); TextView rating = rootView.findViewById(R.id.drink_rating); - rating.setText(getString(R.string.drink_rating_placeholder, 4.65)); + if (drink.getRating() != -1) { + rating.setText(getString(R.string.drink_rating_placeholder, drink.getRating())); + } else { + rating.setVisibility(View.GONE); + } TextView description = rootView.findViewById(R.id.drink_description); - description.setText("a description"); + if (!drink.getDescription().equals("null")) { + description.setText(drink.getDescription()); + } else { + description.setVisibility(View.GONE); + } + return rootView; } } diff --git a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/drink/fragments/DrinkIngredientsFragment.java b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/drink/fragments/DrinkIngredientsFragment.java index d15c027..7b24bb2 100644 --- a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/drink/fragments/DrinkIngredientsFragment.java +++ b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/drink/fragments/DrinkIngredientsFragment.java @@ -23,14 +23,14 @@ public class DrinkIngredientsFragment extends Fragment { // Required empty public constructor } - private static final String DRINK_ID = "DRINK_ID"; + private static final String DRINK_INGREDIENTS = "DRINK_INGREDIENTS"; - private int drinkId; + private ArrayList ingredients; - public static DrinkIngredientsFragment newInstance(int drinkId) { + public static DrinkIngredientsFragment newInstance(ArrayList ingredients) { DrinkIngredientsFragment fragment = new DrinkIngredientsFragment(); Bundle args = new Bundle(); - args.putInt(DRINK_ID, drinkId); + args.putParcelableArrayList(DRINK_INGREDIENTS, ingredients); fragment.setArguments(args); return fragment; } @@ -39,17 +39,13 @@ public class DrinkIngredientsFragment extends Fragment { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); assert getArguments() != null; - drinkId = getArguments().getInt(DRINK_ID); + ingredients = getArguments().getParcelableArrayList(DRINK_INGREDIENTS); } @Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final View rootView = inflater.inflate(R.layout.reusable_recycler_list, container, false); - ArrayList ingredients = new ArrayList<>(); - ingredients.add(new Ingredient("Βότκα", true)); - ingredients.add(new Ingredient("Γάλα", false)); - Context context = getContext(); assert context != null; IngredientItemAdapter itemAdapter = new IngredientItemAdapter(context, ingredients); diff --git a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/drink/fragments/DrinkRatingsFragment.java b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/drink/fragments/DrinkRatingsFragment.java index 9c864fa..2faa31b 100644 --- a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/drink/fragments/DrinkRatingsFragment.java +++ b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/drink/fragments/DrinkRatingsFragment.java @@ -23,14 +23,14 @@ public class DrinkRatingsFragment extends Fragment { // Required empty public constructor } - private static final String DRINK_ID = "DRINK_ID"; + private static final String DRINK_RATINGS = "DRINK_RATINGS"; - private int drinkId; + private ArrayList ratings; - public static DrinkRatingsFragment newInstance(int drinkId) { + public static DrinkRatingsFragment newInstance(ArrayList ratings) { DrinkRatingsFragment fragment = new DrinkRatingsFragment(); Bundle args = new Bundle(); - args.putInt(DRINK_ID, drinkId); + args.putParcelableArrayList(DRINK_RATINGS, ratings); fragment.setArguments(args); return fragment; } @@ -39,22 +39,13 @@ public class DrinkRatingsFragment extends Fragment { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); assert getArguments() != null; - drinkId = getArguments().getInt(DRINK_ID); + ratings = getArguments().getParcelableArrayList(DRINK_RATINGS); } @Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final View rootView = inflater.inflate(R.layout.reusable_recycler_list, container, false); - ArrayList ratings = new ArrayList<>(); - ratings.add(new ItemRating(5, "Ανώνυμος", "Πάρα πολύ καλό!", "2018-01-04", ItemRating.PortionSize.MEDIUM)); - ratings.add(new ItemRating(5, "Ανύπαρκτος", "Εξαιρετικό service.\nΘα ξαναπάω!", "2018-06-08", ItemRating.PortionSize.BIG)); - ratings.add(new ItemRating(4, "Γαρδένιος ο Stoner", "-", "2018-06-08", ItemRating.PortionSize.BIG)); - ratings.add(new ItemRating(4, "Μαγκούστα", "Μου άρεσε.", "2018-06-08", ItemRating.PortionSize.MEDIUM)); - ratings.add(new ItemRating(5, "Νταλίκας", "Τέλειο.", "2018-06-08", ItemRating.PortionSize.BIG)); - ratings.add(new ItemRating(2, "Ms Godzila", "Το φαϊ άργησε πάρα πολύ!", "2018-06-08", null)); - ratings.add(new ItemRating(4, "eddie lives inside you", "-", "2018-06-08", ItemRating.PortionSize.MEDIUM)); - Context context = getContext(); assert context != null; ItemRatingsAdapter itemAdapter = new ItemRatingsAdapter(context, ratings); 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 2d261ba..7c19a52 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 @@ -2,6 +2,7 @@ package gr.auth.databases.flavours.activities.food; import android.app.Activity; import android.content.Intent; +import android.os.AsyncTask; import android.os.Bundle; import android.view.MenuItem; import android.view.View; @@ -10,6 +11,9 @@ import android.widget.Toast; import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.android.material.tabs.TabLayout; +import org.json.JSONArray; +import org.json.JSONObject; + import java.util.ArrayList; import java.util.List; @@ -27,25 +31,41 @@ import gr.auth.databases.flavours.activities.food.fragments.FoodIngredientsFragm import gr.auth.databases.flavours.activities.food.fragments.FoodRatingsFragment; import gr.auth.databases.flavours.activities.ingredients.IngredientsActivity; import gr.auth.databases.flavours.base.BaseActivity; +import gr.auth.databases.flavours.model.Food; import gr.auth.databases.flavours.model.Ingredient; +import gr.auth.databases.flavours.model.ItemRating; import gr.auth.databases.flavours.model.ItemSummary; +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.ingredients.IngredientsActivity.INGREDIENT_PICK_RESULT; +import static gr.auth.databases.flavours.session.SessionManager.foodsUserViewUrl; public class FoodActivity extends BaseActivity { + public static final String BUNDLE_ARG_FOOD = "BUNDLE_ARG_FOOD"; private static final int ADD_INGREDIENT_REQUEST = 420; + private ItemSummary mFood; + private Food food; + private ArrayList ingredients = new ArrayList<>(); + private ArrayList ratings = new ArrayList<>(); private ViewPager viewPager; private FloatingActionButton FAB; + private TabLayout tabLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_food); + Bundle extras = getIntent().getExtras(); + assert extras != null; + mFood = extras.getParcelable(BUNDLE_ARG_FOOD); + Toolbar toolbar = findViewById(R.id.food_toolbar); - toolbar.setTitle("Γεμιστά"); + toolbar.setTitle(mFood.getItemName()); setSupportActionBar(toolbar); ActionBar actionbar = getSupportActionBar(); if (actionbar != null) { @@ -89,9 +109,10 @@ public class FoodActivity extends BaseActivity { } }); - setupViewPager(viewPager); - TabLayout tabLayout = findViewById(R.id.food_tabs); - tabLayout.setupWithViewPager(viewPager); + tabLayout = findViewById(R.id.food_tabs); + + FoodTask foodTask = new FoodTask(); + foodTask.execute(); } @Override @@ -128,9 +149,9 @@ public class FoodActivity extends BaseActivity { private void setupViewPager(ViewPager viewPager) { RestaurantPagerAdapter adapter = new RestaurantPagerAdapter(getSupportFragmentManager()); - adapter.addFrag(FoodInfoFragment.newInstance(64), "INFO"); - adapter.addFrag(FoodIngredientsFragment.newInstance(64), "INGREDIENTS"); - adapter.addFrag(FoodRatingsFragment.newInstance(64), "RATINGS"); + adapter.addFrag(FoodInfoFragment.newInstance(food), "INFO"); + adapter.addFrag(FoodIngredientsFragment.newInstance(ingredients), "INGREDIENTS"); + adapter.addFrag(FoodRatingsFragment.newInstance(ratings), "RATINGS"); viewPager.setAdapter(adapter); } @@ -163,4 +184,95 @@ public class FoodActivity extends BaseActivity { return mFragmentTitleList.get(position); } } + + private class FoodTask extends AsyncTask { + private static final String JSON_TAG_FOOD_INFO = "foodInfo"; + private static final String JSON_TAG_FOOD_ID = "food_id"; + private static final String JSON_TAG_FOOD_NAME = "food_name"; + private static final String JSON_TAG_FOOD_DESCRIPTION = "food_description"; + private static final String JSON_TAG_FOOD_CALORIES = "food_calories"; + private static final String JSON_TAG_FOOD_AVG_RATING_OBJ = "averageRating"; + private static final String JSON_TAG_FOOD_AVG_RATING = "rating_grade__avg"; + + private static final String JSON_TAG_FOOD_RESTAURANT_NAME = "restaurantName"; + + private static final String JSON_TAG_FOOD_INGREDIENTS = "ingredients"; + private static final String JSON_TAG_FOOD_INGREDIENT_NAME = "ingredient_name"; + private static final String JSON_TAG_FOOD_INGREDIENT_HAS_ALCOHOL = "ingredient_has_alcohol"; + + private static final String JSON_TAG_FOOD_RATINGS = "ratings"; + private static final String JSON_TAG_FOOD_RATING_GRADE = "rating_grade"; + private static final String JSON_TAG_FOOD_RATING_USERNAME = "username"; + private static final String JSON_TAG_FOOD_RATING_TEXT = "rating_text"; + 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() { + } + + @Override + protected Integer doInBackground(Void... params) { + String requestUrl = foodsUserViewUrl + mFood.getId() + "/"; + + //Builds the request + Request request = new Request.Builder() + .url(requestUrl) + .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); + + JSONObject jsonFoodInfo = jsonResponse.getJSONObject(JSON_TAG_FOOD_INFO); + + double avgRestaurantRating = -1; + if (!jsonResponse.getJSONObject(JSON_TAG_FOOD_AVG_RATING_OBJ).isNull(JSON_TAG_FOOD_AVG_RATING)) { + avgRestaurantRating = jsonResponse.getJSONObject(JSON_TAG_FOOD_AVG_RATING_OBJ).getDouble(JSON_TAG_FOOD_AVG_RATING); + } + + food = new Food(jsonFoodInfo.getInt(JSON_TAG_FOOD_ID), + jsonFoodInfo.getString(JSON_TAG_FOOD_NAME), + jsonResponse.getString(JSON_TAG_FOOD_RESTAURANT_NAME), + jsonFoodInfo.getString(JSON_TAG_FOOD_DESCRIPTION), + jsonFoodInfo.getInt(JSON_TAG_FOOD_CALORIES), + avgRestaurantRating); + + JSONArray jsonIngredients = jsonResponse.getJSONArray(JSON_TAG_FOOD_INGREDIENTS); + for (int ingredientIndex = 0; ingredientIndex < jsonIngredients.length(); ++ingredientIndex) { + JSONObject ingredient = jsonIngredients.getJSONObject(ingredientIndex); + ingredients.add(new Ingredient(ingredient.getString(JSON_TAG_FOOD_INGREDIENT_NAME), + ingredient.getBoolean(JSON_TAG_FOOD_INGREDIENT_HAS_ALCOHOL))); + } + + JSONArray jsonRatings = jsonResponse.getJSONArray(JSON_TAG_FOOD_RATINGS); + for (int ratingIndex = 0; ratingIndex < jsonRatings.length(); ++ratingIndex) { + JSONObject rating = jsonRatings.getJSONObject(ratingIndex); + ratings.add(new ItemRating(rating.getInt(JSON_TAG_FOOD_RATING_GRADE), + rating.getString(JSON_TAG_FOOD_RATING_USERNAME), + rating.getString(JSON_TAG_FOOD_RATING_TEXT), + rating.getString(JSON_TAG_FOOD_RATING_DATE), + ItemRating.PortionSize.getEnumTypeFromString( + rating.getString(JSON_TAG_FOOD_RATING_PORTION_SIZE)))); + } + + return 1; + } catch (Exception e) { + e.printStackTrace(); + return 2; + } + } + + @Override + protected void onPostExecute(Integer result) { + setupViewPager(viewPager); + tabLayout.setupWithViewPager(viewPager); + } + } } diff --git a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/food/fragments/FoodInfoFragment.java b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/food/fragments/FoodInfoFragment.java index ab731c7..76c41db 100644 --- a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/food/fragments/FoodInfoFragment.java +++ b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/food/fragments/FoodInfoFragment.java @@ -9,6 +9,7 @@ import android.widget.TextView; import androidx.annotation.NonNull; import androidx.fragment.app.Fragment; import gr.auth.databases.flavours.R; +import gr.auth.databases.flavours.model.Food; public class FoodInfoFragment extends Fragment { @@ -16,14 +17,14 @@ public class FoodInfoFragment extends Fragment { // Required empty public constructor } - private static final String FOOD_ID = "FOOD_ID"; + private static final String FOOD_INFO = "FOOD_INFO"; - private int foodId; + private Food food; - public static FoodInfoFragment newInstance(int foodId) { + public static FoodInfoFragment newInstance(Food food) { FoodInfoFragment fragment = new FoodInfoFragment(); Bundle args = new Bundle(); - args.putInt(FOOD_ID, foodId); + args.putParcelable(FOOD_INFO, food); fragment.setArguments(args); return fragment; } @@ -32,7 +33,7 @@ public class FoodInfoFragment extends Fragment { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); assert getArguments() != null; - foodId = getArguments().getInt(FOOD_ID); + food = getArguments().getParcelable(FOOD_INFO); } @Override @@ -40,13 +41,22 @@ public class FoodInfoFragment extends Fragment { final View rootView = inflater.inflate(R.layout.fragment_food_info, container, false); TextView restaurantServing = rootView.findViewById(R.id.food_serving_restaurant); - restaurantServing.setText(getString(R.string.food_serving_restaurant_placeholder, "Restaurant's name")); + restaurantServing.setText(getString(R.string.food_serving_restaurant_placeholder, food.getRestaurantName())); TextView calories = rootView.findViewById(R.id.food_calories); - calories.setText(getString(R.string.food_calories_placeholder, 500)); + calories.setText(getString(R.string.food_calories_placeholder, food.getCalories())); TextView rating = rootView.findViewById(R.id.food_rating); - rating.setText(getString(R.string.food_rating_placeholder, 4.65)); + if (food.getRating() != -1) { + rating.setText(getString(R.string.food_rating_placeholder, food.getRating())); + } else { + rating.setVisibility(View.GONE); + } TextView description = rootView.findViewById(R.id.food_description); - description.setText("some awesome description"); + if (!food.getDescription().equals("null")) { + description.setText(food.getDescription()); + } else { + description.setVisibility(View.GONE); + } + return rootView; } } diff --git a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/food/fragments/FoodIngredientsFragment.java b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/food/fragments/FoodIngredientsFragment.java index 2b98032..f1a711c 100644 --- a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/food/fragments/FoodIngredientsFragment.java +++ b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/food/fragments/FoodIngredientsFragment.java @@ -23,14 +23,14 @@ public class FoodIngredientsFragment extends Fragment { // Required empty public constructor } - private static final String FOOD_ID = "FOOD_ID"; + private static final String FOOD_INGREDIENTS = "FOOD_INGREDIENTS"; - private int foodId; + private ArrayList ingredients; - public static FoodIngredientsFragment newInstance(int foodId) { + public static FoodIngredientsFragment newInstance(ArrayList ingredients) { FoodIngredientsFragment fragment = new FoodIngredientsFragment(); Bundle args = new Bundle(); - args.putInt(FOOD_ID, foodId); + args.putParcelableArrayList(FOOD_INGREDIENTS, ingredients); fragment.setArguments(args); return fragment; } @@ -39,17 +39,13 @@ public class FoodIngredientsFragment extends Fragment { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); assert getArguments() != null; - foodId = getArguments().getInt(FOOD_ID); + ingredients = getArguments().getParcelableArrayList(FOOD_INGREDIENTS); } @Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final View rootView = inflater.inflate(R.layout.reusable_recycler_list, container, false); - ArrayList ingredients = new ArrayList<>(); - ingredients.add(new Ingredient("Κιμάς", false)); - ingredients.add(new Ingredient("Ρύζι", false)); - Context context = getContext(); assert context != null; IngredientItemAdapter itemAdapter = new IngredientItemAdapter(context, ingredients); diff --git a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/food/fragments/FoodRatingsFragment.java b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/food/fragments/FoodRatingsFragment.java index 4280537..fe8c4e5 100644 --- a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/food/fragments/FoodRatingsFragment.java +++ b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/food/fragments/FoodRatingsFragment.java @@ -23,14 +23,14 @@ public class FoodRatingsFragment extends Fragment { // Required empty public constructor } - private static final String FOOD_ID = "FOOD_ID"; + private static final String FOOD_RATINGS = "FOOD_RATINGS"; - private int foodId; + private ArrayList ratings; - public static FoodRatingsFragment newInstance(int foodId) { + public static FoodRatingsFragment newInstance(ArrayList ratings) { FoodRatingsFragment fragment = new FoodRatingsFragment(); Bundle args = new Bundle(); - args.putInt(FOOD_ID, foodId); + args.putParcelableArrayList(FOOD_RATINGS, ratings); fragment.setArguments(args); return fragment; } @@ -39,22 +39,13 @@ public class FoodRatingsFragment extends Fragment { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); assert getArguments() != null; - foodId = getArguments().getInt(FOOD_ID); + ratings = getArguments().getParcelableArrayList(FOOD_RATINGS); } @Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final View rootView = inflater.inflate(R.layout.reusable_recycler_list, container, false); - ArrayList ratings = new ArrayList<>(); - ratings.add(new ItemRating(5, "Ανώνυμος", "Πάρα πολύ καλό!", "2018-01-04", ItemRating.PortionSize.MEDIUM)); - ratings.add(new ItemRating(5, "Ανύπαρκτος", "Εξαιρετικό service.\nΘα ξαναπάω!", "2018-06-08", ItemRating.PortionSize.BIG)); - ratings.add(new ItemRating(4, "Γαρδένιος ο Stoner", "-", "2018-06-08", ItemRating.PortionSize.BIG)); - ratings.add(new ItemRating(4, "Μαγκούστα", "Μου άρεσε.", "2018-06-08", ItemRating.PortionSize.MEDIUM)); - ratings.add(new ItemRating(5, "Νταλίκας", "Τέλειο.", "2018-06-08", ItemRating.PortionSize.BIG)); - ratings.add(new ItemRating(2, "Ms Godzila", "Το φαϊ άργησε πάρα πολύ!", "2018-06-08", null)); - ratings.add(new ItemRating(4, "eddie lives inside you", "-", "2018-06-08", ItemRating.PortionSize.MEDIUM)); - Context context = getContext(); assert context != null; ItemRatingsAdapter itemAdapter = new ItemRatingsAdapter(context, ratings); 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 9877e88..e39aad4 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 @@ -43,6 +43,8 @@ import okhttp3.ResponseBody; 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; +import static gr.auth.databases.flavours.activities.food.FoodActivity.BUNDLE_ARG_FOOD; import static gr.auth.databases.flavours.session.SessionManager.restaurantsUserViewUrl; public class RestaurantActivity extends BaseActivity @@ -150,12 +152,14 @@ public class RestaurantActivity extends BaseActivity @Override public void onRestaurantFoodsFragmentInteraction(ItemSummary item) { Intent intent = new Intent(this, FoodActivity.class); + intent.putExtra(BUNDLE_ARG_FOOD, item); startActivity(intent); } @Override public void onRestaurantDrinksFragmentInteraction(ItemSummary item) { Intent intent = new Intent(this, DrinkActivity.class); + intent.putExtra(BUNDLE_ARG_DRINK, item); startActivity(intent); } @@ -208,7 +212,6 @@ public class RestaurantActivity extends BaseActivity private static final String JSON_TAG_RESTAURANT_OPENING = "restaurant_opening"; private static final String JSON_TAG_RESTAURANT_CLOSING = "restaurant_closing"; private static final String JSON_TAG_RESTAURANT_AVG_RATING_OBJ = "averageRating"; - private static final String JSON_TAG_RESTAURANT_AVG_RATING = "rating_grade__avg"; private static final String JSON_TAG_RESTAURANT_RATINGS_PER_DIET = "avgRatingPerDiet"; diff --git a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/restaurant/fragments/RestaurantItemAdapter.java b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/restaurant/fragments/RestaurantItemAdapter.java index 425c319..de1bacd 100644 --- a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/restaurant/fragments/RestaurantItemAdapter.java +++ b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/restaurant/fragments/RestaurantItemAdapter.java @@ -60,7 +60,7 @@ public class RestaurantItemAdapter extends RecyclerView.Adapter CREATOR = new Parcelable.Creator() { + public Drink createFromParcel(Parcel in) { + return new Drink(in); + } + + public Drink[] newArray(int size) { + return new Drink[size]; + } + }; + + private Drink(Parcel in) { + id = in.readInt(); + name = in.readString(); + restaurantName = in.readString(); + description = in.readString(); + hasAlcohol = in.readByte() != 0; + rating = in.readDouble(); + } +} diff --git a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/model/Food.java b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/model/Food.java new file mode 100644 index 0000000..24392a3 --- /dev/null +++ b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/model/Food.java @@ -0,0 +1,77 @@ +package gr.auth.databases.flavours.model; + +import android.os.Parcel; +import android.os.Parcelable; + +public class Food implements Parcelable { + private int id, calories; + private String name, restaurantName, description; + private double rating; + + public Food(int id, String name, String restaurantName, String description, int calories, double rating) { + this.id = id; + this.name = name; + this.restaurantName = restaurantName; + this.description = description; + this.calories = calories; + this.rating = rating; + } + + public int getId() { + return id; + } + + public String getName() { + return name; + } + + public String getRestaurantName() { + return restaurantName; + } + + public String getDescription() { + return description; + } + + public int getCalories() { + return calories; + } + + public double getRating() { + return rating; + } + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel out, int flags) { + out.writeInt(id); + out.writeString(name); + out.writeString(restaurantName); + out.writeString(description); + out.writeInt(calories); + out.writeDouble(rating); + } + + public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { + public Food createFromParcel(Parcel in) { + return new Food(in); + } + + public Food[] newArray(int size) { + return new Food[size]; + } + }; + + private Food(Parcel in) { + id = in.readInt(); + name = in.readString(); + restaurantName = in.readString(); + description = in.readString(); + calories = in.readInt(); + rating = in.readDouble(); + } +} diff --git a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/model/ItemRating.java b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/model/ItemRating.java index 3de5c3d..d7edda4 100644 --- a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/model/ItemRating.java +++ b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/model/ItemRating.java @@ -42,6 +42,19 @@ public class ItemRating extends Rating { return UNSUPPORTED; } } + + public static PortionSize getEnumTypeFromString(String possible) { + switch (possible.toLowerCase()) { + case "small": + return SMALL; + case "medium": + return MEDIUM; + case "big": + return BIG; + default: + return UNSUPPORTED; + } + } } private PortionSize portionSize; diff --git a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/model/ItemSummary.java b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/model/ItemSummary.java index 4e3c925..78217a4 100644 --- a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/model/ItemSummary.java +++ b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/model/ItemSummary.java @@ -53,7 +53,7 @@ public class ItemSummary implements Parcelable { this.type = type; } - public String getItem() { + public String getItemName() { return itemName; } diff --git a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/model/Rating.java b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/model/Rating.java index 49f917a..3b02b5c 100644 --- a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/model/Rating.java +++ b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/model/Rating.java @@ -37,10 +37,10 @@ public class Rating implements Parcelable { @Override public void writeToParcel(Parcel out, int flags) { - out.writeInt(getGrade()); - out.writeString(getUsername()); - out.writeString(getText()); - out.writeString(getDate()); + out.writeInt(grade); + out.writeString(username); + out.writeString(text); + out.writeString(date); } public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { 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 f5a912a..ce9bfd9 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 @@ -57,6 +57,8 @@ public class SessionManager { public static final String restaurantsUserViewUrl = baseServerUrl + "api/restaurantUserView/"; public static final String getUserDietsUrl = baseServerUrl + "api/userDiets/"; public static final String rateRestaurantUrl = baseServerUrl + "api/userratesrestaurant/"; + public static final String foodsUserViewUrl = baseServerUrl + "api/foodUserView/"; + public static final String drinksUserViewUrl = baseServerUrl + "api/drinkUserView/"; // Client & Cookies private final OkHttpClient client; diff --git a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/utils/ItemRatingsAdapter.java b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/utils/ItemRatingsAdapter.java index d8d308a..98853b4 100644 --- a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/utils/ItemRatingsAdapter.java +++ b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/utils/ItemRatingsAdapter.java @@ -47,7 +47,12 @@ public class ItemRatingsAdapter extends RecyclerView.Adapter