Commit 73521eec authored by Robert Hitt's avatar Robert Hitt

Added search function

parent 2f56174d
package srct.whatsopen.views.activities;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.preference.PreferenceManager;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
......@@ -21,7 +24,9 @@ import srct.whatsopen.MyApplication;
import srct.whatsopen.R;
import srct.whatsopen.presenters.MainPresenter;
import srct.whatsopen.views.MainView;
import srct.whatsopen.views.adapters.FacilityListAdapter;
import srct.whatsopen.views.adapters.FacilityListFragmentPagerAdapter;
import srct.whatsopen.views.fragments.FacilityListFragment;
public class MainActivity extends AppCompatActivity implements MainView {
......@@ -71,6 +76,9 @@ public class MainActivity extends AppCompatActivity implements MainView {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
configureSearchView(menu);
return true;
}
......@@ -121,6 +129,40 @@ public class MainActivity extends AppCompatActivity implements MainView {
}
}
private void configureSearchView(Menu menu) {
// I think this is for doing the search asynchronously
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
// Set QueryTextListener
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
// kinda hacky way to get the Fragment through the multitudinous layers of
// ViewPager spaghetti. I mean, not that hacky, but it looks gross
Fragment fragment = getSupportFragmentManager()
.findFragmentByTag("android:switcher:" + R.id.view_pager + ":" +
mViewPager.getCurrentItem());
if(fragment != null) {
FacilityListAdapter adapter = (FacilityListAdapter)
((FacilityListFragment) fragment).getRecyclerView().getAdapter();
adapter.getFilter().filter(newText);
}
return true;
}
});
}
@Override
public void showProgressBar() {
mViewPager.setVisibility(View.INVISIBLE);
......
......@@ -12,11 +12,14 @@ import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Calendar;
import butterknife.BindView;
......@@ -25,6 +28,7 @@ import butterknife.ButterKnife;
import butterknife.OnClick;
import io.realm.OrderedRealmCollection;
import io.realm.RealmRecyclerViewAdapter;
import io.realm.RealmResults;
import srct.whatsopen.R;
import srct.whatsopen.model.Facility;
import srct.whatsopen.views.FacilityView;
......@@ -36,7 +40,8 @@ import srct.whatsopen.presenters.FacilityPresenter;
*/
public class FacilityListAdapter extends
RealmRecyclerViewAdapter<Facility, FacilityListAdapter.ViewHolder> {
RealmRecyclerViewAdapter<Facility, FacilityListAdapter.ViewHolder> implements
Filterable {
public FacilityListAdapter(Context context,
OrderedRealmCollection<Facility> data) {
......@@ -51,8 +56,7 @@ public class FacilityListAdapter extends
View facilityView = inflater.inflate(R.layout.item_facility, parent, false);
ViewHolder viewHolder = new ViewHolder(facilityView);
return viewHolder;
return new ViewHolder(facilityView);
}
@Override
......@@ -127,6 +131,48 @@ public class FacilityListAdapter extends
layout.setPadding(0, paddingTop, 0, paddingBottom);
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
/*
FilterResults results = new FilterResults();
// if there's no text in the search i.e. user hasn't typed anything yet
if(constraint == null || constraint.length() == 0) {
results.values = getData();
results.count = getData() != null ? getData().size() : 0;
}
else if(getData() != null) {
ArrayList<Facility> filteredFacilities = new ArrayList<>();
for(Facility f : getData()) {
// search for Facilities which match the search text
if(f.getName().toUpperCase().contains(constraint.toString().toUpperCase())){
filteredFacilities.add(f);
}
results.values = filteredFacilities;
results.count = filteredFacilities.size();
}
}
*/
return new FilterResults();
}
@Override
protected void publishResults(CharSequence constraint, FilterResults filterResults) {
if(constraint != null && getData() != null) {
RealmResults<Facility> results = getData().where()
.contains("mName", constraint.toString()).findAll();
updateData(results);
}
}
};
}
// Set up for the Recycler View cells
public class ViewHolder extends RecyclerView.ViewHolder implements FacilityView {
......
......@@ -140,6 +140,10 @@ public class FacilityListFragment extends android.support.v4.app.Fragment implem
return getActivity();
}
public RecyclerView getRecyclerView() {
return mRecyclerView;
}
// Redraws RecyclerView if the settings have changed for it
private void setPreferenceChangeListener() {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getContext());
......
......@@ -5,7 +5,8 @@
<item
android:id="@+id/action_search"
android:icon="@drawable/ic_search_white_24dp"
app:showAsAction="ifRoom"
app:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView"
android:title="Search"/>
<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