|
@@ -1,69 +1,76 @@
|
|
|
package eu.kanade.mangafeed.ui.library;
|
|
|
|
|
|
-import android.support.annotation.Nullable;
|
|
|
-import android.support.v4.app.Fragment;
|
|
|
-import android.support.v4.app.FragmentManager;
|
|
|
+import android.view.View;
|
|
|
+import android.view.ViewGroup;
|
|
|
+import android.widget.Filter;
|
|
|
+import android.widget.Filterable;
|
|
|
|
|
|
import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
|
|
|
-import eu.kanade.mangafeed.data.database.models.Category;
|
|
|
import eu.kanade.mangafeed.data.database.models.Manga;
|
|
|
-import eu.kanade.mangafeed.ui.reader.viewer.common.SmartFragmentStatePagerAdapter;
|
|
|
+import eu.kanade.mangafeed.ui.main.MainActivity;
|
|
|
+import rx.Observable;
|
|
|
+import uk.co.ribot.easyadapter.EasyAdapter;
|
|
|
|
|
|
-class LibraryCategoryAdapter extends SmartFragmentStatePagerAdapter {
|
|
|
+public class LibraryCategoryAdapter extends EasyAdapter<Manga> implements Filterable {
|
|
|
|
|
|
- private LibraryFragment fragment;
|
|
|
- private List<Category> categories;
|
|
|
- private Map<Integer, List<Manga>> mangas;
|
|
|
+ List<Manga> mangas;
|
|
|
+ Filter filter;
|
|
|
+ private LibraryPresenter presenter;
|
|
|
|
|
|
- public LibraryCategoryAdapter(LibraryFragment fragment, FragmentManager fm) {
|
|
|
- super(fm);
|
|
|
- this.fragment = fragment;
|
|
|
+ public LibraryCategoryAdapter(MainActivity activity) {
|
|
|
+ super(activity, LibraryHolder.class);
|
|
|
+ filter = new LibraryFilter();
|
|
|
+ presenter = ((LibraryFragment) activity.getActiveFragment()).getPresenter();
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public Fragment getItem(int position) {
|
|
|
- Category category = categories.get(position);
|
|
|
- return LibraryCategoryFragment.newInstance(fragment, category,
|
|
|
- mangas != null ? mangas.get(category.id) : null);
|
|
|
+ public void setNewItems(List<Manga> list) {
|
|
|
+ super.setItems(list);
|
|
|
+ mangas = list;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public int getCount() {
|
|
|
- return categories == null ? 0 : categories.size();
|
|
|
+ public Filter getFilter() {
|
|
|
+ return filter;
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public CharSequence getPageTitle(int position) {
|
|
|
- return categories.get(position).name;
|
|
|
- }
|
|
|
-
|
|
|
- public void setCategories(List<Category> categories) {
|
|
|
- this.categories = categories;
|
|
|
- notifyDataSetChanged();
|
|
|
- }
|
|
|
+ private class LibraryFilter extends Filter {
|
|
|
+ @Override
|
|
|
+ protected FilterResults performFiltering(CharSequence charSequence) {
|
|
|
+ FilterResults results = new FilterResults();
|
|
|
+ String query = charSequence.toString().toLowerCase();
|
|
|
|
|
|
- public void setMangasOnCategories(Map<Integer, List<Manga>> mangas) {
|
|
|
- this.mangas = mangas;
|
|
|
- for (Map.Entry<Integer, List<Manga>> entry : mangas.entrySet()) {
|
|
|
- LibraryCategoryFragment fragment = getFragment(entry.getKey());
|
|
|
- if (fragment != null) {
|
|
|
- fragment.setMangas(entry.getValue());
|
|
|
+ if (query.length() == 0) {
|
|
|
+ results.values = mangas;
|
|
|
+ results.count = mangas.size();
|
|
|
+ } else {
|
|
|
+ List<Manga> 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;
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- @Nullable
|
|
|
- public LibraryCategoryFragment getFragment(int categoryId) {
|
|
|
- if (categories != null) {
|
|
|
- for (int i = 0; i < categories.size(); i++) {
|
|
|
- if (categories.get(i).id == categoryId) {
|
|
|
- return (LibraryCategoryFragment) getRegisteredFragment(i);
|
|
|
- }
|
|
|
- }
|
|
|
+ @Override
|
|
|
+ public void publishResults(CharSequence constraint, FilterResults results) {
|
|
|
+ setItems((List<Manga>) results.values);
|
|
|
}
|
|
|
- return null;
|
|
|
}
|
|
|
|
|
|
-}
|
|
|
+ @Override
|
|
|
+ public View getView(int position, View convertView, ViewGroup parent) {
|
|
|
+ View view = super.getView(position, convertView, parent);
|
|
|
+ LibraryHolder holder = (LibraryHolder) view.getTag();
|
|
|
+ Manga manga = getItem(position);
|
|
|
+ holder.loadCover(manga, presenter.sourceManager.get(manga.source), presenter.coverCache);
|
|
|
+ return view;
|
|
|
+ }
|
|
|
+}
|