diff --git a/app/src/main/java/eu/kanade/mangafeed/event/LibraryMangasEvent.java b/app/src/main/java/eu/kanade/mangafeed/event/LibraryMangasEvent.java new file mode 100644 index 000000000..329b6a764 --- /dev/null +++ b/app/src/main/java/eu/kanade/mangafeed/event/LibraryMangasEvent.java @@ -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> mangas; + + public LibraryMangasEvent(Map> mangas) { + this.mangas = mangas; + } + + public Map> getMangas() { + return mangas; + } +} diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/base/activity/BaseActivity.java b/app/src/main/java/eu/kanade/mangafeed/ui/base/activity/BaseActivity.java index 8b86f1e65..b67399a92 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/base/activity/BaseActivity.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/base/activity/BaseActivity.java @@ -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; } diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryAdapter.java b/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryAdapter.java index 75ff027a2..c5ca4f7f3 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryAdapter.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryAdapter.java @@ -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 implements Filterable { +class LibraryAdapter extends FragmentStatePagerAdapter { - List mangas; - Filter filter; - private LibraryPresenter presenter; + private List categories; - public LibraryAdapter(LibraryFragment fragment) { - super(fragment.getActivity(), LibraryHolder.class); - filter = new LibraryFilter(); - presenter = fragment.getPresenter(); - } - - public void setNewItems(List 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 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); - } + public Fragment getItem(int position) { + Category category = categories.get(position); + return LibraryCategoryFragment.newInstance(category); } @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 int getCount() { + return categories == null ? 0 : categories.size(); } -} + + @Override + public CharSequence getPageTitle(int position) { + return categories.get(position).name; + } + + public void setCategories(List categories) { + this.categories = categories; + notifyDataSetChanged(); + } + +} \ No newline at end of file diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryCategoryAdapter.java b/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryCategoryAdapter.java index c2227bcb3..0d109846d 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryCategoryAdapter.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryCategoryAdapter.java @@ -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 implements Filterable { - private LibraryFragment fragment; - private List categories; - private Map> mangas; + List 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 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; } - @Override - public int getCount() { - return categories == null ? 0 : categories.size(); - } + private class LibraryFilter extends Filter { + @Override + protected FilterResults performFiltering(CharSequence charSequence) { + FilterResults results = new FilterResults(); + String query = charSequence.toString().toLowerCase(); - @Override - public CharSequence getPageTitle(int position) { - return categories.get(position).name; - } - - public void setCategories(List categories) { - this.categories = categories; - notifyDataSetChanged(); - } - - public void setMangasOnCategories(Map> mangas) { - this.mangas = mangas; - for (Map.Entry> 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 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); } } - @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; + @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; } - -} \ No newline at end of file +} diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryCategoryFragment.java b/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryCategoryFragment.java index 59fd7dd00..207a5ce5c 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryCategoryFragment.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryCategoryFragment.java @@ -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,60 +15,78 @@ 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 mangas; + protected LibraryCategoryAdapter adapter; + @State Category category; - public static LibraryCategoryFragment newInstance(LibraryFragment parent, Category category, - List 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 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; } + @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) protected void onMangaClick(int position) { Intent intent = MangaActivity.newIntent( getActivity(), adapter.getItem(position) ); - parent.getPresenter().onOpenManga(); getActivity().startActivity(intent); } public void setMangas(List mangas) { - adapter.setNewItems(mangas); + if (mangas != null) { + adapter.setNewItems(mangas); + } else { + adapter.getItems().clear(); + } } } diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryFragment.java b/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryFragment.java index 6b3cd743b..198a0be63 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryFragment.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryFragment.java @@ -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 { - @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 { 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 { return super.onOptionsItemSelected(item); } - public void onNextMangas(Map> mangas) { - adapter.setMangasOnCategories(mangas); - } - public void onNextCategories(List categories) { List 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 { actualCategories.addAll(categories); adapter.setCategories(actualCategories); tabs.setupWithViewPager(categoriesPager); + + tabs.setVisibility(actualCategories.size() == 1 ? View.GONE : View.VISIBLE); } } diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryPresenter.java b/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryPresenter.java index 7336920bb..8fef13b56 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryPresenter.java @@ -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 { @@ -27,7 +27,6 @@ public class LibraryPresenter extends BasePresenter { @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 { () -> 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>> 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 selectedMangas) { @@ -62,17 +61,5 @@ public class LibraryPresenter extends BasePresenter { .subscribe()); } - public Observable>> 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); - } } diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/main/MainActivity.java b/app/src/main/java/eu/kanade/mangafeed/ui/main/MainActivity.java index 195608ff9..7fe225271 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/main/MainActivity.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/main/MainActivity.java @@ -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; + } + } diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/manga/MangaActivity.java b/app/src/main/java/eu/kanade/mangafeed/ui/manga/MangaActivity.java index ca911e811..e37007b29 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/manga/MangaActivity.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/manga/MangaActivity.java @@ -52,11 +52,10 @@ public class MangaActivity extends BaseRxActivity { 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(); diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 01c0453d6..c0cddd69b 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -3,16 +3,22 @@ android:layout_height="match_parent" android:gravity="center"> - + + + + + + android:layout_below="@id/appbar"> - + diff --git a/app/src/main/res/layout/activity_preferences.xml b/app/src/main/res/layout/activity_preferences.xml index 55943d6e8..b44b7a9ba 100644 --- a/app/src/main/res/layout/activity_preferences.xml +++ b/app/src/main/res/layout/activity_preferences.xml @@ -3,9 +3,7 @@ android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> - + - - - - - - + \ No newline at end of file