From 49c69be38ebda313c83708199e02719589a11bed Mon Sep 17 00:00:00 2001 From: inorichi Date: Mon, 19 Oct 2015 23:54:19 +0200 Subject: [PATCH] Fix wrong manga received through EventBus --- .../presenter/MangaChaptersPresenter.java | 24 +++++++--- .../presenter/MangaDetailPresenter.java | 44 +++++++++++-------- .../presenter/MangaInfoPresenter.java | 9 +--- .../ui/activity/MangaDetailActivity.java | 7 +++ .../ui/fragment/MangaChaptersFragment.java | 9 +++- 5 files changed, 60 insertions(+), 33 deletions(-) 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 829d86dae..833d212ef 100644 --- a/app/src/main/java/eu/kanade/mangafeed/presenter/MangaChaptersPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/presenter/MangaChaptersPresenter.java @@ -39,7 +39,7 @@ public class MangaChaptersPresenter extends BasePresenter this::getDbChaptersObs, (view, chapters) -> { view.onNextChapters(chapters); - EventBus.getDefault().postSticky(new ChapterCountEvent(chapters.size())); + EventBus.getDefault().postSticky( new ChapterCountEvent(chapters.size()) ); } ); @@ -61,19 +61,30 @@ public class MangaChaptersPresenter extends BasePresenter super.onDropView(); } + @Override + protected void onDestroy() { + super.onDestroy(); + EventBus.getDefault().removeStickyEvent(ChapterCountEvent.class); + } + @EventBusHook public void onEventMainThread(Manga manga) { if (this.manga == null) { this.manga = manga; start(DB_CHAPTERS); + + // Get chapters if it's an online source + if (getView() != null && getView().isOnlineManga()) { + refreshChapters(); + } } } - public void refreshChapters(MangaChaptersFragment view) { - if (manga != null) { - view.setSwipeRefreshing(); - start(ONLINE_CHAPTERS); - } + public void refreshChapters() { + if (getView() != null) + getView().setSwipeRefreshing(); + + start(ONLINE_CHAPTERS); } private Observable> getDbChaptersObs() { @@ -89,4 +100,5 @@ public class MangaChaptersPresenter extends BasePresenter .flatMap(chapters -> db.insertOrRemoveChapters(manga, chapters)) .observeOn(AndroidSchedulers.mainThread()); } + } 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 b2dd690ef..204d07029 100644 --- a/app/src/main/java/eu/kanade/mangafeed/presenter/MangaDetailPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/presenter/MangaDetailPresenter.java @@ -1,5 +1,7 @@ package eu.kanade.mangafeed.presenter; +import android.os.Bundle; + import javax.inject.Inject; import de.greenrobot.event.EventBus; @@ -7,7 +9,6 @@ import eu.kanade.mangafeed.data.helpers.DatabaseHelper; import eu.kanade.mangafeed.data.models.Manga; import eu.kanade.mangafeed.ui.activity.MangaDetailActivity; import rx.Observable; -import rx.Subscription; import rx.android.schedulers.AndroidSchedulers; import rx.schedulers.Schedulers; @@ -15,32 +16,39 @@ public class MangaDetailPresenter extends BasePresenter { @Inject DatabaseHelper db; - private Manga manga; - private Subscription mangaSubscription; + private long mangaId; + private static final int DB_MANGA = 1; @Override - protected void onTakeView(MangaDetailActivity view) { - super.onTakeView(view); - if (manga != null) - view.setManga(manga); + protected void onCreate(Bundle savedState) { + super.onCreate(savedState); - getManga(view); + restartableLatestCache(DB_MANGA, + this::getDbMangaObservable, + (view, manga) -> { + view.setManga(manga); + EventBus.getDefault().postSticky(manga); + }); } - private void getManga(MangaDetailActivity view) { - if (mangaSubscription != null) - return; + @Override + protected void onDestroy() { + super.onDestroy(); + // Avoid fragments receiving wrong manga + EventBus.getDefault().removeStickyEvent(Manga.class); + } - add(mangaSubscription = db.getManga(view.getMangaId()) + private Observable getDbMangaObservable() { + return db.getManga(mangaId) .subscribeOn(Schedulers.io()) .take(1) .flatMap(Observable::from) - .observeOn(AndroidSchedulers.mainThread()) - .subscribe(manga -> { - this.manga = manga; - view.setManga(manga); - EventBus.getDefault().postSticky(manga); - })); + .observeOn(AndroidSchedulers.mainThread()); + } + + public void queryManga(long mangaId) { + this.mangaId = mangaId; + start(DB_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 9c3aace89..d960f21de 100644 --- a/app/src/main/java/eu/kanade/mangafeed/presenter/MangaInfoPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/presenter/MangaInfoPresenter.java @@ -10,9 +10,6 @@ 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 { @@ -52,10 +49,8 @@ public class MangaInfoPresenter extends BasePresenter { @EventBusHook public void onEventMainThread(Manga manga) { - if (!manga.equals(this.manga)) { - this.manga = manga; - start(GET_MANGA); - } + this.manga = manga; + start(GET_MANGA); } @EventBusHook 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 48551794a..64e1b43f9 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 @@ -56,6 +56,9 @@ public class MangaDetailActivity extends BaseActivity { is_online = intent.getBooleanExtra(MANGA_ONLINE, false); setupViewPager(); + + if (savedInstanceState == null) + getPresenter().queryManga(manga_id); } @Override @@ -95,6 +98,10 @@ public class MangaDetailActivity extends BaseActivity { setToolbarTitle(manga.title); } + public boolean isOnlineManga() { + return is_online; + } + class MangaDetailAdapter extends FragmentPagerAdapter { final int PAGE_COUNT = 2; 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 87575bd24..161948c24 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 @@ -19,6 +19,7 @@ import butterknife.ButterKnife; import eu.kanade.mangafeed.R; import eu.kanade.mangafeed.data.models.Chapter; 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 uk.co.ribot.easyadapter.EasyRecyclerAdapter; @@ -65,7 +66,7 @@ public class MangaChaptersFragment extends BaseFragment public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_refresh: - getPresenter().refreshChapters(this); + getPresenter().refreshChapters(); break; } return super.onOptionsItemSelected(item); @@ -77,7 +78,7 @@ public class MangaChaptersFragment extends BaseFragment } private void setSwipeRefreshListener() { - swipeRefresh.setOnRefreshListener(() -> getPresenter().refreshChapters(this)); + swipeRefresh.setOnRefreshListener(() -> getPresenter().refreshChapters()); } public void onNextChapters(List chapters) { @@ -91,4 +92,8 @@ public class MangaChaptersFragment extends BaseFragment public void setSwipeRefreshing() { swipeRefresh.setRefreshing(true); } + + public boolean isOnlineManga() { + return ((MangaDetailActivity)getActivity()).isOnlineManga(); + } }