Browse Source

add sort by date fetched in library (#4773)

* add sort by date fetched in library

* chapter fetch date to 8
Riztard Lanthorn 4 years ago
parent
commit
ddd4cc10ff

+ 10 - 0
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()
 }

+ 10 - 0
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.
  */

+ 11 - 0
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")
             }

+ 5 - 1
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")
                     }

+ 1 - 0
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")

+ 1 - 0
app/src/main/res/values/strings.xml

@@ -42,6 +42,7 @@
     <string name="action_sort_last_read">Last read</string>
     <string name="action_sort_last_checked">Last checked</string>
     <string name="action_sort_latest_chapter">Latest chapter</string>
+    <string name="action_sort_chapter_fetch_date">Date fetched</string>
     <string name="action_sort_date_added">Date added</string>
     <string name="action_search">Search</string>
     <string name="action_search_settings">Search settings</string>