Refactoring the logic for the last few commits

This commit is contained in:
Jays2Kings 2021-07-11 18:18:09 -04:00
parent 7ad228d19f
commit 5b98b09ce3
7 changed files with 34 additions and 50 deletions

View File

@ -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

View File

@ -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"

View File

@ -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)
}

View File

@ -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()
}

View File

@ -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

View File

@ -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)

View File

@ -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 <T : Chapter> sortComparator(ignoreAsc: Boolean = false): Comparator<T> {
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) }