|
|
@ -1,13 +1,19 @@ |
|
|
|
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; |
|
|
|
|
|
|
|
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; |
|
|
|
|
|
|
@ -29,23 +35,44 @@ import gr.auth.databases.flavours.activities.restaurant.fragments.RestaurantInfo |
|
|
|
import gr.auth.databases.flavours.activities.restaurant.fragments.RestaurantRatingsFragment; |
|
|
|
import gr.auth.databases.flavours.base.BaseActivity; |
|
|
|
import gr.auth.databases.flavours.model.ItemSummary; |
|
|
|
import gr.auth.databases.flavours.model.Restaurant; |
|
|
|
import gr.auth.databases.flavours.model.RestaurantRating; |
|
|
|
import gr.auth.databases.flavours.model.RestaurantUserView; |
|
|
|
import okhttp3.Request; |
|
|
|
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.session.SessionManager.restaurantsUserViewUrl; |
|
|
|
|
|
|
|
public class RestaurantActivity extends BaseActivity |
|
|
|
implements RestaurantFoodsFragment.RestaurantFoodsFragmentInteractionListener, |
|
|
|
RestaurantDrinksFragment.RestaurantDrinksFragmentInteractionListener { |
|
|
|
public static final String BUNDLE_ARG_RESTAURANT = "BUNDLE_ARG_RESTAURANT"; |
|
|
|
|
|
|
|
private static Restaurant mRestaurant; |
|
|
|
private static RestaurantUserView restaurant; |
|
|
|
private ArrayList<ItemSummary> foods = new ArrayList<>(); |
|
|
|
private ArrayList<ItemSummary> drinks = new ArrayList<>(); |
|
|
|
ArrayList<RestaurantRating> ratings = new ArrayList<>(); |
|
|
|
|
|
|
|
private ViewPager viewPager; |
|
|
|
private TabLayout tabLayout; |
|
|
|
private FloatingActionButton FAB; |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
protected void onCreate(Bundle savedInstanceState) { |
|
|
|
super.onCreate(savedInstanceState); |
|
|
|
setContentView(R.layout.activity_restaurant); |
|
|
|
|
|
|
|
Bundle extras = getIntent().getExtras(); |
|
|
|
assert extras != null; |
|
|
|
mRestaurant = (Restaurant) extras.getParcelable(BUNDLE_ARG_RESTAURANT); |
|
|
|
|
|
|
|
Toolbar toolbar = findViewById(R.id.restaurant_toolbar); |
|
|
|
toolbar.setTitle("Αιθερόπλοο"); |
|
|
|
setSupportActionBar(toolbar); |
|
|
|
toolbar.setTitle(mRestaurant.getName()); |
|
|
|
ActionBar actionbar = getSupportActionBar(); |
|
|
|
if (actionbar != null) { |
|
|
|
actionbar.setDisplayHomeAsUpEnabled(true); |
|
|
@ -92,9 +119,10 @@ public class RestaurantActivity extends BaseActivity |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
setupViewPager(viewPager); |
|
|
|
TabLayout tabLayout = findViewById(R.id.restaurant_tabs); |
|
|
|
tabLayout.setupWithViewPager(viewPager); |
|
|
|
tabLayout = findViewById(R.id.restaurant_tabs); |
|
|
|
|
|
|
|
RestaurantTask restaurantTask = new RestaurantTask(); |
|
|
|
restaurantTask.execute(); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
@ -132,10 +160,10 @@ public class RestaurantActivity extends BaseActivity |
|
|
|
|
|
|
|
private void setupViewPager(ViewPager viewPager) { |
|
|
|
RestaurantPagerAdapter adapter = new RestaurantPagerAdapter(getSupportFragmentManager()); |
|
|
|
adapter.addFrag(RestaurantInfoFragment.newInstance(64), "INFO"); |
|
|
|
adapter.addFrag(RestaurantFoodsFragment.newInstance(64), "FOOD"); |
|
|
|
adapter.addFrag(RestaurantDrinksFragment.newInstance(64), "DRINKS"); |
|
|
|
adapter.addFrag(RestaurantRatingsFragment.newInstance(64), "RATINGS"); |
|
|
|
adapter.addFrag(RestaurantInfoFragment.newInstance(restaurant), "INFO"); |
|
|
|
adapter.addFrag(RestaurantFoodsFragment.newInstance(foods), "FOOD"); |
|
|
|
adapter.addFrag(RestaurantDrinksFragment.newInstance(drinks), "DRINKS"); |
|
|
|
adapter.addFrag(RestaurantRatingsFragment.newInstance(ratings), "RATINGS"); |
|
|
|
viewPager.setAdapter(adapter); |
|
|
|
} |
|
|
|
|
|
|
@ -168,4 +196,128 @@ public class RestaurantActivity extends BaseActivity |
|
|
|
return mFragmentTitleList.get(position); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private class RestaurantTask extends AsyncTask<Void, Void, Integer> { |
|
|
|
private static final String JSON_TAG_RESTAURANT_INFO = "restaurantInfo"; |
|
|
|
private static final String JSON_TAG_RESTAURANT_ID = "restaurant_id"; |
|
|
|
private static final String JSON_TAG_RESTAURANT_NAME = "restaurant_name"; |
|
|
|
private static final String JSON_TAG_RESTAURANT_CATEGORY = "restaurant_category"; |
|
|
|
private static final String JSON_TAG_RESTAURANT_LONGITUDE = "restaurant_longitude"; |
|
|
|
private static final String JSON_TAG_RESTAURANT_LATITUDE = "restaurant_latitude"; |
|
|
|
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"; |
|
|
|
private static final String JSON_TAG_RESTAURANT_RATINGS_PER_DIET_NAME = "diet_name"; |
|
|
|
private static final String JSON_TAG_RESTAURANT_RATINGS_PER_DIET_RATING = "average_rating"; |
|
|
|
|
|
|
|
private static final String JSON_TAG_RESTAURANT_FOODS = "foods"; |
|
|
|
private static final String JSON_TAG_RESTAURANT_FOOD_ID = "food_id"; |
|
|
|
private static final String JSON_TAG_RESTAURANT_FOOD_NAME = "food_name"; |
|
|
|
|
|
|
|
private static final String JSON_TAG_RESTAURANT_DRINKS = "drinks"; |
|
|
|
private static final String JSON_TAG_RESTAURANT_DRINK_ID = "drink_id"; |
|
|
|
private static final String JSON_TAG_RESTAURANT_DRINK_NAME = "drink_name"; |
|
|
|
|
|
|
|
private static final String JSON_TAG_RESTAURANT_RATINGS = "ratings"; |
|
|
|
private static final String JSON_TAG_RESTAURANT_RATING_GRADE = "rating_grade"; |
|
|
|
private static final String JSON_TAG_RESTAURANT_RATING_USERNAME = "username"; |
|
|
|
private static final String JSON_TAG_RESTAURANT_RATING_TEXT = "rating_text"; |
|
|
|
private static final String JSON_TAG_RESTAURANT_RATING_DATE = "rating_date"; |
|
|
|
private static final String JSON_TAG_RESTAURANT_RATING_ACCESSIBILITY = "rating_accessibility"; |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
protected void onPreExecute() { |
|
|
|
foods.clear(); |
|
|
|
drinks.clear(); |
|
|
|
ratings.clear(); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
protected Integer doInBackground(Void... params) { |
|
|
|
String requestUrl = restaurantsUserViewUrl + mRestaurant.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 jsonResponce = 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<>(); |
|
|
|
|
|
|
|
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), |
|
|
|
dietAvgRating.getDouble(JSON_TAG_RESTAURANT_RATINGS_PER_DIET_RATING))); |
|
|
|
} |
|
|
|
|
|
|
|
restaurant = new RestaurantUserView(jsonRestaurantInfo.getInt(JSON_TAG_RESTAURANT_ID), |
|
|
|
jsonRestaurantInfo.getString(JSON_TAG_RESTAURANT_NAME), |
|
|
|
jsonRestaurantInfo.getString(JSON_TAG_RESTAURANT_CATEGORY), |
|
|
|
jsonRestaurantInfo.getDouble(JSON_TAG_RESTAURANT_LONGITUDE), |
|
|
|
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), |
|
|
|
ratingsPerDiet); |
|
|
|
|
|
|
|
JSONArray jsonFoods = jsonResponce.getJSONArray(JSON_TAG_RESTAURANT_FOODS); |
|
|
|
Log.d("THISSS", "jsonFoods size = " + jsonFoods.length()); |
|
|
|
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()); |
|
|
|
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); |
|
|
|
for (int ratingIndex = 0; ratingIndex < jsonRatings.length(); ++ratingIndex) { |
|
|
|
JSONObject rating = jsonRatings.getJSONObject(ratingIndex); |
|
|
|
ratings.add(new RestaurantRating(rating.getInt(JSON_TAG_RESTAURANT_RATING_GRADE), |
|
|
|
rating.getString(JSON_TAG_RESTAURANT_RATING_USERNAME), |
|
|
|
rating.getString(JSON_TAG_RESTAURANT_RATING_TEXT), |
|
|
|
rating.getString(JSON_TAG_RESTAURANT_RATING_DATE), |
|
|
|
RestaurantRating.Accessibility.getEnumTypeFromString( |
|
|
|
rating.getString(JSON_TAG_RESTAURANT_RATING_ACCESSIBILITY)))); |
|
|
|
} |
|
|
|
|
|
|
|
return 1; |
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
return 2; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
protected void onPostExecute(Integer result) { //Handle attempt result
|
|
|
|
setupViewPager(viewPager); |
|
|
|
tabLayout.setupWithViewPager(viewPager); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|