refactor(library): Simplify code

This commit is contained in:
Ahmad Ansori Palembani 2024-08-15 17:05:45 +07:00
parent a60776b0a5
commit bf164608a0
Signed by: null2264
GPG key ID: BA64F8B60AF3EFB6

View file

@ -222,6 +222,12 @@ class LibraryPresenter(
}
}
private suspend fun subscribeLibrary() {
getLibraryFlow().collectLatest { library ->
loadedManga = library
}
}
/** Get favorited manga for library and sort and filter it */
fun getLibrary() {
presenterScope.launch {
@ -1038,13 +1044,6 @@ class LibraryPresenter(
}
}
private suspend fun subscribeLibrary() {
getLibraryFlow().collectLatest { library ->
loadedManga = library
}
}
// TODO: Simplify this code
/**
* Get the categories and all its manga from the database.
*
@ -1052,19 +1051,46 @@ class LibraryPresenter(
*/
private suspend fun getLibraryFromDB(): Pair<List<LibraryItem>, List<LibraryItem>> {
removeArticles = preferences.removeArticles().get()
val categories = getCategories.await().toMutableList()
allCategories = getCategories.await().toMutableList()
// In default grouping, which uses category as group, a manga can be in multiple categories.
// So before we group it by different type, we should make the list unique.
val libraryManga = getLibraryManga.await().apply { if (groupType > BY_DEFAULT) { distinctBy { it.id } } }
val showAll = showAllCategories
val hiddenItems = mutableListOf<LibraryItem>()
val items = if (groupType <= BY_DEFAULT || !libraryIsGrouped) {
val categoryAll = Category.createAll(
context,
val (items, categories, hiddenItems) = if (groupType <= BY_DEFAULT || !libraryIsGrouped) {
getLibraryItems(
libraryManga,
preferences.librarySortingMode().get(),
preferences.librarySortingAscending().get(),
showAll,
)
} else {
getCustomMangaItems(
libraryManga,
preferences.librarySortingMode().get(),
preferences.librarySortingAscending().get(),
)
}
this.categories = categories
return items to hiddenItems
}
private suspend fun getLibraryItems(
libraryManga: List<LibraryManga>,
sortingMode: Int,
isAscending: Boolean,
showAll: Boolean,
): Triple<List<LibraryItem>, List<Category>, List<LibraryItem>> {
val categories = allCategories.toMutableList()
val hiddenItems = mutableListOf<LibraryItem>()
val categoryAll = Category.createAll(
context,
sortingMode,
isAscending,
)
val catItemAll = LibraryHeaderItem({ categoryAll }, -1)
val categorySet = mutableSetOf<Int>()
@ -1139,28 +1165,23 @@ class LibraryPresenter(
categories.forEach {
it.isHidden = it.id in categoriesHidden && showAll && categories.size > 1
}
this.categories = if (!libraryIsGrouped) {
return Triple(
items,
if (!libraryIsGrouped) {
arrayListOf(categoryAll)
} else {
categories
}
items
} else {
val (items, customCategories) = getCustomMangaItems(libraryManga)
this.categories = customCategories
items
}
this.allCategories = categories
return items to hiddenItems
},
hiddenItems,
)
}
private fun getCustomMangaItems(
libraryManga: List<LibraryManga>,
): Pair<List<LibraryItem>,
List<Category>,> {
sortingMode: Int,
isAscending: Boolean,
): Triple<List<LibraryItem>, List<Category>, List<LibraryItem>> {
val tagItems: MutableMap<String, LibraryHeaderItem> = mutableMapOf()
// internal function to make headers
@ -1267,8 +1288,8 @@ class LibraryPresenter(
val headers = tagItems.map { item ->
Category.createCustom(
item.key,
preferences.librarySortingMode().get(),
preferences.librarySortingAscending().get(),
sortingMode,
isAscending,
).apply {
id = item.value.catId
if (name.contains(sourceSplitter)) {
@ -1324,7 +1345,7 @@ class LibraryPresenter(
}
headers.forEachIndexed { index, category -> category.order = index }
return items to headers
return Triple(items, headers, listOf())
}
private fun mapTrackingOrder(status: String): String {