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)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
query -> view.getAdapter().getFilter().filter(query)
);
.subscribe(view.getAdapter().getFilter()::filter);
}
public void initializeMangas() {

View File

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