Read downloaded chapters

This commit is contained in:
inorichi 2015-11-04 11:25:29 +01:00
parent b0a8740e8d
commit fa28ccc19a
4 changed files with 23 additions and 28 deletions

View File

@ -140,7 +140,6 @@ public class MangaChaptersPresenter extends BasePresenter<MangaChaptersFragment>
remove(downloadSubscription); remove(downloadSubscription);
add(downloadSubscription = selectedChapters add(downloadSubscription = selectedChapters
.subscribeOn(Schedulers.io())
.subscribe(chapter -> { .subscribe(chapter -> {
EventBus.getDefault().post( EventBus.getDefault().post(
new DownloadChapterEvent(manga, chapter)); new DownloadChapterEvent(manga, chapter));

View File

@ -38,7 +38,6 @@ public class ReaderPresenter extends BasePresenter<ReaderActivity> {
private static final int GET_PAGE_LIST = 1; private static final int GET_PAGE_LIST = 1;
private static final int GET_PAGE_IMAGES = 2; private static final int GET_PAGE_IMAGES = 2;
private static final int GET_LOCAL_IMAGES = 3;
@Override @Override
protected void onCreate(Bundle savedState) { protected void onCreate(Bundle savedState) {
@ -47,7 +46,7 @@ public class ReaderPresenter extends BasePresenter<ReaderActivity> {
restartableLatestCache(GET_PAGE_LIST, restartableLatestCache(GET_PAGE_LIST,
() -> getPageListObservable() () -> getPageListObservable()
.doOnNext(pages -> pageList = pages) .doOnNext(pages -> pageList = pages)
.doOnCompleted(this::prepareChapter), .doOnCompleted( () -> start(GET_PAGE_IMAGES) ),
(view, pages) -> { (view, pages) -> {
view.onPageListReady(pages); view.onPageListReady(pages);
if (currentPage != 0) if (currentPage != 0)
@ -62,9 +61,6 @@ public class ReaderPresenter extends BasePresenter<ReaderActivity> {
}, },
(view, error) -> Timber.e("An error occurred while downloading an image")); (view, error) -> Timber.e("An error occurred while downloading an image"));
restartableReplay(GET_LOCAL_IMAGES,
this::getLocalImagesObservable,
(view, page) -> {});
} }
@Override @Override
@ -100,26 +96,31 @@ public class ReaderPresenter extends BasePresenter<ReaderActivity> {
} }
private Observable<List<Page>> getPageListObservable() { private Observable<List<Page>> getPageListObservable() {
if (chapter.downloaded != Chapter.DOWNLOADED)
return source.pullPageListFromNetwork(chapter.url) return source.pullPageListFromNetwork(chapter.url)
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()); .observeOn(AndroidSchedulers.mainThread());
else
return Observable.just(downloadManager.getSavedPageList(source, manga, chapter));
} }
private Observable<Page> getPageImagesObservable() { private Observable<Page> getPageImagesObservable() {
return Observable.merge( Observable<Page> pages;
Observable.from(pageList).filter(page -> page.getImageUrl() != null),
source.getRemainingImageUrlsFromPageList(pageList) if (chapter.downloaded != Chapter.DOWNLOADED) {
) pages = Observable
.flatMap(source::getCachedImage) .merge(Observable.from(pageList).filter(page -> page.getImageUrl() != null),
source.getRemainingImageUrlsFromPageList(pageList))
.flatMap(source::getCachedImage);
} else {
File chapterDir = downloadManager.getAbsoluteChapterDirectory(source, manga, chapter);
pages = Observable.from(pageList)
.flatMap(page -> downloadManager.getDownloadedImage(page, source, chapterDir))
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()); .observeOn(AndroidSchedulers.mainThread());
} }
return pages
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()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()); .observeOn(AndroidSchedulers.mainThread());
} }
@ -128,13 +129,6 @@ public class ReaderPresenter extends BasePresenter<ReaderActivity> {
this.currentPage = currentPage; this.currentPage = currentPage;
} }
private void prepareChapter() {
if (chapter.downloaded != Chapter.DOWNLOADED)
start(GET_PAGE_IMAGES);
else
start(GET_LOCAL_IMAGES);
}
private void saveChapter() { private void saveChapter() {
chapter.last_page_read = currentPage; chapter.last_page_read = currentPage;
if (currentPage == pageList.size() - 1) { if (currentPage == pageList.size() - 1) {

View File

@ -142,6 +142,7 @@ public class MangaChaptersFragment extends BaseRxFragment<MangaChaptersPresenter
return true; return true;
case R.id.action_download: case R.id.action_download:
getPresenter().downloadChapters(getSelectedChapters()); getPresenter().downloadChapters(getSelectedChapters());
closeActionMode();
return true; return true;
} }
return false; return false;

View File

@ -7,6 +7,7 @@
android:entries="@array/download_threads" android:entries="@array/download_threads"
android:entryValues="@array/download_threads" android:entryValues="@array/download_threads"
android:defaultValue="1" android:defaultValue="1"
android:order="1"
android:summary="%s"/> android:summary="%s"/>
</PreferenceScreen> </PreferenceScreen>