Total chapter count shown when sorting by total chapters

This commit is contained in:
Jay 2020-02-21 19:59:54 -08:00
parent 8e8cbbca3b
commit 3940d42185
8 changed files with 81 additions and 44 deletions

View File

@ -4,6 +4,7 @@ import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.View
import androidx.core.content.ContextCompat
import com.google.android.material.card.MaterialCardView
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.util.system.dpToPx
@ -14,14 +15,18 @@ import kotlinx.android.synthetic.main.unread_download_badge.view.*
class LibraryBadge @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null):
MaterialCardView(context, attrs) {
fun setUnreadDownload(unread: Int, downloads: Int) {
fun setUnreadDownload(unread: Int, downloads: Int, showTotalChapters: Boolean) {
// Update the unread count and its visibility.
with(unread_text) {
text = if (unread == -1) "0" else unread.toString()
setTextColor(if (unread == -1) context.getResourceColor(android.R.attr.colorAccent)
setTextColor(if (unread == -1 && !showTotalChapters)
context.getResourceColor(android.R.attr.colorAccent)
else Color.WHITE)
setBackgroundColor(
if (showTotalChapters) ContextCompat.getColor(context, R.color.md_red_500)
else context.getResourceColor(android.R.attr.colorAccent))
visibility = when {
unread > 0 || unread == -1 -> View.VISIBLE
unread > 0 || unread == -1 || showTotalChapters -> View.VISIBLE
else -> View.GONE
}
}
@ -41,7 +46,9 @@ class LibraryBadge @JvmOverloads constructor(context: Context, attrs: AttributeS
// Show the angles divider if both unread and downloads exists
unread_angle.visibility = if (download_text.visibility == View.VISIBLE && unread_text
.visibility != View.GONE) View.VISIBLE else View.GONE
unread_angle.setColorFilter(
if (showTotalChapters) ContextCompat.getColor(context, R.color.md_red_500)
else context.getResourceColor(android.R.attr.colorAccent))
if (unread_angle.visibility == View.VISIBLE) {
download_text.updatePaddingRelative(end = 8.dpToPx)
unread_text.updatePaddingRelative(start = 2.dpToPx)

View File

@ -7,7 +7,6 @@ import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.signature.ObjectKey
import eu.kanade.tachiyomi.data.database.models.MangaImpl
import eu.kanade.tachiyomi.data.glide.GlideApp
import eu.kanade.tachiyomi.source.LocalSource
import eu.kanade.tachiyomi.util.view.gone
import kotlinx.android.synthetic.main.catalogue_grid_item.*
import kotlinx.android.synthetic.main.unread_download_badge.*
@ -80,17 +79,7 @@ class LibraryGridHolder(
compact_title.text = title.text
badge_view.setUnreadDownload(
when (item.unreadType) {
1 -> item.manga.unread
0 -> if (item.manga.unread > 0) -1 else -2
else -> -2
},
when {
item.downloadCount == -1 -> -1
item.manga.source == LocalSource.ID -> -2
else -> item.downloadCount
})
setUnreadBadge(badge_view, item)
play_layout.visibility = if (item.manga.unread > 0 && item.unreadType > -1)
View.VISIBLE else View.GONE

View File

@ -1,11 +1,8 @@
package eu.kanade.tachiyomi.ui.library
import android.view.View
import androidx.core.content.ContextCompat
import eu.davidea.flexibleadapter.FlexibleAdapter
import eu.kanade.tachiyomi.source.LocalSource
import eu.kanade.tachiyomi.ui.base.holder.BaseFlexibleViewHolder
import androidx.recyclerview.widget.RecyclerView
import eu.davidea.flexibleadapter.items.IFlexible
/**
* Generic class used to hold the displayed data of a manga in the library.
@ -28,6 +25,22 @@ abstract class LibraryHolder(
abstract fun onSetValues(item: LibraryItem)
fun setUnreadBadge(badge: LibraryBadge, item: LibraryItem) {
badge.setUnreadDownload(
when {
item.chapterCount > -1 -> item.chapterCount
item.unreadType == 1 -> item.manga.unread
item.unreadType == 0 -> if (item.manga.unread > 0) -1 else -2
else -> -2
},
when {
item.downloadCount == -1 -> -1
item.manga.source == LocalSource.ID -> -2
else -> item.downloadCount
},
item.chapterCount > -1)
}
/**
* Called when an item is released.
*

View File

@ -28,6 +28,7 @@ class LibraryItem(val manga: LibraryManga,
var downloadCount = -1
var unreadType = 1
var chapterCount = -1
override fun getLayoutRes(): Int {
return if (libraryLayout.getOrDefault() == 0)

View File

@ -7,7 +7,6 @@ import com.bumptech.glide.signature.ObjectKey
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.models.MangaImpl
import eu.kanade.tachiyomi.data.glide.GlideApp
import eu.kanade.tachiyomi.source.LocalSource
import kotlinx.android.synthetic.main.catalogue_list_item.*
import kotlinx.android.synthetic.main.catalogue_list_item.view.*
import kotlinx.android.synthetic.main.unread_download_badge.*
@ -36,18 +35,7 @@ class LibraryListHolder(
override fun onSetValues(item: LibraryItem) {
// Update the title of the manga.
title.text = item.manga.currentTitle()
badge_view.setUnreadDownload(
when (item.unreadType) {
1 -> item.manga.unread
0 -> if (item.manga.unread > 0) -1 else -2
else -> -2
},
when {
item.downloadCount == -1 -> -1
item.manga.source == LocalSource.ID -> -2
else -> item.downloadCount
})
setUnreadBadge(badge_view, item)
subtitle.text = item.manga.originalAuthor()?.trim()
subtitle.visibility = if (!item.manga.originalAuthor().isNullOrBlank()) View.VISIBLE

View File

@ -79,6 +79,8 @@ class LibraryPresenter(
private var currentMangaMap:LibraryMap? = null
private var totalChapters:Map<Long, Int>? = null
fun isDownloading() = downloadManager.hasQueue()
fun onDestroy() {
@ -94,6 +96,7 @@ class LibraryPresenter(
fun getLibrary() {
launchUI {
totalChapters = null
val freshStart = !preferences.libraryAsSingleList().getOrDefault()
&& (currentMangaMap?.values?.firstOrNull()?.firstOrNull()?.header != null)
val mangaMap = withContext(Dispatchers.IO) {
@ -107,6 +110,9 @@ class LibraryPresenter(
}
currentMangaMap = mangaMap
updateView(categories, mangaMap, freshStart)
withContext(Dispatchers.IO) {
setTotalChapters()
}
}
}
@ -254,6 +260,8 @@ class LibraryPresenter(
}
val sortFn: (LibraryItem, LibraryItem) -> Int = { i1, i2 ->
i1.chapterCount = -1
i2.chapterCount = -1
sortCategory(i1, i2, lastReadManga, category)
}
val comparator = Comparator(sortFn)
@ -273,16 +281,14 @@ class LibraryPresenter(
var counter = 0
db.getLastReadManga().executeAsBlocking().associate { it.id!! to counter++ }
}
val totalChapterManga by lazy {
var counter = 0
db.getTotalChapterManga().executeAsBlocking().associate { it.id!! to counter++ }
}
val ascending = preferences.librarySortingAscending().getOrDefault()
val useDnD = preferences.libraryAsSingleList().getOrDefault() && !preferences
.hideCategories().getOrDefault()
val sortFn: (LibraryItem, LibraryItem) -> Int = { i1, i2 ->
i1.chapterCount = -1
i2.chapterCount = -1
val compare = when {
sortingMode == LibrarySort.DRAG_AND_DROP || useDnD ->
sortCategory(i1, i2, lastReadManga)
@ -303,8 +309,11 @@ class LibraryPresenter(
else -> i1.manga.unread.compareTo(i2.manga.unread)
}
sortingMode == LibrarySort.TOTAL -> {
val manga1TotalChapter = totalChapterManga[i1.manga.id!!] ?: 0
val mange2TotalChapter = totalChapterManga[i2.manga.id!!] ?: 0
setTotalChapters()
val manga1TotalChapter = totalChapters!![i1.manga.id!!] ?: 0
val mange2TotalChapter = totalChapters!![i2.manga.id!!] ?: 0
i1.chapterCount = totalChapters!![i1.manga.id!!] ?: 0
i2.chapterCount = totalChapters!![i2.manga.id!!] ?: 0
manga1TotalChapter.compareTo(mange2TotalChapter)
}
else -> 0
@ -324,11 +333,23 @@ class LibraryPresenter(
return map.mapValues { entry -> entry.value.sortedWith(comparator) }
}
private fun setTotalChapters() {
if (totalChapters != null) return
val mangaMap = rawMangaMap ?: return
totalChapters = mangaMap.flatMap{
it.value
}.associate {
it.manga.id!! to db.getChapters(it.manga).executeAsBlocking().size
}
}
private fun sortCategory(i1: LibraryItem, i2: LibraryItem,
lastReadManga: Map<Long, Int>, initCat: Category? = null): Int {
lastReadManga: Map<Long, Int>,
initCat: Category? = null):
Int {
return if (initCat != null || i1.manga.category == i2.manga.category) {
val category = initCat ?: allCategories.find { it.id == i1.manga.category }
when {
val compare = when {
category?.mangaSort != null -> {
var sort = when (category.sortingMode()) {
LibrarySort.ALPHA -> sortAlphabetical(i1, i2)
@ -344,6 +365,14 @@ class LibraryPresenter(
val manga2LastRead = lastReadManga[i2.manga.id!!] ?: lastReadManga.size
manga1LastRead.compareTo(manga2LastRead)
}
LibrarySort.TOTAL -> {
setTotalChapters()
val manga1TotalChapter = totalChapters!![i1.manga.id!!] ?: 0
val mange2TotalChapter = totalChapters!![i2.manga.id!!] ?: 0
i1.chapterCount = totalChapters!![i1.manga.id!!] ?: 0
i2.chapterCount = totalChapters!![i2.manga.id!!] ?: 0
manga1TotalChapter.compareTo(mange2TotalChapter)
}
else -> sortAlphabetical(i1, i2)
}
if (!category.isAscending())
@ -363,6 +392,11 @@ class LibraryPresenter(
}
else -> 0
}
if (compare == 0) {
if (category?.isAscending() != false) sortAlphabetical(i1, i2)
else sortAlphabetical(i2, i1)
}
else compare
}
else {
val category = allCategories.find { it.id == i1.manga.category }?.order ?: -1
@ -620,9 +654,7 @@ class LibraryPresenter(
}
}
}
rawMangaMap = rawMap
currentMangaMap = currentMap
requestSortUpdate()
getLibrary()
}
}

View File

@ -516,6 +516,7 @@ class SortFilterBottomSheet @JvmOverloads constructor(context: Context, attrs: A
}
else {
val order = when (menuId) {
R.id.action_total_chaps -> 4
R.id.action_last_read -> 3
R.id.action_unread -> 2
R.id.action_update -> 1

View File

@ -16,8 +16,14 @@
android:id="@+id/action_update"
android:title="@string/action_sort_last_updated"
android:icon="@drawable/ic_blank_24dp"/>
<item
android:id="@+id/action_unread"
android:title="@string/action_filter_unread"
android:icon="@drawable/ic_blank_24dp"/>
<item
android:id="@+id/action_total_chaps"
android:title="@string/action_sort_total"
android:icon="@drawable/ic_blank_24dp"/>
</menu>