Use Observable to filter library

This commit is contained in:
inorichi 2015-09-30 21:07:45 +02:00
parent e175a75031
commit 710638e2eb
2 changed files with 38 additions and 33 deletions

View File

@ -59,9 +59,7 @@ public class LibraryPresenter extends BasePresenter {
searchViewSubscription = Observable.switchOnNext(searchViewPublishSubject) searchViewSubscription = Observable.switchOnNext(searchViewPublishSubject)
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe( .subscribe(view.getAdapter().getFilter()::filter);
query -> view.getAdapter().getFilter().filter(query)
);
} }
public void initializeMangas() { public void initializeMangas() {

View File

@ -19,25 +19,30 @@ import android.content.Context;
import android.widget.Filter; import android.widget.Filter;
import android.widget.Filterable; import android.widget.Filterable;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import eu.kanade.mangafeed.data.models.Manga; import eu.kanade.mangafeed.data.models.Manga;
import rx.Observable;
import uk.co.ribot.easyadapter.EasyAdapter; import uk.co.ribot.easyadapter.EasyAdapter;
import uk.co.ribot.easyadapter.ItemViewHolder; import uk.co.ribot.easyadapter.ItemViewHolder;
public class CatalogueArrayAdapter<T> extends EasyAdapter<T> implements Filterable { public class CatalogueArrayAdapter<T> extends EasyAdapter<T> implements Filterable {
List<Manga> mangas; List<Manga> mangas;
Filter filter;
public CatalogueArrayAdapter(Context context, Class<? extends ItemViewHolder> itemViewHolderClass, List<T> listItems) { public CatalogueArrayAdapter(Context context, Class<? extends ItemViewHolder> itemViewHolderClass, List<T> listItems) {
super(context, itemViewHolderClass, listItems); super(context, itemViewHolderClass, listItems);
mangas = (List<Manga>)getItems(); mangas = (List<Manga>)getItems();
filter = new CatalogueFilter();
} }
@Override @Override
public Filter getFilter() { public Filter getFilter() {
return new Filter() { return filter;
}
private class CatalogueFilter extends Filter {
@Override @Override
protected FilterResults performFiltering(CharSequence charSequence) { protected FilterResults performFiltering(CharSequence charSequence) {
FilterResults results = new FilterResults(); FilterResults results = new FilterResults();
@ -47,24 +52,26 @@ public class CatalogueArrayAdapter<T> extends EasyAdapter<T> implements Filterab
results.values = mangas; results.values = mangas;
results.count = mangas.size(); results.count = mangas.size();
} else { } else {
ArrayList<Manga> filterResultsData = new ArrayList<>(); List<Manga> filteredMangas = Observable.from(mangas)
for (Manga manga: mangas) { .filter(x ->
if (manga.title.toLowerCase().contains(query) || x.title.toLowerCase().contains(query) ||
manga.author.toLowerCase().contains(query) || x.author.toLowerCase().contains(query) ||
manga.artist.toLowerCase().contains(query)) { x.artist.toLowerCase().contains(query))
filterResultsData.add(manga); .toList()
} .toBlocking()
} .single();
results.values = filterResultsData; results.values = filteredMangas;
results.count = filterResultsData.size(); results.count = filteredMangas.size();
} }
return results; return results;
} }
@Override @Override
protected void publishResults(CharSequence constraint, FilterResults results) { public void publishResults(CharSequence constraint, FilterResults results) {
setItems((List<T>) results.values); setItems((List<T>) results.values);
} }
};
} }
} }