Categories improvements

This commit is contained in:
inorichi 2015-12-26 00:07:45 +01:00
parent e3bc13630d
commit 4900284c48
14 changed files with 208 additions and 211 deletions

View file

@ -0,0 +1,19 @@
package eu.kanade.mangafeed.event;
import java.util.List;
import java.util.Map;
import eu.kanade.mangafeed.data.database.models.Manga;
public class LibraryMangasEvent {
private final Map<Integer, List<Manga>> mangas;
public LibraryMangasEvent(Map<Integer, List<Manga>> mangas) {
this.mangas = mangas;
}
public Map<Integer, List<Manga>> getMangas() {
return mangas;
}
}

View file

@ -1,7 +1,6 @@
package eu.kanade.mangafeed.ui.base.activity;
import android.content.Context;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
@ -36,11 +35,6 @@ public class BaseActivity extends AppCompatActivity {
getSupportActionBar().setSubtitle(getString(titleResource));
}
public void setToolbarElevation(int elevation) {
if (getSupportActionBar() != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
getSupportActionBar().setElevation(elevation);
}
public Context getActivity() {
return this;
}

View file

@ -1,75 +1,40 @@
package eu.kanade.mangafeed.ui.library;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import java.util.List;
import eu.kanade.mangafeed.data.database.models.Manga;
import rx.Observable;
import uk.co.ribot.easyadapter.EasyAdapter;
import eu.kanade.mangafeed.data.database.models.Category;
public class LibraryAdapter extends EasyAdapter<Manga> implements Filterable {
class LibraryAdapter extends FragmentStatePagerAdapter {
List<Manga> mangas;
Filter filter;
private LibraryPresenter presenter;
private List<Category> categories;
public LibraryAdapter(LibraryFragment fragment) {
super(fragment.getActivity(), LibraryHolder.class);
filter = new LibraryFilter();
presenter = fragment.getPresenter();
}
public void setNewItems(List<Manga> list) {
super.setItems(list);
mangas = list;
public LibraryAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Filter getFilter() {
return filter;
}
private class LibraryFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
FilterResults results = new FilterResults();
String query = charSequence.toString().toLowerCase();
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;
public Fragment getItem(int position) {
Category category = categories.get(position);
return LibraryCategoryFragment.newInstance(category);
}
@Override
public void publishResults(CharSequence constraint, FilterResults results) {
setItems((List<Manga>) results.values);
}
public int getCount() {
return categories == null ? 0 : categories.size();
}
@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;
public CharSequence getPageTitle(int position) {
return categories.get(position).name;
}
public void setCategories(List<Category> categories) {
this.categories = categories;
notifyDataSetChanged();
}
}

View file

@ -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();
}
public void setNewItems(List<Manga> list) {
super.setItems(list);
mangas = list;
}
@Override
public Fragment getItem(int position) {
Category category = categories.get(position);
return LibraryCategoryFragment.newInstance(fragment, category,
mangas != null ? mangas.get(category.id) : null);
public Filter getFilter() {
return filter;
}
private class LibraryFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
FilterResults results = new FilterResults();
String query = charSequence.toString().toLowerCase();
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;
}
@Override
public int getCount() {
return categories == null ? 0 : categories.size();
public void publishResults(CharSequence constraint, FilterResults results) {
setItems((List<Manga>) results.values);
}
}
@Override
public CharSequence getPageTitle(int position) {
return categories.get(position).name;
}
public void setCategories(List<Category> categories) {
this.categories = categories;
notifyDataSetChanged();
}
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());
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;
}
}
}
@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);
}
}
}
return null;
}
}

View file

@ -2,7 +2,6 @@ package eu.kanade.mangafeed.ui.library;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -16,46 +15,61 @@ import butterknife.OnItemClick;
import eu.kanade.mangafeed.R;
import eu.kanade.mangafeed.data.database.models.Category;
import eu.kanade.mangafeed.data.database.models.Manga;
import eu.kanade.mangafeed.event.LibraryMangasEvent;
import eu.kanade.mangafeed.ui.base.fragment.BaseFragment;
import eu.kanade.mangafeed.ui.main.MainActivity;
import eu.kanade.mangafeed.ui.manga.MangaActivity;
import eu.kanade.mangafeed.util.EventBusHook;
import icepick.Icepick;
import icepick.State;
public class LibraryCategoryFragment extends BaseFragment {
@Bind(R.id.gridView) GridView grid;
private LibraryFragment parent;
private LibraryAdapter adapter;
private Category category;
private List<Manga> mangas;
protected LibraryCategoryAdapter adapter;
@State Category category;
public static LibraryCategoryFragment newInstance(LibraryFragment parent, Category category,
List<Manga> mangas) {
public static LibraryCategoryFragment newInstance(Category category) {
LibraryCategoryFragment fragment = new LibraryCategoryFragment();
fragment.initialize(parent, category, mangas);
fragment.category = category;
return fragment;
}
private void initialize(LibraryFragment parent, Category category, List<Manga> mangas) {
this.parent = parent;
this.category = category;
this.mangas = mangas;
}
@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_category, container, false);
ButterKnife.bind(this, view);
Icepick.restoreInstanceState(this, savedState);
adapter = new LibraryAdapter(parent);
adapter = new LibraryCategoryAdapter((MainActivity) getActivity());
grid.setAdapter(adapter);
if (mangas != null) {
setMangas(mangas);
return view;
}
return view;
@Override
public void onResume() {
super.onResume();
registerForStickyEvents();
}
@Override
public void onPause() {
unregisterForEvents();
super.onPause();
}
@Override
public void onSaveInstanceState(Bundle outState) {
Icepick.saveInstanceState(this, outState);
super.onSaveInstanceState(outState);
}
@EventBusHook
public void onEventMainThread(LibraryMangasEvent event) {
setMangas(event.getMangas().get(category.id));
}
@OnItemClick(R.id.gridView)
@ -64,12 +78,15 @@ public class LibraryCategoryFragment extends BaseFragment {
getActivity(),
adapter.getItem(position)
);
parent.getPresenter().onOpenManga();
getActivity().startActivity(intent);
}
public void setMangas(List<Manga> mangas) {
if (mangas != null) {
adapter.setNewItems(mangas);
} else {
adapter.getItems().clear();
}
}
}

View file

@ -2,6 +2,7 @@ package eu.kanade.mangafeed.ui.library;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
@ -13,23 +14,24 @@ import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import butterknife.Bind;
import butterknife.ButterKnife;
import eu.kanade.mangafeed.R;
import eu.kanade.mangafeed.data.database.models.Category;
import eu.kanade.mangafeed.data.database.models.Manga;
import eu.kanade.mangafeed.data.sync.LibraryUpdateService;
import eu.kanade.mangafeed.ui.base.fragment.BaseRxFragment;
import eu.kanade.mangafeed.ui.main.MainActivity;
import nucleus.factory.RequiresPresenter;
@RequiresPresenter(LibraryPresenter.class)
public class LibraryFragment extends BaseRxFragment<LibraryPresenter> {
@Bind(R.id.tabs) TabLayout tabs;
TabLayout tabs;
AppBarLayout appBar;
@Bind(R.id.view_pager) ViewPager categoriesPager;
private LibraryCategoryAdapter adapter;
protected LibraryAdapter adapter;
public static LibraryFragment newInstance() {
return new LibraryFragment();
@ -49,12 +51,23 @@ public class LibraryFragment extends BaseRxFragment<LibraryPresenter> {
setToolbarTitle(getString(R.string.label_library));
ButterKnife.bind(this, view);
adapter = new LibraryCategoryAdapter(this, getChildFragmentManager());
appBar = ((MainActivity) getActivity()).getAppBar();
tabs = (TabLayout) inflater.inflate(R.layout.tab_layout, appBar, false);
appBar.addView(tabs);
adapter = new LibraryAdapter(getChildFragmentManager());
categoriesPager.setAdapter(adapter);
return view;
}
@Override
public void onDestroyView() {
appBar.removeView(tabs);
super.onDestroyView();
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.library, menu);
@ -75,14 +88,9 @@ public class LibraryFragment extends BaseRxFragment<LibraryPresenter> {
return super.onOptionsItemSelected(item);
}
public void onNextMangas(Map<Integer, List<Manga>> mangas) {
adapter.setMangasOnCategories(mangas);
}
public void onNextCategories(List<Category> categories) {
List<Category> actualCategories = new ArrayList<>();
// TODO should we always add this?
Category defaultCat = Category.create("Default");
defaultCat.id = 0;
actualCategories.add(defaultCat);
@ -90,6 +98,8 @@ public class LibraryFragment extends BaseRxFragment<LibraryPresenter> {
actualCategories.addAll(categories);
adapter.setCategories(actualCategories);
tabs.setupWithViewPager(categoriesPager);
tabs.setVisibility(actualCategories.size() == 1 ? View.GONE : View.VISIBLE);
}
}

View file

@ -8,15 +8,15 @@ import java.util.Map;
import javax.inject.Inject;
import de.greenrobot.event.EventBus;
import eu.kanade.mangafeed.data.cache.CoverCache;
import eu.kanade.mangafeed.data.database.DatabaseHelper;
import eu.kanade.mangafeed.data.database.models.Category;
import eu.kanade.mangafeed.data.database.models.Manga;
import eu.kanade.mangafeed.data.preference.PreferencesHelper;
import eu.kanade.mangafeed.data.source.SourceManager;
import eu.kanade.mangafeed.event.LibraryMangasEvent;
import eu.kanade.mangafeed.ui.base.presenter.BasePresenter;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
public class LibraryPresenter extends BasePresenter<LibraryFragment> {
@ -27,7 +27,6 @@ public class LibraryPresenter extends BasePresenter<LibraryFragment> {
@Inject SourceManager sourceManager;
private static final int GET_CATEGORIES = 1;
private static final int GET_MANGAS = 2;
@Override
protected void onCreate(Bundle savedState) {
@ -37,20 +36,20 @@ public class LibraryPresenter extends BasePresenter<LibraryFragment> {
() -> db.getCategories().createObservable(),
LibraryFragment::onNextCategories);
restartableLatestCache(GET_MANGAS,
this::getLibraryMangasObservable,
LibraryFragment::onNextMangas);
start(GET_CATEGORIES);
add(getLibraryMangasObservable()
.subscribe(mangas ->
EventBus.getDefault().postSticky(new LibraryMangasEvent(mangas))));
}
@Override
protected void onTakeView(LibraryFragment view) {
super.onTakeView(view);
if (!isSubscribed(GET_MANGAS)) {
start(GET_MANGAS);
}
public Observable<Map<Integer, List<Manga>>> getLibraryMangasObservable() {
return db.getLibraryMangas().createObservable()
.flatMap(mangas -> Observable.from(mangas)
.groupBy(manga -> manga.category)
.flatMap(group -> group.toList()
.map(list -> Pair.create(group.getKey(), list)))
.toMap(pair -> pair.first, pair -> pair.second));
}
public void deleteMangas(Observable<Manga> selectedMangas) {
@ -62,17 +61,5 @@ public class LibraryPresenter extends BasePresenter<LibraryFragment> {
.subscribe());
}
public Observable<Map<Integer, List<Manga>>> getLibraryMangasObservable() {
return db.getLibraryMangas().createObservable()
.flatMap(mangas -> Observable.from(mangas)
.groupBy(manga -> manga.category)
.flatMap(group -> group.toList()
.map(list -> Pair.create(group.getKey(), list)))
.toMap(pair -> pair.first, pair -> pair.second))
.observeOn(AndroidSchedulers.mainThread());
}
public void onOpenManga() {
stop(GET_MANGAS);
}
}

View file

@ -2,6 +2,7 @@ package eu.kanade.mangafeed.ui.main;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.widget.FrameLayout;
@ -22,6 +23,7 @@ import nucleus.view.ViewWithPresenter;
public class MainActivity extends BaseActivity {
@Bind(R.id.appbar) AppBarLayout appBar;
@Bind(R.id.toolbar) Toolbar toolbar;
@Bind(R.id.drawer_container) FrameLayout container;
@ -111,8 +113,16 @@ public class MainActivity extends BaseActivity {
fragmentStack.replace(fragment);
}
public Fragment getActiveFragment() {
return fragmentStack.peek();
}
public Toolbar getToolbar() {
return toolbar;
}
public AppBarLayout getAppBar() {
return appBar;
}
}

View file

@ -52,11 +52,10 @@ public class MangaActivity extends BaseRxActivity<MangaPresenter> {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
App.get(this).getComponent().inject(this);
setContentView(R.layout.activity_manga_detail);
setContentView(R.layout.activity_manga);
ButterKnife.bind(this);
setupToolbar(toolbar);
setToolbarElevation(0);
Intent intent = getIntent();

View file

@ -3,16 +3,22 @@
android:layout_height="match_parent"
android:gravity="center">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppTheme.Dark">
<include layout="@layout/toolbar" />
</android.support.design.widget.AppBarLayout>
<!-- the layout which will contain (host) the drawerLayout -->
<FrameLayout
android:id="@+id/drawer_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/toolbar">
android:layout_below="@id/appbar">
<!-- the layout which will be the content of the activity (which will be hosted inside the drawer (NOT the list of the drawer)) -->
<FrameLayout

View file

@ -1,5 +1,4 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
@ -14,14 +13,7 @@
<include layout="@layout/toolbar" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppTheme.Dark"
app:tabGravity="fill"
android:background="@color/colorPrimary"
app:tabIndicatorColor="@color/white" />
<include layout="@layout/tab_layout" />
</android.support.design.widget.AppBarLayout>

View file

@ -3,9 +3,7 @@
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar"/>
<include layout="@layout/toolbar"/>
<FrameLayout
android:id="@+id/settings_content"

View file

@ -1,27 +1,8 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppTheme.Dark">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppTheme.Dark"
app:tabGravity="fill"
android:background="@color/colorPrimary"
app:tabIndicatorColor="@color/white" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"

View file

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.TabLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppTheme.Dark"
app:tabGravity="fill"
android:background="@color/colorPrimary"
android:elevation="4dp"
app:tabIndicatorColor="@color/white"/>