refactor(library): Turn getCategory to an extension method

This commit is contained in:
Ahmad Ansori Palembani 2024-06-21 21:02:06 +07:00
parent 5f61a3fe4a
commit 25644af5cf
Signed by: null2264
GPG key ID: BA64F8B60AF3EFB6

View file

@ -260,11 +260,12 @@ class LibraryPresenter(
view?.onNextLibraryUpdate(sectionedLibraryItems[currentCategory] ?: blankItem()) view?.onNextLibraryUpdate(sectionedLibraryItems[currentCategory] ?: blankItem())
} }
fun blankItem(id: Int = currentCategory): List<LibraryItem> { fun blankItem(id: Int = currentCategory, categories: List<Category>? = null): List<LibraryItem> {
val actualCategories = categories ?: this.categories
return listOf( return listOf(
LibraryItem( LibraryItem(
LibraryManga.createBlank(id), LibraryManga.createBlank(id),
LibraryHeaderItem({ getCategory(id) }, id), LibraryHeaderItem({ actualCategories.getOrDefault(id) }, id),
viewContext, viewContext,
), ),
) )
@ -682,9 +683,9 @@ class LibraryPresenter(
* *
* @param categoryId id of the category to get * @param categoryId id of the category to get
*/ */
private fun getCategory(categoryId: Int): Category { private fun List<Category>.getOrDefault(categoryId: Int): Category {
val category = categories.find { categoryId == it.id } ?: createDefaultCategory() val category = this.find { categoryId == it.id } ?: createDefaultCategory()
category.isAlone = categories.size <= 1 category.isAlone = this.size <= 1
return category return category
} }
@ -768,7 +769,7 @@ class LibraryPresenter(
val headerItems = if (libraryIsGrouped) val headerItems = if (libraryIsGrouped)
categories.mapNotNull { category -> categories.mapNotNull { category ->
val id = category.id ?: return@mapNotNull null val id = category.id ?: return@mapNotNull null
id to LibraryHeaderItem({ getCategory(id) }, id) id to LibraryHeaderItem({ categories.getOrDefault(id) }, id)
}.toMap() }.toMap()
else null else null
@ -818,7 +819,7 @@ class LibraryPresenter(
categories.associateWith { categories.associateWith {
libraryManga[it.id] ?: headerItems?.get(it.id!!)?.let { headerItem -> libraryManga[it.id] ?: headerItems?.get(it.id!!)?.let { headerItem ->
// J2K behaviour, not sure why, but J2K added blank manga if library is grouped. // J2K behaviour, blank manga is added if library is grouped, used as placeholder for empty state.
// Since headerItems is always null if library is not grouped, that already act as a check for // Since headerItems is always null if library is not grouped, that already act as a check for
// "is library grouped". // "is library grouped".
listOf( listOf(
@ -865,9 +866,9 @@ class LibraryPresenter(
if (id == null) { if (id == null) {
null null
} else { } else {
id to LibraryHeaderItem({ getCategory(id) }, id) id to LibraryHeaderItem({ categories.getOrDefault(id) }, id)
} }
} + (-1 to catItemAll) + (0 to LibraryHeaderItem({ getCategory(0) }, 0)) } + (-1 to catItemAll) + (0 to LibraryHeaderItem({ categories.getOrDefault(0) }, 0))
).toMap() ).toMap()
val items = libraryManga.mapNotNull { val items = libraryManga.mapNotNull {
@ -963,7 +964,7 @@ class LibraryPresenter(
return tagItems[swappedName]!! return tagItems[swappedName]!!
} }
} }
val headerItem = LibraryHeaderItem({ getCategory(it) }, tagItems.count()) val headerItem = LibraryHeaderItem({ categories.getOrDefault(it) }, tagItems.count())
tagItems[name] = headerItem tagItems[name] = headerItem
return headerItem return headerItem
} }
@ -1360,7 +1361,7 @@ class LibraryPresenter(
.set(categoriesHidden.map { it.toString() }.toMutableSet()) .set(categoriesHidden.map { it.toString() }.toMutableSet())
} else { } else {
val categoriesHidden = preferences.collapsedDynamicCategories().get().toMutableSet() val categoriesHidden = preferences.collapsedDynamicCategories().get().toMutableSet()
val category = getCategory(categoryId) val category = categories.getOrDefault(categoryId)
val dynamicName = getDynamicCategoryName(category) val dynamicName = getDynamicCategoryName(category)
if (dynamicName in categoriesHidden) { if (dynamicName in categoriesHidden) {
categoriesHidden.remove(dynamicName) categoriesHidden.remove(dynamicName)