Allow to delete downloaded chapters

This commit is contained in:
inorichi 2015-11-04 12:02:55 +01:00
parent fa28ccc19a
commit 3b9f4cb6f1
5 changed files with 35 additions and 16 deletions

View File

@ -226,4 +226,8 @@ public class DownloadManager {
return new File(preferences.getDownloadsDirectory(), chapterRelativePath); return new File(preferences.getDownloadsDirectory(), chapterRelativePath);
} }
public void deleteChapter(Source source, Manga manga, Chapter chapter) {
File path = getAbsoluteChapterDirectory(source, manga, chapter);
DiskUtils.deleteFiles(path);
}
} }

View File

@ -41,6 +41,7 @@ public class MangaChaptersPresenter extends BasePresenter<MangaChaptersFragment>
private Subscription markReadSubscription; private Subscription markReadSubscription;
private Subscription downloadSubscription; private Subscription downloadSubscription;
private Subscription deleteSubscription;
@Override @Override
protected void onCreate(Bundle savedState) { protected void onCreate(Bundle savedState) {
@ -118,9 +119,6 @@ public class MangaChaptersPresenter extends BasePresenter<MangaChaptersFragment>
} }
public void markChaptersRead(Observable<Chapter> selectedChapters, boolean read) { public void markChaptersRead(Observable<Chapter> selectedChapters, boolean read) {
if (markReadSubscription != null)
remove(markReadSubscription);
add(markReadSubscription = selectedChapters add(markReadSubscription = selectedChapters
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.map(chapter -> { .map(chapter -> {
@ -130,16 +128,14 @@ public class MangaChaptersPresenter extends BasePresenter<MangaChaptersFragment>
.toList() .toList()
.flatMap(db::insertChapters) .flatMap(db::insertChapters)
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.doOnCompleted( () -> remove(markReadSubscription) )
.subscribe(result -> { .subscribe(result -> {
})); }));
} }
public void downloadChapters(Observable<Chapter> selectedChapters) { public void downloadChapters(Observable<Chapter> selectedChapters) {
if (downloadSubscription != null)
remove(downloadSubscription);
add(downloadSubscription = selectedChapters add(downloadSubscription = selectedChapters
.doOnCompleted(() -> remove(downloadSubscription))
.subscribe(chapter -> { .subscribe(chapter -> {
EventBus.getDefault().post( EventBus.getDefault().post(
new DownloadChapterEvent(manga, chapter)); new DownloadChapterEvent(manga, chapter));
@ -155,4 +151,13 @@ public class MangaChaptersPresenter extends BasePresenter<MangaChaptersFragment>
chapter.downloaded = Chapter.NOT_DOWNLOADED; chapter.downloaded = Chapter.NOT_DOWNLOADED;
} }
} }
public void deleteChapters(Observable<Chapter> selectedChapters) {
deleteSubscription = selectedChapters
.doOnCompleted( () -> remove(deleteSubscription) )
.subscribe(chapter -> {
downloadManager.deleteChapter(source, manga, chapter);
chapter.downloaded = Chapter.NOT_DOWNLOADED;
});
}
} }

View File

@ -34,6 +34,7 @@ public class ReaderPresenter extends BasePresenter<ReaderActivity> {
private Manga manga; private Manga manga;
private Chapter chapter; private Chapter chapter;
private List<Page> pageList; private List<Page> pageList;
private boolean isDownloaded;
@State int currentPage; @State int currentPage;
private static final int GET_PAGE_LIST = 1; private static final int GET_PAGE_LIST = 1;
@ -52,13 +53,11 @@ public class ReaderPresenter extends BasePresenter<ReaderActivity> {
if (currentPage != 0) if (currentPage != 0)
view.setSelectedPage(currentPage); 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, restartableReplay(GET_PAGE_IMAGES,
this::getPageImagesObservable, this::getPageImagesObservable,
(view, page) -> { (view, page) -> {},
},
(view, error) -> Timber.e("An error occurred while downloading an image")); (view, error) -> Timber.e("An error occurred while downloading an image"));
} }
@ -77,8 +76,9 @@ public class ReaderPresenter extends BasePresenter<ReaderActivity> {
@Override @Override
protected void onDestroy() { protected void onDestroy() {
source.savePageList(chapter.url, pageList); if (!isDownloaded)
saveChapter(); source.savePageList(chapter.url, pageList);
saveChapterProgress();
super.onDestroy(); super.onDestroy();
} }
@ -87,6 +87,7 @@ public class ReaderPresenter extends BasePresenter<ReaderActivity> {
source = event.getSource(); source = event.getSource();
manga = event.getManga(); manga = event.getManga();
chapter = event.getChapter(); chapter = event.getChapter();
isDownloaded = chapter.downloaded == Chapter.DOWNLOADED;
if (chapter.last_page_read != 0 && !chapter.read) if (chapter.last_page_read != 0 && !chapter.read)
currentPage = chapter.last_page_read; currentPage = chapter.last_page_read;
@ -96,7 +97,7 @@ public class ReaderPresenter extends BasePresenter<ReaderActivity> {
} }
private Observable<List<Page>> getPageListObservable() { private Observable<List<Page>> getPageListObservable() {
if (chapter.downloaded != Chapter.DOWNLOADED) if (!isDownloaded)
return source.pullPageListFromNetwork(chapter.url) return source.pullPageListFromNetwork(chapter.url)
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()); .observeOn(AndroidSchedulers.mainThread());
@ -107,7 +108,7 @@ public class ReaderPresenter extends BasePresenter<ReaderActivity> {
private Observable<Page> getPageImagesObservable() { private Observable<Page> getPageImagesObservable() {
Observable<Page> pages; Observable<Page> pages;
if (chapter.downloaded != Chapter.DOWNLOADED) { if (!isDownloaded) {
pages = Observable pages = Observable
.merge(Observable.from(pageList).filter(page -> page.getImageUrl() != null), .merge(Observable.from(pageList).filter(page -> page.getImageUrl() != null),
source.getRemainingImageUrlsFromPageList(pageList)) source.getRemainingImageUrlsFromPageList(pageList))
@ -129,7 +130,7 @@ public class ReaderPresenter extends BasePresenter<ReaderActivity> {
this.currentPage = currentPage; this.currentPage = currentPage;
} }
private void saveChapter() { private void saveChapterProgress() {
chapter.last_page_read = currentPage; chapter.last_page_read = currentPage;
if (currentPage == pageList.size() - 1) { if (currentPage == pageList.size() - 1) {
chapter.read = true; chapter.read = true;

View File

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

View File

@ -8,6 +8,11 @@
android:icon="@drawable/ic_file_download" android:icon="@drawable/ic_file_download"
app:showAsAction="ifRoom"/> app:showAsAction="ifRoom"/>
<item android:id="@+id/action_delete"
android:title="@string/action_delete"
android:icon="@drawable/ic_action_delete"
app:showAsAction="ifRoom"/>
<item android:id="@+id/action_mark_as_read" <item android:id="@+id/action_mark_as_read"
android:title="@string/action_mark_as_read" android:title="@string/action_mark_as_read"
android:icon="@drawable/ic_action_done_all" android:icon="@drawable/ic_action_done_all"