From 3940d42185e74d733a06acc387b430c03abd7ea7 Mon Sep 17 00:00:00 2001 From: Jay Date: Fri, 21 Feb 2020 19:59:54 -0800 Subject: [PATCH] Total chapter count shown when sorting by total chapters --- .../tachiyomi/ui/library/LibraryBadge.kt | 15 ++++-- .../tachiyomi/ui/library/LibraryGridHolder.kt | 13 +---- .../tachiyomi/ui/library/LibraryHolder.kt | 21 ++++++-- .../tachiyomi/ui/library/LibraryItem.kt | 1 + .../tachiyomi/ui/library/LibraryListHolder.kt | 14 +---- .../tachiyomi/ui/library/LibraryPresenter.kt | 54 +++++++++++++++---- .../library/filter/SortFilterBottomSheet.kt | 1 + app/src/main/res/menu/cat_sort.xml | 6 +++ 8 files changed, 81 insertions(+), 44 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryBadge.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryBadge.kt index 878648da3a..fd7ca0fe33 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryBadge.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryBadge.kt @@ -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) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryGridHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryGridHolder.kt index 0a1520dfee..b86f254cba 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryGridHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryGridHolder.kt @@ -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 diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryHolder.kt index be317101dc..abb43c848e 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryHolder.kt @@ -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. * diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryItem.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryItem.kt index dc3c79ed0e..b6c6b12974 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryItem.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryItem.kt @@ -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) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryListHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryListHolder.kt index 70dba508a2..bba487ae8b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryListHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryListHolder.kt @@ -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 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 3843af8e74..ac9e7452ea 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 @@ -79,6 +79,8 @@ class LibraryPresenter( private var currentMangaMap:LibraryMap? = null + private var totalChapters:Map? = 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, initCat: Category? = null): Int { + lastReadManga: Map, + 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() } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/filter/SortFilterBottomSheet.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/filter/SortFilterBottomSheet.kt index b2d4cc2f35..f1c9b08704 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/filter/SortFilterBottomSheet.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/filter/SortFilterBottomSheet.kt @@ -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 diff --git a/app/src/main/res/menu/cat_sort.xml b/app/src/main/res/menu/cat_sort.xml index 901afa1d06..12fb44dd5e 100644 --- a/app/src/main/res/menu/cat_sort.xml +++ b/app/src/main/res/menu/cat_sort.xml @@ -16,8 +16,14 @@ android:id="@+id/action_update" android:title="@string/action_sort_last_updated" android:icon="@drawable/ic_blank_24dp"/> + + + \ No newline at end of file