From e175a75031ffe027a79345dfabd67619add5167a Mon Sep 17 00:00:00 2001 From: inorichi Date: Wed, 30 Sep 2015 19:35:27 +0200 Subject: [PATCH] Search working --- .../mangafeed/presenter/LibraryPresenter.java | 4 +- .../ui/adapter/CatalogueArrayAdapter.java | 70 +++++++++++++++++++ .../ui/fragment/LibraryFragment.java | 9 ++- .../eu/kanade/mangafeed/view/LibraryView.java | 2 + 4 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/eu/kanade/mangafeed/ui/adapter/CatalogueArrayAdapter.java 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 d292f2386..67a1bed77 100644 --- a/app/src/main/java/eu/kanade/mangafeed/presenter/LibraryPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/presenter/LibraryPresenter.java @@ -59,7 +59,9 @@ public class LibraryPresenter extends BasePresenter { searchViewSubscription = Observable.switchOnNext(searchViewPublishSubject) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) - .subscribe(Timber::w); + .subscribe( + query -> view.getAdapter().getFilter().filter(query) + ); } 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 new file mode 100644 index 000000000..9dd1febc8 --- /dev/null +++ b/app/src/main/java/eu/kanade/mangafeed/ui/adapter/CatalogueArrayAdapter.java @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2014 Ribot Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package eu.kanade.mangafeed.ui.adapter; + +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 uk.co.ribot.easyadapter.EasyAdapter; +import uk.co.ribot.easyadapter.ItemViewHolder; + +public class CatalogueArrayAdapter extends EasyAdapter implements Filterable { + + List mangas; + + public CatalogueArrayAdapter(Context context, Class itemViewHolderClass, List listItems) { + super(context, itemViewHolderClass, listItems); + mangas = (List)getItems(); + } + + @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); + } + }; + } +} diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/LibraryFragment.java b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/LibraryFragment.java index 25b7610c3..ed131ba99 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/LibraryFragment.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/LibraryFragment.java @@ -17,6 +17,7 @@ import eu.kanade.mangafeed.R; import eu.kanade.mangafeed.data.models.Manga; import eu.kanade.mangafeed.presenter.LibraryPresenter; import eu.kanade.mangafeed.ui.activity.MainActivity; +import eu.kanade.mangafeed.ui.adapter.CatalogueArrayAdapter; import eu.kanade.mangafeed.ui.adapter.MangaLibraryHolder; import eu.kanade.mangafeed.view.LibraryView; import timber.log.Timber; @@ -27,7 +28,7 @@ public class LibraryFragment extends BaseFragment implements LibraryView { @Bind(R.id.gridView) GridView grid; LibraryPresenter presenter; - EasyAdapter adapter; + CatalogueArrayAdapter adapter; MainActivity activity; public static LibraryFragment newInstance() { @@ -93,7 +94,7 @@ public class LibraryFragment extends BaseFragment implements LibraryView { public void setMangas(List mangas) { if (adapter == null) { - adapter = new EasyAdapter<>( + adapter = new CatalogueArrayAdapter<>( getActivity(), MangaLibraryHolder.class, mangas @@ -116,4 +117,8 @@ public class LibraryFragment extends BaseFragment implements LibraryView { //activity.getSupportActionBar(). } + public CatalogueArrayAdapter getAdapter() { + return adapter; + } + } diff --git a/app/src/main/java/eu/kanade/mangafeed/view/LibraryView.java b/app/src/main/java/eu/kanade/mangafeed/view/LibraryView.java index d6a6b5030..64e3cedea 100644 --- a/app/src/main/java/eu/kanade/mangafeed/view/LibraryView.java +++ b/app/src/main/java/eu/kanade/mangafeed/view/LibraryView.java @@ -4,8 +4,10 @@ import java.util.ArrayList; import java.util.List; import eu.kanade.mangafeed.data.models.Manga; +import eu.kanade.mangafeed.ui.adapter.CatalogueArrayAdapter; public interface LibraryView extends BaseView { void setMangas(List mangas); + CatalogueArrayAdapter getAdapter(); }