Fix wrong manga received through EventBus

This commit is contained in:
inorichi 2015-10-19 23:54:19 +02:00
parent 0c77a7034a
commit 49c69be38e
5 changed files with 60 additions and 33 deletions

View file

@ -39,7 +39,7 @@ public class MangaChaptersPresenter extends BasePresenter<MangaChaptersFragment>
this::getDbChaptersObs, this::getDbChaptersObs,
(view, chapters) -> { (view, chapters) -> {
view.onNextChapters(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<MangaChaptersFragment>
super.onDropView(); super.onDropView();
} }
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().removeStickyEvent(ChapterCountEvent.class);
}
@EventBusHook @EventBusHook
public void onEventMainThread(Manga manga) { public void onEventMainThread(Manga manga) {
if (this.manga == null) { if (this.manga == null) {
this.manga = manga; this.manga = manga;
start(DB_CHAPTERS); start(DB_CHAPTERS);
// Get chapters if it's an online source
if (getView() != null && getView().isOnlineManga()) {
refreshChapters();
}
} }
} }
public void refreshChapters(MangaChaptersFragment view) { public void refreshChapters() {
if (manga != null) { if (getView() != null)
view.setSwipeRefreshing(); getView().setSwipeRefreshing();
start(ONLINE_CHAPTERS);
} start(ONLINE_CHAPTERS);
} }
private Observable<List<Chapter>> getDbChaptersObs() { private Observable<List<Chapter>> getDbChaptersObs() {
@ -89,4 +100,5 @@ public class MangaChaptersPresenter extends BasePresenter<MangaChaptersFragment>
.flatMap(chapters -> db.insertOrRemoveChapters(manga, chapters)) .flatMap(chapters -> db.insertOrRemoveChapters(manga, chapters))
.observeOn(AndroidSchedulers.mainThread()); .observeOn(AndroidSchedulers.mainThread());
} }
} }

View file

@ -1,5 +1,7 @@
package eu.kanade.mangafeed.presenter; package eu.kanade.mangafeed.presenter;
import android.os.Bundle;
import javax.inject.Inject; import javax.inject.Inject;
import de.greenrobot.event.EventBus; 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.data.models.Manga;
import eu.kanade.mangafeed.ui.activity.MangaDetailActivity; import eu.kanade.mangafeed.ui.activity.MangaDetailActivity;
import rx.Observable; import rx.Observable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers; import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers; import rx.schedulers.Schedulers;
@ -15,32 +16,39 @@ public class MangaDetailPresenter extends BasePresenter<MangaDetailActivity> {
@Inject DatabaseHelper db; @Inject DatabaseHelper db;
private Manga manga; private long mangaId;
private Subscription mangaSubscription; private static final int DB_MANGA = 1;
@Override @Override
protected void onTakeView(MangaDetailActivity view) { protected void onCreate(Bundle savedState) {
super.onTakeView(view); super.onCreate(savedState);
if (manga != null)
view.setManga(manga);
getManga(view); restartableLatestCache(DB_MANGA,
this::getDbMangaObservable,
(view, manga) -> {
view.setManga(manga);
EventBus.getDefault().postSticky(manga);
});
} }
private void getManga(MangaDetailActivity view) { @Override
if (mangaSubscription != null) protected void onDestroy() {
return; super.onDestroy();
// Avoid fragments receiving wrong manga
EventBus.getDefault().removeStickyEvent(Manga.class);
}
add(mangaSubscription = db.getManga(view.getMangaId()) private Observable<Manga> getDbMangaObservable() {
return db.getManga(mangaId)
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.take(1) .take(1)
.flatMap(Observable::from) .flatMap(Observable::from)
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread());
.subscribe(manga -> { }
this.manga = manga;
view.setManga(manga); public void queryManga(long mangaId) {
EventBus.getDefault().postSticky(manga); this.mangaId = mangaId;
})); start(DB_MANGA);
} }
} }

View file

@ -10,9 +10,6 @@ import eu.kanade.mangafeed.ui.fragment.MangaInfoFragment;
import eu.kanade.mangafeed.util.EventBusHook; import eu.kanade.mangafeed.util.EventBusHook;
import eu.kanade.mangafeed.util.events.ChapterCountEvent; import eu.kanade.mangafeed.util.events.ChapterCountEvent;
import rx.Observable; import rx.Observable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import timber.log.Timber; import timber.log.Timber;
public class MangaInfoPresenter extends BasePresenter<MangaInfoFragment> { public class MangaInfoPresenter extends BasePresenter<MangaInfoFragment> {
@ -52,10 +49,8 @@ public class MangaInfoPresenter extends BasePresenter<MangaInfoFragment> {
@EventBusHook @EventBusHook
public void onEventMainThread(Manga manga) { public void onEventMainThread(Manga manga) {
if (!manga.equals(this.manga)) { this.manga = manga;
this.manga = manga; start(GET_MANGA);
start(GET_MANGA);
}
} }
@EventBusHook @EventBusHook

View file

@ -56,6 +56,9 @@ public class MangaDetailActivity extends BaseActivity<MangaDetailPresenter> {
is_online = intent.getBooleanExtra(MANGA_ONLINE, false); is_online = intent.getBooleanExtra(MANGA_ONLINE, false);
setupViewPager(); setupViewPager();
if (savedInstanceState == null)
getPresenter().queryManga(manga_id);
} }
@Override @Override
@ -95,6 +98,10 @@ public class MangaDetailActivity extends BaseActivity<MangaDetailPresenter> {
setToolbarTitle(manga.title); setToolbarTitle(manga.title);
} }
public boolean isOnlineManga() {
return is_online;
}
class MangaDetailAdapter extends FragmentPagerAdapter { class MangaDetailAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 2; final int PAGE_COUNT = 2;

View file

@ -19,6 +19,7 @@ import butterknife.ButterKnife;
import eu.kanade.mangafeed.R; import eu.kanade.mangafeed.R;
import eu.kanade.mangafeed.data.models.Chapter; import eu.kanade.mangafeed.data.models.Chapter;
import eu.kanade.mangafeed.presenter.MangaChaptersPresenter; import eu.kanade.mangafeed.presenter.MangaChaptersPresenter;
import eu.kanade.mangafeed.ui.activity.MangaDetailActivity;
import eu.kanade.mangafeed.ui.adapter.ChapterListHolder; import eu.kanade.mangafeed.ui.adapter.ChapterListHolder;
import nucleus.factory.RequiresPresenter; import nucleus.factory.RequiresPresenter;
import uk.co.ribot.easyadapter.EasyRecyclerAdapter; import uk.co.ribot.easyadapter.EasyRecyclerAdapter;
@ -65,7 +66,7 @@ public class MangaChaptersFragment extends BaseFragment<MangaChaptersPresenter>
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.action_refresh: case R.id.action_refresh:
getPresenter().refreshChapters(this); getPresenter().refreshChapters();
break; break;
} }
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
@ -77,7 +78,7 @@ public class MangaChaptersFragment extends BaseFragment<MangaChaptersPresenter>
} }
private void setSwipeRefreshListener() { private void setSwipeRefreshListener() {
swipeRefresh.setOnRefreshListener(() -> getPresenter().refreshChapters(this)); swipeRefresh.setOnRefreshListener(() -> getPresenter().refreshChapters());
} }
public void onNextChapters(List<Chapter> chapters) { public void onNextChapters(List<Chapter> chapters) {
@ -91,4 +92,8 @@ public class MangaChaptersFragment extends BaseFragment<MangaChaptersPresenter>
public void setSwipeRefreshing() { public void setSwipeRefreshing() {
swipeRefresh.setRefreshing(true); swipeRefresh.setRefreshing(true);
} }
public boolean isOnlineManga() {
return ((MangaDetailActivity)getActivity()).isOnlineManga();
}
} }