From d8d93ee344e346335ee0a30d54f0b717e937f81a Mon Sep 17 00:00:00 2001 From: Bram van de Kerkhof Date: Mon, 5 Sep 2016 11:08:16 +0200 Subject: [PATCH] Added read filter to chapter select. (#431) * Added read filter to chapter select. * Can now select how far back the chapter should be deleted after read. --- .../data/preference/PreferenceKeys.kt | 4 +- .../data/preference/PreferencesHelper.kt | 4 +- .../ui/manga/chapter/ChaptersFragment.kt | 38 +++++++++++++------ .../ui/manga/chapter/ChaptersPresenter.kt | 23 ++++++++++- .../tachiyomi/ui/reader/ReaderPresenter.kt | 37 +++++++++--------- app/src/main/res/menu/chapters.xml | 8 +++- app/src/main/res/raw/changelog_debug.xml | 11 +++++- app/src/main/res/values/arrays.xml | 18 +++++++++ app/src/main/res/values/keys.xml | 4 +- app/src/main/res/values/strings.xml | 11 ++++-- app/src/main/res/xml/pref_downloads.xml | 21 ++++------ 11 files changed, 118 insertions(+), 61 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt index 667a1eb90..605e927e5 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt @@ -66,9 +66,7 @@ class PreferenceKeys(context: Context) { val downloadOnlyOverWifi = context.getString(R.string.pref_download_only_over_wifi_key) - val removeAfterRead = context.getString(R.string.pref_remove_after_read_key) - - val removeAfterReadPrevious = context.getString(R.string.pref_remove_after_read_previous_key) + val removeAfterReadSlots = context.getString(R.string.pref_remove_after_read_slots_key) val removeAfterMarkedAsRead = context.getString(R.string.pref_remove_after_marked_as_read_key) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt index 6ebbdbcea..0707afdbb 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt @@ -116,9 +116,7 @@ class PreferencesHelper(context: Context) { fun downloadOnlyOverWifi() = prefs.getBoolean(keys.downloadOnlyOverWifi, true) - fun removeAfterRead() = prefs.getBoolean(keys.removeAfterRead, false) - - fun removeAfterReadPrevious() = prefs.getBoolean(keys.removeAfterReadPrevious, false) + fun removeAfterReadSlots() = prefs.getInt(keys.removeAfterReadSlots, -1) fun removeAfterMarkedAsRead() = prefs.getBoolean(keys.removeAfterMarkedAsRead, false) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersFragment.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersFragment.kt index 2353f8c3b..577f96fa0 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersFragment.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersFragment.kt @@ -116,8 +116,25 @@ class ChaptersFragment : BaseRxFragment(), ActionMode.Callbac override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { inflater.inflate(R.menu.chapters, menu) - menu.findItem(R.id.action_filter_unread).isChecked = presenter.onlyUnread() - menu.findItem(R.id.action_filter_downloaded).isChecked = presenter.onlyDownloaded() + } + + override fun onPrepareOptionsMenu(menu: Menu) { + // Initialize menu items. + val menuFilterRead = menu.findItem(R.id.action_filter_read) + val menuFilterUnread = menu.findItem(R.id.action_filter_unread) + val menuFilterDownloaded = menu.findItem(R.id.action_filter_downloaded) + + // Set correct checkbox values. + menuFilterRead.isChecked = presenter.onlyRead() + menuFilterUnread.isChecked = presenter.onlyUnread() + menuFilterDownloaded.isChecked = presenter.onlyDownloaded() + + if (presenter.onlyRead()) + //Disable unread filter option if read filter is enabled. + menuFilterUnread.isEnabled = false + if (presenter.onlyUnread()) + //Disable read filter option if unread filter is enabled. + menuFilterRead.isEnabled = false } override fun onOptionsItemSelected(item: MenuItem): Boolean { @@ -126,8 +143,14 @@ class ChaptersFragment : BaseRxFragment(), ActionMode.Callbac R.id.manga_download -> showDownloadDialog() R.id.action_sorting_mode -> showSortingDialog() R.id.action_filter_unread -> { + item.isChecked = !item.isChecked + presenter.setUnreadFilter(item.isChecked) + activity.supportInvalidateOptionsMenu() + } + R.id.action_filter_read -> { item.isChecked = !item.isChecked presenter.setReadFilter(item.isChecked) + activity.supportInvalidateOptionsMenu() } R.id.action_filter_downloaded -> { item.isChecked = !item.isChecked @@ -145,8 +168,7 @@ class ChaptersFragment : BaseRxFragment(), ActionMode.Callbac fun onNextManga(manga: Manga) { // Set initial values - setReadFilter() - setDownloadedFilter() + activity.supportInvalidateOptionsMenu() } fun onNextChapters(chapters: List) { @@ -394,12 +416,4 @@ class ChaptersFragment : BaseRxFragment(), ActionMode.Callbac private fun setContextTitle(count: Int) { actionMode?.title = getString(R.string.label_selected, count) } - - fun setReadFilter() { - activity.supportInvalidateOptionsMenu() - } - - fun setDownloadedFilter() { - activity.supportInvalidateOptionsMenu() - } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersPresenter.kt index 402f15623..3eb6e2ff4 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersPresenter.kt @@ -209,6 +209,9 @@ class ChaptersPresenter : BasePresenter() { if (onlyUnread()) { observable = observable.filter { !it.read } } + if (onlyRead()) { + observable = observable.filter { it.read } + } if (onlyDownloaded()) { observable = observable.filter { it.isDownloaded } } @@ -349,12 +352,23 @@ class ChaptersPresenter : BasePresenter() { * * @param onlyUnread whether to display only unread chapters or all chapters. */ - fun setReadFilter(onlyUnread: Boolean) { + fun setUnreadFilter(onlyUnread: Boolean) { manga.readFilter = if (onlyUnread) Manga.SHOW_UNREAD else Manga.SHOW_ALL db.updateFlags(manga).executeAsBlocking() refreshChapters() } + /** + * Sets the read filter and requests an UI update. + * + * @param onlyRead whether to display only read chapters or all chapters. + */ + fun setReadFilter(onlyRead: Boolean) { + manga.readFilter = if (onlyRead) Manga.SHOW_READ else Manga.SHOW_ALL + db.updateFlags(manga).executeAsBlocking() + refreshChapters() + } + /** * Sets the download filter and requests an UI update. * @@ -411,6 +425,13 @@ class ChaptersPresenter : BasePresenter() { return manga.readFilter == Manga.SHOW_UNREAD } + /** + * Whether the display only read filter is enabled. + */ + fun onlyRead(): Boolean { + return manga.readFilter == Manga.SHOW_READ + } + /** * Whether the sorting method is descending or ascending. */ diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt index bdd3eb0fa..a905b1cdd 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt @@ -228,12 +228,14 @@ class ReaderPresenter : BasePresenter() { * strategy set for the manga. * * @param chapter the current active chapter. + * @param previousChapterAmount the desired number of chapters preceding the current active chapter (Default: 1). + * @param nextChapterAmount the desired number of chapters succeeding the current active chapter (Default: 1). */ - private fun getAdjacentChaptersStrategy(chapter: ReaderChapter) = when (manga.sorting) { + private fun getAdjacentChaptersStrategy(chapter: ReaderChapter, previousChapterAmount: Int = 1, nextChapterAmount: Int = 1) = when (manga.sorting) { Manga.SORTING_SOURCE -> { val currChapterIndex = chapterList.indexOfFirst { chapter.id == it.id } - val nextChapter = chapterList.getOrNull(currChapterIndex + 1) - val prevChapter = chapterList.getOrNull(currChapterIndex - 1) + val nextChapter = chapterList.getOrNull(currChapterIndex + nextChapterAmount) + val prevChapter = chapterList.getOrNull(currChapterIndex - previousChapterAmount) Pair(prevChapter, nextChapter) } Manga.SORTING_NUMBER -> { @@ -241,18 +243,18 @@ class ReaderPresenter : BasePresenter() { val chapterNumber = chapter.chapter_number var prevChapter: ReaderChapter? = null - for (i in (currChapterIndex - 1) downTo 0) { + for (i in (currChapterIndex - previousChapterAmount) downTo 0) { val c = chapterList[i] - if (c.chapter_number < chapterNumber && c.chapter_number >= chapterNumber - 1) { + if (c.chapter_number < chapterNumber && c.chapter_number >= chapterNumber - previousChapterAmount) { prevChapter = c break } } var nextChapter: ReaderChapter? = null - for (i in (currChapterIndex + 1) until chapterList.size) { + for (i in (currChapterIndex + nextChapterAmount) until chapterList.size) { val c = chapterList[i] - if (c.chapter_number > chapterNumber && c.chapter_number <= chapterNumber + 1) { + if (c.chapter_number > chapterNumber && c.chapter_number <= chapterNumber + nextChapterAmount) { nextChapter = c break } @@ -344,7 +346,6 @@ class ReaderPresenter : BasePresenter() { fun onChapterLeft() { // Reference these locally because they are needed later from another thread. val chapter = chapter - val prevChapter = prevChapter val pages = chapter.pages ?: return @@ -355,21 +356,21 @@ class ReaderPresenter : BasePresenter() { chapter.read = true } + // Cache current page list progress for online chapters to allow a faster reopen if (!chapter.isDownloaded) { source.let { if (it is OnlineSource) it.savePageList(chapter, pages) } } - // Cache current page list progress for online chapters to allow a faster reopen if (chapter.read) { - // Check if remove after read is selected by user - if (prefs.removeAfterRead()) { - if (prefs.removeAfterReadPrevious() ) { - if (prevChapter != null) { - deleteChapter(prevChapter, manga) - } - } else { - deleteChapter(chapter, manga) - } + val removeAfterReadSlots = prefs.removeAfterReadSlots() + when (removeAfterReadSlots) { + // Setting disabled + -1 -> { /**Empty function**/ } + // Remove current read chapter + 0 -> deleteChapter(chapter, manga) + // Remove previous chapter specified by user in settings. + else -> getAdjacentChaptersStrategy(chapter, removeAfterReadSlots) + .first?.let { deleteChapter(it, manga) } } } diff --git a/app/src/main/res/menu/chapters.xml b/app/src/main/res/menu/chapters.xml index cb5c01e38..4512d8637 100644 --- a/app/src/main/res/menu/chapters.xml +++ b/app/src/main/res/menu/chapters.xml @@ -9,13 +9,17 @@ app:showAsAction="ifRoom"> + android:title="@string/action_filter_read"/> + diff --git a/app/src/main/res/raw/changelog_debug.xml b/app/src/main/res/raw/changelog_debug.xml index f3d439346..345c21927 100644 --- a/app/src/main/res/raw/changelog_debug.xml +++ b/app/src/main/res/raw/changelog_debug.xml @@ -1,13 +1,20 @@ - + + [b]Important![/b] Delete after read has been updated. + This means the value has been reset set to disabled. + This can be changed in Settings > Downloads + + + + [b]Important![/b] Now chapters follow the order of the sources. [b]It's required that you update your entire library before reading in order for them to be synced.[/b] Old behavior can be restored for a manga in the overflow menu of the chapters tab. - + Kissmanga covers may not load anymore. The only workaround is to update the details of the manga from the info tab, or clearing the database (the latter won't fix covers from library manga). diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index c21c4f27e..1b61924f6 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -48,6 +48,24 @@ 3 + + @string/disabled + @string/last_read_chapter + @string/second_to_last + @string/third_to_last + @string/fourth_to_last + @string/fifth_to_last + + + + -1 + 0 + 1 + 2 + 3 + 4 + + @string/rapid_decoder @string/skia_decoder diff --git a/app/src/main/res/values/keys.xml b/app/src/main/res/values/keys.xml index a32a81968..1149d78f4 100644 --- a/app/src/main/res/values/keys.xml +++ b/app/src/main/res/values/keys.xml @@ -41,13 +41,11 @@ pref_download_directory_key pref_download_slots_key + remove_after_read_slots pref_download_only_over_wifi_key pref_remove_after_marked_as_read_key pref_category_remove_after_read_key - pref_remove_after_read_key - pref_remove_after_read_previous_key - last_used_category pref_source_languages diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c621a2fa2..4f4e63295 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -19,6 +19,7 @@ Filter Downloaded Unread + Read Remove filter Search Select all @@ -142,11 +143,13 @@ Only download over Wi-Fi Remove when marked as read Remove after read - Remove after read - Current chapter - Previous chapter Custom directory - + Disabled + Last read chapter + Second to last chapter + Third to last chapter + Fourth to last chapter + Fifth to last chapter Languages diff --git a/app/src/main/res/xml/pref_downloads.xml b/app/src/main/res/xml/pref_downloads.xml index ce777236a..8eff6f5ba 100644 --- a/app/src/main/res/xml/pref_downloads.xml +++ b/app/src/main/res/xml/pref_downloads.xml @@ -25,25 +25,20 @@ + android:title="@string/pref_remove_after_read" /> - - - +