Add documentation to library presenter
This commit is contained in:
parent
2b6c435aba
commit
5d0ce6cb3f
@ -53,28 +53,27 @@ class LibraryPresenter(
|
|||||||
|
|
||||||
private val loggedServices by lazy { Injekt.get<TrackManager>().services.filter { it.isLogged } }
|
private val loggedServices by lazy { Injekt.get<TrackManager>().services.filter { it.isLogged } }
|
||||||
|
|
||||||
/**
|
/** Current categories of the library. */
|
||||||
* Categories of the library.
|
|
||||||
*/
|
|
||||||
var categories: List<Category> = emptyList()
|
var categories: List<Category> = emptyList()
|
||||||
private set
|
private set
|
||||||
|
|
||||||
|
/** All categories of the library, in case they are hidden because of hide categories is on */
|
||||||
var allCategories: List<Category> = emptyList()
|
var allCategories: List<Category> = emptyList()
|
||||||
private set
|
private set
|
||||||
|
|
||||||
/**
|
/** List of all manga to update the */
|
||||||
* List of all manga to update the
|
|
||||||
*/
|
|
||||||
var libraryItems: List<LibraryItem> = emptyList()
|
var libraryItems: List<LibraryItem> = emptyList()
|
||||||
private var allLibraryItems: List<LibraryItem> = emptyList()
|
private var allLibraryItems: List<LibraryItem> = emptyList()
|
||||||
|
|
||||||
private var totalChapters: Map<Long, Int>? = null
|
private var totalChapters: Map<Long, Int>? = null
|
||||||
|
|
||||||
|
/** Save the current list to speed up loading later */
|
||||||
fun onDestroy() {
|
fun onDestroy() {
|
||||||
lastLibraryItems = libraryItems
|
lastLibraryItems = libraryItems
|
||||||
lastCategories = categories
|
lastCategories = categories
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Restore the static items to speed up reloading the view */
|
||||||
fun onRestore() {
|
fun onRestore() {
|
||||||
libraryItems = lastLibraryItems ?: return
|
libraryItems = lastLibraryItems ?: return
|
||||||
categories = lastCategories ?: return
|
categories = lastCategories ?: return
|
||||||
@ -82,6 +81,7 @@ class LibraryPresenter(
|
|||||||
lastLibraryItems = null
|
lastLibraryItems = null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Get favorited manga for library and sort and filter it */
|
||||||
fun getLibrary() {
|
fun getLibrary() {
|
||||||
launchUI {
|
launchUI {
|
||||||
totalChapters = null
|
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<LibraryItem>): List<LibraryItem> {
|
private fun applyFilters(items: List<LibraryItem>): List<LibraryItem> {
|
||||||
val filterDownloaded = preferences.filterDownloaded().getOrDefault()
|
val filterDownloaded = preferences.filterDownloaded().getOrDefault()
|
||||||
|
|
||||||
val filterUnread = preferences.filterUnread().getOrDefault()
|
val filterUnread = preferences.filterUnread().getOrDefault()
|
||||||
@ -124,7 +124,7 @@ class LibraryPresenter(
|
|||||||
|
|
||||||
val filterTrackers = FilterBottomSheet.FILTER_TRACKER
|
val filterTrackers = FilterBottomSheet.FILTER_TRACKER
|
||||||
|
|
||||||
return map.filter f@{ item ->
|
return items.filter f@{ item ->
|
||||||
if (item.manga.isBlank()) {
|
if (item.manga.isBlank()) {
|
||||||
return@f filterDownloaded == 0 && filterUnread == 0 && filterCompleted == 0 &&
|
return@f filterDownloaded == 0 && filterUnread == 0 && filterCompleted == 0 &&
|
||||||
filterTracked == 0 && filterMangaType == 0
|
filterTracked == 0 && filterMangaType == 0
|
||||||
@ -193,7 +193,7 @@ class LibraryPresenter(
|
|||||||
/**
|
/**
|
||||||
* Sets downloaded chapter count to each manga.
|
* Sets downloaded chapter count to each manga.
|
||||||
*
|
*
|
||||||
* @param map the map of manga.
|
* @param itemList the map of manga.
|
||||||
*/
|
*/
|
||||||
private fun setDownloadCount(itemList: List<LibraryItem>) {
|
private fun setDownloadCount(itemList: List<LibraryItem>) {
|
||||||
if (!preferences.downloadBadge().getOrDefault()) {
|
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<LibraryItem>): List<LibraryItem> {
|
private fun applySort(itemList: List<LibraryItem>): List<LibraryItem> {
|
||||||
val sortingMode = preferences.librarySortingMode().getOrDefault()
|
val sortingMode = preferences.librarySortingMode().getOrDefault()
|
||||||
|
|
||||||
val lastReadManga by lazy {
|
val lastReadManga by lazy {
|
||||||
@ -280,9 +280,10 @@ class LibraryPresenter(
|
|||||||
else
|
else
|
||||||
Collections.reverseOrder(sortFn)
|
Collections.reverseOrder(sortFn)
|
||||||
|
|
||||||
return map.sortedWith(comparator)
|
return itemList.sortedWith(comparator)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Set the total chapters for the manga in the library */
|
||||||
private fun setTotalChapters() {
|
private fun setTotalChapters() {
|
||||||
if (totalChapters != null) return
|
if (totalChapters != null) return
|
||||||
val mangaMap = allLibraryItems
|
val mangaMap = allLibraryItems
|
||||||
@ -291,6 +292,10 @@ class LibraryPresenter(
|
|||||||
}.toMap()
|
}.toMap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Gets the category by id
|
||||||
|
*
|
||||||
|
* @param categoryId id of the categoty to get
|
||||||
|
*/
|
||||||
private fun getCategory(categoryId: Int): Category {
|
private fun getCategory(categoryId: Int): Category {
|
||||||
val category = categories.find { it.id == categoryId } ?: createDefaultCategory()
|
val category = categories.find { it.id == categoryId } ?: createDefaultCategory()
|
||||||
if (category.isFirst == null) {
|
if (category.isFirst == null) {
|
||||||
@ -301,14 +306,20 @@ class LibraryPresenter(
|
|||||||
return category
|
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(
|
private fun sortCategory(
|
||||||
i1: LibraryItem,
|
i1: LibraryItem,
|
||||||
i2: LibraryItem,
|
i2: LibraryItem,
|
||||||
lastReadManga: Map<Long, Int>,
|
lastReadManga: Map<Long, Int>
|
||||||
initCat: Category? = null
|
|
||||||
): Int {
|
): Int {
|
||||||
return if (initCat != null || i1.manga.category == i2.manga.category) {
|
return if (i1.manga.category == i2.manga.category) {
|
||||||
val category = initCat ?: allCategories.find { it.id == i1.manga.category } ?: return 0
|
val category = allCategories.find { it.id == i1.manga.category } ?: return 0
|
||||||
if (category.mangaOrder.isNullOrEmpty() && category.mangaSort == null) {
|
if (category.mangaOrder.isNullOrEmpty() && category.mangaSort == null) {
|
||||||
category.changeSortTo(preferences.librarySortingMode().getOrDefault())
|
category.changeSortTo(preferences.librarySortingMode().getOrDefault())
|
||||||
if (category.id == 0) preferences.defaultMangaOrder()
|
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 {
|
private fun sortAlphabetical(i1: LibraryItem, i2: LibraryItem): Int {
|
||||||
return if (preferences.removeArticles().getOrDefault())
|
return if (preferences.removeArticles().getOrDefault())
|
||||||
i1.manga.title.removeArticles().compareTo(i2.manga.title.removeArticles(), true)
|
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.
|
* 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<LibraryItem> {
|
private fun getLibraryFromDB(): List<LibraryItem> {
|
||||||
val categories = db.getCategories().executeAsBlocking().toMutableList()
|
val categories = db.getCategories().executeAsBlocking().toMutableList()
|
||||||
@ -436,6 +453,7 @@ class LibraryPresenter(
|
|||||||
return items
|
return items
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Create a default category with the sort set */
|
||||||
private fun createDefaultCategory(): Category {
|
private fun createDefaultCategory(): Category {
|
||||||
val default = Category.createDefault(context)
|
val default = Category.createDefault(context)
|
||||||
default.order = -1
|
default.order = -1
|
||||||
@ -445,9 +463,7 @@ class LibraryPresenter(
|
|||||||
return default
|
return default
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** Requests the library to be filtered. */
|
||||||
* Requests the library to be filtered.
|
|
||||||
*/
|
|
||||||
fun requestFilterUpdate() {
|
fun requestFilterUpdate() {
|
||||||
launchUI {
|
launchUI {
|
||||||
var mangaMap = allLibraryItems
|
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() {
|
fun requestDownloadBadgesUpdate() {
|
||||||
launchUI {
|
launchUI {
|
||||||
val mangaMap = allLibraryItems
|
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() {
|
fun requestUnreadBadgesUpdate() {
|
||||||
launchUI {
|
launchUI {
|
||||||
val mangaMap = allLibraryItems
|
val mangaMap = allLibraryItems
|
||||||
@ -488,9 +500,7 @@ class LibraryPresenter(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** Requests the library to be sorted. */
|
||||||
* Requests the library to be sorted.
|
|
||||||
*/
|
|
||||||
private fun requestSortUpdate() {
|
private fun requestSortUpdate() {
|
||||||
launchUI {
|
launchUI {
|
||||||
var mangaMap = libraryItems
|
var mangaMap = libraryItems
|
||||||
@ -528,6 +538,7 @@ class LibraryPresenter(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Remove manga from the library and delete the downloads */
|
||||||
fun confirmDeletion(mangas: List<Manga>) {
|
fun confirmDeletion(mangas: List<Manga>) {
|
||||||
scope.launch {
|
scope.launch {
|
||||||
val mangaToDelete = mangas.distinctBy { it.id }
|
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) {
|
fun updateManga(manga: LibraryManga) {
|
||||||
scope.launch {
|
scope.launch {
|
||||||
val rawMap = allLibraryItems
|
val rawMap = allLibraryItems
|
||||||
@ -559,6 +571,7 @@ class LibraryPresenter(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Undo the removal of the manga once in library */
|
||||||
fun reAddMangas(mangas: List<Manga>) {
|
fun reAddMangas(mangas: List<Manga>) {
|
||||||
scope.launch {
|
scope.launch {
|
||||||
val mangaToAdd = mangas.distinctBy { it.id }
|
val mangaToAdd = mangas.distinctBy { it.id }
|
||||||
@ -587,11 +600,13 @@ class LibraryPresenter(
|
|||||||
getLibrary()
|
getLibrary()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Returns first unread chapter of a manga */
|
||||||
fun getFirstUnread(manga: Manga): Chapter? {
|
fun getFirstUnread(manga: Manga): Chapter? {
|
||||||
val chapters = db.getChapters(manga).executeAsBlocking()
|
val chapters = db.getChapters(manga).executeAsBlocking()
|
||||||
return chapters.sortedByDescending { it.source_order }.find { !it.read }
|
return chapters.sortedByDescending { it.source_order }.find { !it.read }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Update a category's sorting */
|
||||||
fun sortCategory(catId: Int, order: Int) {
|
fun sortCategory(catId: Int, order: Int) {
|
||||||
val category = categories.find { catId == it.id } ?: return
|
val category = categories.find { catId == it.id } ?: return
|
||||||
category.mangaSort = ('a' + (order - 1))
|
category.mangaSort = ('a' + (order - 1))
|
||||||
@ -607,6 +622,7 @@ class LibraryPresenter(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Update a category's order */
|
||||||
fun rearrangeCategory(catId: Int?, mangaIds: List<Long>) {
|
fun rearrangeCategory(catId: Int?, mangaIds: List<Long>) {
|
||||||
scope.launch {
|
scope.launch {
|
||||||
val category = categories.find { catId == it.id } ?: return@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(
|
fun moveMangaToCategory(
|
||||||
manga: LibraryManga,
|
manga: LibraryManga,
|
||||||
catId: Int?,
|
catId: Int?,
|
||||||
@ -655,6 +672,7 @@ class LibraryPresenter(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Returns if manga is in a category by id */
|
||||||
fun mangaIsInCategory(manga: LibraryManga, catId: Int?): Boolean {
|
fun mangaIsInCategory(manga: LibraryManga, catId: Int?): Boolean {
|
||||||
val categories = db.getCategoriesForManga(manga).executeAsBlocking().map { it.id }
|
val categories = db.getCategoriesForManga(manga).executeAsBlocking().map { it.id }
|
||||||
return catId in categories
|
return catId in categories
|
||||||
@ -664,6 +682,7 @@ class LibraryPresenter(
|
|||||||
private var lastLibraryItems: List<LibraryItem>? = null
|
private var lastLibraryItems: List<LibraryItem>? = null
|
||||||
private var lastCategories: List<Category>? = null
|
private var lastCategories: List<Category>? = null
|
||||||
|
|
||||||
|
/** Give library manga to a date added based on min chapter fetch + remove custom info */
|
||||||
fun updateDB() {
|
fun updateDB() {
|
||||||
val db: DatabaseHelper = Injekt.get()
|
val db: DatabaseHelper = Injekt.get()
|
||||||
db.inTransaction {
|
db.inTransaction {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user