From 9f30033a7a2e2e29987e73325b73b24fe32a70e7 Mon Sep 17 00:00:00 2001 From: NoodleMage Date: Sat, 20 Feb 2016 11:50:38 +0100 Subject: [PATCH] Added option te delete chapter after reading. Go to Setting > Downloads to toggle: default = false. --- .../data/preference/PreferencesHelper.java | 8 +++ .../tachiyomi/ui/reader/ReaderPresenter.java | 66 ++++++++++++++++++- app/src/main/res/values/keys.xml | 1 + app/src/main/res/values/strings.xml | 1 + app/src/main/res/xml/pref_downloads.xml | 4 ++ 5 files changed, 77 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.java b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.java index 4e93f17681..774befc8a6 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.java +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.java @@ -185,6 +185,14 @@ public class PreferencesHelper { return prefs.getBoolean(getKey(R.string.pref_download_only_over_wifi_key), true); } + /** + * Returns preference of deletion after reading chapter + * @return preference of deletion + */ + public boolean deleteChapterAfterReading() { + return prefs.getBoolean(getKey(R.string.pref_delete_after_reading_key), false); + } + public static int getLibraryUpdateInterval(Context context) { return PreferenceManager.getDefaultSharedPreferences(context).getInt( context.getString(R.string.pref_library_update_interval_key), 0); diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.java b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.java index aede73ffd5..359707b600 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.java +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.java @@ -80,8 +80,9 @@ public class ReaderPresenter extends BasePresenter { (view, pair) -> view.onAdjacentChapters(pair.first, pair.second)); startable(PRELOAD_NEXT_CHAPTER, this::getPreloadNextChapterObservable, - next -> {}, - error -> Timber.e("Error preloading chapter")); + next -> { + }, + error -> Timber.e("Error preloading chapter")); restartable(GET_MANGA_SYNC, () -> getMangaSyncObservable().subscribe()); @@ -316,6 +317,16 @@ public class ReaderPresenter extends BasePresenter { // Save current progress of the chapter. Mark as read if the chapter is finished if (activePage.isLastPage()) { activeChapter.read = true; + + // Check if user want to delete manga after reading it. + if (prefs.deleteChapterAfterReading()) { + // Check if the chapter is downloaded. + if (activeChapter.isDownloaded()) { + // Delete chapter. + Observable chapterObservable = Observable.just(activeChapter); + onDelete(chapterObservable, manga); + } + } } db.insertChapter(activeChapter).asRxObservable().subscribe(); } @@ -328,7 +339,7 @@ public class ReaderPresenter extends BasePresenter { // If the current chapter has been read, we check with this one if (activeChapter.read) lastChapterReadLocal = (int) Math.floor(activeChapter.chapter_number); - // If not, we check if the previous chapter has been read + // If not, we check if the previous chapter has been read else if (previousChapter != null && previousChapter.read) lastChapterReadLocal = (int) Math.floor(previousChapter.chapter_number); @@ -399,6 +410,55 @@ public class ReaderPresenter extends BasePresenter { } } + /** + * Start deleting chapter + * + * @param chapters selected chapters + * @param manga manga that belongs to chapter + * @return success of deletion. + */ + protected boolean onDelete(Observable chapters, Manga manga) { + Observable observable = chapters + .concatMap(chapter -> { + deleteChapter(chapter, manga); + return Observable.just(chapter); + }) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .doOnNext(chapter -> { + chapter.status = Download.NOT_DOWNLOADED; + }); + + deleteChapters(observable); + + return true; + } + + /** + * Delete selected chapter + * + * @param chapter chapter that is selected + * @param manga manga that belongs to chapter + */ + public void deleteChapter(Chapter chapter, Manga manga) { + Source source = sourceManager.get(manga.source); + downloadManager.deleteChapter(source, manga, chapter); + } + + /** + * Delete selected chapter observable + * + * @param selectedChapters chapter that are selected + */ + public void deleteChapters(Observable selectedChapters) { + add(selectedChapters + .subscribe(chapter -> { + downloadManager.getQueue().remove(chapter); + }, error -> { + Timber.e(error.getMessage()); + })); + } + public void updateMangaViewer(int viewer) { manga.viewer = viewer; db.insertManga(manga).executeAsBlocking(); diff --git a/app/src/main/res/values/keys.xml b/app/src/main/res/values/keys.xml index c89ef298a6..10de11a88b 100644 --- a/app/src/main/res/values/keys.xml +++ b/app/src/main/res/values/keys.xml @@ -32,6 +32,7 @@ pref_download_directory_key pref_download_slots_key pref_download_only_over_wifi_key + pref_delete_after_reading_key pref_clear_chapter_cache_key pref_clear_database_key diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0d12cfe8a5..42c6e3dd55 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -123,6 +123,7 @@ Downloads directory Simultaneous downloads Only download over Wi-Fi + Delete chapter after reading Clear chapter cache diff --git a/app/src/main/res/xml/pref_downloads.xml b/app/src/main/res/xml/pref_downloads.xml index ee85035d2e..2788c23984 100644 --- a/app/src/main/res/xml/pref_downloads.xml +++ b/app/src/main/res/xml/pref_downloads.xml @@ -10,6 +10,10 @@ android:key="@string/pref_download_only_over_wifi_key" android:defaultValue="true"/> + +