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);
}
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 downloadSubscription;
private Subscription deleteSubscription;
@Override
protected void onCreate(Bundle savedState) {
@ -118,9 +119,6 @@ public class MangaChaptersPresenter extends BasePresenter<MangaChaptersFragment>
}
public void markChaptersRead(Observable<Chapter> 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<MangaChaptersFragment>
.toList()
.flatMap(db::insertChapters)
.observeOn(AndroidSchedulers.mainThread())
.doOnCompleted( () -> remove(markReadSubscription) )
.subscribe(result -> {
}));
}
public void downloadChapters(Observable<Chapter> 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<MangaChaptersFragment>
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 Chapter chapter;
private List<Page> pageList;
private boolean isDownloaded;
@State int currentPage;
private static final int GET_PAGE_LIST = 1;
@ -52,13 +53,11 @@ public class ReaderPresenter extends BasePresenter<ReaderActivity> {
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<ReaderActivity> {
@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<ReaderActivity> {
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<ReaderActivity> {
}
private Observable<List<Page>> 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<ReaderActivity> {
private Observable<Page> getPageImagesObservable() {
Observable<Page> 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<ReaderActivity> {
this.currentPage = currentPage;
}
private void saveChapter() {
private void saveChapterProgress() {
chapter.last_page_read = currentPage;
if (currentPage == pageList.size() - 1) {
chapter.read = true;

View File

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

View File

@ -8,6 +8,11 @@
android:icon="@drawable/ic_file_download"
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"
android:title="@string/action_mark_as_read"
android:icon="@drawable/ic_action_done_all"