From 5b98b09ce3aa16f49738d9a497842371247d7de5 Mon Sep 17 00:00:00 2001 From: Jays2Kings Date: Sun, 11 Jul 2021 18:18:09 -0400 Subject: [PATCH] Refactoring the logic for the last few commits --- .../tachiyomi/data/database/models/Manga.kt | 47 +++++++------------ .../data/preference/PreferenceKeys.kt | 2 + .../data/preference/PreferencesHelper.kt | 2 +- .../ui/manga/MangaDetailsPresenter.kt | 22 ++++----- .../manga/chapter/ChaptersSortBottomSheet.kt | 2 +- .../tachiyomi/ui/reader/ReaderPresenter.kt | 2 +- .../tachiyomi/util/chapter/ChapterSort.kt | 7 +-- 7 files changed, 34 insertions(+), 50 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/database/models/Manga.kt b/app/src/main/java/eu/kanade/tachiyomi/data/database/models/Manga.kt index 98621072c8..64ec782f8b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/database/models/Manga.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/database/models/Manga.kt @@ -52,48 +52,35 @@ interface Manga : SManga { viewer_flags = viewer_flags and mask.inv() or (flag and mask) } - fun sortDescending(): Boolean = chapter_flags and CHAPTER_SORT_MASK == CHAPTER_SORT_DESC - fun hideChapterTitles(): Boolean = displayMode == CHAPTER_DISPLAY_NUMBER + val sortDescending: Boolean + get() = chapter_flags and CHAPTER_SORT_MASK == CHAPTER_SORT_DESC - fun usesLocalSort(): Boolean = chapter_flags and CHAPTER_SORT_LOCAL_MASK == CHAPTER_SORT_LOCAL + val hideChapterTitles: Boolean + get() = displayMode == CHAPTER_DISPLAY_NUMBER - fun usesLocalFilter(): Boolean = chapter_flags and CHAPTER_FILTER_LOCAL_MASK == CHAPTER_FILTER_LOCAL + val usesLocalSort: Boolean + get() = chapter_flags and CHAPTER_SORT_LOCAL_MASK == CHAPTER_SORT_LOCAL - fun sortDescending(defaultDesc: Boolean): Boolean { - return if (usesLocalSort()) sortDescending() else defaultDesc - } + val usesLocalFilter: Boolean + get() = chapter_flags and CHAPTER_FILTER_LOCAL_MASK == CHAPTER_FILTER_LOCAL - fun chapterOrder(defaultOrder: Int): Int { - return if (usesLocalSort()) sorting else defaultOrder - } + fun sortDescending(preferences: PreferencesHelper): Boolean = + if (usesLocalSort) sortDescending else preferences.chaptersDescAsDefault().get() + + fun chapterOrder(preferences: PreferencesHelper): Int = + if (usesLocalSort) sorting else preferences.sortChapterOrder().get() fun readFilter(preferences: PreferencesHelper): Int = - readFilter(preferences.filterChapterByRead().get()) - - fun readFilter(defaultFilter: Int): Int { - return if (usesLocalFilter()) readFilter else defaultFilter - } + if (usesLocalFilter) readFilter else preferences.filterChapterByRead().get() fun downloadedFilter(preferences: PreferencesHelper): Int = - downloadedFilter(preferences.filterChapterByDownloaded().get()) - - fun downloadedFilter(defaultFilter: Int): Int { - return if (usesLocalFilter()) downloadedFilter else defaultFilter - } + if (usesLocalFilter) downloadedFilter else preferences.filterChapterByDownloaded().get() fun bookmarkedFilter(preferences: PreferencesHelper): Int = - bookmarkedFilter(preferences.filterChapterByBookmarked().get()) - - fun bookmarkedFilter(defaultFilter: Int): Int { - return if (usesLocalFilter()) bookmarkedFilter else defaultFilter - } + if (usesLocalFilter) bookmarkedFilter else preferences.filterChapterByBookmarked().get() fun hideChapterTitle(preferences: PreferencesHelper): Boolean = - hideChapterTitle(preferences.hideChapterTitlesByDefault().get()) - - fun hideChapterTitle(default: Boolean): Boolean { - return if (usesLocalFilter()) hideChapterTitles() else default - } + if (usesLocalFilter) hideChapterTitles else preferences.hideChapterTitlesByDefault().get() fun showChapterTitle(defaultShow: Boolean): Boolean = chapter_flags and CHAPTER_DISPLAY_MASK == CHAPTER_DISPLAY_NUMBER 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 b9e03faf36..78dba72b07 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 @@ -241,6 +241,8 @@ object PreferenceKeys { const val hideChapterTitles = "hide_chapter_titles" + const val chaptersDescAsDefault = "chapters_desc_as_default" + fun trackUsername(syncId: Int) = "pref_mangasync_username_$syncId" fun trackPassword(syncId: Int) = "pref_mangasync_password_$syncId" 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 086c2008ef..37eaa43dec 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 @@ -434,7 +434,7 @@ class PreferencesHelper(val context: Context) { fun hideChapterTitlesByDefault() = flowPrefs.getBoolean(Keys.hideChapterTitles, false) - fun chaptersDescAsDefault() = rxPrefs.getBoolean("chapters_desc_as_default", true) + fun chaptersDescAsDefault() = flowPrefs.getBoolean(Keys.chaptersDescAsDefault, true) fun sortChapterByAscendingOrDescending() = prefs.getInt(Keys.defaultChapterSortByAscendingOrDescending, Manga.CHAPTER_SORT_DESC) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsPresenter.kt index e62b708ba5..44efd4ed93 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsPresenter.kt @@ -27,7 +27,6 @@ import eu.kanade.tachiyomi.data.library.CustomMangaManager import eu.kanade.tachiyomi.data.library.LibraryServiceListener import eu.kanade.tachiyomi.data.library.LibraryUpdateService import eu.kanade.tachiyomi.data.preference.PreferencesHelper -import eu.kanade.tachiyomi.data.preference.getOrDefault import eu.kanade.tachiyomi.data.track.TrackManager import eu.kanade.tachiyomi.data.track.TrackService import eu.kanade.tachiyomi.data.track.UnattendedTrackService @@ -86,7 +85,7 @@ class MangaDetailsPresenter( private val customMangaManager: CustomMangaManager by injectLazy() private val mangaShortcutManager: MangaShortcutManager by injectLazy() - private val chapterSort by lazy { ChapterSort(manga, chapterFilter, preferences) } + private val chapterSort = ChapterSort(manga, chapterFilter, preferences) var isLockedFromSearch = false var hasRequested = false @@ -214,9 +213,9 @@ class MangaDetailsPresenter( /** * Whether the sorting method is descending or ascending. */ - fun sortDescending() = manga.sortDescending(globalSort()) + fun sortDescending() = manga.sortDescending(preferences) - fun sortingOrder() = manga.chapterOrder(globalSorting()) + fun sortingOrder() = manga.chapterOrder(preferences) /** * Applies the view filters to the list of chapters obtained from the database. @@ -504,12 +503,11 @@ class MangaDetailsPresenter( asyncUpdateMangaAndChapters() } - private fun globalSort(): Boolean = preferences.chaptersDescAsDefault().getOrDefault() - - private fun globalSorting(): Int = preferences.sortChapterOrder().get() - fun mangaSortMatchesDefault(): Boolean { - return (manga.sortDescending() == globalSort() && manga.sorting == globalSorting()) || !manga.usesLocalSort() + return ( + manga.sortDescending == preferences.chaptersDescAsDefault().get() && + manga.sorting == preferences.sortChapterOrder().get() + ) || !manga.usesLocalSort } fun mangaFilterMatchesDefault(): Boolean { @@ -517,8 +515,8 @@ class MangaDetailsPresenter( manga.readFilter == preferences.filterChapterByRead().get() && manga.downloadedFilter == preferences.filterChapterByDownloaded().get() && manga.bookmarkedFilter == preferences.filterChapterByBookmarked().get() && - manga.hideChapterTitles() == preferences.hideChapterTitlesByDefault().get() - ) || !manga.usesLocalFilter() + manga.hideChapterTitles == preferences.hideChapterTitlesByDefault().get() + ) || !manga.usesLocalFilter } fun resetSortingToDefault() { @@ -601,7 +599,7 @@ class MangaDetailsPresenter( else -> Manga.SHOW_ALL } ) - preferences.hideChapterTitlesByDefault().set(manga.hideChapterTitles()) + preferences.hideChapterTitlesByDefault().set(manga.hideChapterTitles) manga.setFilterToGlobal() asyncUpdateMangaAndChapters() } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersSortBottomSheet.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersSortBottomSheet.kt index 5ee48fb7e0..86f111bc25 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersSortBottomSheet.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersSortBottomSheet.kt @@ -97,7 +97,7 @@ class ChaptersSortBottomSheet(controller: MangaDetailsController) : binding.setAsDefaultSort.setOnClickListener { presenter.setGlobalChapterSort( presenter.manga.sorting, - presenter.manga.sortDescending() + presenter.manga.sortDescending ) binding.setAsDefaultSort.isInvisible = true binding.resetAsDefaultSort.isInvisible = true 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 116f22ab0f..5f8034db80 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 @@ -118,7 +118,7 @@ class ReaderPresenter( val chaptersForReader = chapterFilter.filterChaptersForReader(dbChapters, manga, selectedChapter) - when (manga.chapterOrder(preferences.sortChapterOrder().get())) { + when (manga.chapterOrder(preferences)) { Manga.CHAPTER_SORTING_SOURCE -> ChapterLoadBySource().get(chaptersForReader) Manga.CHAPTER_SORTING_NUMBER -> ChapterLoadByNumber().get(chaptersForReader, selectedChapter) Manga.CHAPTER_SORTING_UPLOAD_DATE -> ChapterLoadByDate().get(chaptersForReader) diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/chapter/ChapterSort.kt b/app/src/main/java/eu/kanade/tachiyomi/util/chapter/ChapterSort.kt index f41013718d..ddb91d2773 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/chapter/ChapterSort.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/chapter/ChapterSort.kt @@ -3,7 +3,6 @@ package eu.kanade.tachiyomi.util.chapter import eu.kanade.tachiyomi.data.database.models.Chapter import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.preference.PreferencesHelper -import eu.kanade.tachiyomi.data.preference.getOrDefault import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get @@ -25,8 +24,6 @@ class ChapterSort(val manga: Manga, val chapterFilter: ChapterFilter = Injekt.ge else -> rawChapters } - val sortDescending = - manga.sortDescending(preferences.chaptersDescAsDefault().getOrDefault()) return chapters.sortedWith(sortComparator()) } @@ -40,9 +37,9 @@ class ChapterSort(val manga: Manga, val chapterFilter: ChapterFilter = Injekt.ge fun sortComparator(ignoreAsc: Boolean = false): Comparator { val sortDescending = !ignoreAsc && - manga.sortDescending(preferences.chaptersDescAsDefault().getOrDefault()) + manga.sortDescending(preferences) val sortFunction: (T, T) -> Int = - when (manga.chapterOrder(preferences.sortChapterOrder().get())) { + when (manga.chapterOrder(preferences)) { Manga.CHAPTER_SORTING_SOURCE -> when (sortDescending) { true -> { c1, c2 -> c1.source_order.compareTo(c2.source_order) } false -> { c1, c2 -> c2.source_order.compareTo(c1.source_order) }