From ddd4cc10ff12d2497a001821c678f2c1a90eef34 Mon Sep 17 00:00:00 2001 From: Riztard Lanthorn Date: Mon, 5 Apr 2021 04:18:28 +0700 Subject: [PATCH] add sort by date fetched in library (#4773) * add sort by date fetched in library * chapter fetch date to 8 --- .../tachiyomi/data/database/queries/MangaQueries.kt | 10 ++++++++++ .../tachiyomi/data/database/queries/RawQueries.kt | 10 ++++++++++ .../kanade/tachiyomi/ui/library/LibraryPresenter.kt | 11 +++++++++++ .../tachiyomi/ui/library/LibrarySettingsSheet.kt | 6 +++++- .../eu/kanade/tachiyomi/ui/library/LibrarySort.kt | 1 + app/src/main/res/values/strings.xml | 1 + 6 files changed, 38 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/MangaQueries.kt b/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/MangaQueries.kt index 99ae7cbb84..ec305d8d06 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/MangaQueries.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/MangaQueries.kt @@ -164,4 +164,14 @@ interface MangaQueries : DbProvider { .build() ) .prepare() + + fun getChapterFetchDateManga() = db.get() + .listOfObjects(Manga::class.java) + .withQuery( + RawQuery.builder() + .query(getChapterFetchDateMangaQuery()) + .observesTables(MangaTable.TABLE) + .build() + ) + .prepare() } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/RawQueries.kt b/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/RawQueries.kt index 2d88c96c80..dac40b4491 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/RawQueries.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/RawQueries.kt @@ -123,6 +123,16 @@ fun getLatestChapterMangaQuery() = ORDER by max DESC """ +fun getChapterFetchDateMangaQuery() = + """ + SELECT ${Manga.TABLE}.*, MAX(${Chapter.TABLE}.${Chapter.COL_DATE_FETCH}) AS max + FROM ${Manga.TABLE} + JOIN ${Chapter.TABLE} + ON ${Manga.TABLE}.${Manga.COL_ID} = ${Chapter.TABLE}.${Chapter.COL_MANGA_ID} + GROUP BY ${Manga.TABLE}.${Manga.COL_ID} + ORDER by max DESC +""" + /** * Query to get the categories for a manga. */ diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt index 4f3cf065f6..d7d2804459 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt @@ -235,6 +235,10 @@ class LibraryPresenter( var counter = 0 db.getLatestChapterManga().executeAsBlocking().associate { it.id!! to counter++ } } + val chapterFetchDateManga by lazy { + var counter = 0 + db.getChapterFetchDateManga().executeAsBlocking().associate { it.id!! to counter++ } + } val sortAscending = preferences.librarySortingAscending().get() val sortFn: (LibraryItem, LibraryItem) -> Int = { i1, i2 -> @@ -266,6 +270,13 @@ class LibraryPresenter( ?: latestChapterManga.size manga1latestChapter.compareTo(manga2latestChapter) } + LibrarySort.CHAPTER_FETCH_DATE -> { + val manga1chapterFetchDate = chapterFetchDateManga[i1.manga.id!!] + ?: chapterFetchDateManga.size + val manga2chapterFetchDate = chapterFetchDateManga[i2.manga.id!!] + ?: chapterFetchDateManga.size + manga1chapterFetchDate.compareTo(manga2chapterFetchDate) + } LibrarySort.DATE_ADDED -> i2.manga.date_added.compareTo(i1.manga.date_added) else -> throw Exception("Unknown sorting mode") } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibrarySettingsSheet.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibrarySettingsSheet.kt index 607f03f02e..44ec85ae22 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibrarySettingsSheet.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibrarySettingsSheet.kt @@ -157,11 +157,12 @@ class LibrarySettingsSheet( private val lastChecked = Item.MultiSort(R.string.action_sort_last_checked, this) private val unread = Item.MultiSort(R.string.action_filter_unread, this) private val latestChapter = Item.MultiSort(R.string.action_sort_latest_chapter, this) + private val chapterFetchDate = Item.MultiSort(R.string.action_sort_chapter_fetch_date, this) private val dateAdded = Item.MultiSort(R.string.action_sort_date_added, this) override val header = null override val items = - listOf(alphabetically, lastRead, lastChecked, unread, total, latestChapter, dateAdded) + listOf(alphabetically, lastRead, lastChecked, unread, total, latestChapter, chapterFetchDate, dateAdded) override val footer = null override fun initModels() { @@ -184,6 +185,8 @@ class LibrarySettingsSheet( if (sorting == LibrarySort.TOTAL) order else Item.MultiSort.SORT_NONE latestChapter.state = if (sorting == LibrarySort.LATEST_CHAPTER) order else Item.MultiSort.SORT_NONE + chapterFetchDate.state = + if (sorting == LibrarySort.CHAPTER_FETCH_DATE) order else Item.MultiSort.SORT_NONE dateAdded.state = if (sorting == LibrarySort.DATE_ADDED) order else Item.MultiSort.SORT_NONE } @@ -211,6 +214,7 @@ class LibrarySettingsSheet( unread -> LibrarySort.UNREAD total -> LibrarySort.TOTAL latestChapter -> LibrarySort.LATEST_CHAPTER + chapterFetchDate -> LibrarySort.CHAPTER_FETCH_DATE dateAdded -> LibrarySort.DATE_ADDED else -> throw Exception("Unknown sorting") } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibrarySort.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibrarySort.kt index 5f6e6da8c3..28e583c84a 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibrarySort.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibrarySort.kt @@ -8,6 +8,7 @@ object LibrarySort { const val UNREAD = 3 const val TOTAL = 4 const val LATEST_CHAPTER = 6 + const val CHAPTER_FETCH_DATE = 8 const val DATE_ADDED = 7 @Deprecated("Removed in favor of searching by source") diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c1bfc7fcbc..7a52414111 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -42,6 +42,7 @@ Last read Last checked Latest chapter + Date fetched Date added Search Search settings