|
@@ -2,16 +2,19 @@ package eu.kanade.mangafeed.presenter;
|
|
|
|
|
|
import android.os.Bundle;
|
|
|
|
|
|
+import java.io.File;
|
|
|
import java.util.List;
|
|
|
|
|
|
import javax.inject.Inject;
|
|
|
|
|
|
import de.greenrobot.event.EventBus;
|
|
|
import eu.kanade.mangafeed.data.helpers.DatabaseHelper;
|
|
|
+import eu.kanade.mangafeed.data.helpers.DownloadManager;
|
|
|
import eu.kanade.mangafeed.data.helpers.PreferencesHelper;
|
|
|
import eu.kanade.mangafeed.data.models.Chapter;
|
|
|
+import eu.kanade.mangafeed.data.models.Manga;
|
|
|
import eu.kanade.mangafeed.data.models.Page;
|
|
|
-import eu.kanade.mangafeed.events.SourceChapterEvent;
|
|
|
+import eu.kanade.mangafeed.events.SourceMangaChapterEvent;
|
|
|
import eu.kanade.mangafeed.sources.base.Source;
|
|
|
import eu.kanade.mangafeed.ui.activity.ReaderActivity;
|
|
|
import eu.kanade.mangafeed.util.EventBusHook;
|
|
@@ -25,14 +28,17 @@ public class ReaderPresenter extends BasePresenter<ReaderActivity> {
|
|
|
|
|
|
@Inject PreferencesHelper prefs;
|
|
|
@Inject DatabaseHelper db;
|
|
|
+ @Inject DownloadManager downloadManager;
|
|
|
|
|
|
private Source source;
|
|
|
+ private Manga manga;
|
|
|
private Chapter chapter;
|
|
|
private List<Page> pageList;
|
|
|
@State int currentPage;
|
|
|
|
|
|
private static final int GET_PAGE_LIST = 1;
|
|
|
private static final int GET_PAGE_IMAGES = 2;
|
|
|
+ private static final int GET_LOCAL_IMAGES = 3;
|
|
|
|
|
|
@Override
|
|
|
protected void onCreate(Bundle savedState) {
|
|
@@ -41,7 +47,7 @@ public class ReaderPresenter extends BasePresenter<ReaderActivity> {
|
|
|
restartableLatestCache(GET_PAGE_LIST,
|
|
|
() -> getPageListObservable()
|
|
|
.doOnNext(pages -> pageList = pages)
|
|
|
- .doOnCompleted(() -> start(GET_PAGE_IMAGES)),
|
|
|
+ .doOnCompleted(this::prepareChapter),
|
|
|
(view, pages) -> {
|
|
|
view.onPageListReady(pages);
|
|
|
if (currentPage != 0)
|
|
@@ -55,6 +61,10 @@ public class ReaderPresenter extends BasePresenter<ReaderActivity> {
|
|
|
(view, page) -> {
|
|
|
},
|
|
|
(view, error) -> Timber.e("An error occurred while downloading an image"));
|
|
|
+
|
|
|
+ restartableReplay(GET_LOCAL_IMAGES,
|
|
|
+ this::getLocalImagesObservable,
|
|
|
+ (view, page) -> {});
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -77,14 +87,16 @@ public class ReaderPresenter extends BasePresenter<ReaderActivity> {
|
|
|
}
|
|
|
|
|
|
@EventBusHook
|
|
|
- public void onEventMainThread(SourceChapterEvent event) {
|
|
|
+ public void onEventMainThread(SourceMangaChapterEvent event) {
|
|
|
source = event.getSource();
|
|
|
+ manga = event.getManga();
|
|
|
chapter = event.getChapter();
|
|
|
if (chapter.last_page_read != 0 && !chapter.read)
|
|
|
currentPage = chapter.last_page_read;
|
|
|
|
|
|
- start(1);
|
|
|
- EventBus.getDefault().removeStickyEvent(SourceChapterEvent.class);
|
|
|
+ start(GET_PAGE_LIST);
|
|
|
+
|
|
|
+ EventBus.getDefault().removeStickyEvent(SourceMangaChapterEvent.class);
|
|
|
}
|
|
|
|
|
|
private Observable<List<Page>> getPageListObservable() {
|
|
@@ -103,10 +115,26 @@ public class ReaderPresenter extends BasePresenter<ReaderActivity> {
|
|
|
.observeOn(AndroidSchedulers.mainThread());
|
|
|
}
|
|
|
|
|
|
+ private Observable<Page> getLocalImagesObservable() {
|
|
|
+ File chapterDir = downloadManager.getAbsoluteChapterDirectory(source, manga, chapter);
|
|
|
+
|
|
|
+ return Observable.from(pageList)
|
|
|
+ .flatMap(page -> downloadManager.getDownloadedImage(page, source, chapterDir))
|
|
|
+ .subscribeOn(Schedulers.io())
|
|
|
+ .observeOn(AndroidSchedulers.mainThread());
|
|
|
+ }
|
|
|
+
|
|
|
public void setCurrentPage(int currentPage) {
|
|
|
this.currentPage = currentPage;
|
|
|
}
|
|
|
|
|
|
+ private void prepareChapter() {
|
|
|
+ if (chapter.downloaded != Chapter.DOWNLOADED)
|
|
|
+ start(GET_PAGE_IMAGES);
|
|
|
+ else
|
|
|
+ start(GET_LOCAL_IMAGES);
|
|
|
+ }
|
|
|
+
|
|
|
private void saveChapter() {
|
|
|
chapter.last_page_read = currentPage;
|
|
|
if (currentPage == pageList.size() - 1) {
|