2015-10-17 13:51:54 +02:00
|
|
|
package eu.kanade.mangafeed.presenter;
|
|
|
|
|
|
2015-10-17 21:31:10 +02:00
|
|
|
import javax.inject.Inject;
|
|
|
|
|
|
|
|
|
|
import eu.kanade.mangafeed.data.helpers.DatabaseHelper;
|
|
|
|
|
import eu.kanade.mangafeed.data.helpers.SourceManager;
|
|
|
|
|
import eu.kanade.mangafeed.data.models.Manga;
|
|
|
|
|
import eu.kanade.mangafeed.sources.Source;
|
2015-10-17 13:51:54 +02:00
|
|
|
import eu.kanade.mangafeed.ui.fragment.MangaChaptersFragment;
|
2015-10-17 21:31:10 +02:00
|
|
|
import rx.Subscription;
|
|
|
|
|
import rx.android.schedulers.AndroidSchedulers;
|
|
|
|
|
import rx.schedulers.Schedulers;
|
2015-10-17 13:51:54 +02:00
|
|
|
|
2015-10-17 16:33:29 +02:00
|
|
|
public class MangaChaptersPresenter extends BasePresenter<MangaChaptersFragment> {
|
2015-10-17 21:31:10 +02:00
|
|
|
|
|
|
|
|
@Inject DatabaseHelper db;
|
|
|
|
|
@Inject SourceManager sourceManager;
|
|
|
|
|
|
|
|
|
|
private Subscription chaptersSubscription;
|
|
|
|
|
private Subscription onlineChaptersSubscription;
|
|
|
|
|
private boolean doingRequest = false;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onTakeView(MangaChaptersFragment view) {
|
|
|
|
|
super.onTakeView(view);
|
|
|
|
|
|
|
|
|
|
getChapters(view.getMangaId());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void refreshChapters(Manga manga) {
|
|
|
|
|
if (manga != null && !doingRequest)
|
|
|
|
|
getChaptersFromSource(manga);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void getChapters(long manga_id) {
|
|
|
|
|
if (chaptersSubscription != null)
|
|
|
|
|
remove(chaptersSubscription);
|
|
|
|
|
|
|
|
|
|
chaptersSubscription = db.getChapters(manga_id)
|
|
|
|
|
.subscribeOn(Schedulers.io())
|
|
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
|
|
|
.compose(deliverLatestCache())
|
|
|
|
|
.subscribe(this.split(MangaChaptersFragment::onNextChapters));
|
|
|
|
|
|
|
|
|
|
add(chaptersSubscription);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void getChaptersFromSource(Manga manga) {
|
|
|
|
|
if (onlineChaptersSubscription != null)
|
|
|
|
|
remove(onlineChaptersSubscription);
|
|
|
|
|
|
|
|
|
|
Source source = sourceManager.get(manga.source);
|
|
|
|
|
doingRequest = true;
|
|
|
|
|
|
|
|
|
|
onlineChaptersSubscription = source.pullChaptersFromNetwork(manga.url)
|
|
|
|
|
.subscribeOn(Schedulers.io())
|
|
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
|
|
|
.compose(deliverLatestCache())
|
|
|
|
|
.subscribe(this.split((view, chapters) -> {
|
|
|
|
|
doingRequest = false;
|
|
|
|
|
}), throwable -> {
|
|
|
|
|
doingRequest = false;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
add(onlineChaptersSubscription);
|
|
|
|
|
}
|
2015-10-17 13:51:54 +02:00
|
|
|
}
|