|
|
@ -1,10 +1,15 @@ |
|
|
|
package gr.auth.databases.flavours.activities.main; |
|
|
|
|
|
|
|
import android.Manifest; |
|
|
|
import android.content.Context; |
|
|
|
import android.content.DialogInterface; |
|
|
|
import android.content.Intent; |
|
|
|
import android.content.pm.PackageManager; |
|
|
|
import android.location.Location; |
|
|
|
import android.location.LocationManager; |
|
|
|
import android.os.AsyncTask; |
|
|
|
import android.os.Build; |
|
|
|
import android.os.Bundle; |
|
|
|
import android.util.Log; |
|
|
|
import android.view.LayoutInflater; |
|
|
|
import android.view.Menu; |
|
|
|
import android.view.MenuItem; |
|
|
@ -34,12 +39,14 @@ import gr.auth.databases.flavours.activities.main.fragments.MainListFragment; |
|
|
|
import gr.auth.databases.flavours.activities.main.fragments.MainMapFragment; |
|
|
|
import gr.auth.databases.flavours.base.BaseActivity; |
|
|
|
import gr.auth.databases.flavours.model.Restaurant; |
|
|
|
import okhttp3.HttpUrl; |
|
|
|
import okhttp3.Request; |
|
|
|
import okhttp3.Response; |
|
|
|
import okhttp3.ResponseBody; |
|
|
|
|
|
|
|
import static gr.auth.databases.flavours.session.SessionManager.restaurantsUrl; |
|
|
|
|
|
|
|
public class MainActivity extends BaseActivity { |
|
|
|
private static final HttpUrl restaurantsUrl = HttpUrl.parse("http://83.212.109.171:8181/api/restaurant/"); |
|
|
|
private static final int LOCATION_PERMISSIONS_REQUEST_CODE_FOR_DISTANCE_FILTER = 9000; |
|
|
|
private static final int NUM_PAGES = 2; |
|
|
|
|
|
|
|
private ArrayList<Restaurant> restaurants = new ArrayList<>(); |
|
|
@ -47,7 +54,7 @@ public class MainActivity extends BaseActivity { |
|
|
|
private ViewPager viewPager; |
|
|
|
private MenuItem menuMapItem; |
|
|
|
private FloatingActionButton FAB; |
|
|
|
private ArrayList<Integer> mSelectedRestaurantTypes; |
|
|
|
private ArrayList<Integer> mSelectedRestaurantTypes = new ArrayList<>(); |
|
|
|
private int maxDistance = -1, maxCalories = -1; |
|
|
|
|
|
|
|
@Override |
|
|
@ -139,8 +146,6 @@ public class MainActivity extends BaseActivity { |
|
|
|
|
|
|
|
switch (item.getItemId()) { |
|
|
|
case R.id.main_filters_type: |
|
|
|
mSelectedRestaurantTypes = new ArrayList<>(); |
|
|
|
|
|
|
|
builder.setTitle(R.string.main_filters_menu_type) |
|
|
|
.setMultiChoiceItems(R.array.restaurant_type, null, |
|
|
|
new DialogInterface.OnMultiChoiceClickListener() { |
|
|
@ -159,7 +164,8 @@ public class MainActivity extends BaseActivity { |
|
|
|
.setPositiveButton(R.string.main_filters_dialog_positive, new DialogInterface.OnClickListener() { |
|
|
|
@Override |
|
|
|
public void onClick(DialogInterface dialog, int id) { |
|
|
|
//TODO
|
|
|
|
MainTask mainTask = new MainTask(); |
|
|
|
mainTask.execute(); |
|
|
|
} |
|
|
|
}) |
|
|
|
.setNegativeButton(R.string.main_filters_dialog_cancel, new DialogInterface.OnClickListener() { |
|
|
@ -183,6 +189,18 @@ public class MainActivity extends BaseActivity { |
|
|
|
if (!distanceString.isEmpty()) { |
|
|
|
maxDistance = Integer.parseInt(distanceString); |
|
|
|
} |
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
|
|
|
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && |
|
|
|
checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { |
|
|
|
String[] PERMISSIONS_LOCATION = { |
|
|
|
Manifest.permission.ACCESS_FINE_LOCATION, |
|
|
|
Manifest.permission.ACCESS_COARSE_LOCATION}; |
|
|
|
requestPermissions(PERMISSIONS_LOCATION, LOCATION_PERMISSIONS_REQUEST_CODE_FOR_DISTANCE_FILTER); |
|
|
|
} else { |
|
|
|
MainTask mainTask = new MainTask(); |
|
|
|
mainTask.execute(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
}) |
|
|
|
.setNegativeButton(R.string.main_filters_dialog_cancel, new DialogInterface.OnClickListener() { |
|
|
@ -205,6 +223,8 @@ public class MainActivity extends BaseActivity { |
|
|
|
if (!caloriesString.isEmpty()) { |
|
|
|
maxCalories = Integer.parseInt(caloriesString); |
|
|
|
} |
|
|
|
MainTask mainTask = new MainTask(); |
|
|
|
mainTask.execute(); |
|
|
|
} |
|
|
|
}) |
|
|
|
.setNegativeButton(R.string.main_filters_dialog_cancel, new DialogInterface.OnClickListener() { |
|
|
@ -215,7 +235,11 @@ public class MainActivity extends BaseActivity { |
|
|
|
builder.create().show(); |
|
|
|
break; |
|
|
|
case R.id.main_filters_clear: |
|
|
|
//TODO
|
|
|
|
mSelectedRestaurantTypes.clear(); |
|
|
|
maxDistance = -1; |
|
|
|
maxCalories = -1; |
|
|
|
MainTask mainTask = new MainTask(); |
|
|
|
mainTask.execute(); |
|
|
|
break; |
|
|
|
} |
|
|
|
return false; |
|
|
@ -238,44 +262,6 @@ public class MainActivity extends BaseActivity { |
|
|
|
return super.onOptionsItemSelected(item); |
|
|
|
} |
|
|
|
|
|
|
|
/*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"); |
|
|
|
viewPager.setAdapter(adapter); |
|
|
|
} |
|
|
|
|
|
|
|
private class MainPagerAdapter extends FragmentPagerAdapter { |
|
|
|
private final List<Fragment> mFragmentList = new ArrayList<>(); |
|
|
|
private final List<String> mFragmentTitleList = new ArrayList<>(); |
|
|
|
|
|
|
|
MainPagerAdapter(FragmentManager manager) { |
|
|
|
super(manager); |
|
|
|
} |
|
|
|
|
|
|
|
@NonNull |
|
|
|
@Override |
|
|
|
public Fragment getItem(int position) { |
|
|
|
return mFragmentList.get(position); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public int getCount() { |
|
|
|
return mFragmentList.size(); |
|
|
|
} |
|
|
|
|
|
|
|
void addFrag(Fragment fragment, String title) { |
|
|
|
mFragmentList.add(fragment); |
|
|
|
mFragmentTitleList.add(title); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public CharSequence getPageTitle(int position) { |
|
|
|
return mFragmentTitleList.get(position); |
|
|
|
} |
|
|
|
}*/ |
|
|
|
|
|
|
|
private class MainPagerAdapter extends FragmentStatePagerAdapter { |
|
|
|
MainPagerAdapter(FragmentManager fm) { |
|
|
|
super(fm); |
|
|
@ -297,6 +283,14 @@ public class MainActivity extends BaseActivity { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { |
|
|
|
if (requestCode == LOCATION_PERMISSIONS_REQUEST_CODE_FOR_DISTANCE_FILTER) { |
|
|
|
MainTask mainTask = new MainTask(); |
|
|
|
mainTask.execute(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private class MainTask extends AsyncTask<Void, Void, Integer> { |
|
|
|
private static final String JSON_TAG_RESTAURANT_ID = "restaurant_id"; |
|
|
|
private static final String JSON_TAG_RESTAURANT_NAME = "restaurant_name"; |
|
|
@ -308,20 +302,64 @@ public class MainActivity extends BaseActivity { |
|
|
|
|
|
|
|
@Override |
|
|
|
protected void onPreExecute() { //Show a progress dialog until done
|
|
|
|
restaurants.clear(); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
protected Integer doInBackground(Void... params) { |
|
|
|
StringBuilder requestUrl = new StringBuilder(restaurantsUrl); |
|
|
|
if (!mSelectedRestaurantTypes.isEmpty()) { |
|
|
|
requestUrl.append("?filter_restaurant_type="); |
|
|
|
for (int typeIndex : mSelectedRestaurantTypes) { |
|
|
|
requestUrl.append(Restaurant.RestaurantType.getWorkingTypeFromId(typeIndex)); |
|
|
|
} |
|
|
|
} |
|
|
|
if (maxDistance != -1) { |
|
|
|
if (!mSelectedRestaurantTypes.isEmpty()) { |
|
|
|
requestUrl.append("&"); |
|
|
|
} else { |
|
|
|
requestUrl.append("?"); |
|
|
|
} |
|
|
|
|
|
|
|
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); |
|
|
|
// get the last know location from your location manager.
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
|
|
|
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && |
|
|
|
checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { |
|
|
|
String[] PERMISSIONS_LOCATION = { |
|
|
|
Manifest.permission.ACCESS_FINE_LOCATION, |
|
|
|
Manifest.permission.ACCESS_COARSE_LOCATION}; |
|
|
|
requestPermissions(PERMISSIONS_LOCATION, LOCATION_PERMISSIONS_REQUEST_CODE_FOR_DISTANCE_FILTER); |
|
|
|
return 3; |
|
|
|
} |
|
|
|
} |
|
|
|
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); |
|
|
|
|
|
|
|
requestUrl.append("filter_restaurant_radius=").append(maxDistance * 1. / 1000) |
|
|
|
.append("&filter_restaurant_longitude=").append(location.getLongitude()) |
|
|
|
.append("&filter_restaurant_latitude=").append(location.getLatitude()); |
|
|
|
} |
|
|
|
if (maxCalories != -1) { |
|
|
|
if (!mSelectedRestaurantTypes.isEmpty() || maxDistance != -1) { |
|
|
|
requestUrl.append("&"); |
|
|
|
} else { |
|
|
|
requestUrl.append("?"); |
|
|
|
} |
|
|
|
requestUrl.append("filter_restaurant_calories=").append(maxCalories); |
|
|
|
} |
|
|
|
|
|
|
|
//Builds the signup request
|
|
|
|
Request request = new Request.Builder() |
|
|
|
.url(restaurantsUrl) |
|
|
|
.url(requestUrl.toString()) |
|
|
|
.build(); |
|
|
|
|
|
|
|
try { |
|
|
|
//Makes request & handles response
|
|
|
|
Response response = client.newCall(request).execute(); |
|
|
|
|
|
|
|
String result = response.body().string(); |
|
|
|
ResponseBody responseBody = response.body(); |
|
|
|
assert responseBody != null; |
|
|
|
String result = responseBody.string(); |
|
|
|
JSONArray jsonRestaurants = new JSONArray(result); |
|
|
|
|
|
|
|
for (int restaurantIndex = 0; restaurantIndex < jsonRestaurants.length(); ++restaurantIndex) { |
|
|
@ -343,6 +381,9 @@ public class MainActivity extends BaseActivity { |
|
|
|
|
|
|
|
@Override |
|
|
|
protected void onPostExecute(Integer result) { //Handle attempt result
|
|
|
|
viewPager.setAdapter(null); |
|
|
|
PagerAdapter pagerAdapter = new MainPagerAdapter(getSupportFragmentManager()); |
|
|
|
viewPager.setAdapter(pagerAdapter); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|