Browse Source

API connection for restaurant rating, Fixes

master
Apostolos Fanakis 6 years ago
parent
commit
0e8daf3c14
  1. 154
      UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/RateRestaurantActivity.java
  2. 43
      UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/restaurant/RestaurantActivity.java
  3. 12
      UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/restaurant/fragments/RestaurantInfoFragment.java
  4. 48
      UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/model/DietRatingPair.java
  5. 19
      UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/model/DietSummary.java
  6. 10
      UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/model/RestaurantUserView.java
  7. 2
      UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/session/SessionManager.java

154
UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/RateRestaurantActivity.java

@ -1,13 +1,17 @@
package gr.auth.databases.flavours.activities;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
@ -17,14 +21,29 @@ import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.Toolbar;
import gr.auth.databases.flavours.R;
import gr.auth.databases.flavours.base.BaseActivity;
import gr.auth.databases.flavours.model.DietSummary;
import gr.auth.databases.flavours.model.Restaurant;
import gr.auth.databases.flavours.model.RestaurantRating;
import okhttp3.FormBody;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import static gr.auth.databases.flavours.session.SessionManager.getUserDietsUrl;
import static gr.auth.databases.flavours.session.SessionManager.rateRestaurantUrl;
public class RateRestaurantActivity extends BaseActivity implements AdapterView.OnItemSelectedListener {
public static final String BUNDLE_RATE_RESTAURANT_NAME = "BUNDLE_RATE_RESTAURANT_NAME";
public static final String BUNDLE_RATE_RESTAURANT = "BUNDLE_RATE_RESTAURANT";
List<String> spinnerArray = new ArrayList<>();
List<DietSummary> userDiets = new ArrayList<>();
private Restaurant restaurant;
private EditText ratingText;
private int ratingGrade;
private int ratingGrade = -1;
private RestaurantRating.Accessibility ratingAccessibility;
private int ratingDiet = -1;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -32,9 +51,8 @@ public class RateRestaurantActivity extends BaseActivity implements AdapterView.
setContentView(R.layout.activity_rate_restaurant);
Bundle extras = getIntent().getExtras();
if (extras != null) {
//itemType = (ItemSummary.ItemType) extras.getSerializable(BUNDLE_ADD_ITEM_ITEM_TYPE);
}
assert extras != null;
restaurant = extras.getParcelable(BUNDLE_RATE_RESTAURANT);
Toolbar toolbar = findViewById(R.id.rate_restaurant_toolbar);
setSupportActionBar(toolbar);
@ -58,30 +76,22 @@ public class RateRestaurantActivity extends BaseActivity implements AdapterView.
accessibilitySpinner.setAdapter(accessibilityAdapter);
accessibilitySpinner.setOnItemSelectedListener(this);
List<String> spinnerArray = new ArrayList<String>();
spinnerArray.add("Όγκος");
spinnerArray.add("Πολύ όγκος");
Spinner dietSpinner = findViewById(R.id.rate_restaurant_diet);
ArrayAdapter<String> dietAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_spinner_item, spinnerArray);
dietAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dietSpinner.setAdapter(dietAdapter);
dietSpinner.setOnItemSelectedListener(this);
dietSpinner.setVisibility(View.VISIBLE);
ratingText = findViewById(R.id.rate_restaurant_text);
AppCompatButton rateButton = findViewById(R.id.rate_restaurant_rate_btn);
rateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//TODO
RateRestaurantTask rateRestaurantTask = new RateRestaurantTask();
rateRestaurantTask.execute();
}
});
createDrawer();
drawer.setSelection(-1);
GetUserDietsTask getUserDietsTask = new GetUserDietsTask();
getUserDietsTask.execute();
}
@Override
@ -111,10 +121,116 @@ public class RateRestaurantActivity extends BaseActivity implements AdapterView.
} else if (parent.getId() == R.id.rate_restaurant_accessibility) {
ratingAccessibility = RestaurantRating.Accessibility.getEnumTypeFromId(pos);
} else if (parent.getId() == R.id.rate_restaurant_diet) {
if (pos == 0) {
ratingDiet = -1;
} else {
ratingDiet = userDiets.get(pos - 1).getId();
}
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
private class GetUserDietsTask extends AsyncTask<Void, Void, Integer> {
private static final String JSON_TAG_DIET_ID = "diet_id";
private static final String JSON_TAG_DIET_NAME = "diet_name";
@Override
protected void onPreExecute() {
}
@Override
protected Integer doInBackground(Void... params) {
//Builds the request
Request request = new Request.Builder()
.url(getUserDietsUrl)
.build();
try {
//Makes request & handles response
Response response = client.newCall(request).execute();
ResponseBody responseBody = response.body();
assert responseBody != null;
String result = responseBody.string();
JSONArray jsonResponse = new JSONArray(result);
spinnerArray.add("None");
for (int dietIndex = 0; dietIndex < jsonResponse.length(); ++dietIndex) {
JSONObject diet = (JSONObject) jsonResponse.get(dietIndex);
spinnerArray.add(diet.getString(JSON_TAG_DIET_NAME));
userDiets.add(new DietSummary(diet.getInt(JSON_TAG_DIET_ID),
diet.getString(JSON_TAG_DIET_NAME)));
}
return 1;
} catch (Exception e) {
e.printStackTrace();
return 2;
}
}
@Override
protected void onPostExecute(Integer result) { //Handle attempt result
Spinner dietSpinner = findViewById(R.id.rate_restaurant_diet);
ArrayAdapter<String> dietAdapter = new ArrayAdapter<>(RateRestaurantActivity.this,
android.R.layout.simple_spinner_item, spinnerArray);
dietAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dietSpinner.setAdapter(dietAdapter);
dietSpinner.setOnItemSelectedListener(RateRestaurantActivity.this);
dietSpinner.setVisibility(View.VISIBLE);
}
}
private class RateRestaurantTask extends AsyncTask<Void, Void, Integer> {
private static final String REQ_RATE_RESTAURANT_ID = "restaurant";
private static final String REQ_RATE_RESTAURANT_GRADE = "rating_grade";
private static final String REQ_RATE_RESTAURANT_ACCESSIBILITY = "rating_accessibility";
private static final String REQ_RATE_RESTAURANT_DIET = "diet";
private static final String REQ_RATE_RESTAURANT_TEXT = "rating_text";
@Override
protected void onPreExecute() {
}
@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)
.add(REQ_RATE_RESTAURANT_TEXT, ratingText.getText().toString())
.add("user", "1");
if (ratingAccessibility != null) {
builder.add(REQ_RATE_RESTAURANT_ACCESSIBILITY,
"" + RestaurantRating.Accessibility.getWorkingTypeFromId(ratingAccessibility.getId()));
}
if (ratingDiet != -1) {
builder.add(REQ_RATE_RESTAURANT_DIET, "" + ratingDiet);
}
RequestBody formBody = builder.build();
Request request = new Request.Builder()
.url(rateRestaurantUrl)
.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) { //Handle attempt result
Toast.makeText(RateRestaurantActivity.this, "Rating added!", Toast.LENGTH_SHORT).show();
RateRestaurantActivity.this.finish();
}
}
}

43
UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/restaurant/RestaurantActivity.java

@ -3,8 +3,6 @@ package gr.auth.databases.flavours.activities.restaurant;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.util.Pair;
import android.view.MenuItem;
import android.view.View;
@ -34,6 +32,7 @@ import gr.auth.databases.flavours.activities.restaurant.fragments.RestaurantFood
import gr.auth.databases.flavours.activities.restaurant.fragments.RestaurantInfoFragment;
import gr.auth.databases.flavours.activities.restaurant.fragments.RestaurantRatingsFragment;
import gr.auth.databases.flavours.base.BaseActivity;
import gr.auth.databases.flavours.model.DietRatingPair;
import gr.auth.databases.flavours.model.ItemSummary;
import gr.auth.databases.flavours.model.Restaurant;
import gr.auth.databases.flavours.model.RestaurantRating;
@ -43,6 +42,7 @@ import okhttp3.Response;
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.session.SessionManager.restaurantsUserViewUrl;
public class RestaurantActivity extends BaseActivity
@ -50,11 +50,11 @@ public class RestaurantActivity extends BaseActivity
RestaurantDrinksFragment.RestaurantDrinksFragmentInteractionListener {
public static final String BUNDLE_ARG_RESTAURANT = "BUNDLE_ARG_RESTAURANT";
private static Restaurant mRestaurant;
private static RestaurantUserView restaurant;
private Restaurant mRestaurant;
private RestaurantUserView restaurant;
private ArrayList<ItemSummary> foods = new ArrayList<>();
private ArrayList<ItemSummary> drinks = new ArrayList<>();
ArrayList<RestaurantRating> ratings = new ArrayList<>();
private ArrayList<RestaurantRating> ratings = new ArrayList<>();
private ViewPager viewPager;
private TabLayout tabLayout;
@ -68,7 +68,7 @@ public class RestaurantActivity extends BaseActivity
Bundle extras = getIntent().getExtras();
assert extras != null;
mRestaurant = (Restaurant) extras.getParcelable(BUNDLE_ARG_RESTAURANT);
mRestaurant = extras.getParcelable(BUNDLE_ARG_RESTAURANT);
Toolbar toolbar = findViewById(R.id.restaurant_toolbar);
setSupportActionBar(toolbar);
@ -97,6 +97,7 @@ public class RestaurantActivity extends BaseActivity
startActivity(intent);
} else if (viewPager.getCurrentItem() == 3) {
Intent intent = new Intent(view.getContext(), RateRestaurantActivity.class);
intent.putExtra(BUNDLE_RATE_RESTAURANT, mRestaurant);
startActivity(intent);
}
}
@ -253,18 +254,23 @@ public class RestaurantActivity extends BaseActivity
ResponseBody responseBody = response.body();
assert responseBody != null;
String result = responseBody.string();
JSONObject jsonResponce = new JSONObject(result);
JSONObject jsonResponse = new JSONObject(result);
JSONObject jsonRestaurantInfo = jsonResponce.getJSONObject(JSON_TAG_RESTAURANT_INFO);
JSONArray jsonRatingsPerDiet = jsonResponce.getJSONArray(JSON_TAG_RESTAURANT_RATINGS_PER_DIET);
ArrayList<Pair<String, Double>> ratingsPerDiet = new ArrayList<>();
JSONObject jsonRestaurantInfo = jsonResponse.getJSONObject(JSON_TAG_RESTAURANT_INFO);
JSONArray jsonRatingsPerDiet = jsonResponse.getJSONArray(JSON_TAG_RESTAURANT_RATINGS_PER_DIET);
ArrayList<DietRatingPair> ratingsPerDiet = new ArrayList<>();
for (int dietIndex = 0; dietIndex < jsonRatingsPerDiet.length(); ++dietIndex) {
JSONObject dietAvgRating = (JSONObject) jsonRatingsPerDiet.get(dietIndex);
ratingsPerDiet.add(new Pair<>(dietAvgRating.getString(JSON_TAG_RESTAURANT_RATINGS_PER_DIET_NAME),
ratingsPerDiet.add(new DietRatingPair(dietAvgRating.getString(JSON_TAG_RESTAURANT_RATINGS_PER_DIET_NAME),
dietAvgRating.getDouble(JSON_TAG_RESTAURANT_RATINGS_PER_DIET_RATING)));
}
double avgRestaurantRating = -1;
if (!jsonResponse.getJSONObject(JSON_TAG_RESTAURANT_AVG_RATING_OBJ).isNull(JSON_TAG_RESTAURANT_AVG_RATING)) {
avgRestaurantRating = jsonResponse.getJSONObject(JSON_TAG_RESTAURANT_AVG_RATING_OBJ).getDouble(JSON_TAG_RESTAURANT_AVG_RATING);
}
restaurant = new RestaurantUserView(jsonRestaurantInfo.getInt(JSON_TAG_RESTAURANT_ID),
jsonRestaurantInfo.getString(JSON_TAG_RESTAURANT_NAME),
jsonRestaurantInfo.getString(JSON_TAG_RESTAURANT_CATEGORY),
@ -272,31 +278,26 @@ public class RestaurantActivity extends BaseActivity
jsonRestaurantInfo.getDouble(JSON_TAG_RESTAURANT_LATITUDE),
jsonRestaurantInfo.getString(JSON_TAG_RESTAURANT_OPENING),
jsonRestaurantInfo.getString(JSON_TAG_RESTAURANT_CLOSING),
jsonResponce.getJSONObject(JSON_TAG_RESTAURANT_AVG_RATING_OBJ).getDouble(JSON_TAG_RESTAURANT_AVG_RATING),
avgRestaurantRating,
ratingsPerDiet);
JSONArray jsonFoods = jsonResponce.getJSONArray(JSON_TAG_RESTAURANT_FOODS);
Log.d("THISSS", "jsonFoods size = " + jsonFoods.length());
JSONArray jsonFoods = jsonResponse.getJSONArray(JSON_TAG_RESTAURANT_FOODS);
for (int foodIndex = 0; foodIndex < jsonFoods.length(); ++foodIndex) {
JSONObject food = jsonFoods.getJSONObject(foodIndex);
foods.add(new ItemSummary(food.getString(JSON_TAG_RESTAURANT_FOOD_NAME),
food.getInt(JSON_TAG_RESTAURANT_FOOD_ID),
ItemSummary.ItemType.FOOD));
}
Log.d("THISSS", "foods size = " + foods.size());
JSONArray jsonDrinks = jsonResponce.getJSONArray(JSON_TAG_RESTAURANT_DRINKS);
Log.d("THISSS", "jsonDrinks size = " + jsonDrinks.length());
JSONArray jsonDrinks = jsonResponse.getJSONArray(JSON_TAG_RESTAURANT_DRINKS);
for (int drinkIndex = 0; drinkIndex < jsonDrinks.length(); ++drinkIndex) {
JSONObject drink = jsonDrinks.getJSONObject(drinkIndex);
drinks.add(new ItemSummary(drink.getString(JSON_TAG_RESTAURANT_DRINK_NAME),
drink.getInt(JSON_TAG_RESTAURANT_DRINK_ID),
ItemSummary.ItemType.DRINK));
}
Log.d("THISSS", "drinks size = " + drinks.size());
JSONArray jsonRatings = jsonResponce.getJSONArray(JSON_TAG_RESTAURANT_RATINGS);
JSONArray jsonRatings = jsonResponse.getJSONArray(JSON_TAG_RESTAURANT_RATINGS);
for (int ratingIndex = 0; ratingIndex < jsonRatings.length(); ++ratingIndex) {
JSONObject rating = jsonRatings.getJSONObject(ratingIndex);
ratings.add(new RestaurantRating(rating.getInt(JSON_TAG_RESTAURANT_RATING_GRADE),
@ -315,7 +316,7 @@ public class RestaurantActivity extends BaseActivity
}
@Override
protected void onPostExecute(Integer result) { //Handle attempt result
protected void onPostExecute(Integer result) {
setupViewPager(viewPager);
tabLayout.setupWithViewPager(viewPager);
}

12
UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/restaurant/fragments/RestaurantInfoFragment.java

@ -1,7 +1,6 @@
package gr.auth.databases.flavours.activities.restaurant.fragments;
import android.os.Bundle;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -19,6 +18,7 @@ import com.google.android.gms.maps.model.MarkerOptions;
import androidx.annotation.NonNull;
import gr.auth.databases.flavours.R;
import gr.auth.databases.flavours.base.BaseFragment;
import gr.auth.databases.flavours.model.DietRatingPair;
import gr.auth.databases.flavours.model.RestaurantUserView;
public class RestaurantInfoFragment extends BaseFragment implements OnMapReadyCallback {
@ -61,16 +61,20 @@ public class RestaurantInfoFragment extends BaseFragment implements OnMapReadyCa
TextView type = rootView.findViewById(R.id.restaurant_type);
type.setText(getString(R.string.restaurant_type, restaurant.getType()));
TextView averageRatings = rootView.findViewById(R.id.restaurant_average_rating);
averageRatings.setText(getString(R.string.restaurant_average_rating, restaurant.getAverageRating()));
if (restaurant.getAverageRating() != -1) {
averageRatings.setText(getString(R.string.restaurant_average_rating, restaurant.getAverageRating()));
} else {
averageRatings.setVisibility(View.GONE);
}
if (!restaurant.getAverageRatingPerDiet().isEmpty()) {
LinearLayout averageRatingPerDiet = rootView.findViewById(R.id.restaurant_average_rating_per_diet);
averageRatingPerDiet.setVisibility(View.VISIBLE);
for (Pair<String, Double> rating : restaurant.getAverageRatingPerDiet()) {
for (DietRatingPair rating : restaurant.getAverageRatingPerDiet()) {
TextView dietRatingView = new TextView(getContext());
dietRatingView.setText(getString(R.string.restaurant_diet_average_rating,
rating.first, rating.second));
rating.getDietName(), rating.getRating()));
averageRatingPerDiet.addView(dietRatingView);
}
}

48
UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/model/DietRatingPair.java

@ -0,0 +1,48 @@
package gr.auth.databases.flavours.model;
import android.os.Parcel;
import android.os.Parcelable;
public class DietRatingPair implements Parcelable {
private String dietName;
private double rating;
public DietRatingPair(String dietName, double rating) {
this.dietName = dietName;
this.rating = rating;
}
public String getDietName() {
return dietName;
}
public double getRating() {
return rating;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel out, int flags) {
out.writeString(dietName);
out.writeDouble(rating);
}
public static final Parcelable.Creator<DietRatingPair> CREATOR = new Parcelable.Creator<DietRatingPair>() {
public DietRatingPair createFromParcel(Parcel in) {
return new DietRatingPair(in);
}
public DietRatingPair[] newArray(int size) {
return new DietRatingPair[size];
}
};
private DietRatingPair(Parcel in) {
dietName = in.readString();
rating = in.readDouble();
}
}

19
UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/model/DietSummary.java

@ -0,0 +1,19 @@
package gr.auth.databases.flavours.model;
public class DietSummary {
private int id;
private String name;
public DietSummary(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
}

10
UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/model/RestaurantUserView.java

@ -1,17 +1,16 @@
package gr.auth.databases.flavours.model;
import android.os.Parcel;
import android.util.Pair;
import java.util.ArrayList;
public class RestaurantUserView extends Restaurant {
private double averageRating;
private ArrayList<Pair<String, Double>> averageRatingPerDiet;
private ArrayList<DietRatingPair> averageRatingPerDiet;
public RestaurantUserView(int id, String name, String type, double longitude, double latitude,
String openingTime, String closingTime, double averageRating,
ArrayList<Pair<String, Double>> averageRatingPerDiet) {
ArrayList<DietRatingPair> averageRatingPerDiet) {
super(id, name, type, longitude, latitude, openingTime, closingTime);
this.averageRating = averageRating;
this.averageRatingPerDiet = averageRatingPerDiet;
@ -21,14 +20,14 @@ public class RestaurantUserView extends Restaurant {
return averageRating;
}
public ArrayList<Pair<String, Double>> getAverageRatingPerDiet() {
public ArrayList<DietRatingPair> getAverageRatingPerDiet() {
return averageRatingPerDiet;
}
private RestaurantUserView(Parcel in) {
super(in);
averageRating = in.readDouble();
averageRatingPerDiet = (ArrayList<Pair<String, Double>>) in.readArrayList(Pair.class.getClassLoader());
averageRatingPerDiet = (ArrayList<DietRatingPair>) in.readArrayList(DietRatingPair.class.getClassLoader());
}
public static final Creator<RestaurantUserView> CREATOR = new Creator<RestaurantUserView>() {
@ -47,6 +46,7 @@ public class RestaurantUserView extends Restaurant {
public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
dest.writeDouble(averageRating);
//dest.writeList(averageRatingPerDiet);
dest.writeList(averageRatingPerDiet);
}

2
UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/session/SessionManager.java

@ -55,6 +55,8 @@ public class SessionManager {
public static final String setUserBirthday = baseServerUrl + "api/setUserBirthday/";
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 rateRestaurantUrl = baseServerUrl + "api/userratesrestaurant/";
// Client & Cookies
private final OkHttpClient client;

Loading…
Cancel
Save