From a71bdc14c558c3d6436c3bb5dd9a5212fe39e364 Mon Sep 17 00:00:00 2001 From: Jays2Kings Date: Mon, 26 Apr 2021 21:16:51 -0400 Subject: [PATCH] LibrarySort is now an enum with lot of helper methods and stuff With it came the refactoring of Category sort options Also a new icon for last fetched --- .../data/database/models/Category.kt | 88 ++--------- .../ui/category/CategoryPresenter.kt | 3 +- .../ui/category/ManageCategoryDialog.kt | 3 +- .../ui/library/LibraryCategoryAdapter.kt | 26 ++-- .../tachiyomi/ui/library/LibraryController.kt | 5 +- .../ui/library/LibraryHeaderHolder.kt | 140 ++++++------------ .../tachiyomi/ui/library/LibraryHolder.kt | 2 +- .../tachiyomi/ui/library/LibraryPresenter.kt | 40 ++--- .../tachiyomi/ui/library/LibrarySort.kt | 66 +++++++-- .../ic_calendar_text_outline_24dp.xml | 8 + 10 files changed, 167 insertions(+), 214 deletions(-) create mode 100644 app/src/main/res/drawable/ic_calendar_text_outline_24dp.xml diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/database/models/Category.kt b/app/src/main/java/eu/kanade/tachiyomi/data/database/models/Category.kt index e59a744bc3..027200a55d 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/database/models/Category.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/database/models/Category.kt @@ -1,6 +1,7 @@ package eu.kanade.tachiyomi.data.database.models import android.content.Context +import androidx.annotation.StringRes import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.ui.library.LibrarySort import java.io.Serializable @@ -21,9 +22,6 @@ interface Category : Serializable { var isAlone: Boolean - val nameLower: String - get() = name.toLowerCase() - var isHidden: Boolean var isDynamic: Boolean @@ -34,73 +32,24 @@ interface Category : Serializable { return ((mangaSort?.minus('a') ?: 0) % 2) != 1 } - fun sortingMode(nullAsDND: Boolean = false): Int? = when (mangaSort) { - ALPHA_ASC, ALPHA_DSC -> LibrarySort.ALPHA - UPDATED_ASC, UPDATED_DSC -> LibrarySort.LATEST_CHAPTER - UNREAD_ASC, UNREAD_DSC -> LibrarySort.UNREAD - LAST_READ_ASC, LAST_READ_DSC -> LibrarySort.LAST_READ - TOTAL_ASC, TOTAL_DSC -> LibrarySort.TOTAL - DRAG_AND_DROP -> LibrarySort.DRAG_AND_DROP - 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 - } + fun sortingMode(nullAsDND: Boolean = false): LibrarySort? = LibrarySort.valueOf(mangaSort) + ?: if (nullAsDND && !isDynamic) LibrarySort.DragAndDrop else null val isDragAndDrop - get() = (mangaSort == null || mangaSort == DRAG_AND_DROP) && !isDynamic + get() = ( + mangaSort == null || + mangaSort == LibrarySort.DragAndDrop.categoryValue + ) && !isDynamic - fun sortRes(): Int = when (mangaSort) { - ALPHA_ASC, ALPHA_DSC -> R.string.title - UPDATED_ASC, UPDATED_DSC -> R.string.latest_chapter - UNREAD_ASC, UNREAD_DSC -> R.string.unread - LAST_READ_ASC, LAST_READ_DSC -> R.string.last_read - TOTAL_ASC, TOTAL_DSC -> R.string.total_chapters - 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 - } - - fun catSortingMode(): Int? = when (mangaSort) { - ALPHA_ASC, ALPHA_DSC -> 0 - UPDATED_ASC, UPDATED_DSC -> 1 - UNREAD_ASC, UNREAD_DSC -> 2 - LAST_READ_ASC, LAST_READ_DSC -> 3 - TOTAL_ASC, TOTAL_DSC -> 4 - DATE_ADDED_ASC, DATE_ADDED_DSC -> 5 - LAST_FETCHED_ASC, LAST_FETCHED_DSC -> 6 - else -> null - } + @StringRes + fun sortRes(): Int = + (LibrarySort.valueOf(mangaSort) ?: LibrarySort.DragAndDrop).stringRes(isDynamic) fun changeSortTo(sort: Int) { - mangaSort = when (sort) { - LibrarySort.ALPHA -> ALPHA_ASC - LibrarySort.LATEST_CHAPTER -> UPDATED_ASC - LibrarySort.UNREAD -> UNREAD_ASC - LibrarySort.LAST_READ -> LAST_READ_ASC - LibrarySort.TOTAL -> ALPHA_ASC - LibrarySort.DATE_ADDED -> DATE_ADDED_ASC - LibrarySort.LAST_FETCHED -> LAST_FETCHED_ASC - else -> ALPHA_ASC - } + mangaSort = (LibrarySort.valueOf(sort) ?: LibrarySort.Title).categoryValue } companion object { - const val DRAG_AND_DROP = 'D' - const val ALPHA_ASC = 'a' - const val ALPHA_DSC = 'b' - const val UPDATED_ASC = 'c' - const val UPDATED_DSC = 'd' - const val UNREAD_ASC = 'e' - const val UNREAD_DSC = 'f' - const val LAST_READ_ASC = 'g' - const val LAST_READ_DSC = 'h' - const val TOTAL_ASC = 'i' - const val TOTAL_DSC = 'j' - const val DATE_ADDED_ASC = 'k' - 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 { this.name = name } @@ -112,18 +61,9 @@ interface Category : Serializable { fun createCustom(name: String, libSort: Int, ascending: Boolean): Category = create(name).apply { - mangaSort = when (libSort) { - LibrarySort.ALPHA -> ALPHA_ASC - LibrarySort.LATEST_CHAPTER -> UPDATED_ASC - LibrarySort.UNREAD -> UNREAD_ASC - LibrarySort.LAST_READ -> LAST_READ_ASC - LibrarySort.TOTAL -> TOTAL_ASC - LibrarySort.DATE_ADDED -> DATE_ADDED_ASC - LibrarySort.LAST_FETCHED -> LAST_FETCHED_ASC - LibrarySort.DRAG_AND_DROP -> DRAG_AND_DROP - else -> DRAG_AND_DROP - } - if (mangaSort != DRAG_AND_DROP && !ascending) { + val librarySort = LibrarySort.valueOf(libSort) ?: LibrarySort.DragAndDrop + changeSortTo(librarySort.mainValue) + if (mangaSort != LibrarySort.DragAndDrop.categoryValue && !ascending) { mangaSort = mangaSort?.plus(1) } isDynamic = true diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/category/CategoryPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/category/CategoryPresenter.kt index 7d35d3f1a1..47c649ee61 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/category/CategoryPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/category/CategoryPresenter.kt @@ -4,6 +4,7 @@ import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.database.DatabaseHelper import eu.kanade.tachiyomi.data.database.models.Category import eu.kanade.tachiyomi.data.preference.PreferencesHelper +import eu.kanade.tachiyomi.ui.library.LibrarySort import eu.kanade.tachiyomi.util.system.executeOnIO import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -75,7 +76,7 @@ class CategoryPresenter( cat.order = categories.maxOf { it.order } + 1 // Insert into database. - cat.mangaSort = Category.ALPHA_ASC + cat.mangaSort = LibrarySort.Title.categoryValue db.insertCategory(cat).executeAsBlocking() val cats = db.getCategories().executeAsBlocking() val newCat = cats.find { it.name == name } ?: return false diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/category/ManageCategoryDialog.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/category/ManageCategoryDialog.kt index f16546b073..ddb119e872 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/category/ManageCategoryDialog.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/category/ManageCategoryDialog.kt @@ -19,6 +19,7 @@ import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.getOrDefault import eu.kanade.tachiyomi.databinding.MangaCategoryDialogBinding import eu.kanade.tachiyomi.ui.base.controller.DialogController +import eu.kanade.tachiyomi.ui.library.LibrarySort import eu.kanade.tachiyomi.util.view.withFadeTransaction import uy.kohesive.injekt.injectLazy @@ -78,7 +79,7 @@ class ManageCategoryDialog(bundle: Bundle? = null) : if (this.category == null) { val categories = db.getCategories().executeAsBlocking() category.order = categories.maxOf { it.order } + 1 - category.mangaSort = Category.ALPHA_ASC + category.mangaSort = LibrarySort.Title.categoryValue val dbCategory = db.insertCategory(category).executeAsBlocking() category.id = dbCategory.insertedId()?.toInt() this.category = category diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryAdapter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryAdapter.kt index a4363341fb..9ef5b1c9c5 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryAdapter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryAdapter.kt @@ -140,7 +140,7 @@ class LibraryCategoryAdapter(val controller: LibraryController) : is LibraryItem -> { val text = if (item.manga.isBlank()) return item.header?.category?.name.orEmpty() else when (getSort(position)) { - LibrarySort.DRAG_AND_DROP -> { + LibrarySort.DragAndDrop -> { if (item.header.category.isDynamic) { val category = db.getCategoriesForManga(item.manga).executeAsBlocking().firstOrNull()?.name category ?: recyclerView.context.getString(R.string.default_value) @@ -150,7 +150,7 @@ class LibraryCategoryAdapter(val controller: LibraryController) : else title.take(10) } } - LibrarySort.LAST_FETCHED -> { + LibrarySort.LastFetched -> { val id = item.manga.id ?: return "" val history = db.getChapters(id).executeAsBlocking() val last = history.maxOfOrNull { it.date_fetch } @@ -163,7 +163,7 @@ class LibraryCategoryAdapter(val controller: LibraryController) : "N/A" } } - LibrarySort.LAST_READ -> { + LibrarySort.LastRead -> { val id = item.manga.id ?: return "" val history = db.getHistoryByMangaId(id).executeAsBlocking() val last = history.maxOfOrNull { it.last_read } @@ -176,12 +176,12 @@ class LibraryCategoryAdapter(val controller: LibraryController) : "N/A" } } - LibrarySort.UNREAD -> { + LibrarySort.Unread -> { val unread = item.manga.unread if (unread > 0) recyclerView.context.getString(R.string._unread, unread) else recyclerView.context.getString(R.string.read) } - LibrarySort.TOTAL -> { + LibrarySort.TotalChapters -> { val total = item.manga.totalChapters if (total > 0) recyclerView.resources.getQuantityString( R.plurals.chapters_plural, @@ -192,7 +192,7 @@ class LibraryCategoryAdapter(val controller: LibraryController) : "N/A" } } - LibrarySort.LATEST_CHAPTER -> { + LibrarySort.LatestChapter -> { val lastUpdate = item.manga.last_update if (lastUpdate > 0) { recyclerView.context.getString( @@ -203,7 +203,7 @@ class LibraryCategoryAdapter(val controller: LibraryController) : "N/A" } } - LibrarySort.DATE_ADDED -> { + LibrarySort.DateAdded -> { val added = item.manga.date_added if (added > 0) { recyclerView.context.getString(R.string.added_, added.timeSpanFromNow(preferences.context)) @@ -211,7 +211,7 @@ class LibraryCategoryAdapter(val controller: LibraryController) : "N/A" } } - else -> { + LibrarySort.Title -> { val title = if (preferences.removeArticles().getOrDefault()) { item.manga.title.removeArticles() } else { @@ -230,13 +230,9 @@ class LibraryCategoryAdapter(val controller: LibraryController) : } } - private fun getSort(position: Int): Int { + private fun getSort(position: Int): LibrarySort { val header = (getItem(position) as? LibraryItem)?.header - return if (header != null) { - header.category.sortingMode() ?: LibrarySort.DRAG_AND_DROP - } else { - LibrarySort.DRAG_AND_DROP - } + return header?.category?.sortingMode() ?: LibrarySort.DragAndDrop } interface LibraryListener { @@ -244,7 +240,7 @@ class LibraryCategoryAdapter(val controller: LibraryController) : fun onItemReleased(position: Int) fun canDrag(): Boolean fun updateCategory(catId: Int): Boolean - fun sortCategory(catId: Int, sortBy: Int) + fun sortCategory(catId: Int, sortBy: Char) fun selectAll(position: Int) fun allSelected(position: Int): Boolean fun toggleCategoryVisibility(position: Int) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt index a3d5d863f7..841c5359e0 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt @@ -97,7 +97,6 @@ import eu.kanade.tachiyomi.util.view.updateLayoutParams import eu.kanade.tachiyomi.util.view.updatePaddingRelative import eu.kanade.tachiyomi.util.view.withFadeTransaction import eu.kanade.tachiyomi.widget.EndAnimatorListener -import kotlinx.coroutines.cancel import kotlinx.coroutines.delay import kotlinx.coroutines.flow.drop import kotlinx.coroutines.flow.launchIn @@ -1422,9 +1421,9 @@ class LibraryController( } } - override fun sortCategory(catId: Int, sortBy: Int) { + override fun sortCategory(catId: Int, sortBy: Char) { val category = presenter.categories.find { it.id == catId } - if (category?.isDynamic == false && ('a' + (sortBy - 1)) == Category.DRAG_AND_DROP) { + if (category?.isDynamic == false && sortBy == LibrarySort.DragAndDrop.categoryValue) { val item = adapter.findCategoryHeader(catId) ?: return val libraryItems = adapter.getSectionItems(item) .filterIsInstance() diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryHeaderHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryHeaderHolder.kt index 7577e81ab0..283791c8f6 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryHeaderHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryHeaderHolder.kt @@ -4,6 +4,7 @@ import android.app.Activity import android.graphics.Color import android.util.TypedValue import android.view.View +import androidx.annotation.DrawableRes import androidx.constraintlayout.widget.ConstraintLayout import androidx.core.content.ContextCompat import androidx.core.view.isInvisible @@ -90,15 +91,7 @@ class LibraryHeaderHolder(val view: View, private val adapter: LibraryCategoryAd val isAscending = category.isAscending() val sortingMode = category.sortingMode() - val sortDrawable = when { - 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 || - sortingMode == LibrarySort.LAST_FETCHED - ) !isAscending else isAscending -> R.drawable.ic_arrow_downward_24dp - else -> R.drawable.ic_arrow_upward_24dp - } + val sortDrawable = getSortRes(sortingMode, isAscending, R.drawable.ic_sort_24dp) binding.categorySort.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, sortDrawable, 0) binding.categorySort.setText(category.sortRes()) @@ -146,66 +139,13 @@ class LibraryHeaderHolder(val view: View, private val adapter: LibraryCategoryAd val category = (adapter.getItem(flexibleAdapterPosition) as? LibraryHeaderItem)?.category ?: return adapter.controller.activity?.let { activity -> - val items = mutableListOf( - MaterialMenuSheet.MenuSheetItem( - LibrarySort.ALPHA, - R.drawable.ic_sort_by_alpha_24dp, - R.string.title - ), - MaterialMenuSheet.MenuSheetItem( - LibrarySort.LAST_READ, - R.drawable.ic_recent_read_outline_24dp, - R.string.last_read - ), - MaterialMenuSheet.MenuSheetItem( - LibrarySort.LATEST_CHAPTER, - R.drawable.ic_new_releases_24dp, - R.string.latest_chapter - ), - MaterialMenuSheet.MenuSheetItem( - LibrarySort.LAST_FETCHED, - R.drawable.ic_check_24dp, - R.string.last_fetched - ), - MaterialMenuSheet.MenuSheetItem( - LibrarySort.UNREAD, - R.drawable.ic_eye_24dp, - R.string.unread - ), - MaterialMenuSheet.MenuSheetItem( - LibrarySort.TOTAL, - R.drawable.ic_sort_by_numeric_24dp, - R.string.total_chapters - ), - MaterialMenuSheet.MenuSheetItem( - LibrarySort.DATE_ADDED, - R.drawable.ic_heart_outline_24dp, - R.string.date_added - ) - ) - if (category.isDynamic) { - items.add( - MaterialMenuSheet.MenuSheetItem( - LibrarySort.DRAG_AND_DROP, - R.drawable.ic_label_outline_24dp, - R.string.category - ) - ) - } else { - items.add( - MaterialMenuSheet.MenuSheetItem( - LibrarySort.DRAG_AND_DROP, - R.drawable.ic_swap_vert_24dp, - R.string.drag_and_drop - ) - ) - } + val items = LibrarySort.values().map { it.menuSheetItem(category.isDynamic) } val sortingMode = category.sortingMode(true) val sheet = MaterialMenuSheet( activity, items, activity.getString(R.string.sort_by), - sortingMode + sortingMode?.mainValue ) { sheet, item -> onCatSortClicked(category, item) val nCategory = (adapter.getItem(flexibleAdapterPosition) as? LibraryHeaderItem)?.category @@ -216,46 +156,62 @@ class LibraryHeaderHolder(val view: View, private val adapter: LibraryCategoryAd } val isAscending = category.isAscending() val drawableRes = getSortRes(sortingMode, isAscending) - sheet.setDrawable(sortingMode ?: -1, drawableRes) + sheet.setDrawable(sortingMode?.mainValue ?: -1, drawableRes) sheet.show() } } - private fun getSortRes(sortingMode: Int?, isAscending: Boolean): Int = when { - sortingMode == LibrarySort.DRAG_AND_DROP -> R.drawable.ic_check_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 + private fun getSortRes( + sortMode: LibrarySort?, + isAscending: Boolean, + @DrawableRes defaultDrawableRes: Int = R.drawable.ic_check_24dp + ): Int { + sortMode ?: return defaultDrawableRes + return when (sortMode) { + LibrarySort.DragAndDrop -> defaultDrawableRes + else -> { + if (if (sortMode.hasInvertedSort) !isAscending else isAscending) { + R.drawable.ic_arrow_downward_24dp + } else { + R.drawable.ic_arrow_upward_24dp + } + } + } + } + + private fun getSortRes( + sortingMode: Int?, + isAscending: Boolean, + @DrawableRes defaultDrawableRes: Int = R.drawable.ic_check_24dp + ): Int { + sortingMode ?: return defaultDrawableRes + return when (val sortMode = LibrarySort.valueOf(sortingMode)) { + LibrarySort.DragAndDrop -> defaultDrawableRes + else -> { + if (if (sortMode?.hasInvertedSort == true) !isAscending else isAscending) { + R.drawable.ic_arrow_downward_24dp + } else { + R.drawable.ic_arrow_upward_24dp + } + } + } } private fun onCatSortClicked(category: Category, menuId: Int?) { val modType = if (menuId == null) { - val t = (category.mangaSort?.minus('a') ?: 0) + 1 - if (t % 2 != 0) t + 1 - else t - 1 - } else { - val order = when (menuId) { - LibrarySort.DRAG_AND_DROP -> { - adapter.libraryListener.sortCategory(category.id!!, 'D' - 'a' + 1) - return - } - LibrarySort.LAST_FETCHED -> 6 - LibrarySort.DATE_ADDED -> 5 - LibrarySort.TOTAL -> 4 - LibrarySort.LAST_READ -> 3 - LibrarySort.UNREAD -> 2 - LibrarySort.LATEST_CHAPTER -> 1 - else -> 0 + val sortingMode = category.sortingMode() ?: LibrarySort.Title + if (category.isAscending()) { + sortingMode.categoryValueDescending + } else { + sortingMode.categoryValue } - if (order == category.catSortingMode()) { + } else { + val sortingMode = LibrarySort.valueOf(menuId) ?: LibrarySort.Title + if (sortingMode != LibrarySort.DragAndDrop && sortingMode == category.sortingMode()) { onCatSortClicked(category, null) return } - (2 * order + 1) + sortingMode.categoryValue } adapter.libraryListener.sortCategory(category.id!!, modType) } 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 03d3ff643a..9f86d3f1be 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 @@ -27,7 +27,7 @@ abstract class LibraryHolder( abstract fun onSetValues(item: LibraryItem) fun setUnreadBadge(badge: LibraryBadge, item: LibraryItem) { - val showTotal = item.header.category.sortingMode() == LibrarySort.TOTAL + val showTotal = item.header.category.sortingMode() == LibrarySort.TotalChapters badge.setUnreadDownload( when { showTotal -> item.manga.totalChapters 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 f004af8b03..4e5cf59358 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 @@ -359,31 +359,35 @@ class LibraryPresenter( } val compare = when { category.mangaSort != null -> { - var sort = when (category.sortingMode()) { - LibrarySort.ALPHA -> sortAlphabetical(i1, i2) - LibrarySort.LATEST_CHAPTER -> i2.manga.last_update.compareTo(i1.manga.last_update) - LibrarySort.UNREAD -> when { + var sort = when (category.sortingMode() ?: LibrarySort.Title) { + LibrarySort.Title -> sortAlphabetical(i1, i2) + LibrarySort.LatestChapter -> i2.manga.last_update.compareTo(i1.manga.last_update) + LibrarySort.Unread -> when { i1.manga.unread == i2.manga.unread -> 0 i1.manga.unread == 0 -> if (category.isAscending()) 1 else -1 i2.manga.unread == 0 -> if (category.isAscending()) -1 else 1 else -> i1.manga.unread.compareTo(i2.manga.unread) } - LibrarySort.LAST_READ -> { - val manga1LastRead = lastReadManga[i1.manga.id!!] ?: lastReadManga.size - val manga2LastRead = lastReadManga[i2.manga.id!!] ?: lastReadManga.size + LibrarySort.LastRead -> { + val manga1LastRead = + lastReadManga[i1.manga.id!!] ?: lastReadManga.size + val manga2LastRead = + lastReadManga[i2.manga.id!!] ?: lastReadManga.size manga1LastRead.compareTo(manga2LastRead) } - LibrarySort.TOTAL -> { + LibrarySort.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 + LibrarySort.LastFetched -> { + 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) - else -> { - if (LibrarySort.DRAG_AND_DROP == category.sortingMode() && category.isDynamic) { + LibrarySort.DateAdded -> i2.manga.date_added.compareTo(i1.manga.date_added) + LibrarySort.DragAndDrop -> { + if (category.isDynamic) { val category1 = allCategories.find { i1.manga.category == it.id }?.order ?: 0 @@ -813,12 +817,12 @@ class LibraryPresenter( } /** Update a category's sorting */ - fun sortCategory(catId: Int, order: Int) { + fun sortCategory(catId: Int, order: Char) { val category = categories.find { catId == it.id } ?: return - category.mangaSort = ('a' + (order - 1)) + category.mangaSort = order if (catId == -1 || category.isDynamic) { - val sort = category.sortingMode() ?: LibrarySort.ALPHA - preferences.librarySortingMode().set(sort) + val sort = category.sortingMode() ?: LibrarySort.Title + preferences.librarySortingMode().set(sort.mainValue) preferences.librarySortingAscending().set(category.isAscending()) categories.forEach { it.mangaSort = category.mangaSort 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 0506561ac9..a3da20a961 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 @@ -1,13 +1,61 @@ package eu.kanade.tachiyomi.ui.library -object LibrarySort { +import androidx.annotation.DrawableRes +import androidx.annotation.StringRes +import eu.kanade.tachiyomi.R +import eu.kanade.tachiyomi.ui.base.MaterialMenuSheet - const val ALPHA = 0 - const val LAST_READ = 1 - const val LATEST_CHAPTER = 2 - const val UNREAD = 3 - const val TOTAL = 4 - const val DATE_ADDED = 5 - const val LAST_FETCHED = 6 - const val DRAG_AND_DROP = 7 +enum class LibrarySort( + val mainValue: Int, + @StringRes private val stringRes: Int, + @DrawableRes private val iconRes: Int, + private val catValue: Int = mainValue, + @StringRes private val dynamicStringRes: Int = stringRes, + @DrawableRes private val dynamicIconRes: Int = iconRes +) { + + Title(0, R.string.title, R.drawable.ic_sort_by_alpha_24dp), + LastRead(1, R.string.last_read, R.drawable.ic_recent_read_outline_24dp, 3), + LatestChapter(2, R.string.latest_chapter, R.drawable.ic_new_releases_24dp, 1), + LastFetched(6, R.string.last_fetched, R.drawable.ic_calendar_text_outline_24dp), + Unread(3, R.string.unread, R.drawable.ic_eye_24dp, 2), + TotalChapters(4, R.string.total_chapters, R.drawable.ic_sort_by_numeric_24dp), + DateAdded(5, R.string.date_added, R.drawable.ic_heart_outline_24dp), + DragAndDrop( + 7, + R.string.drag_and_drop, + R.drawable.ic_swap_vert_24dp, + 7, + R.string.category, + R.drawable.ic_label_outline_24dp + ) + ; + + val categoryValue: Char + get() = if (this == DragAndDrop) 'D' else 'a' + catValue * 2 + + val categoryValueDescending: Char + get() = if (this == DragAndDrop) 'D' else 'b' + catValue * 2 + + @StringRes + fun stringRes(isDynamic: Boolean) = if (isDynamic) dynamicStringRes else stringRes + + @DrawableRes + fun iconRes(isDynamic: Boolean) = if (isDynamic) dynamicIconRes else iconRes + + val hasInvertedSort: Boolean + get() = this in listOf(LastRead, DateAdded, LatestChapter, LastFetched) + + fun menuSheetItem(isDynamic: Boolean): MaterialMenuSheet.MenuSheetItem { + return MaterialMenuSheet.MenuSheetItem( + mainValue, + iconRes(isDynamic), + stringRes(isDynamic) + ) + } + + companion object { + fun valueOf(value: Int) = values().find { it.mainValue == value } + fun valueOf(char: Char?) = values().find { it.categoryValue == char || it.categoryValueDescending == char } + } } diff --git a/app/src/main/res/drawable/ic_calendar_text_outline_24dp.xml b/app/src/main/res/drawable/ic_calendar_text_outline_24dp.xml new file mode 100644 index 0000000000..29c8952454 --- /dev/null +++ b/app/src/main/res/drawable/ic_calendar_text_outline_24dp.xml @@ -0,0 +1,8 @@ + + + + \ No newline at end of file