Use EventBus

This commit is contained in:
inorichi
2015-10-18 01:22:05 +02:00
parent 1802dd04e4
commit a52e33b628
6 changed files with 70 additions and 84 deletions
@@ -16,6 +16,7 @@ public class MangaChaptersPresenter extends BasePresenter<MangaChaptersFragment>
@Inject DatabaseHelper db;
@Inject SourceManager sourceManager;
private Manga manga;
private Subscription chaptersSubscription;
private Subscription onlineChaptersSubscription;
private boolean doingRequest = false;
@@ -23,26 +24,34 @@ public class MangaChaptersPresenter extends BasePresenter<MangaChaptersFragment>
@Override
protected void onTakeView(MangaChaptersFragment view) {
super.onTakeView(view);
getChapters(view.getMangaId());
registerForStickyEvents();
}
public void refreshChapters(Manga manga) {
@Override
protected void onDropView() {
unregisterForEvents();
super.onDropView();
}
public void onEventMainThread(Manga manga) {
this.manga = manga;
getChapters();
}
public void refreshChapters() {
if (manga != null && !doingRequest)
getChaptersFromSource(manga);
}
private void getChapters(long manga_id) {
public void getChapters() {
if (chaptersSubscription != null)
remove(chaptersSubscription);
return;
chaptersSubscription = db.getChapters(manga_id)
add(chaptersSubscription = db.getChapters(manga.id)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.compose(deliverLatestCache())
.subscribe(this.split(MangaChaptersFragment::onNextChapters));
add(chaptersSubscription);
.subscribe(this.split(MangaChaptersFragment::onNextChapters)));
}
public void getChaptersFromSource(Manga manga) {
@@ -2,7 +2,9 @@ package eu.kanade.mangafeed.presenter;
import javax.inject.Inject;
import de.greenrobot.event.EventBus;
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;
@@ -13,25 +15,32 @@ public class MangaDetailPresenter extends BasePresenter<MangaDetailActivity> {
@Inject DatabaseHelper db;
private Manga manga;
private Subscription mangaSubscription;
@Override
protected void onTakeView(MangaDetailActivity view) {
super.onTakeView(view);
if (manga != null)
view.setManga(manga);
if (mangaSubscription == null)
initializeManga(view);
getManga(view);
}
private void initializeManga(MangaDetailActivity view) {
mangaSubscription = db.getManga(view.getMangaId())
private void getManga(MangaDetailActivity view) {
if (mangaSubscription != null)
return;
add(mangaSubscription = db.getManga(view.getMangaId())
.subscribeOn(Schedulers.io())
.take(1)
.flatMap(Observable::from)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(view::onMangaNext);
add(mangaSubscription);
.subscribe(manga -> {
this.manga = manga;
view.setManga(manga);
EventBus.getDefault().postSticky(manga);
}));
}
}
@@ -3,34 +3,47 @@ package eu.kanade.mangafeed.presenter;
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 rx.Observable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
public class MangaInfoPresenter extends BasePresenter<MangaInfoFragment> {
@Inject DatabaseHelper db;
private Manga manga;
private Subscription mangaInfoSubscription;
@Override
protected void onTakeView(MangaInfoFragment view) {
super.onTakeView(view);
getMangaInfo(view);
registerForStickyEvents();
}
private void getMangaInfo(MangaInfoFragment view) {
if (mangaInfoSubscription != null)
remove(mangaInfoSubscription);
@Override
protected void onDropView() {
unregisterForEvents();
super.onDropView();
}
mangaInfoSubscription = db.getManga(view.getMangaId())
.observeOn(AndroidSchedulers.mainThread())
public void onEventMainThread(Manga manga) {
this.manga = manga;
getMangaInfo();
}
private void getMangaInfo() {
if (mangaInfoSubscription != null)
return;
add(mangaInfoSubscription = db.getManga(manga.id)
.subscribeOn(Schedulers.io())
.take(1)
.flatMap(Observable::from)
.subscribe(view::setMangaInfo);
add(mangaInfoSubscription);
.observeOn(AndroidSchedulers.mainThread())
.compose(deliverLatestCache())
.subscribe(split(MangaInfoFragment::setMangaInfo)));
}
}