From 0c77a7034ad69f8551925dbb8f97e44956c3e14d Mon Sep 17 00:00:00 2001 From: inorichi Date: Mon, 19 Oct 2015 17:27:41 +0200 Subject: [PATCH] Show chapter count --- .../presenter/MangaChaptersPresenter.java | 9 +++- .../presenter/MangaInfoPresenter.java | 46 ++++++++++++++----- .../ui/fragment/MangaChaptersFragment.java | 8 ++-- .../ui/fragment/MangaInfoFragment.java | 5 +- .../kanade/mangafeed/util/EventBusHook.java | 7 +++ .../util/events/ChapterCountEvent.java | 13 ++++++ 6 files changed, 71 insertions(+), 17 deletions(-) create mode 100644 app/src/main/java/eu/kanade/mangafeed/util/EventBusHook.java create mode 100644 app/src/main/java/eu/kanade/mangafeed/util/events/ChapterCountEvent.java 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 1b21ef79fd..829d86dae7 100644 --- a/app/src/main/java/eu/kanade/mangafeed/presenter/MangaChaptersPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/presenter/MangaChaptersPresenter.java @@ -8,11 +8,14 @@ import java.util.List; import javax.inject.Inject; +import de.greenrobot.event.EventBus; import eu.kanade.mangafeed.data.helpers.DatabaseHelper; import eu.kanade.mangafeed.data.helpers.SourceManager; import eu.kanade.mangafeed.data.models.Chapter; import eu.kanade.mangafeed.data.models.Manga; import eu.kanade.mangafeed.ui.fragment.MangaChaptersFragment; +import eu.kanade.mangafeed.util.EventBusHook; +import eu.kanade.mangafeed.util.events.ChapterCountEvent; import rx.Observable; import rx.android.schedulers.AndroidSchedulers; import rx.schedulers.Schedulers; @@ -34,7 +37,10 @@ public class MangaChaptersPresenter extends BasePresenter restartableLatestCache(DB_CHAPTERS, this::getDbChaptersObs, - MangaChaptersFragment::onNextChapters + (view, chapters) -> { + view.onNextChapters(chapters); + EventBus.getDefault().postSticky(new ChapterCountEvent(chapters.size())); + } ); restartableLatestCache(ONLINE_CHAPTERS, @@ -55,6 +61,7 @@ public class MangaChaptersPresenter extends BasePresenter super.onDropView(); } + @EventBusHook public void onEventMainThread(Manga manga) { if (this.manga == null) { this.manga = manga; diff --git a/app/src/main/java/eu/kanade/mangafeed/presenter/MangaInfoPresenter.java b/app/src/main/java/eu/kanade/mangafeed/presenter/MangaInfoPresenter.java index 735023af24..9c3aace89b 100644 --- a/app/src/main/java/eu/kanade/mangafeed/presenter/MangaInfoPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/presenter/MangaInfoPresenter.java @@ -1,20 +1,42 @@ package eu.kanade.mangafeed.presenter; +import android.os.Bundle; + import javax.inject.Inject; import eu.kanade.mangafeed.data.helpers.DatabaseHelper; import eu.kanade.mangafeed.data.models.Manga; import eu.kanade.mangafeed.ui.fragment.MangaInfoFragment; +import eu.kanade.mangafeed.util.EventBusHook; +import eu.kanade.mangafeed.util.events.ChapterCountEvent; import rx.Observable; import rx.Subscription; import rx.android.schedulers.AndroidSchedulers; +import rx.schedulers.Schedulers; +import timber.log.Timber; public class MangaInfoPresenter extends BasePresenter { @Inject DatabaseHelper db; private Manga manga; - private Subscription mangaInfoSubscription; + private int count = -1; + + private static final int GET_MANGA = 1; + private static final int GET_CHAPTER_COUNT = 2; + + @Override + protected void onCreate(Bundle savedState) { + super.onCreate(savedState); + + restartableLatestCache(GET_MANGA, + () -> Observable.just(manga), + MangaInfoFragment::setMangaInfo); + + restartableLatestCache(GET_CHAPTER_COUNT, + () -> Observable.just(count), + MangaInfoFragment::setChapterCount); + } @Override protected void onTakeView(MangaInfoFragment view) { @@ -28,18 +50,20 @@ public class MangaInfoPresenter extends BasePresenter { super.onDropView(); } + @EventBusHook public void onEventMainThread(Manga manga) { - this.manga = manga; - getMangaInfo(); + if (!manga.equals(this.manga)) { + this.manga = manga; + start(GET_MANGA); + } } - private void getMangaInfo() { - if (mangaInfoSubscription != null) - remove(mangaInfoSubscription); - - add(mangaInfoSubscription = Observable.just(manga) - .observeOn(AndroidSchedulers.mainThread()) - .compose(deliverLatestCache()) - .subscribe(split(MangaInfoFragment::setMangaInfo))); + @EventBusHook + public void onEventMainThread(ChapterCountEvent event) { + if (count != event.getCount()) { + count = event.getCount(); + start(GET_CHAPTER_COUNT); + } } + } 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 ef97f10fb1..87575bd248 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 @@ -27,7 +27,7 @@ import uk.co.ribot.easyadapter.EasyRecyclerAdapter; public class MangaChaptersFragment extends BaseFragment { @Bind(R.id.chapter_list) RecyclerView chapters; - @Bind(R.id.swipe_refresh) SwipeRefreshLayout swipe_refresh; + @Bind(R.id.swipe_refresh) SwipeRefreshLayout swipeRefresh; private EasyRecyclerAdapter adapter; @@ -77,7 +77,7 @@ public class MangaChaptersFragment extends BaseFragment } private void setSwipeRefreshListener() { - swipe_refresh.setOnRefreshListener(() -> getPresenter().refreshChapters(this)); + swipeRefresh.setOnRefreshListener(() -> getPresenter().refreshChapters(this)); } public void onNextChapters(List chapters) { @@ -85,10 +85,10 @@ public class MangaChaptersFragment extends BaseFragment } public void onNextOnlineChapters() { - swipe_refresh.setRefreshing(false); + swipeRefresh.setRefreshing(false); } public void setSwipeRefreshing() { - swipe_refresh.setRefreshing(true); + swipeRefresh.setRefreshing(true); } } diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/MangaInfoFragment.java b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/MangaInfoFragment.java index 30c8aa62aa..698eaaa223 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/MangaInfoFragment.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/MangaInfoFragment.java @@ -50,7 +50,6 @@ public class MangaInfoFragment extends BaseFragment { public void setMangaInfo(Manga manga) { mArtist.setText(manga.artist); mAuthor.setText(manga.author); - mChapters.setText("0"); // TODO mGenres.setText(manga.genre); mStatus.setText("Ongoing"); //TODO mDescription.setText(manga.description); @@ -61,4 +60,8 @@ public class MangaInfoFragment extends BaseFragment { .centerCrop() .into(mCover); } + + public void setChapterCount(int count) { + mChapters.setText(String.valueOf(count)); + } } diff --git a/app/src/main/java/eu/kanade/mangafeed/util/EventBusHook.java b/app/src/main/java/eu/kanade/mangafeed/util/EventBusHook.java new file mode 100644 index 0000000000..025da748ea --- /dev/null +++ b/app/src/main/java/eu/kanade/mangafeed/util/EventBusHook.java @@ -0,0 +1,7 @@ +package eu.kanade.mangafeed.util; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Target; + +@Target({ElementType.METHOD}) +public @interface EventBusHook {} \ No newline at end of file diff --git a/app/src/main/java/eu/kanade/mangafeed/util/events/ChapterCountEvent.java b/app/src/main/java/eu/kanade/mangafeed/util/events/ChapterCountEvent.java new file mode 100644 index 0000000000..f1f33df746 --- /dev/null +++ b/app/src/main/java/eu/kanade/mangafeed/util/events/ChapterCountEvent.java @@ -0,0 +1,13 @@ +package eu.kanade.mangafeed.util.events; + +public class ChapterCountEvent { + private int count; + + public ChapterCountEvent(int count) { + this.count = count; + } + + public int getCount() { + return count; + } +}