From 5d0ce6cb3f57e817f5067cf1042aa66241272021 Mon Sep 17 00:00:00 2001 From: Jay Date: Mon, 13 Apr 2020 23:54:50 -0400 Subject: [PATCH] Add documentation to library presenter --- .../tachiyomi/ui/library/LibraryPresenter.kt | 83 ++++++++++++------- 1 file changed, 51 insertions(+), 32 deletions(-) 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 ded7f9b5ee..64ffa4ea46 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 @@ -53,28 +53,27 @@ class LibraryPresenter( private val loggedServices by lazy { Injekt.get().services.filter { it.isLogged } } - /** - * Categories of the library. - */ + /** Current categories of the library. */ var categories: List = emptyList() private set + /** All categories of the library, in case they are hidden because of hide categories is on */ var allCategories: List = emptyList() private set - /** - * List of all manga to update the - */ + /** List of all manga to update the */ var libraryItems: List = emptyList() private var allLibraryItems: List = emptyList() private var totalChapters: Map? = null + /** Save the current list to speed up loading later */ fun onDestroy() { lastLibraryItems = libraryItems lastCategories = categories } + /** Restore the static items to speed up reloading the view */ fun onRestore() { libraryItems = lastLibraryItems ?: return categories = lastCategories ?: return @@ -82,6 +81,7 @@ class LibraryPresenter( lastLibraryItems = null } + /** Get favorited manga for library and sort and filter it */ fun getLibrary() { launchUI { totalChapters = null @@ -107,11 +107,11 @@ class LibraryPresenter( } /** - * Applies library filters to the given map of manga. + * Applies library filters to the given list of manga. * - * @param map the map to filter. + * @param items the items to filter. */ - private fun applyFilters(map: List): List { + private fun applyFilters(items: List): List { val filterDownloaded = preferences.filterDownloaded().getOrDefault() val filterUnread = preferences.filterUnread().getOrDefault() @@ -124,7 +124,7 @@ class LibraryPresenter( val filterTrackers = FilterBottomSheet.FILTER_TRACKER - return map.filter f@{ item -> + return items.filter f@{ item -> if (item.manga.isBlank()) { return@f filterDownloaded == 0 && filterUnread == 0 && filterCompleted == 0 && filterTracked == 0 && filterMangaType == 0 @@ -193,7 +193,7 @@ class LibraryPresenter( /** * Sets downloaded chapter count to each manga. * - * @param map the map of manga. + * @param itemList the map of manga. */ private fun setDownloadCount(itemList: List) { if (!preferences.downloadBadge().getOrDefault()) { @@ -217,11 +217,11 @@ class LibraryPresenter( } /** - * Applies library sorting to the given map of manga. + * Applies library sorting to the given list of manga. * - * @param map the map to sort. + * @param itemList the map to sort. */ - private fun applySort(map: List): List { + private fun applySort(itemList: List): List { val sortingMode = preferences.librarySortingMode().getOrDefault() val lastReadManga by lazy { @@ -280,9 +280,10 @@ class LibraryPresenter( else Collections.reverseOrder(sortFn) - return map.sortedWith(comparator) + return itemList.sortedWith(comparator) } + /** Set the total chapters for the manga in the library */ private fun setTotalChapters() { if (totalChapters != null) return val mangaMap = allLibraryItems @@ -291,6 +292,10 @@ class LibraryPresenter( }.toMap() } + /** Gets the category by id + * + * @param categoryId id of the categoty to get + */ private fun getCategory(categoryId: Int): Category { val category = categories.find { it.id == categoryId } ?: createDefaultCategory() if (category.isFirst == null) { @@ -301,14 +306,20 @@ class LibraryPresenter( return category } + /** + * Sort 2 manga by the category's sorting + * + * @param i1 the first manga + * @param i2 the second manga to compare + * @param lastReadManga map of the last read of the library + */ private fun sortCategory( i1: LibraryItem, i2: LibraryItem, - lastReadManga: Map, - initCat: Category? = null + lastReadManga: Map ): Int { - return if (initCat != null || i1.manga.category == i2.manga.category) { - val category = initCat ?: allCategories.find { it.id == i1.manga.category } ?: return 0 + return if (i1.manga.category == i2.manga.category) { + val category = allCategories.find { it.id == i1.manga.category } ?: return 0 if (category.mangaOrder.isNullOrEmpty() && category.mangaSort == null) { category.changeSortTo(preferences.librarySortingMode().getOrDefault()) if (category.id == 0) preferences.defaultMangaOrder() @@ -371,6 +382,12 @@ class LibraryPresenter( } } + /** + * Sort 2 manga by the their title (and remove articles if need be) + * + * @param i1 the first manga + * @param i2 the second manga to compare + */ private fun sortAlphabetical(i1: LibraryItem, i2: LibraryItem): Int { return if (preferences.removeArticles().getOrDefault()) i1.manga.title.removeArticles().compareTo(i2.manga.title.removeArticles(), true) @@ -380,7 +397,7 @@ class LibraryPresenter( /** * Get the categories and all its manga from the database. * - * @return an observable of the categories and its manga. + * @return an list of all the manga in a itemized form. */ private fun getLibraryFromDB(): List { val categories = db.getCategories().executeAsBlocking().toMutableList() @@ -436,6 +453,7 @@ class LibraryPresenter( return items } + /** Create a default category with the sort set */ private fun createDefaultCategory(): Category { val default = Category.createDefault(context) default.order = -1 @@ -445,9 +463,7 @@ class LibraryPresenter( return default } - /** - * Requests the library to be filtered. - */ + /** Requests the library to be filtered. */ fun requestFilterUpdate() { launchUI { var mangaMap = allLibraryItems @@ -458,9 +474,7 @@ class LibraryPresenter( } } - /** - * Requests the library to have download badges added/removed. - */ + /** Requests the library to have download badges added/removed. */ fun requestDownloadBadgesUpdate() { launchUI { val mangaMap = allLibraryItems @@ -473,9 +487,7 @@ class LibraryPresenter( } } - /** - * Requests the library to have unread badges changed. - */ + /** Requests the library to have unread badges changed. */ fun requestUnreadBadgesUpdate() { launchUI { val mangaMap = allLibraryItems @@ -488,9 +500,7 @@ class LibraryPresenter( } } - /** - * Requests the library to be sorted. - */ + /** Requests the library to be sorted. */ private fun requestSortUpdate() { launchUI { var mangaMap = libraryItems @@ -528,6 +538,7 @@ class LibraryPresenter( } } + /** Remove manga from the library and delete the downloads */ fun confirmDeletion(mangas: List) { scope.launch { val mangaToDelete = mangas.distinctBy { it.id } @@ -541,6 +552,7 @@ class LibraryPresenter( } } + /** Called when Library Service updates a manga, update the item as well */ fun updateManga(manga: LibraryManga) { scope.launch { val rawMap = allLibraryItems @@ -559,6 +571,7 @@ class LibraryPresenter( } } + /** Undo the removal of the manga once in library */ fun reAddMangas(mangas: List) { scope.launch { val mangaToAdd = mangas.distinctBy { it.id } @@ -587,11 +600,13 @@ class LibraryPresenter( getLibrary() } + /** Returns first unread chapter of a manga */ fun getFirstUnread(manga: Manga): Chapter? { val chapters = db.getChapters(manga).executeAsBlocking() return chapters.sortedByDescending { it.source_order }.find { !it.read } } + /** Update a category's sorting */ fun sortCategory(catId: Int, order: Int) { val category = categories.find { catId == it.id } ?: return category.mangaSort = ('a' + (order - 1)) @@ -607,6 +622,7 @@ class LibraryPresenter( } } + /** Update a category's order */ fun rearrangeCategory(catId: Int?, mangaIds: List) { scope.launch { val category = categories.find { catId == it.id } ?: return@launch @@ -618,6 +634,7 @@ class LibraryPresenter( } } + /** Shift a manga's category via drag & drop */ fun moveMangaToCategory( manga: LibraryManga, catId: Int?, @@ -655,6 +672,7 @@ class LibraryPresenter( } } + /** Returns if manga is in a category by id */ fun mangaIsInCategory(manga: LibraryManga, catId: Int?): Boolean { val categories = db.getCategoriesForManga(manga).executeAsBlocking().map { it.id } return catId in categories @@ -664,6 +682,7 @@ class LibraryPresenter( private var lastLibraryItems: List? = null private var lastCategories: List? = null + /** Give library manga to a date added based on min chapter fetch + remove custom info */ fun updateDB() { val db: DatabaseHelper = Injekt.get() db.inTransaction {