Commit fff5654f authored by Willard's avatar Willard

Remove redundant code in checkboxes by using generics and interfaces

parent b229f697
......@@ -26,6 +26,7 @@ import com.google.gson.GsonBuilder;
import com.testapp.entities.Cuisine;
import com.testapp.entities.Dish;
import com.testapp.entities.DishType;
import com.testapp.entities.Filter;
import com.testapp.entities.Location;
import java.io.File;
import java.io.IOException;
......@@ -41,9 +42,7 @@ import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class NavDrawerActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
public class NavDrawerActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private CanteeneoApiInterface service;
private static Context c;
......@@ -104,81 +103,35 @@ public class NavDrawerActivity extends AppCompatActivity
String message = intent.getStringExtra(LoginActivity.EXTRA_MESSAGE);
Toast.makeText(NavDrawerActivity.this, message, Toast.LENGTH_SHORT).show();
populateDishTypeFilters();
populateCuisineFilters();
populateLocationFilters();
populateFilter(service.getDishTypes(), R.id.type_checkboxes);
populateFilter(service.getCuisines(), R.id.cuisine_checkboxes);
populateFilter(service.getLocations(), R.id.location_checkboxes);
}
public void populateDishTypeFilters() {
final LinearLayout type_checkboxes = (LinearLayout)findViewById(R.id.type_checkboxes);
type_checkboxes.removeAllViewsInLayout();
public <T extends Filter> void populateFilter(Call<List<T>> call, int layout_id) {
final LinearLayout checkboxes = (LinearLayout)findViewById(layout_id);
checkboxes.removeAllViewsInLayout();
Call<List<DishType>> call = service.getDishTypes();
call.enqueue(new Callback<List<DishType>>() {
call.enqueue(new Callback<List<T>>() {
@Override
public void onResponse(Call<List<DishType>> call, Response<List<DishType>> response) {
List<DishType> types = response.body();
LayoutInflater checkboxInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for(DishType t: types) {
CheckBox checkbox = (CheckBox)checkboxInflater.inflate(R.layout.search_checkbox, null);
checkbox.setId(t.getId());
checkbox.setText(t.getName());
type_checkboxes.addView(checkbox);
public void onResponse(Call<List<T>> call, Response<List<T>> response) {
List<T> filters = response.body();
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for(T filter: filters) {
CheckBox checkbox = (CheckBox)inflater.inflate(R.layout.search_checkbox, null);
checkbox.setId(filter.getId());
checkbox.setText(filter.getName());
checkbox.setChecked(true);
checkboxes.addView(checkbox);
}
}
@Override
public void onFailure(Call<List<DishType>> call, Throwable t) {
}
});
}
public void populateCuisineFilters() {
final LinearLayout cuisine_checkboxes = (LinearLayout)findViewById(R.id.cuisine_checkboxes);
cuisine_checkboxes.removeAllViewsInLayout();
Call<List<Cuisine>> call = service.getCuisines();
call.enqueue(new Callback<List<Cuisine>>() {
@Override
public void onResponse(Call<List<Cuisine>> call, Response<List<Cuisine>> response) {
List<Cuisine> cuisines = response.body();
LayoutInflater checkboxInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for(Cuisine c: cuisines) {
CheckBox checkbox = (CheckBox)checkboxInflater.inflate(R.layout.search_checkbox, null);
checkbox.setId(c.getId());
checkbox.setText(c.getName());
cuisine_checkboxes.addView(checkbox);
}
}
public void onFailure(Call<List<T>> call, Throwable t) {
@Override
public void onFailure(Call<List<Cuisine>> call, Throwable t) {
}
});
}
public void populateLocationFilters() {
final LinearLayout location_checkboxes = (LinearLayout)findViewById(R.id.location_checkboxes);
location_checkboxes.removeAllViewsInLayout();
Call<List<Location>> call = service.getLocations();
call.enqueue(new Callback<List<Location>>() {
@Override
public void onResponse(Call<List<Location>> call, Response<List<Location>> response) {
List<Location> locations = response.body();
LayoutInflater checkboxInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for(Location l: locations) {
CheckBox checkbox = (CheckBox)checkboxInflater.inflate(R.layout.search_checkbox, null);
checkbox.setId(l.getId());
checkbox.setText(l.getName());
location_checkboxes.addView(checkbox);
}
}
@Override
public void onFailure(Call<List<Location>> call, Throwable t) {
}
});
}
......
package com.testapp.entities;
public class Cuisine {
public class Cuisine implements Filter {
int id;
String name;
......
package com.testapp.entities;
public class DishType {
public class DishType implements Filter {
int id;
String name;
......
package com.testapp.entities;
public interface Filter {
int getId();
void setId(int id);
String getName();
void setName(String name);
}
package com.testapp.entities;
public class Location {
public class Location implements Filter {
int id;
String name;
......
......@@ -85,7 +85,6 @@
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
<TextView
......
......@@ -26,9 +26,6 @@
<string name="drawer_5_2"> JSEC</string>
<string name="drawer_5_3"> Zekaf</string>
<string name="drawer_6">Cuisine</string>
<string name="drawer_6_1"> American</string>
<string name="drawer_6_2"> Japanese</string>
<string name="drawer_6_3"> Chinese</string>
<string name="hint_search">Search</string>
<string name="navigation_drawer_open">Open navigation drawer</string>
......
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