Commit f7b8d3da authored by Willard's avatar Willard

Overhaul searching and filtering (done client side instead of server side)

parent 15839804
...@@ -28,13 +28,8 @@ import retrofit2.http.Query; ...@@ -28,13 +28,8 @@ import retrofit2.http.Query;
public interface CanteeneoApiInterface { public interface CanteeneoApiInterface {
@GET("api/search/dish") @GET("api/dishes")
Call<List<Dish>> searchByName(@Query("name") String name, Call<List<Dish>> getAllDishes();
@Query("type_filters") String types,
@Query("cuisine_filters") String cuisines,
@Query("location_filters") String location,
@Query("min_price") int min_price,
@Query("max_price") int max_price);
@FormUrlEncoded @FormUrlEncoded
@POST("api/users/new") @POST("api/users/new")
......
...@@ -6,27 +6,31 @@ import android.view.ViewGroup; ...@@ -6,27 +6,31 @@ import android.view.ViewGroup;
import android.widget.BaseAdapter; import android.widget.BaseAdapter;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import com.squareup.picasso.Picasso; import com.squareup.picasso.Picasso;
import com.testapp.entities.Dish; import com.testapp.entities.Dish;
import java.util.ArrayList; import java.util.ArrayList;
public class DishAdapter extends BaseAdapter { public class DishAdapter extends BaseAdapter {
private Activity context; private Activity context;
ArrayList<Dish> dishes; ArrayList<Dish> dishes;
ArrayList<Dish> filteredDishes;
public DishAdapter(Activity context, ArrayList<Dish> dishes) { public DishAdapter(Activity context, ArrayList<Dish> dishes) {
this.context = context; this.context = context;
this.dishes = dishes; this.dishes = dishes;
filteredDishes = new ArrayList<>();
} }
@Override @Override
public int getCount() { public int getCount() {
return dishes.size(); return filteredDishes.size();
} }
@Override @Override
public Dish getItem(int position) { public Dish getItem(int position) {
return dishes.get(position); return filteredDishes.get(position);
} }
@Override @Override
...@@ -48,7 +52,7 @@ public class DishAdapter extends BaseAdapter { ...@@ -48,7 +52,7 @@ public class DishAdapter extends BaseAdapter {
holder = (DishViewHolder)convertView.getTag(); holder = (DishViewHolder)convertView.getTag();
} }
Dish d = dishes.get(position); Dish d = filteredDishes.get(position);
holder.name.setText(d.getName()); holder.name.setText(d.getName());
holder.price.setText(d.getPrice()+""); holder.price.setText(d.getPrice()+"");
Picasso.with(context).load("http://" + parent.getResources().getString(R.string.server_ip) + ":5000/static/uploads/"+ d.getImagePath()).fit().centerCrop().tag(context).into(holder.image); Picasso.with(context).load("http://" + parent.getResources().getString(R.string.server_ip) + ":5000/static/uploads/"+ d.getImagePath()).fit().centerCrop().tag(context).into(holder.image);
...@@ -56,9 +60,33 @@ public class DishAdapter extends BaseAdapter { ...@@ -56,9 +60,33 @@ public class DishAdapter extends BaseAdapter {
return convertView; return convertView;
} }
public void filter(String nameFilter, ArrayList<Integer> typeFilter, ArrayList<Integer> cuisineFilter, ArrayList<Integer> locationFilter, double minPrice, double maxPrice) {
filteredDishes.clear();
for(Dish dish: dishes) {
boolean skip = false;
String[] keywords = nameFilter.toLowerCase().split(" ");
for(String keyword: keywords) {
if(dish.getName().toLowerCase().indexOf(keyword) == -1) {
skip = true;
break;
}
}
if(skip) continue;
if(typeFilter.indexOf(dish.getTypeId()) == -1) continue;
if(cuisineFilter.indexOf(dish.getCuisineId()) == -1) continue;
if(locationFilter.indexOf(dish.getLocationId()) == -1) continue;
if(dish.getPrice() < minPrice || dish.getPrice() > maxPrice) continue;
filteredDishes.add(dish);
}
}
static class DishViewHolder { static class DishViewHolder {
TextView name; TextView name;
TextView price; TextView price;
ImageView image; ImageView image;
} }
} }
\ No newline at end of file
...@@ -15,6 +15,7 @@ import android.support.v7.app.ActionBarDrawerToggle; ...@@ -15,6 +15,7 @@ import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView; import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
...@@ -46,6 +47,8 @@ public class NavDrawerActivity extends AppCompatActivity implements NavigationVi ...@@ -46,6 +47,8 @@ public class NavDrawerActivity extends AppCompatActivity implements NavigationVi
public static String EXTRA_MESSAGE = "com.testapp.MESSAGE"; public static String EXTRA_MESSAGE = "com.testapp.MESSAGE";
String query = "";
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
...@@ -72,8 +75,6 @@ public class NavDrawerActivity extends AppCompatActivity implements NavigationVi ...@@ -72,8 +75,6 @@ public class NavDrawerActivity extends AppCompatActivity implements NavigationVi
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this); navigationView.setNavigationItemSelectedListener(this);
Intent intent = getIntent();
populateFilter(AppUtils.service.getDishTypes(), R.id.type_checkboxes); populateFilter(AppUtils.service.getDishTypes(), R.id.type_checkboxes);
populateFilter(AppUtils.service.getCuisines(), R.id.cuisine_checkboxes); populateFilter(AppUtils.service.getCuisines(), R.id.cuisine_checkboxes);
populateFilter(AppUtils.service.getLocations(), R.id.location_checkboxes); populateFilter(AppUtils.service.getLocations(), R.id.location_checkboxes);
...@@ -96,6 +97,15 @@ public class NavDrawerActivity extends AppCompatActivity implements NavigationVi ...@@ -96,6 +97,15 @@ public class NavDrawerActivity extends AppCompatActivity implements NavigationVi
} }
}); });
Button applyFilterButton = (Button)findViewById(R.id.apply_filter_button);
applyFilterButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
refreshDishes();
((DrawerLayout)NavDrawerActivity.this.findViewById(R.id.drawer_layout)).closeDrawer(Gravity.LEFT);
}
});
RelativeLayout loggedInHeader = (RelativeLayout)findViewById(R.id.logged_in_header); RelativeLayout loggedInHeader = (RelativeLayout)findViewById(R.id.logged_in_header);
if(AppUtils.isLoggedIn()) { if(AppUtils.isLoggedIn()) {
loggedInHeader.setVisibility(View.VISIBLE); loggedInHeader.setVisibility(View.VISIBLE);
...@@ -122,6 +132,23 @@ public class NavDrawerActivity extends AppCompatActivity implements NavigationVi ...@@ -122,6 +132,23 @@ public class NavDrawerActivity extends AppCompatActivity implements NavigationVi
} else { } else {
loggedInHeader.setVisibility(View.GONE); loggedInHeader.setVisibility(View.GONE);
} }
Call<List<Dish>> call = AppUtils.service.getAllDishes();
call.enqueue(new Callback<List<Dish>>() {
@Override
public void onResponse(Call<List<Dish>> call, Response<List<Dish>> response) {
List<Dish> newDishes = response.body();
dishes.clear();
dishes.addAll(newDishes);
refreshDishes();
}
@Override
public void onFailure(Call<List<Dish>> call, Throwable t) {
}
});
} }
public <T extends Filter> void populateFilter(Call<List<T>> call, int layout_id) { public <T extends Filter> void populateFilter(Call<List<T>> call, int layout_id) {
...@@ -176,91 +203,63 @@ public class NavDrawerActivity extends AppCompatActivity implements NavigationVi ...@@ -176,91 +203,63 @@ public class NavDrawerActivity extends AppCompatActivity implements NavigationVi
@Override @Override
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.nav_drawer, menu); getMenuInflater().inflate(R.menu.nav_drawer, menu);
MenuItem searchItem = menu.findItem(R.id.action_search); MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView sv = (SearchView) searchItem.getActionView(); final SearchView sv = (SearchView) searchItem.getActionView();
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() { sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override @Override
public boolean onQueryTextSubmit(String query) { public boolean onQueryTextSubmit(String query) {
searchDish(query);
return false; return false;
} }
@Override @Override
public boolean onQueryTextChange(String newText) { public boolean onQueryTextChange(String newText) {
query = newText;
refreshDishes();
return false; return false;
} }
}); });
return true;
}
@Override MenuItem refreshItem = menu.findItem(R.id.action_refresh);
public boolean onOptionsItemSelected(MenuItem item) { refreshItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
// Handle action bar item clicks here. The action bar will @Override
// automatically handle clicks on the Home/Up button, so long public boolean onMenuItemClick(MenuItem item) {
// as you specify a parent activity in AndroidManifest.xml. refreshDishes();
int id = item.getItemId(); return false;
}
//noinspection SimplifiableIfStatement });
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item); return true;
} }
@SuppressWarnings("StatementWithEmptyBody") @SuppressWarnings("StatementWithEmptyBody")
@Override @Override
public boolean onNavigationItemSelected(MenuItem item) { public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_all_items) {
// Handle the camera action
} else if (id == R.id.nav_filter) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
//drawer.closeDrawer(GravityCompat.START);
return true; return true;
} }
private String getChecked(int layoutId) { private ArrayList<Integer> getChecked(int layoutId) {
LinearLayout checkboxes = (LinearLayout)findViewById(layoutId); LinearLayout checkboxes = (LinearLayout)findViewById(layoutId);
String checked = ""; ArrayList<Integer> checked = new ArrayList<>();
for(int i = 0; i < checkboxes.getChildCount(); i++) { for(int i = 0; i < checkboxes.getChildCount(); i++) {
View v = checkboxes.getChildAt(i); View v = checkboxes.getChildAt(i);
if(v instanceof CheckBox) { if(v instanceof CheckBox) {
CheckBox c = (CheckBox)v; CheckBox c = (CheckBox)v;
if(c.isChecked()) { if(c.isChecked()) {
checked += c.getId(); checked.add(c.getId());
} }
} }
} }
return checked; return checked;
} }
private void searchDish(String name) { public void refreshDishes() {
Call<List<Dish>> call = AppUtils.service.searchByName(name, getChecked(R.id.type_checkboxes), getChecked(R.id.cuisine_checkboxes), getChecked(R.id.location_checkboxes), 0, 1000); adapter.filter(query, getChecked(R.id.type_checkboxes), getChecked(R.id.cuisine_checkboxes), getChecked(R.id.location_checkboxes), 0, 1000);
call.enqueue(new Callback<List<Dish>>() { adapter.notifyDataSetChanged();
@Override System.out.println(adapter.filteredDishes.size());
public void onResponse(Call<List<Dish>> call, Response<List<Dish>> response) {
List<Dish> newDishes = response.body();
dishes.clear();
dishes.addAll(newDishes);
adapter.notifyDataSetChanged();
}
@Override
public void onFailure(Call<List<Dish>> call, Throwable t) {
}
});
} }
} }
...@@ -9,6 +9,15 @@ public class Dish { ...@@ -9,6 +9,15 @@ public class Dish {
double price; double price;
String description; String description;
@SerializedName("type_id")
int typeId;
@SerializedName("cuisine_id")
int cuisineId;
@SerializedName("location_id")
int locationId;
@SerializedName("image_path") @SerializedName("image_path")
String imagePath; String imagePath;
...@@ -70,4 +79,28 @@ public class Dish { ...@@ -70,4 +79,28 @@ public class Dish {
public void setDescription(String description) { public void setDescription(String description) {
this.description = description; this.description = description;
} }
public int getTypeId() {
return typeId;
}
public void setTypeId(int typeId) {
this.typeId = typeId;
}
public int getCuisineId() {
return cuisineId;
}
public void setCuisineId(int cuisineId) {
this.cuisineId = cuisineId;
}
public int getLocationId() {
return locationId;
}
public void setLocationId(int locationId) {
this.locationId = locationId;
}
} }
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M17.65,6.35C16.2,4.9 14.21,4 12,4c-4.42,0 -7.99,3.58 -7.99,8s3.57,8 7.99,8c3.73,0 6.84,-2.55 7.73,-6h-2.08c-0.82,2.33 -3.04,4 -5.65,4 -3.31,0 -6,-2.69 -6,-6s2.69,-6 6,-6c1.66,0 3.14,0.69 4.22,1.78L13,11h7V4l-2.35,2.35z"/>
</vector>
...@@ -174,10 +174,10 @@ ...@@ -174,10 +174,10 @@
</LinearLayout> </LinearLayout>
<Button <Button
android:text="@string/hint_search" android:text="@string/apply_filter"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:id="@+id/search_button" android:id="@+id/apply_filter_button"
android:elevation="10dp" android:elevation="10dp"
style="@style/Widget.AppCompat.Button" /> style="@style/Widget.AppCompat.Button" />
......
...@@ -5,14 +5,14 @@ ...@@ -5,14 +5,14 @@
<item <item
android:id="@+id/action_search" android:id="@+id/action_search"
android:orderInCategory="100" android:orderInCategory="100"
android:title="@string/hint_search" android:title="@string/apply_filter"
android:icon="@android:drawable/ic_menu_search" android:icon="@android:drawable/ic_menu_search"
app:actionViewClass="android.support.v7.widget.SearchView" app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always" /> app:showAsAction="always" />
<item <item
android:id="@+id/action_settings" android:id="@+id/action_refresh"
android:orderInCategory="101" android:orderInCategory="101"
android:icon="@drawable/ic_menu_manage" android:icon="@drawable/ic_refresh"
android:title="@string/action_settings" android:title="@string/action_refresh"
app:showAsAction="always" /> app:showAsAction="always" />
</menu> </menu>
...@@ -24,15 +24,15 @@ ...@@ -24,15 +24,15 @@
<string name="drawer_4">Price Range</string> <string name="drawer_4">Price Range</string>
<string name="drawer_5">Locations</string> <string name="drawer_5">Locations</string>
<string name="drawer_6">Cuisine</string> <string name="drawer_6">Cuisine</string>
<string name="hint_search">Search</string> <string name="apply_filter">Apply Filters</string>
<string name="navigation_drawer_open">Open navigation drawer</string> <string name="navigation_drawer_open">Open navigation drawer</string>
<string name="navigation_drawer_close">Close navigation drawer</string> <string name="navigation_drawer_close">Close navigation drawer</string>
<string name="action_settings">Settings</string> <string name="action_refresh">Refresh</string>
<string name="title_activity_search">SearchActivity</string> <string name="title_activity_search">SearchActivity</string>
<string name="server_ip">192.168.11.18</string> <string name="server_ip">10.0.2.2</string>
<string-array name="sampleEntries"> <string-array name="sampleEntries">
<item>Item 111</item> <item>Item 111</item>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment