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 1b21ef79f..829d86dae 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 735023af2..9c3aace89 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 ef97f10fb..87575bd24 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 30c8aa62a..698eaaa22 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 000000000..025da748e --- /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 000000000..f1f33df74 --- /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; + } +}