From 710638e2ebcc3c6420fcc762b564b6141c0bdaff Mon Sep 17 00:00:00 2001 From: inorichi Date: Wed, 30 Sep 2015 21:07:45 +0200 Subject: [PATCH] Use Observable to filter library --- .../mangafeed/presenter/LibraryPresenter.java | 4 +- .../ui/adapter/CatalogueArrayAdapter.java | 67 ++++++++++--------- 2 files changed, 38 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/eu/kanade/mangafeed/presenter/LibraryPresenter.java b/app/src/main/java/eu/kanade/mangafeed/presenter/LibraryPresenter.java index 67a1bed773..67579e3df5 100644 --- a/app/src/main/java/eu/kanade/mangafeed/presenter/LibraryPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/presenter/LibraryPresenter.java @@ -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() { diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/adapter/CatalogueArrayAdapter.java b/app/src/main/java/eu/kanade/mangafeed/ui/adapter/CatalogueArrayAdapter.java index 9dd1febc8e..de2e1493e3 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/adapter/CatalogueArrayAdapter.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/adapter/CatalogueArrayAdapter.java @@ -19,52 +19,59 @@ 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 extends EasyAdapter implements Filterable { List mangas; + Filter filter; public CatalogueArrayAdapter(Context context, Class itemViewHolderClass, List listItems) { super(context, itemViewHolderClass, listItems); mangas = (List)getItems(); + filter = new CatalogueFilter(); } @Override public Filter getFilter() { - return new Filter() { - @Override - protected FilterResults performFiltering(CharSequence charSequence) { - FilterResults results = new FilterResults(); - String query = charSequence.toString().toLowerCase(); - - if (query == null || query.length() == 0) { - results.values = mangas; - results.count = mangas.size(); - } else { - ArrayList 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(); - } - return results; - } - - @Override - protected void publishResults(CharSequence constraint, FilterResults results) { - setItems((List) results.values); - } - }; + return filter; } + + private class CatalogueFilter extends Filter { + @Override + protected FilterResults performFiltering(CharSequence charSequence) { + FilterResults results = new FilterResults(); + String query = charSequence.toString().toLowerCase(); + + if (query == null || query.length() == 0) { + results.values = mangas; + results.count = mangas.size(); + } else { + List 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 + public void publishResults(CharSequence constraint, FilterResults results) { + setItems((List) results.values); + } + } + + }