From 1802dd04e4eca9d22436f83ac13762dcfbc922fe Mon Sep 17 00:00:00 2001 From: inorichi Date: Sat, 17 Oct 2015 21:31:10 +0200 Subject: [PATCH] More changes to MangaDetailActivity and its fragments --- .../data/helpers/DatabaseHelper.java | 5 + .../data/managers/ChapterManager.java | 2 + .../data/managers/ChapterManagerImpl.java | 13 ++ .../presenter/MangaChaptersPresenter.java | 61 ++++++ .../presenter/MangaDetailPresenter.java | 30 +++ .../ui/activity/MangaDetailActivity.java | 88 ++++---- .../ui/fragment/LibraryFragment.java | 26 ++- .../ui/fragment/MangaChaptersFragment.java | 59 +++++ .../res/layout/activity_manga_catalogue.xml | 201 ------------------ .../res/layout/fragment_manga_chapters.xml | 7 + app/src/main/res/menu/chapters.xml | 10 + 11 files changed, 249 insertions(+), 253 deletions(-) delete mode 100644 app/src/main/res/layout/activity_manga_catalogue.xml create mode 100644 app/src/main/res/menu/chapters.xml diff --git a/app/src/main/java/eu/kanade/mangafeed/data/helpers/DatabaseHelper.java b/app/src/main/java/eu/kanade/mangafeed/data/helpers/DatabaseHelper.java index 2cdff7a0b3..9c2eb8c288 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/helpers/DatabaseHelper.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/helpers/DatabaseHelper.java @@ -57,6 +57,11 @@ public class DatabaseHelper implements MangaManager, ChapterManager { return mChapterManager.getChapters(manga); } + @Override + public Observable> getChapters(long manga_id) { + return mChapterManager.getChapters(manga_id); + } + @Override public Observable insertChapter(Chapter chapter) { return mChapterManager.insertChapter(chapter); diff --git a/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManager.java b/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManager.java index cbc1bf718b..2c0f118c04 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManager.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManager.java @@ -15,6 +15,8 @@ public interface ChapterManager { Observable> getChapters(Manga manga); + Observable> getChapters(long manga_id); + Observable insertChapter(Chapter chapter); Observable> insertChapters(List chapters); diff --git a/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManagerImpl.java b/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManagerImpl.java index 1d8da1e9fe..e8ffffc4d4 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManagerImpl.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManagerImpl.java @@ -38,6 +38,19 @@ public class ChapterManagerImpl extends BaseManager implements ChapterManager { return prepareGetChapters(manga).createObservable(); } + @Override + public Observable> getChapters(long manga_id) { + return db.get() + .listOfObjects(Chapter.class) + .withQuery(Query.builder() + .table(ChaptersTable.TABLE) + .where(ChaptersTable.COLUMN_MANGA_ID + "=?") + .whereArgs(manga_id) + .build()) + .prepare() + .createObservable(); + } + @Override public Observable insertChapter(Chapter chapter) { return db.put() diff --git a/app/src/main/java/eu/kanade/mangafeed/presenter/MangaChaptersPresenter.java b/app/src/main/java/eu/kanade/mangafeed/presenter/MangaChaptersPresenter.java index 5e3de9e6db..292a0fa2de 100644 --- a/app/src/main/java/eu/kanade/mangafeed/presenter/MangaChaptersPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/presenter/MangaChaptersPresenter.java @@ -1,6 +1,67 @@ package eu.kanade.mangafeed.presenter; +import javax.inject.Inject; + +import eu.kanade.mangafeed.data.helpers.DatabaseHelper; +import eu.kanade.mangafeed.data.helpers.SourceManager; +import eu.kanade.mangafeed.data.models.Manga; +import eu.kanade.mangafeed.sources.Source; import eu.kanade.mangafeed.ui.fragment.MangaChaptersFragment; +import rx.Subscription; +import rx.android.schedulers.AndroidSchedulers; +import rx.schedulers.Schedulers; public class MangaChaptersPresenter extends BasePresenter { + + @Inject DatabaseHelper db; + @Inject SourceManager sourceManager; + + private Subscription chaptersSubscription; + private Subscription onlineChaptersSubscription; + private boolean doingRequest = false; + + @Override + protected void onTakeView(MangaChaptersFragment view) { + super.onTakeView(view); + + getChapters(view.getMangaId()); + } + + public void refreshChapters(Manga manga) { + if (manga != null && !doingRequest) + getChaptersFromSource(manga); + } + + private void getChapters(long manga_id) { + if (chaptersSubscription != null) + remove(chaptersSubscription); + + chaptersSubscription = db.getChapters(manga_id) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .compose(deliverLatestCache()) + .subscribe(this.split(MangaChaptersFragment::onNextChapters)); + + add(chaptersSubscription); + } + + public void getChaptersFromSource(Manga manga) { + if (onlineChaptersSubscription != null) + remove(onlineChaptersSubscription); + + Source source = sourceManager.get(manga.source); + doingRequest = true; + + onlineChaptersSubscription = source.pullChaptersFromNetwork(manga.url) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .compose(deliverLatestCache()) + .subscribe(this.split((view, chapters) -> { + doingRequest = false; + }), throwable -> { + doingRequest = false; + }); + + add(onlineChaptersSubscription); + } } diff --git a/app/src/main/java/eu/kanade/mangafeed/presenter/MangaDetailPresenter.java b/app/src/main/java/eu/kanade/mangafeed/presenter/MangaDetailPresenter.java index c88bf4e042..5e03cf3f22 100644 --- a/app/src/main/java/eu/kanade/mangafeed/presenter/MangaDetailPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/presenter/MangaDetailPresenter.java @@ -1,7 +1,37 @@ package eu.kanade.mangafeed.presenter; +import javax.inject.Inject; + +import eu.kanade.mangafeed.data.helpers.DatabaseHelper; import eu.kanade.mangafeed.ui.activity.MangaDetailActivity; +import rx.Observable; +import rx.Subscription; +import rx.android.schedulers.AndroidSchedulers; +import rx.schedulers.Schedulers; public class MangaDetailPresenter extends BasePresenter { + @Inject DatabaseHelper db; + + private Subscription mangaSubscription; + + @Override + protected void onTakeView(MangaDetailActivity view) { + super.onTakeView(view); + + if (mangaSubscription == null) + initializeManga(view); + } + + private void initializeManga(MangaDetailActivity view) { + mangaSubscription = db.getManga(view.getMangaId()) + .subscribeOn(Schedulers.io()) + .take(1) + .flatMap(Observable::from) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(view::onMangaNext); + + add(mangaSubscription); + } + } diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/activity/MangaDetailActivity.java b/app/src/main/java/eu/kanade/mangafeed/ui/activity/MangaDetailActivity.java index 4f2260d944..458c766012 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/activity/MangaDetailActivity.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/activity/MangaDetailActivity.java @@ -32,6 +32,7 @@ public class MangaDetailActivity extends BaseActivity { @Bind(R.id.viewpager) ViewPager view_pager; + private MangaDetailAdapter adapter; private long manga_id; private boolean is_online = false; @@ -73,59 +74,70 @@ public class MangaDetailActivity extends BaseActivity { } private void setupViewPager() { - view_pager.setAdapter(new MangaDetailAdapter( + adapter = new MangaDetailAdapter( getSupportFragmentManager(), getActivity(), - manga_id)); + manga_id); + view_pager.setAdapter(adapter); tabs.setupWithViewPager(view_pager); if (!is_online) view_pager.setCurrentItem(MangaDetailAdapter.CHAPTERS_FRAGMENT); } -} - -class MangaDetailAdapter extends FragmentPagerAdapter { - - final int PAGE_COUNT = 2; - private String tab_titles[]; - private Context context; - private long manga_id; - - final static int INFO_FRAGMENT = 0; - final static int CHAPTERS_FRAGMENT = 1; - - public MangaDetailAdapter(FragmentManager fm, Context context, long manga_id) { - super(fm); - this.context = context; - tab_titles = new String[]{ - context.getString(R.string.manga_detail_tab), - context.getString(R.string.manga_chapters_tab) - }; - this.manga_id = manga_id; + public long getMangaId() { + return manga_id; } - @Override - public int getCount() { - return PAGE_COUNT; + public void onMangaNext(Manga manga) { + ((MangaChaptersFragment) adapter.getItem(MangaDetailAdapter.CHAPTERS_FRAGMENT)) + .onMangaNext(manga); } - @Override - public Fragment getItem(int position) { - switch (position) { - case INFO_FRAGMENT: - return MangaInfoFragment.newInstance(manga_id); - case CHAPTERS_FRAGMENT: - return MangaChaptersFragment.newInstance(manga_id); + class MangaDetailAdapter extends FragmentPagerAdapter { - default: return null; + final int PAGE_COUNT = 2; + private String tab_titles[]; + private Context context; + private long manga_id; + + final static int INFO_FRAGMENT = 0; + final static int CHAPTERS_FRAGMENT = 1; + + public MangaDetailAdapter(FragmentManager fm, Context context, long manga_id) { + super(fm); + this.context = context; + tab_titles = new String[]{ + context.getString(R.string.manga_detail_tab), + context.getString(R.string.manga_chapters_tab) + }; + this.manga_id = manga_id; + } + + @Override + public int getCount() { + return PAGE_COUNT; + } + + @Override + public Fragment getItem(int position) { + switch (position) { + case INFO_FRAGMENT: + return MangaInfoFragment.newInstance(manga_id); + case CHAPTERS_FRAGMENT: + return MangaChaptersFragment.newInstance(manga_id); + + default: + return null; + } + } + + @Override + public CharSequence getPageTitle(int position) { + // Generate title based on item position + return tab_titles[position]; } } - @Override - public CharSequence getPageTitle(int position) { - // Generate title based on item position - return tab_titles[position]; - } } 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 c49c9b5a7a..4c072cd1de 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 @@ -14,6 +14,7 @@ import android.widget.GridView; import butterknife.Bind; import butterknife.ButterKnife; +import butterknife.OnItemClick; import eu.kanade.mangafeed.R; import eu.kanade.mangafeed.data.models.Manga; import eu.kanade.mangafeed.presenter.LibraryPresenter; @@ -53,7 +54,7 @@ public class LibraryFragment extends BaseFragment { ButterKnife.bind(this, view); createAdapter(); - setMangaClickListener(); + setMangaLongClickListener(); return view; } @@ -80,7 +81,7 @@ public class LibraryFragment extends BaseFragment { }); } - public void createAdapter() { + private void createAdapter() { adapter = new LibraryAdapter<>(getActivity()); grid.setAdapter(adapter); } @@ -89,11 +90,16 @@ public class LibraryFragment extends BaseFragment { return adapter; } - public void setMangaClickListener() { - grid.setOnItemClickListener( - (parent, view, position, id) -> - onMangaClick(position) + @OnItemClick(R.id.gridView) + protected void onMangaClick(int position) { + Intent intent = MangaDetailActivity.newIntent( + getActivity(), + adapter.getItem(position) ); + getActivity().startActivity(intent); + } + + private void setMangaLongClickListener() { grid.setMultiChoiceModeListener(new GridView.MultiChoiceModeListener() { @Override public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) { @@ -131,12 +137,4 @@ public class LibraryFragment extends BaseFragment { }); } - private void onMangaClick(int position) { - Intent intent = MangaDetailActivity.newIntent( - getActivity(), - adapter.getItem(position) - ); - getActivity().startActivity(intent); - } - } diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/MangaChaptersFragment.java b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/MangaChaptersFragment.java index 5757b32b26..233174f7d9 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/MangaChaptersFragment.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/MangaChaptersFragment.java @@ -2,20 +2,37 @@ package eu.kanade.mangafeed.ui.fragment; import android.os.Bundle; import android.support.v4.app.Fragment; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; +import java.util.List; + +import butterknife.Bind; import butterknife.ButterKnife; import eu.kanade.mangafeed.R; +import eu.kanade.mangafeed.data.models.Chapter; +import eu.kanade.mangafeed.data.models.Manga; import eu.kanade.mangafeed.presenter.MangaChaptersPresenter; import eu.kanade.mangafeed.ui.activity.MangaDetailActivity; +import eu.kanade.mangafeed.ui.adapter.ChapterListHolder; import nucleus.factory.RequiresPresenter; +import timber.log.Timber; +import uk.co.ribot.easyadapter.EasyRecyclerAdapter; @RequiresPresenter(MangaChaptersPresenter.class) public class MangaChaptersFragment extends BaseFragment { + @Bind(R.id.chapter_list) RecyclerView chapters; + private long manga_id; + private Manga manga; + private EasyRecyclerAdapter adapter; public static Fragment newInstance(long manga_id) { MangaChaptersFragment fragment = new MangaChaptersFragment(); @@ -28,6 +45,8 @@ public class MangaChaptersFragment extends BaseFragment @Override public void onCreate(Bundle savedState) { super.onCreate(savedState); + setHasOptionsMenu(true); + manga_id = getArguments().getLong(MangaDetailActivity.MANGA_ID); } @@ -38,6 +57,46 @@ public class MangaChaptersFragment extends BaseFragment View view = inflater.inflate(R.layout.fragment_manga_chapters, container, false); ButterKnife.bind(this, view); + chapters.setLayoutManager(new LinearLayoutManager(getActivity())); + createAdapter(); + return view; } + + @Override + public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { + inflater.inflate(R.menu.chapters, menu); + super.onCreateOptionsMenu(menu, inflater); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case R.id.action_refresh: + getPresenter().refreshChapters(manga); + break; + } + return super.onOptionsItemSelected(item); + } + + private void createAdapter() { + adapter = new EasyRecyclerAdapter<>(getActivity(), ChapterListHolder.class); + chapters.setAdapter(adapter); + } + + public long getMangaId() { + return manga_id; + } + + public Manga getManga() { + return manga; + } + + public void onNextChapters(List chapters) { + adapter.setItems(chapters); + } + + public void onMangaNext(Manga manga) { + this.manga = manga; + } } diff --git a/app/src/main/res/layout/activity_manga_catalogue.xml b/app/src/main/res/layout/activity_manga_catalogue.xml deleted file mode 100644 index 4d6b5dfc88..0000000000 --- a/app/src/main/res/layout/activity_manga_catalogue.xml +++ /dev/null @@ -1,201 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_manga_chapters.xml b/app/src/main/res/layout/fragment_manga_chapters.xml index 3509b84116..273ad35e61 100644 --- a/app/src/main/res/layout/fragment_manga_chapters.xml +++ b/app/src/main/res/layout/fragment_manga_chapters.xml @@ -3,4 +3,11 @@ android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> + + + + \ No newline at end of file diff --git a/app/src/main/res/menu/chapters.xml b/app/src/main/res/menu/chapters.xml new file mode 100644 index 0000000000..5acf05f040 --- /dev/null +++ b/app/src/main/res/menu/chapters.xml @@ -0,0 +1,10 @@ + + +