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.source.LocalSource
import eu.kanade.tachiyomi.ui.base.MaterialFastScroll import eu.kanade.tachiyomi.ui.base.MaterialFastScroll
import eu.kanade.tachiyomi.ui.base.MaterialMenuSheet 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.CategoryController
import eu.kanade.tachiyomi.ui.category.ManageCategoryDialog import eu.kanade.tachiyomi.ui.category.ManageCategoryDialog
import eu.kanade.tachiyomi.ui.library.LibraryGroup.BY_DEFAULT import eu.kanade.tachiyomi.ui.library.LibraryGroup.BY_DEFAULT
@ -114,7 +114,7 @@ import kotlin.random.nextInt
class LibraryController( class LibraryController(
bundle: Bundle? = null, bundle: Bundle? = null,
val preferences: PreferencesHelper = Injekt.get() val preferences: PreferencesHelper = Injekt.get()
) : BaseController<LibraryControllerBinding>(bundle), ) : BaseCoroutineController<LibraryControllerBinding, LibraryPresenter>(bundle),
ActionMode.Callback, ActionMode.Callback,
FlexibleAdapter.OnItemClickListener, FlexibleAdapter.OnItemClickListener,
FlexibleAdapter.OnItemLongClickListener, FlexibleAdapter.OnItemLongClickListener,
@ -165,8 +165,7 @@ class LibraryController(
private var lastItemPosition: Int? = null private var lastItemPosition: Int? = null
private var lastItem: IFlexible<*>? = null private var lastItem: IFlexible<*>? = null
lateinit var presenter: LibraryPresenter override var presenter = LibraryPresenter(this)
private set
private var observeLater: Boolean = false private var observeLater: Boolean = false
var searchItem = SearchGlobalItem() var searchItem = SearchGlobalItem()
@ -439,9 +438,8 @@ class LibraryController(
@SuppressLint("ClickableViewAccessibility") @SuppressLint("ClickableViewAccessibility")
override fun onViewCreated(view: View) { override fun onViewCreated(view: View) {
super.onViewCreated(view) super.onViewCreated(view)
if (!::presenter.isInitialized) presenter = LibraryPresenter(this)
adapter = LibraryCategoryAdapter(this) adapter = LibraryCategoryAdapter(this)
adapter.stateRestorationPolicy = RecyclerView.Adapter.StateRestorationPolicy.PREVENT_WHEN_EMPTY
setRecyclerLayout() setRecyclerLayout()
binding.libraryGridRecycler.recycler.manager.spanSizeLookup = ( binding.libraryGridRecycler.recycler.manager.spanSizeLookup = (
object : GridLayoutManager.SpanSizeLookup() { object : GridLayoutManager.SpanSizeLookup() {
@ -521,12 +519,10 @@ class LibraryController(
createActionModeIfNeeded() createActionModeIfNeeded()
} }
presenter.onRestore()
if (presenter.libraryItems.isNotEmpty()) { if (presenter.libraryItems.isNotEmpty()) {
presenter.restoreLibrary() presenter.restoreLibrary()
} else { } else {
binding.recyclerLayout.alpha = 0f binding.recyclerLayout.alpha = 0f
presenter.getLibrary()
} }
} }
@ -873,7 +869,7 @@ class LibraryController(
super.onActivityResumed(activity) super.onActivityResumed(activity)
if (!isBindingInitialized) return if (!isBindingInitialized) return
updateFilterSheetY() updateFilterSheetY()
if (observeLater && ::presenter.isInitialized) { if (observeLater) {
presenter.getLibrary() presenter.getLibrary()
} }
} }
@ -881,12 +877,6 @@ class LibraryController(
override fun onActivityPaused(activity: Activity) { override fun onActivityPaused(activity: Activity) {
super.onActivityPaused(activity) super.onActivityPaused(activity)
observeLater = true observeLater = true
if (::presenter.isInitialized) presenter.onDestroy()
}
override fun onDestroy() {
if (::presenter.isInitialized) presenter.onDestroy()
super.onDestroy()
} }
override fun onDestroyView(view: View) { override fun onDestroyView(view: View) {
@ -897,7 +887,6 @@ class LibraryController(
} }
displaySheet?.dismiss() displaySheet?.dismiss()
displaySheet = null displaySheet = null
presenter.cancelScope()
super.onDestroyView(view) super.onDestroyView(view)
} }
@ -919,11 +908,10 @@ class LibraryController(
binding.progress.isVisible = false binding.progress.isVisible = false
if (!freshStart) { if (!freshStart) {
justStarted = false justStarted = false
if (binding.recyclerLayout.alpha == 0f) binding.recyclerLayout.animate().alpha(1f).setDuration( } // else binding.recyclerLayout.alpha = 1f
500 if (binding.recyclerLayout.alpha == 0f) {
) binding.recyclerLayout.animate().alpha(1f).setDuration(500).start()
.start() }
} else binding.recyclerLayout.alpha = 1f
if (justStarted && freshStart) { if (justStarted && freshStart) {
scrollToHeader(activeCategory) 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.SourceManager
import eu.kanade.tachiyomi.source.model.SManga import eu.kanade.tachiyomi.source.model.SManga
import eu.kanade.tachiyomi.source.online.HttpSource 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_DEFAULT
import eu.kanade.tachiyomi.ui.library.LibraryGroup.BY_SOURCE import eu.kanade.tachiyomi.ui.library.LibraryGroup.BY_SOURCE
import eu.kanade.tachiyomi.ui.library.LibraryGroup.BY_TAG 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.capitalizeWords
import eu.kanade.tachiyomi.util.lang.removeArticles import eu.kanade.tachiyomi.util.lang.removeArticles
import eu.kanade.tachiyomi.util.system.executeOnIO import eu.kanade.tachiyomi.util.system.executeOnIO
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.Injekt
@ -52,9 +50,7 @@ class LibraryPresenter(
private val coverCache: CoverCache = Injekt.get(), private val coverCache: CoverCache = Injekt.get(),
private val sourceManager: SourceManager = Injekt.get(), private val sourceManager: SourceManager = Injekt.get(),
private val downloadManager: DownloadManager = Injekt.get() private val downloadManager: DownloadManager = Injekt.get()
) { ) : BaseCoroutinePresenter() {
private var scope = CoroutineScope(Job() + Dispatchers.Default)
private val context = preferences.context private val context = preferences.context
@ -87,22 +83,20 @@ class LibraryPresenter(
val libraryIsGrouped val libraryIsGrouped
get() = groupType != UNGROUPED get() = groupType != UNGROUPED
fun cancelScope() {
scope.cancel()
}
/** Save the current list to speed up loading later */ /** Save the current list to speed up loading later */
fun onDestroy() { override fun onDestroy() {
super.onDestroy()
lastLibraryItems = libraryItems lastLibraryItems = libraryItems
lastCategories = categories lastCategories = categories
} }
/** Restore the static items to speed up reloading the view */ override fun onCreate() {
fun onRestore() { super.onCreate()
libraryItems = lastLibraryItems ?: return libraryItems = lastLibraryItems ?: return
categories = lastCategories ?: return categories = lastCategories ?: return
lastCategories = null lastCategories = null
lastLibraryItems = null lastLibraryItems = null
getLibrary()
} }
/** Get favorited manga for library and sort and filter it */ /** Get favorited manga for library and sort and filter it */
@ -110,8 +104,7 @@ class LibraryPresenter(
if (categories.isEmpty()) { if (categories.isEmpty()) {
categories = lastCategories ?: db.getCategories().executeAsBlocking().toMutableList() categories = lastCategories ?: db.getCategories().executeAsBlocking().toMutableList()
} }
scope = CoroutineScope(Job() + Dispatchers.Default) presenterScope.launch {
scope.launch {
val library = withContext(Dispatchers.IO) { getLibraryFromDB() } val library = withContext(Dispatchers.IO) { getLibraryFromDB() }
library.apply { library.apply {
setDownloadCount(library) setDownloadCount(library)
@ -677,7 +670,7 @@ class LibraryPresenter(
/** Requests the library to be filtered. */ /** Requests the library to be filtered. */
fun requestFilterUpdate() { fun requestFilterUpdate() {
scope.launch { presenterScope.launch {
var mangaMap = allLibraryItems var mangaMap = allLibraryItems
mangaMap = applyFilters(mangaMap) mangaMap = applyFilters(mangaMap)
mangaMap = applySort(mangaMap) mangaMap = applySort(mangaMap)
@ -687,7 +680,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() {
scope.launch { presenterScope.launch {
val mangaMap = allLibraryItems val mangaMap = allLibraryItems
setDownloadCount(mangaMap) setDownloadCount(mangaMap)
allLibraryItems = mangaMap allLibraryItems = mangaMap
@ -699,7 +692,7 @@ class LibraryPresenter(
/** Requests the library to have unread badges changed. */ /** Requests the library to have unread badges changed. */
fun requestUnreadBadgesUpdate() { fun requestUnreadBadgesUpdate() {
scope.launch { presenterScope.launch {
val mangaMap = allLibraryItems val mangaMap = allLibraryItems
setUnreadBadge(mangaMap) setUnreadBadge(mangaMap)
allLibraryItems = mangaMap allLibraryItems = mangaMap
@ -711,7 +704,7 @@ class LibraryPresenter(
/** Requests the library to be sorted. */ /** Requests the library to be sorted. */
private fun requestSortUpdate() { private fun requestSortUpdate() {
scope.launch { presenterScope.launch {
var mangaMap = libraryItems var mangaMap = libraryItems
mangaMap = applySort(mangaMap) mangaMap = applySort(mangaMap)
sectionLibrary(mangaMap) sectionLibrary(mangaMap)
@ -743,7 +736,7 @@ class LibraryPresenter(
* @param mangas the list of manga to delete. * @param mangas the list of manga to delete.
*/ */
fun removeMangaFromLibrary(mangas: List<Manga>) { fun removeMangaFromLibrary(mangas: List<Manga>) {
scope.launch { presenterScope.launch {
// Create a set of the list // Create a set of the list
val mangaToDelete = mangas.distinctBy { it.id } val mangaToDelete = mangas.distinctBy { it.id }
mangaToDelete.forEach { it.favorite = false } mangaToDelete.forEach { it.favorite = false }
@ -755,7 +748,7 @@ class LibraryPresenter(
/** Remove manga from the library and delete the downloads */ /** Remove manga from the library and delete the downloads */
fun confirmDeletion(mangas: List<Manga>) { fun confirmDeletion(mangas: List<Manga>) {
scope.launch { presenterScope.launch {
val mangaToDelete = mangas.distinctBy { it.id } val mangaToDelete = mangas.distinctBy { it.id }
mangaToDelete.forEach { manga -> mangaToDelete.forEach { manga ->
coverCache.deleteFromCache(manga) coverCache.deleteFromCache(manga)
@ -769,14 +762,14 @@ class LibraryPresenter(
/** Called when Library Service updates a manga, update the item as well */ /** Called when Library Service updates a manga, update the item as well */
fun updateManga() { fun updateManga() {
scope.launch { presenterScope.launch {
getLibrary() getLibrary()
} }
} }
/** Undo the removal of the manga once in library */ /** Undo the removal of the manga once in library */
fun reAddMangas(mangas: List<Manga>) { fun reAddMangas(mangas: List<Manga>) {
scope.launch { presenterScope.launch {
val mangaToAdd = mangas.distinctBy { it.id } val mangaToAdd = mangas.distinctBy { it.id }
mangaToAdd.forEach { it.favorite = true } mangaToAdd.forEach { it.favorite = true }
db.insertMangas(mangaToAdd).executeOnIO() db.insertMangas(mangaToAdd).executeOnIO()
@ -829,7 +822,7 @@ class LibraryPresenter(
/** Update a category's order */ /** Update a category's order */
fun rearrangeCategory(catId: Int?, mangaIds: List<Long>) { fun rearrangeCategory(catId: Int?, mangaIds: List<Long>) {
scope.launch { presenterScope.launch {
val category = categories.find { catId == it.id } ?: return@launch val category = categories.find { catId == it.id } ?: return@launch
if (category.isDynamic) return@launch if (category.isDynamic) return@launch
category.mangaSort = null category.mangaSort = null
@ -846,7 +839,7 @@ class LibraryPresenter(
catId: Int?, catId: Int?,
mangaIds: List<Long> mangaIds: List<Long>
) { ) {
scope.launch { presenterScope.launch {
val categoryId = catId ?: return@launch val categoryId = catId ?: return@launch
val category = categories.find { catId == it.id } ?: return@launch val category = categories.find { catId == it.id } ?: return@launch
if (category.isDynamic) return@launch if (category.isDynamic) return@launch
@ -944,7 +937,7 @@ class LibraryPresenter(
/** download All unread */ /** download All unread */
fun downloadUnread(mangaList: List<Manga>) { fun downloadUnread(mangaList: List<Manga>) {
scope.launch { presenterScope.launch {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
mangaList.forEach { mangaList.forEach {
val chapters = db.getChapters(it).executeAsBlocking().filter { !it.read } val chapters = db.getChapters(it).executeAsBlocking().filter { !it.read }
@ -958,7 +951,7 @@ class LibraryPresenter(
} }
fun markReadStatus(mangaList: List<Manga>, markRead: Boolean) { fun markReadStatus(mangaList: List<Manga>, markRead: Boolean) {
scope.launch { presenterScope.launch {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
mangaList.forEach { mangaList.forEach {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {