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

@ -61,20 +61,31 @@ public class MangaChaptersPresenter extends BasePresenter<MangaChaptersFragment>
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();
public void refreshChapters() {
if (getView() != null)
getView().setSwipeRefreshing();
start(ONLINE_CHAPTERS);
}
}
private Observable<List<Chapter>> getDbChaptersObs() {
return db.getChapters(manga.id)
@ -89,4 +100,5 @@ public class MangaChaptersPresenter extends BasePresenter<MangaChaptersFragment>
.flatMap(chapters -> db.insertOrRemoveChapters(manga, chapters))
.observeOn(AndroidSchedulers.mainThread());
}
}

View File

@ -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<MangaDetailActivity> {
@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<Manga> 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);
}
}

View File

@ -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<MangaInfoFragment> {
@ -52,11 +49,9 @@ public class MangaInfoPresenter extends BasePresenter<MangaInfoFragment> {
@EventBusHook
public void onEventMainThread(Manga manga) {
if (!manga.equals(this.manga)) {
this.manga = manga;
start(GET_MANGA);
}
}
@EventBusHook
public void onEventMainThread(ChapterCountEvent event) {

View File

@ -56,6 +56,9 @@ public class MangaDetailActivity extends BaseActivity<MangaDetailPresenter> {
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<MangaDetailPresenter> {
setToolbarTitle(manga.title);
}
public boolean isOnlineManga() {
return is_online;
}
class MangaDetailAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 2;

View File

@ -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<MangaChaptersPresenter>
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<MangaChaptersPresenter>
}
private void setSwipeRefreshListener() {
swipeRefresh.setOnRefreshListener(() -> getPresenter().refreshChapters(this));
swipeRefresh.setOnRefreshListener(() -> getPresenter().refreshChapters());
}
public void onNextChapters(List<Chapter> chapters) {
@ -91,4 +92,8 @@ public class MangaChaptersFragment extends BaseFragment<MangaChaptersPresenter>
public void setSwipeRefreshing() {
swipeRefresh.setRefreshing(true);
}
public boolean isOnlineManga() {
return ((MangaDetailActivity)getActivity()).isOnlineManga();
}
}