From 57ba368ae0768347835bc748ffcfd96309c0f92b Mon Sep 17 00:00:00 2001 From: inorichi Date: Fri, 5 Feb 2016 16:24:34 +0100 Subject: [PATCH] Add library search. Closes #64 --- .../ui/library/LibraryCategoryAdapter.java | 61 +++++-------------- .../ui/library/LibraryCategoryFragment.java | 8 +++ .../tachiyomi/ui/library/LibraryFragment.java | 38 +++++++++++- .../ui/library/LibraryPresenter.java | 5 ++ app/src/main/res/menu/library.xml | 4 -- 5 files changed, 63 insertions(+), 53 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryAdapter.java b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryAdapter.java index d4cbfd4ee..aa18afba1 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryAdapter.java +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryAdapter.java @@ -3,8 +3,6 @@ package eu.kanade.tachiyomi.ui.library; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.Filter; -import android.widget.Filterable; import java.util.ArrayList; import java.util.List; @@ -12,28 +10,24 @@ import java.util.List; import eu.davidea.flexibleadapter.FlexibleAdapter; import eu.kanade.tachiyomi.R; import eu.kanade.tachiyomi.data.database.models.Manga; -import rx.Observable; -public class LibraryCategoryAdapter extends FlexibleAdapter - implements Filterable { +public class LibraryCategoryAdapter extends FlexibleAdapter { - List mangas; - Filter filter; + private List mangas; private LibraryCategoryFragment fragment; public LibraryCategoryAdapter(LibraryCategoryFragment fragment) { this.fragment = fragment; mItems = new ArrayList<>(); - filter = new LibraryFilter(); setHasStableIds(true); } public void setItems(List list) { mItems = list; - notifyDataSetChanged(); - // TODO needed for filtering? - mangas = list; + // A copy of manga that it's always unfiltered + mangas = new ArrayList<>(list); + updateDataSet(null); } public void clear() { @@ -47,7 +41,16 @@ public class LibraryCategoryAdapter extends FlexibleAdapter filteredMangas = Observable.from(mangas) - .filter(x -> - (x.title != null && x.title.toLowerCase().contains(query)) || - (x.author != null && x.author.toLowerCase().contains(query)) || - (x.artist != null && 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); - } - } - } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryFragment.java b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryFragment.java index eef465aca..a94b82281 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryFragment.java +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryFragment.java @@ -37,6 +37,7 @@ public class LibraryCategoryFragment extends BaseFragment private List mangas; private Subscription numColumnsSubscription; + private Subscription searchSubscription; public static LibraryCategoryFragment newInstance(int position) { LibraryCategoryFragment fragment = new LibraryCategoryFragment(); @@ -77,12 +78,19 @@ public class LibraryCategoryFragment extends BaseFragment } } + searchSubscription = getLibraryPresenter().searchSubject + .subscribe(text -> { + adapter.setSearchText(text); + adapter.updateDataSet(); + }); + return view; } @Override public void onDestroyView() { numColumnsSubscription.unsubscribe(); + searchSubscription.unsubscribe(); super.onDestroyView(); } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryFragment.java b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryFragment.java index 4a71137f4..b25d56584 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryFragment.java +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryFragment.java @@ -7,6 +7,8 @@ import android.support.design.widget.AppBarLayout; import android.support.design.widget.TabLayout; import android.support.v4.view.ViewPager; import android.support.v7.view.ActionMode; +import android.support.v7.widget.SearchView; +import android.text.TextUtils; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; @@ -48,6 +50,7 @@ public class LibraryFragment extends BaseRxFragment private ActionMode actionMode; @State int activeCategory; + @State String query = ""; public static LibraryFragment newInstance() { return new LibraryFragment(); @@ -60,8 +63,7 @@ public class LibraryFragment extends BaseRxFragment } @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_library, container, false); setToolbarTitle(getString(R.string.label_library)); @@ -75,6 +77,10 @@ public class LibraryFragment extends BaseRxFragment viewPager.setAdapter(adapter); tabs.setupWithViewPager(viewPager); + if (savedState != null) { + getPresenter().searchSubject.onNext(query); + } + return view; } @@ -99,6 +105,29 @@ public class LibraryFragment extends BaseRxFragment @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.library, menu); + + // Initialize search menu + MenuItem searchItem = menu.findItem(R.id.action_search); + final SearchView searchView = (SearchView) searchItem.getActionView(); + + if (!TextUtils.isEmpty(query)) { + searchItem.expandActionView(); + searchView.setQuery(query, true); + searchView.clearFocus(); + } + searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { + @Override + public boolean onQueryTextSubmit(String query) { + onSearchTextChange(query); + return true; + } + + @Override + public boolean onQueryTextChange(String newText) { + onSearchTextChange(newText); + return true; + } + }); } @Override @@ -115,6 +144,11 @@ public class LibraryFragment extends BaseRxFragment return super.onOptionsItemSelected(item); } + private void onSearchTextChange(String query) { + this.query = query; + getPresenter().searchSubject.onNext(query); + } + private void onEditCategories() { Intent intent = CategoryActivity.newIntent(getActivity()); startActivity(intent); diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.java b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.java index b566794fb..71d64f976 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.java +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.java @@ -21,6 +21,7 @@ import eu.kanade.tachiyomi.event.LibraryMangasEvent; import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter; import rx.Observable; import rx.android.schedulers.AndroidSchedulers; +import rx.subjects.BehaviorSubject; public class LibraryPresenter extends BasePresenter { @@ -32,6 +33,8 @@ public class LibraryPresenter extends BasePresenter { protected List categories; protected List selectedMangas; + protected BehaviorSubject searchSubject; + private static final int GET_LIBRARY = 1; @Override @@ -40,6 +43,8 @@ public class LibraryPresenter extends BasePresenter { selectedMangas = new ArrayList<>(); + searchSubject = BehaviorSubject.create(); + restartableLatestCache(GET_LIBRARY, this::getLibraryObservable, (view, pair) -> view.onNextLibraryUpdate(pair.first, pair.second)); diff --git a/app/src/main/res/menu/library.xml b/app/src/main/res/menu/library.xml index be2551849..c3fe24a3f 100644 --- a/app/src/main/res/menu/library.xml +++ b/app/src/main/res/menu/library.xml @@ -2,21 +2,17 @@ xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity"> -