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; ...@@ -26,6 +26,7 @@ import com.google.gson.GsonBuilder;
import com.testapp.entities.Cuisine; import com.testapp.entities.Cuisine;
import com.testapp.entities.Dish; import com.testapp.entities.Dish;
import com.testapp.entities.DishType; import com.testapp.entities.DishType;
import com.testapp.entities.Filter;
import com.testapp.entities.Location; import com.testapp.entities.Location;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
...@@ -41,9 +42,7 @@ import retrofit2.Response; ...@@ -41,9 +42,7 @@ import retrofit2.Response;
import retrofit2.Retrofit; import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory; import retrofit2.converter.gson.GsonConverterFactory;
public class NavDrawerActivity extends AppCompatActivity public class NavDrawerActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
implements NavigationView.OnNavigationItemSelectedListener {
private CanteeneoApiInterface service; private CanteeneoApiInterface service;
private static Context c; private static Context c;
...@@ -104,81 +103,35 @@ public class NavDrawerActivity extends AppCompatActivity ...@@ -104,81 +103,35 @@ public class NavDrawerActivity extends AppCompatActivity
String message = intent.getStringExtra(LoginActivity.EXTRA_MESSAGE); String message = intent.getStringExtra(LoginActivity.EXTRA_MESSAGE);
Toast.makeText(NavDrawerActivity.this, message, Toast.LENGTH_SHORT).show(); Toast.makeText(NavDrawerActivity.this, message, Toast.LENGTH_SHORT).show();
populateDishTypeFilters(); populateFilter(service.getDishTypes(), R.id.type_checkboxes);
populateCuisineFilters(); populateFilter(service.getCuisines(), R.id.cuisine_checkboxes);
populateLocationFilters(); populateFilter(service.getLocations(), R.id.location_checkboxes);
} }
public void populateDishTypeFilters() { public <T extends Filter> void populateFilter(Call<List<T>> call, int layout_id) {
final LinearLayout type_checkboxes = (LinearLayout)findViewById(R.id.type_checkboxes); final LinearLayout checkboxes = (LinearLayout)findViewById(layout_id);
type_checkboxes.removeAllViewsInLayout(); checkboxes.removeAllViewsInLayout();
Call<List<DishType>> call = service.getDishTypes();
call.enqueue(new Callback<List<DishType>>() {
@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);
}
}
call.enqueue(new Callback<List<T>>() {
@Override @Override
public void onFailure(Call<List<DishType>> call, Throwable t) { 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);
} }
});
} }
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 @Override
public void onResponse(Call<List<Cuisine>> call, Response<List<Cuisine>> response) { public void onFailure(Call<List<T>> call, Throwable t) {
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);
}
}
@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; package com.testapp.entities;
public class Cuisine { public class Cuisine implements Filter {
int id; int id;
String name; String name;
......
package com.testapp.entities; package com.testapp.entities;
public class DishType { public class DishType implements Filter {
int id; int id;
String name; 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; package com.testapp.entities;
public class Location { public class Location implements Filter {
int id; int id;
String name; String name;
......
...@@ -85,7 +85,6 @@ ...@@ -85,7 +85,6 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">
</LinearLayout> </LinearLayout>
<TextView <TextView
......
...@@ -26,9 +26,6 @@ ...@@ -26,9 +26,6 @@
<string name="drawer_5_2"> JSEC</string> <string name="drawer_5_2"> JSEC</string>
<string name="drawer_5_3"> Zekaf</string> <string name="drawer_5_3"> Zekaf</string>
<string name="drawer_6">Cuisine</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="hint_search">Search</string>
<string name="navigation_drawer_open">Open navigation drawer</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