From 3b9f4cb6f1d17f38d9cb9527de213fac03fbdeaa Mon Sep 17 00:00:00 2001 From: inorichi Date: Wed, 4 Nov 2015 12:02:55 +0100 Subject: [PATCH] Allow to delete downloaded chapters --- .../data/helpers/DownloadManager.java | 4 ++++ .../presenter/MangaChaptersPresenter.java | 19 ++++++++++++------- .../mangafeed/presenter/ReaderPresenter.java | 19 ++++++++++--------- .../ui/fragment/MangaChaptersFragment.java | 4 ++++ app/src/main/res/menu/chapter_selection.xml | 5 +++++ 5 files changed, 35 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/eu/kanade/mangafeed/data/helpers/DownloadManager.java b/app/src/main/java/eu/kanade/mangafeed/data/helpers/DownloadManager.java index 63dc149494..9c6a94169d 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/helpers/DownloadManager.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/helpers/DownloadManager.java @@ -226,4 +226,8 @@ public class DownloadManager { return new File(preferences.getDownloadsDirectory(), chapterRelativePath); } + public void deleteChapter(Source source, Manga manga, Chapter chapter) { + File path = getAbsoluteChapterDirectory(source, manga, chapter); + DiskUtils.deleteFiles(path); + } } diff --git a/app/src/main/java/eu/kanade/mangafeed/presenter/MangaChaptersPresenter.java b/app/src/main/java/eu/kanade/mangafeed/presenter/MangaChaptersPresenter.java index 3c802bb281..81bbaf4934 100644 --- a/app/src/main/java/eu/kanade/mangafeed/presenter/MangaChaptersPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/presenter/MangaChaptersPresenter.java @@ -41,6 +41,7 @@ public class MangaChaptersPresenter extends BasePresenter private Subscription markReadSubscription; private Subscription downloadSubscription; + private Subscription deleteSubscription; @Override protected void onCreate(Bundle savedState) { @@ -118,9 +119,6 @@ public class MangaChaptersPresenter extends BasePresenter } public void markChaptersRead(Observable selectedChapters, boolean read) { - if (markReadSubscription != null) - remove(markReadSubscription); - add(markReadSubscription = selectedChapters .subscribeOn(Schedulers.io()) .map(chapter -> { @@ -130,16 +128,14 @@ public class MangaChaptersPresenter extends BasePresenter .toList() .flatMap(db::insertChapters) .observeOn(AndroidSchedulers.mainThread()) + .doOnCompleted( () -> remove(markReadSubscription) ) .subscribe(result -> { - })); } public void downloadChapters(Observable selectedChapters) { - if (downloadSubscription != null) - remove(downloadSubscription); - add(downloadSubscription = selectedChapters + .doOnCompleted(() -> remove(downloadSubscription)) .subscribe(chapter -> { EventBus.getDefault().post( new DownloadChapterEvent(manga, chapter)); @@ -155,4 +151,13 @@ public class MangaChaptersPresenter extends BasePresenter chapter.downloaded = Chapter.NOT_DOWNLOADED; } } + + public void deleteChapters(Observable selectedChapters) { + deleteSubscription = selectedChapters + .doOnCompleted( () -> remove(deleteSubscription) ) + .subscribe(chapter -> { + downloadManager.deleteChapter(source, manga, chapter); + chapter.downloaded = Chapter.NOT_DOWNLOADED; + }); + } } diff --git a/app/src/main/java/eu/kanade/mangafeed/presenter/ReaderPresenter.java b/app/src/main/java/eu/kanade/mangafeed/presenter/ReaderPresenter.java index a6869c7f74..3f83d1bcdc 100644 --- a/app/src/main/java/eu/kanade/mangafeed/presenter/ReaderPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/presenter/ReaderPresenter.java @@ -34,6 +34,7 @@ public class ReaderPresenter extends BasePresenter { private Manga manga; private Chapter chapter; private List pageList; + private boolean isDownloaded; @State int currentPage; private static final int GET_PAGE_LIST = 1; @@ -52,13 +53,11 @@ public class ReaderPresenter extends BasePresenter { if (currentPage != 0) view.setSelectedPage(currentPage); }, - (view, error) -> Timber.e("An error occurred while downloading page list") - ); + (view, error) -> Timber.e("An error occurred while downloading page list")); restartableReplay(GET_PAGE_IMAGES, this::getPageImagesObservable, - (view, page) -> { - }, + (view, page) -> {}, (view, error) -> Timber.e("An error occurred while downloading an image")); } @@ -77,8 +76,9 @@ public class ReaderPresenter extends BasePresenter { @Override protected void onDestroy() { - source.savePageList(chapter.url, pageList); - saveChapter(); + if (!isDownloaded) + source.savePageList(chapter.url, pageList); + saveChapterProgress(); super.onDestroy(); } @@ -87,6 +87,7 @@ public class ReaderPresenter extends BasePresenter { source = event.getSource(); manga = event.getManga(); chapter = event.getChapter(); + isDownloaded = chapter.downloaded == Chapter.DOWNLOADED; if (chapter.last_page_read != 0 && !chapter.read) currentPage = chapter.last_page_read; @@ -96,7 +97,7 @@ public class ReaderPresenter extends BasePresenter { } private Observable> getPageListObservable() { - if (chapter.downloaded != Chapter.DOWNLOADED) + if (!isDownloaded) return source.pullPageListFromNetwork(chapter.url) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()); @@ -107,7 +108,7 @@ public class ReaderPresenter extends BasePresenter { private Observable getPageImagesObservable() { Observable pages; - if (chapter.downloaded != Chapter.DOWNLOADED) { + if (!isDownloaded) { pages = Observable .merge(Observable.from(pageList).filter(page -> page.getImageUrl() != null), source.getRemainingImageUrlsFromPageList(pageList)) @@ -129,7 +130,7 @@ public class ReaderPresenter extends BasePresenter { this.currentPage = currentPage; } - private void saveChapter() { + private void saveChapterProgress() { chapter.last_page_read = currentPage; if (currentPage == pageList.size() - 1) { chapter.read = true; diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/MangaChaptersFragment.java b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/MangaChaptersFragment.java index 9645423c69..ee624999af 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/MangaChaptersFragment.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/MangaChaptersFragment.java @@ -144,6 +144,10 @@ public class MangaChaptersFragment extends BaseRxFragment + +