Commit 4a47c878 authored by Robert Hitt's avatar Robert Hitt

Search queries are now less permanent

-ie, previously when a query was entered there wasn't any
way to go back; when backspace was pressed nothing happened.
now when a query is entered I reset the realm data before the search
parent 89d879a3
...@@ -21,32 +21,40 @@ import android.widget.Toast; ...@@ -21,32 +21,40 @@ import android.widget.Toast;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.List;
import butterknife.BindView; import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import butterknife.OnClick; import butterknife.OnClick;
import io.realm.Case;
import io.realm.OrderedRealmCollection; import io.realm.OrderedRealmCollection;
import io.realm.Realm;
import io.realm.RealmRecyclerViewAdapter; import io.realm.RealmRecyclerViewAdapter;
import io.realm.RealmResults; import io.realm.RealmResults;
import io.realm.Sort;
import srct.whatsopen.R; import srct.whatsopen.R;
import srct.whatsopen.model.Facility; import srct.whatsopen.model.Facility;
import srct.whatsopen.views.FacilityView; import srct.whatsopen.views.FacilityView;
import srct.whatsopen.views.activities.DetailActivity; import srct.whatsopen.views.activities.DetailActivity;
import srct.whatsopen.presenters.FacilityPresenter; import srct.whatsopen.presenters.FacilityPresenter;
/**
* Basic RecyclerView boilerplate, with some added Realm stuff
*/
public class FacilityListAdapter extends public class FacilityListAdapter extends
RealmRecyclerViewAdapter<Facility, FacilityListAdapter.ViewHolder> implements RealmRecyclerViewAdapter<Facility, FacilityListAdapter.ViewHolder> implements
Filterable { Filterable {
private String mMode;
private Realm mRealm;
// Mode describes the filtering for the elements to be displayed
public FacilityListAdapter(Context context, public FacilityListAdapter(Context context,
OrderedRealmCollection<Facility> data) { OrderedRealmCollection<Facility> data, String mode, Realm realm) {
super(context, data, true); super(context, data, true);
mMode = mode;
mRealm = realm;
} }
@Override @Override
...@@ -136,43 +144,50 @@ public class FacilityListAdapter extends ...@@ -136,43 +144,50 @@ public class FacilityListAdapter extends
return new Filter() { return new Filter() {
@Override @Override
protected FilterResults performFiltering(CharSequence constraint) { 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(); return new FilterResults();
} }
@Override @Override
protected void publishResults(CharSequence constraint, FilterResults filterResults) { protected void publishResults(CharSequence constraint, FilterResults filterResults) {
resetData();
if(constraint != null && getData() != null) { if(constraint != null && getData() != null) {
RealmResults<Facility> results = getData().where() RealmResults<Facility> results = getData().where()
.contains("mName", constraint.toString()).findAll(); .contains("mName", constraint.toString(), Case.INSENSITIVE)
.findAll();
updateData(results); updateData(results);
} }
} }
}; };
} }
// Reloads the data between searches
private void resetData() {
RealmResults<Facility> results;
switch(mMode) {
case "All":default:
results = mRealm.where(Facility.class)
.findAllSorted("isOpen", Sort.DESCENDING);
break;
case "Favorites":
results = mRealm.where(Facility.class)
.equalTo("isFavorited", true)
.findAllSorted("isOpen", Sort.DESCENDING);
break;
case "Open":
results = mRealm.where(Facility.class)
.equalTo("isOpen", true)
.findAllSorted("isOpen", Sort.DESCENDING);
break;
case "Closed":
results = mRealm.where(Facility.class)
.equalTo("isOpen", false)
.findAllSorted("isOpen", Sort.DESCENDING);
break;
}
updateData(results);
}
// Set up for the Recycler View cells // Set up for the Recycler View cells
public class ViewHolder extends RecyclerView.ViewHolder implements FacilityView { public class ViewHolder extends RecyclerView.ViewHolder implements FacilityView {
......
...@@ -88,33 +88,33 @@ public class FacilityListFragment extends android.support.v4.app.Fragment implem ...@@ -88,33 +88,33 @@ public class FacilityListFragment extends android.support.v4.app.Fragment implem
mRecyclerView.setLayoutManager(new LinearLayoutManager(view.getContext())); mRecyclerView.setLayoutManager(new LinearLayoutManager(view.getContext()));
switch(mMode) { switch(mMode) {
case "All": case "All":default:
mRecyclerView.setAdapter(new FacilityListAdapter(view.getContext(), mRecyclerView.setAdapter(new FacilityListAdapter(view.getContext(),
mRealm.where(Facility.class) mRealm.where(Facility.class)
.findAllSortedAsync("isOpen", Sort.DESCENDING))); .findAllSortedAsync("isOpen", Sort.DESCENDING),
mMode, mRealm));
break; break;
case "Favorites": case "Favorites":
mRecyclerView.setAdapter(new FacilityListAdapter(view.getContext(), mRecyclerView.setAdapter(new FacilityListAdapter(view.getContext(),
mRealm.where(Facility.class) mRealm.where(Facility.class)
.equalTo("isFavorited", true) .equalTo("isFavorited", true)
.findAllSortedAsync("isOpen", Sort.DESCENDING))); .findAllSortedAsync("isOpen", Sort.DESCENDING),
mMode, mRealm));
break; break;
case "Open": case "Open":
mRecyclerView.setAdapter(new FacilityListAdapter(view.getContext(), mRecyclerView.setAdapter(new FacilityListAdapter(view.getContext(),
mRealm.where(Facility.class) mRealm.where(Facility.class)
.equalTo("isOpen", true) .equalTo("isOpen", true)
.findAllSortedAsync("isOpen", Sort.DESCENDING))); .findAllSortedAsync("isOpen", Sort.DESCENDING),
mMode, mRealm));
break; break;
case "Closed": case "Closed":
mRecyclerView.setAdapter(new FacilityListAdapter(view.getContext(), mRecyclerView.setAdapter(new FacilityListAdapter(view.getContext(),
mRealm.where(Facility.class) mRealm.where(Facility.class)
.equalTo("isOpen", false) .equalTo("isOpen", false)
.findAllSortedAsync("isOpen", Sort.DESCENDING))); .findAllSortedAsync("isOpen", Sort.DESCENDING),
mMode, mRealm));
break; break;
default:
mRecyclerView.setAdapter(new FacilityListAdapter(view.getContext(),
mRealm.where(Facility.class)
.findAllSortedAsync("isOpen", Sort.DESCENDING)));
} }
// Speeds things up for static lists // Speeds things up for static lists
......
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