Adding last fetched sort

This commit is contained in:
Jays2Kings 2021-04-26 15:54:39 -04:00
parent 6632ea2d2f
commit 1ceb9692bb
9 changed files with 74 additions and 8 deletions

View File

@ -42,6 +42,7 @@ interface Category : Serializable {
TOTAL_ASC, TOTAL_DSC -> LibrarySort.TOTAL TOTAL_ASC, TOTAL_DSC -> LibrarySort.TOTAL
DRAG_AND_DROP -> LibrarySort.DRAG_AND_DROP DRAG_AND_DROP -> LibrarySort.DRAG_AND_DROP
DATE_ADDED_ASC, DATE_ADDED_DSC -> LibrarySort.DATE_ADDED DATE_ADDED_ASC, DATE_ADDED_DSC -> LibrarySort.DATE_ADDED
LAST_FETCHED_ASC, LAST_FETCHED_DSC -> LibrarySort.LAST_FETCHED
else -> if (nullAsDND && !isDynamic) LibrarySort.DRAG_AND_DROP else null else -> if (nullAsDND && !isDynamic) LibrarySort.DRAG_AND_DROP else null
} }
@ -55,6 +56,7 @@ interface Category : Serializable {
LAST_READ_ASC, LAST_READ_DSC -> R.string.last_read LAST_READ_ASC, LAST_READ_DSC -> R.string.last_read
TOTAL_ASC, TOTAL_DSC -> R.string.total_chapters TOTAL_ASC, TOTAL_DSC -> R.string.total_chapters
DATE_ADDED_ASC, DATE_ADDED_DSC -> R.string.date_added DATE_ADDED_ASC, DATE_ADDED_DSC -> R.string.date_added
LAST_FETCHED_ASC, LAST_FETCHED_DSC -> R.string.last_fetched
else -> if (isDynamic) R.string.category else R.string.drag_and_drop else -> if (isDynamic) R.string.category else R.string.drag_and_drop
} }
@ -65,6 +67,7 @@ interface Category : Serializable {
LAST_READ_ASC, LAST_READ_DSC -> 3 LAST_READ_ASC, LAST_READ_DSC -> 3
TOTAL_ASC, TOTAL_DSC -> 4 TOTAL_ASC, TOTAL_DSC -> 4
DATE_ADDED_ASC, DATE_ADDED_DSC -> 5 DATE_ADDED_ASC, DATE_ADDED_DSC -> 5
LAST_FETCHED_ASC, LAST_FETCHED_DSC -> 6
else -> null else -> null
} }
@ -76,6 +79,7 @@ interface Category : Serializable {
LibrarySort.LAST_READ -> LAST_READ_ASC LibrarySort.LAST_READ -> LAST_READ_ASC
LibrarySort.TOTAL -> ALPHA_ASC LibrarySort.TOTAL -> ALPHA_ASC
LibrarySort.DATE_ADDED -> DATE_ADDED_ASC LibrarySort.DATE_ADDED -> DATE_ADDED_ASC
LibrarySort.LAST_FETCHED -> LAST_FETCHED_ASC
else -> ALPHA_ASC else -> ALPHA_ASC
} }
} }
@ -94,6 +98,8 @@ interface Category : Serializable {
const val TOTAL_DSC = 'j' const val TOTAL_DSC = 'j'
const val DATE_ADDED_ASC = 'k' const val DATE_ADDED_ASC = 'k'
const val DATE_ADDED_DSC = 'l' const val DATE_ADDED_DSC = 'l'
const val LAST_FETCHED_ASC = 'm'
const val LAST_FETCHED_DSC = 'n'
fun create(name: String): Category = CategoryImpl().apply { fun create(name: String): Category = CategoryImpl().apply {
this.name = name this.name = name
@ -113,6 +119,7 @@ interface Category : Serializable {
LibrarySort.LAST_READ -> LAST_READ_ASC LibrarySort.LAST_READ -> LAST_READ_ASC
LibrarySort.TOTAL -> TOTAL_ASC LibrarySort.TOTAL -> TOTAL_ASC
LibrarySort.DATE_ADDED -> DATE_ADDED_ASC LibrarySort.DATE_ADDED -> DATE_ADDED_ASC
LibrarySort.LAST_FETCHED -> LAST_FETCHED_ASC
LibrarySort.DRAG_AND_DROP -> DRAG_AND_DROP LibrarySort.DRAG_AND_DROP -> DRAG_AND_DROP
else -> DRAG_AND_DROP else -> DRAG_AND_DROP
} }

View File

@ -146,6 +146,16 @@ interface MangaQueries : DbProvider {
) )
.prepare() .prepare()
fun getLastFetchedManga() = db.get()
.listOfObjects(Manga::class.java)
.withQuery(
RawQuery.builder()
.query(getLastFetchedMangaQuery())
.observesTables(MangaTable.TABLE)
.build()
)
.prepare()
fun getTotalChapterManga() = db.get().listOfObjects(Manga::class.java) fun getTotalChapterManga() = db.get().listOfObjects(Manga::class.java)
.withQuery(RawQuery.builder().query(getTotalChapterMangaQuery()).observesTables(MangaTable.TABLE).build()).prepare() .withQuery(RawQuery.builder().query(getTotalChapterMangaQuery()).observesTables(MangaTable.TABLE).build()).prepare()
} }

View File

@ -276,6 +276,17 @@ fun getLastReadMangaQuery() =
ORDER BY max DESC ORDER BY max DESC
""" """
fun getLastFetchedMangaQuery() =
"""
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}
WHERE ${Manga.TABLE}.${Manga.COL_FAVORITE} = 1
GROUP BY ${Manga.TABLE}.${Manga.COL_ID}
ORDER BY max DESC
"""
fun getTotalChapterMangaQuery() = fun getTotalChapterMangaQuery() =
""" """
SELECT ${Manga.TABLE}.* SELECT ${Manga.TABLE}.*

View File

@ -130,8 +130,10 @@ class MaterialMenuSheet(
if (clearAll) { if (clearAll) {
clearEndDrawables() clearEndDrawables()
} }
itemAdapter.getAdapterItem(id).sheetItem.endDrawableRes = drawableRes val pos = itemAdapter.getAdapterPosition(id.toLong())
itemAdapter.getAdapterItem(id).isSelected = true val item = itemAdapter.getAdapterItem(pos)
item.sheetItem.endDrawableRes = drawableRes
item.isSelected = true
fastAdapter.notifyAdapterDataSetChanged() fastAdapter.notifyAdapterDataSetChanged()
} }

View File

@ -150,14 +150,27 @@ class LibraryCategoryAdapter(val controller: LibraryController) :
else title.take(10) else title.take(10)
} }
} }
LibrarySort.LAST_FETCHED -> {
val id = item.manga.id ?: return ""
val history = db.getChapters(id).executeAsBlocking()
val last = history.maxOfOrNull { it.date_fetch }
if (last != null && last > 100) {
recyclerView.context.getString(
R.string.fetched_,
last.timeSpanFromNow(preferences.context)
)
} else {
"N/A"
}
}
LibrarySort.LAST_READ -> { LibrarySort.LAST_READ -> {
val id = item.manga.id ?: return "" val id = item.manga.id ?: return ""
val history = db.getHistoryByMangaId(id).executeAsBlocking() val history = db.getHistoryByMangaId(id).executeAsBlocking()
val last = history.maxBy { it.last_read } val last = history.maxOfOrNull { it.last_read }
if (last != null && last.last_read > 100) { if (last != null && last > 100) {
recyclerView.context.getString( recyclerView.context.getString(
R.string.read_, R.string.read_,
last.last_read.timeSpanFromNow(preferences.context) last.timeSpanFromNow(preferences.context)
) )
} else { } else {
"N/A" "N/A"

View File

@ -92,7 +92,11 @@ class LibraryHeaderHolder(val view: View, private val adapter: LibraryCategoryAd
val sortingMode = category.sortingMode() val sortingMode = category.sortingMode()
val sortDrawable = when { val sortDrawable = when {
sortingMode == LibrarySort.DRAG_AND_DROP || sortingMode == null -> R.drawable.ic_sort_24dp sortingMode == LibrarySort.DRAG_AND_DROP || sortingMode == null -> R.drawable.ic_sort_24dp
if (sortingMode == LibrarySort.DATE_ADDED || sortingMode == LibrarySort.LATEST_CHAPTER || sortingMode == LibrarySort.LAST_READ) !isAscending else isAscending -> R.drawable.ic_arrow_downward_24dp if (sortingMode == LibrarySort.DATE_ADDED ||
sortingMode == LibrarySort.LATEST_CHAPTER ||
sortingMode == LibrarySort.LAST_READ ||
sortingMode == LibrarySort.LAST_FETCHED
) !isAscending else isAscending -> R.drawable.ic_arrow_downward_24dp
else -> R.drawable.ic_arrow_upward_24dp else -> R.drawable.ic_arrow_upward_24dp
} }
@ -158,6 +162,11 @@ class LibraryHeaderHolder(val view: View, private val adapter: LibraryCategoryAd
R.drawable.ic_new_releases_24dp, R.drawable.ic_new_releases_24dp,
R.string.latest_chapter R.string.latest_chapter
), ),
MaterialMenuSheet.MenuSheetItem(
LibrarySort.LAST_FETCHED,
R.drawable.ic_check_24dp,
R.string.last_fetched
),
MaterialMenuSheet.MenuSheetItem( MaterialMenuSheet.MenuSheetItem(
LibrarySort.UNREAD, LibrarySort.UNREAD,
R.drawable.ic_eye_24dp, R.drawable.ic_eye_24dp,
@ -216,7 +225,8 @@ class LibraryHeaderHolder(val view: View, private val adapter: LibraryCategoryAd
sortingMode == LibrarySort.DRAG_AND_DROP -> R.drawable.ic_check_24dp sortingMode == LibrarySort.DRAG_AND_DROP -> R.drawable.ic_check_24dp
if (sortingMode == LibrarySort.DATE_ADDED || if (sortingMode == LibrarySort.DATE_ADDED ||
sortingMode == LibrarySort.LATEST_CHAPTER || sortingMode == LibrarySort.LATEST_CHAPTER ||
sortingMode == LibrarySort.LAST_READ sortingMode == LibrarySort.LAST_READ ||
sortingMode == LibrarySort.LAST_FETCHED
) !isAscending else isAscending -> ) !isAscending else isAscending ->
R.drawable.ic_arrow_downward_24dp R.drawable.ic_arrow_downward_24dp
else -> R.drawable.ic_arrow_upward_24dp else -> R.drawable.ic_arrow_upward_24dp
@ -233,6 +243,7 @@ class LibraryHeaderHolder(val view: View, private val adapter: LibraryCategoryAd
adapter.libraryListener.sortCategory(category.id!!, 'D' - 'a' + 1) adapter.libraryListener.sortCategory(category.id!!, 'D' - 'a' + 1)
return return
} }
LibrarySort.LAST_FETCHED -> 6
LibrarySort.DATE_ADDED -> 5 LibrarySort.DATE_ADDED -> 5
LibrarySort.TOTAL -> 4 LibrarySort.TOTAL -> 4
LibrarySort.LAST_READ -> 3 LibrarySort.LAST_READ -> 3

View File

@ -343,6 +343,11 @@ class LibraryPresenter(
db.getLastReadManga().executeAsBlocking().associate { it.id!! to counter++ } db.getLastReadManga().executeAsBlocking().associate { it.id!! to counter++ }
} }
val lastFetchedManga by lazy {
var counter = 0
db.getLastFetchedManga().executeAsBlocking().associate { it.id!! to counter++ }
}
val sortFn: (LibraryItem, LibraryItem) -> Int = { i1, i2 -> val sortFn: (LibraryItem, LibraryItem) -> Int = { i1, i2 ->
if (i1.header.category.id == i2.header.category.id) { if (i1.header.category.id == i2.header.category.id) {
val category = i1.header.category val category = i1.header.category
@ -371,6 +376,11 @@ class LibraryPresenter(
LibrarySort.TOTAL -> { LibrarySort.TOTAL -> {
i1.manga.totalChapters.compareTo(i2.manga.totalChapters) i1.manga.totalChapters.compareTo(i2.manga.totalChapters)
} }
LibrarySort.LAST_FETCHED -> {
val manga1LastRead = lastFetchedManga[i1.manga.id!!] ?: lastFetchedManga.size
val manga2LastRead = lastFetchedManga[i2.manga.id!!] ?: lastFetchedManga.size
manga1LastRead.compareTo(manga2LastRead)
}
LibrarySort.DATE_ADDED -> i2.manga.date_added.compareTo(i1.manga.date_added) LibrarySort.DATE_ADDED -> i2.manga.date_added.compareTo(i1.manga.date_added)
else -> { else -> {
if (LibrarySort.DRAG_AND_DROP == category.sortingMode() && category.isDynamic) { if (LibrarySort.DRAG_AND_DROP == category.sortingMode() && category.isDynamic) {

View File

@ -8,5 +8,6 @@ object LibrarySort {
const val UNREAD = 3 const val UNREAD = 3
const val TOTAL = 4 const val TOTAL = 4
const val DATE_ADDED = 5 const val DATE_ADDED = 5
const val DRAG_AND_DROP = 6 const val LAST_FETCHED = 6
const val DRAG_AND_DROP = 7
} }

View File

@ -146,6 +146,7 @@
<string name="total_chapters">Total chapters</string> <string name="total_chapters">Total chapters</string>
<string name="date_added">Date added</string> <string name="date_added">Date added</string>
<string name="last_read">Last read</string> <string name="last_read">Last read</string>
<string name="last_fetched">Last fetched</string>
<string name="last_updated">Last Updated</string> <string name="last_updated">Last Updated</string>
<string name="latest_chapter">Latest chapter</string> <string name="latest_chapter">Latest chapter</string>
<string name="drag_and_drop">Drag &amp; Drop</string> <string name="drag_and_drop">Drag &amp; Drop</string>