Moving library to new base coroutine classes

This commit is contained in:
Jays2Kings 2021-04-26 15:37:57 -04:00
parent a9125cc591
commit 6632ea2d2f
2 changed files with 29 additions and 48 deletions

View File

@ -57,7 +57,7 @@ import eu.kanade.tachiyomi.databinding.LibraryControllerBinding
import eu.kanade.tachiyomi.source.LocalSource
import eu.kanade.tachiyomi.ui.base.MaterialFastScroll
import eu.kanade.tachiyomi.ui.base.MaterialMenuSheet
import eu.kanade.tachiyomi.ui.base.controller.BaseController
import eu.kanade.tachiyomi.ui.base.controller.BaseCoroutineController
import eu.kanade.tachiyomi.ui.category.CategoryController
import eu.kanade.tachiyomi.ui.category.ManageCategoryDialog
import eu.kanade.tachiyomi.ui.library.LibraryGroup.BY_DEFAULT
@ -114,7 +114,7 @@ import kotlin.random.nextInt
class LibraryController(
bundle: Bundle? = null,
val preferences: PreferencesHelper = Injekt.get()
) : BaseController<LibraryControllerBinding>(bundle),
) : BaseCoroutineController<LibraryControllerBinding, LibraryPresenter>(bundle),
ActionMode.Callback,
FlexibleAdapter.OnItemClickListener,
FlexibleAdapter.OnItemLongClickListener,
@ -165,8 +165,7 @@ class LibraryController(
private var lastItemPosition: Int? = null
private var lastItem: IFlexible<*>? = null
lateinit var presenter: LibraryPresenter
private set
override var presenter = LibraryPresenter(this)
private var observeLater: Boolean = false
var searchItem = SearchGlobalItem()
@ -439,9 +438,8 @@ class LibraryController(
@SuppressLint("ClickableViewAccessibility")
override fun onViewCreated(view: View) {
super.onViewCreated(view)
if (!::presenter.isInitialized) presenter = LibraryPresenter(this)
adapter = LibraryCategoryAdapter(this)
adapter.stateRestorationPolicy = RecyclerView.Adapter.StateRestorationPolicy.PREVENT_WHEN_EMPTY
setRecyclerLayout()
binding.libraryGridRecycler.recycler.manager.spanSizeLookup = (
object : GridLayoutManager.SpanSizeLookup() {
@ -521,12 +519,10 @@ class LibraryController(
createActionModeIfNeeded()
}
presenter.onRestore()
if (presenter.libraryItems.isNotEmpty()) {
presenter.restoreLibrary()
} else {
binding.recyclerLayout.alpha = 0f
presenter.getLibrary()
}
}
@ -873,7 +869,7 @@ class LibraryController(
super.onActivityResumed(activity)
if (!isBindingInitialized) return
updateFilterSheetY()
if (observeLater && ::presenter.isInitialized) {
if (observeLater) {
presenter.getLibrary()
}
}
@ -881,12 +877,6 @@ class LibraryController(
override fun onActivityPaused(activity: Activity) {
super.onActivityPaused(activity)
observeLater = true
if (::presenter.isInitialized) presenter.onDestroy()
}
override fun onDestroy() {
if (::presenter.isInitialized) presenter.onDestroy()
super.onDestroy()
}
override fun onDestroyView(view: View) {
@ -897,7 +887,6 @@ class LibraryController(
}
displaySheet?.dismiss()
displaySheet = null
presenter.cancelScope()
super.onDestroyView(view)
}
@ -919,11 +908,10 @@ class LibraryController(
binding.progress.isVisible = false
if (!freshStart) {
justStarted = false
if (binding.recyclerLayout.alpha == 0f) binding.recyclerLayout.animate().alpha(1f).setDuration(
500
)
.start()
} else binding.recyclerLayout.alpha = 1f
} // else binding.recyclerLayout.alpha = 1f
if (binding.recyclerLayout.alpha == 0f) {
binding.recyclerLayout.animate().alpha(1f).setDuration(500).start()
}
if (justStarted && freshStart) {
scrollToHeader(activeCategory)
}

View File

@ -18,6 +18,7 @@ import eu.kanade.tachiyomi.source.LocalSource
import eu.kanade.tachiyomi.source.SourceManager
import eu.kanade.tachiyomi.source.model.SManga
import eu.kanade.tachiyomi.source.online.HttpSource
import eu.kanade.tachiyomi.ui.base.presenter.BaseCoroutinePresenter
import eu.kanade.tachiyomi.ui.library.LibraryGroup.BY_DEFAULT
import eu.kanade.tachiyomi.ui.library.LibraryGroup.BY_SOURCE
import eu.kanade.tachiyomi.ui.library.LibraryGroup.BY_TAG
@ -30,10 +31,7 @@ import eu.kanade.tachiyomi.ui.library.filter.FilterBottomSheet.Companion.STATE_I
import eu.kanade.tachiyomi.util.lang.capitalizeWords
import eu.kanade.tachiyomi.util.lang.removeArticles
import eu.kanade.tachiyomi.util.system.executeOnIO
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import uy.kohesive.injekt.Injekt
@ -52,9 +50,7 @@ class LibraryPresenter(
private val coverCache: CoverCache = Injekt.get(),
private val sourceManager: SourceManager = Injekt.get(),
private val downloadManager: DownloadManager = Injekt.get()
) {
private var scope = CoroutineScope(Job() + Dispatchers.Default)
) : BaseCoroutinePresenter() {
private val context = preferences.context
@ -87,22 +83,20 @@ class LibraryPresenter(
val libraryIsGrouped
get() = groupType != UNGROUPED
fun cancelScope() {
scope.cancel()
}
/** Save the current list to speed up loading later */
fun onDestroy() {
override fun onDestroy() {
super.onDestroy()
lastLibraryItems = libraryItems
lastCategories = categories
}
/** Restore the static items to speed up reloading the view */
fun onRestore() {
override fun onCreate() {
super.onCreate()
libraryItems = lastLibraryItems ?: return
categories = lastCategories ?: return
lastCategories = null
lastLibraryItems = null
getLibrary()
}
/** Get favorited manga for library and sort and filter it */
@ -110,8 +104,7 @@ class LibraryPresenter(
if (categories.isEmpty()) {
categories = lastCategories ?: db.getCategories().executeAsBlocking().toMutableList()
}
scope = CoroutineScope(Job() + Dispatchers.Default)
scope.launch {
presenterScope.launch {
val library = withContext(Dispatchers.IO) { getLibraryFromDB() }
library.apply {
setDownloadCount(library)
@ -677,7 +670,7 @@ class LibraryPresenter(
/** Requests the library to be filtered. */
fun requestFilterUpdate() {
scope.launch {
presenterScope.launch {
var mangaMap = allLibraryItems
mangaMap = applyFilters(mangaMap)
mangaMap = applySort(mangaMap)
@ -687,7 +680,7 @@ class LibraryPresenter(
/** Requests the library to have download badges added/removed. */
fun requestDownloadBadgesUpdate() {
scope.launch {
presenterScope.launch {
val mangaMap = allLibraryItems
setDownloadCount(mangaMap)
allLibraryItems = mangaMap
@ -699,7 +692,7 @@ class LibraryPresenter(
/** Requests the library to have unread badges changed. */
fun requestUnreadBadgesUpdate() {
scope.launch {
presenterScope.launch {
val mangaMap = allLibraryItems
setUnreadBadge(mangaMap)
allLibraryItems = mangaMap
@ -711,7 +704,7 @@ class LibraryPresenter(
/** Requests the library to be sorted. */
private fun requestSortUpdate() {
scope.launch {
presenterScope.launch {
var mangaMap = libraryItems
mangaMap = applySort(mangaMap)
sectionLibrary(mangaMap)
@ -743,7 +736,7 @@ class LibraryPresenter(
* @param mangas the list of manga to delete.
*/
fun removeMangaFromLibrary(mangas: List<Manga>) {
scope.launch {
presenterScope.launch {
// Create a set of the list
val mangaToDelete = mangas.distinctBy { it.id }
mangaToDelete.forEach { it.favorite = false }
@ -755,7 +748,7 @@ class LibraryPresenter(
/** Remove manga from the library and delete the downloads */
fun confirmDeletion(mangas: List<Manga>) {
scope.launch {
presenterScope.launch {
val mangaToDelete = mangas.distinctBy { it.id }
mangaToDelete.forEach { manga ->
coverCache.deleteFromCache(manga)
@ -769,14 +762,14 @@ class LibraryPresenter(
/** Called when Library Service updates a manga, update the item as well */
fun updateManga() {
scope.launch {
presenterScope.launch {
getLibrary()
}
}
/** Undo the removal of the manga once in library */
fun reAddMangas(mangas: List<Manga>) {
scope.launch {
presenterScope.launch {
val mangaToAdd = mangas.distinctBy { it.id }
mangaToAdd.forEach { it.favorite = true }
db.insertMangas(mangaToAdd).executeOnIO()
@ -829,7 +822,7 @@ class LibraryPresenter(
/** Update a category's order */
fun rearrangeCategory(catId: Int?, mangaIds: List<Long>) {
scope.launch {
presenterScope.launch {
val category = categories.find { catId == it.id } ?: return@launch
if (category.isDynamic) return@launch
category.mangaSort = null
@ -846,7 +839,7 @@ class LibraryPresenter(
catId: Int?,
mangaIds: List<Long>
) {
scope.launch {
presenterScope.launch {
val categoryId = catId ?: return@launch
val category = categories.find { catId == it.id } ?: return@launch
if (category.isDynamic) return@launch
@ -944,7 +937,7 @@ class LibraryPresenter(
/** download All unread */
fun downloadUnread(mangaList: List<Manga>) {
scope.launch {
presenterScope.launch {
withContext(Dispatchers.IO) {
mangaList.forEach {
val chapters = db.getChapters(it).executeAsBlocking().filter { !it.read }
@ -958,7 +951,7 @@ class LibraryPresenter(
}
fun markReadStatus(mangaList: List<Manga>, markRead: Boolean) {
scope.launch {
presenterScope.launch {
withContext(Dispatchers.IO) {
mangaList.forEach {
withContext(Dispatchers.IO) {