refactor: More library flow prep

This commit is contained in:
Ahmad Ansori Palembani 2024-06-20 09:00:14 +07:00
parent c5a009f419
commit 5799842c50
Signed by: null2264
GPG key ID: BA64F8B60AF3EFB6

View file

@ -234,9 +234,9 @@ class LibraryPresenter(
setSourceLanguage(hiddenItems) setSourceLanguage(hiddenItems)
allLibraryItems = library allLibraryItems = library
hiddenLibraryItems = hiddenItems hiddenLibraryItems = hiddenItems
var mangaMap = library val mangaMap = library
mangaMap = applyFilters(mangaMap) .applyFilters()
mangaMap = applySort(mangaMap) .applySort()
val freshStart = libraryItems.isEmpty() val freshStart = libraryItems.isEmpty()
sectionLibrary(mangaMap, freshStart) sectionLibrary(mangaMap, freshStart)
} }
@ -318,7 +318,7 @@ class LibraryPresenter(
* *
* @param items the items to filter. * @param items the items to filter.
*/ */
private fun applyFilters(items: List<LibraryItem>): List<LibraryItem> { private fun List<LibraryItem>.applyFilters(): List<LibraryItem> {
val filterPrefs = ItemPreferences( val filterPrefs = ItemPreferences(
filterDownloaded = preferences.filterDownloaded().get(), filterDownloaded = preferences.filterDownloaded().get(),
filterUnread = preferences.filterUnread().get(), filterUnread = preferences.filterUnread().get(),
@ -344,7 +344,7 @@ class LibraryPresenter(
) )
hasActiveFilters = !filtersOff hasActiveFilters = !filtersOff
val missingCategorySet = categories.mapNotNull { it.id }.toMutableSet() val missingCategorySet = categories.mapNotNull { it.id }.toMutableSet()
val filteredItems = items.filter f@{ item -> val filteredItems = this.filter f@{ item ->
if (!showEmptyCategoriesWhileFiltering && item.manga.isHidden()) { if (!showEmptyCategoriesWhileFiltering && item.manga.isHidden()) {
val subItems = sectionedLibraryItems[item.manga.category]?.takeUnless { it.size <= 1 } val subItems = sectionedLibraryItems[item.manga.category]?.takeUnless { it.size <= 1 }
?: hiddenLibraryItems.filter { it.manga.category == item.manga.category } ?: hiddenLibraryItems.filter { it.manga.category == item.manga.category }
@ -605,7 +605,7 @@ class LibraryPresenter(
* *
* @param itemList the map to sort. * @param itemList the map to sort.
*/ */
private fun applySort(itemList: List<LibraryItem>): List<LibraryItem> { private fun List<LibraryItem>.applySort(): List<LibraryItem> {
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
@ -679,7 +679,7 @@ class LibraryPresenter(
} }
} }
return itemList.sortedWith(Comparator(sortFn)) return this.sortedWith(Comparator(sortFn))
} }
/** Gets the category by id /** Gets the category by id
@ -748,9 +748,9 @@ class LibraryPresenter(
) ?: return@mapNotNull null ) ?: return@mapNotNull null
categorySet.add(it.category) categorySet.add(it.category)
LibraryItem(it, headerItem, viewContext) LibraryItem(it, headerItem, viewContext)
}.groupBy { it.manga.category } }
categories.associateWith { libraryManga[it.id].orEmpty() } categories.associateWith { libraryManga.filter { item -> item.header.category.id == it.id }.orEmpty() }
} }
} }
@ -1066,9 +1066,9 @@ class LibraryPresenter(
/** Requests the library to be filtered. */ /** Requests the library to be filtered. */
fun requestFilterUpdate() { fun requestFilterUpdate() {
presenterScope.launch { presenterScope.launch {
var mangaMap = allLibraryItems val mangaMap = allLibraryItems
mangaMap = applyFilters(mangaMap) .applyFilters()
mangaMap = applySort(mangaMap) .applySort()
sectionLibrary(mangaMap) sectionLibrary(mangaMap)
} }
} }
@ -1102,8 +1102,8 @@ class LibraryPresenter(
/** Requests the library to be sorted. */ /** Requests the library to be sorted. */
private fun requestSortUpdate() { private fun requestSortUpdate() {
presenterScope.launch { presenterScope.launch {
var mangaMap = libraryItems val mangaMap = libraryItems
mangaMap = applySort(mangaMap) .applySort()
sectionLibrary(mangaMap) sectionLibrary(mangaMap)
} }
} }