refactor(library): Use flow to update libraryManga

This commit is contained in:
Ahmad Ansori Palembani 2024-06-18 11:14:56 +07:00
parent 4a9dceaef2
commit 8e32b6b74e
Signed by: null2264
GPG key ID: BA64F8B60AF3EFB6
4 changed files with 37 additions and 29 deletions

View file

@ -544,7 +544,7 @@ open class LibraryController(
}
presenter.groupType = item
shouldScrollToTop = true
presenter.getLibrary()
presenter.updateLibrary()
true
}.show()
}
@ -1033,7 +1033,7 @@ open class LibraryController(
if (type.isEnter) {
binding.filterBottomSheet.filterBottomSheet.isVisible = true
if (type == ControllerChangeType.POP_ENTER) {
presenter.getLibrary()
presenter.updateLibrary()
isPoppingIn = true
}
DownloadJob.callListeners()
@ -1073,7 +1073,7 @@ open class LibraryController(
if (!isBindingInitialized) return
updateFilterSheetY()
if (observeLater) {
presenter.getLibrary()
presenter.updateLibrary()
}
}
@ -1384,7 +1384,7 @@ open class LibraryController(
private fun onRefresh() {
showCategories(false)
presenter.getLibrary()
presenter.updateLibrary()
destroyActionModeIfNeeded()
}
@ -1408,14 +1408,14 @@ open class LibraryController(
val isShowAllCategoriesSet = preferences.showAllCategories().get()
if (!query.isNullOrBlank() && this.query.isBlank() && !isShowAllCategoriesSet) {
presenter.forceShowAllCategories = preferences.showAllCategoriesWhenSearchingSingleCategory().get()
presenter.getLibrary()
presenter.updateLibrary()
} else if (query.isNullOrBlank() && this.query.isNotBlank() && !isShowAllCategoriesSet) {
if (!isSubClass) {
preferences.showAllCategoriesWhenSearchingSingleCategory()
.set(presenter.forceShowAllCategories)
}
presenter.forceShowAllCategories = false
presenter.getLibrary()
presenter.updateLibrary()
}
if (query != this.query && !query.isNullOrBlank()) {
@ -1796,7 +1796,7 @@ open class LibraryController(
val category = (adapter.getItem(position) as? LibraryHeaderItem)?.category ?: return
if (!category.isDynamic) {
ManageCategoryDialog(category) {
presenter.getLibrary()
presenter.updateLibrary()
}.showDialog(router)
}
}
@ -1889,7 +1889,7 @@ open class LibraryController(
isGone = true
setOnClickListener {
presenter.forceShowAllCategories = !presenter.forceShowAllCategories
presenter.getLibrary()
presenter.updateLibrary()
isSelected = presenter.forceShowAllCategories
}
val pad = 12.dpToPx
@ -2168,7 +2168,7 @@ open class LibraryController(
private fun showChangeMangaCategoriesSheet() {
val activity = activity ?: return
selectedMangas.toList().moveCategories(presenter.db, activity) {
presenter.getLibrary()
presenter.updateLibrary()
destroyActionModeIfNeeded()
}
}

View file

@ -49,6 +49,7 @@ import eu.kanade.tachiyomi.util.system.launchUI
import eu.kanade.tachiyomi.util.system.withIOContext
import eu.kanade.tachiyomi.util.system.withUIContext
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
@ -78,7 +79,7 @@ class LibraryPresenter(
private val getLibraryManga: GetLibraryManga by injectLazy()
private val getChapters: GetChapters by injectLazy()
private val libraryManga: List<LibraryManga> = emptyList()
private var libraryManga: List<LibraryManga> = emptyList()
private val context = preferences.context
private val viewContext
@ -164,7 +165,19 @@ class LibraryPresenter(
lastLibraryItems = null
lastAllLibraryItems = null
}
getLibrary()
presenterScope.launch {
getLibraryManga.subscribe().collectLatest { lib ->
libraryManga = lib.apply {
if (groupType > BY_DEFAULT) {
distinctBy { it.id }
}
}
updateLibrary()
}
}
if (!preferences.showLibrarySearchSuggestions().isSet()) {
DelayedLibrarySuggestionsJob.setupTask(context, true)
} else if (preferences.showLibrarySearchSuggestions().get() &&
@ -187,8 +200,8 @@ class LibraryPresenter(
}
}
/** Get favorited manga for library and sort and filter it */
fun getLibrary() {
/** Update library state and sort and filter it */
fun updateLibrary() {
if (categories.isEmpty()) {
val dbCategories = db.getCategories().executeAsBlocking()
if ((dbCategories + Category.createDefault(context)).distinctBy { it.order }.size != dbCategories.size + 1) {
@ -708,11 +721,7 @@ class LibraryPresenter(
private suspend fun getLibraryFromDB(): Pair<List<LibraryItem>, List<LibraryItem>> {
removeArticles = preferences.removeArticles().get()
val categories = db.getCategories().executeAsBlocking().toMutableList()
var libraryManga = getLibraryManga.await()
val showAll = showAllCategories
if (groupType > BY_DEFAULT) {
libraryManga = libraryManga.distinctBy { it.id }
}
val hiddenItems = mutableListOf<LibraryItem>()
val items = if (groupType <= BY_DEFAULT || !libraryIsGrouped) {
@ -1076,7 +1085,7 @@ class LibraryPresenter(
mangaToDelete.forEach { it.favorite = false }
db.insertMangas(mangaToDelete).executeOnIO()
getLibrary()
updateLibrary()
}
}
@ -1102,7 +1111,7 @@ class LibraryPresenter(
/** Called when Library Service updates a manga, update the item as well */
fun updateManga() {
presenterScope.launch {
getLibrary()
updateLibrary()
}
}
@ -1113,7 +1122,7 @@ class LibraryPresenter(
mangaToAdd.forEach { it.favorite = true }
db.insertMangas(mangaToAdd).executeOnIO()
(view as? FilteredLibraryController)?.updateStatsPage()
getLibrary()
updateLibrary()
mangaToAdd.forEach { db.insertManga(it).executeAsBlocking() }
}
}
@ -1201,7 +1210,7 @@ class LibraryPresenter(
db.insertCategory(category).executeAsBlocking()
}
}
getLibrary()
updateLibrary()
}
}
@ -1235,7 +1244,7 @@ class LibraryPresenter(
}
preferences.collapsedDynamicCategories().set(categoriesHidden)
}
getLibrary()
updateLibrary()
}
private fun getDynamicCategoryName(category: Category): String =
@ -1266,7 +1275,7 @@ class LibraryPresenter(
}
}
}
getLibrary()
updateLibrary()
}
fun allCategoriesExpanded(): Boolean {
@ -1309,7 +1318,7 @@ class LibraryPresenter(
mapMangaChapters[manga] = oldChapters
}
getLibrary()
updateLibrary()
}
return mapMangaChapters
}
@ -1321,7 +1330,7 @@ class LibraryPresenter(
mangaList.forEach { (_, chapters) ->
db.updateChaptersProgress(chapters).executeAsBlocking()
}
getLibrary()
updateLibrary()
}
}

View file

@ -17,7 +17,7 @@ class LibraryCategoryView @JvmOverloads constructor(context: Context, attrs: Att
override fun initGeneralPreferences() {
with(binding) {
showAll.bindToPreference(preferences.showAllCategories()) {
controller?.presenter?.getLibrary()
controller?.presenter?.updateLibrary()
binding.categoryShow.isEnabled = it
}
categoryShow.isEnabled = showAll.isChecked
@ -27,7 +27,7 @@ class LibraryCategoryView @JvmOverloads constructor(context: Context, attrs: Att
dynamicToBottom.text = context.getString(R.string.move_dynamic_to_bottom)
.withSubtitle(context, R.string.when_grouping_by_sources_tags)
dynamicToBottom.bindToPreference(preferences.collapsedDynamicAtBottom()) {
controller?.presenter?.getLibrary()
controller?.presenter?.updateLibrary()
}
showEmptyCatsFiltering.bindToPreference(preferences.showEmptyCategoriesWhileFiltering()) {
controller?.presenter?.requestFilterUpdate()

View file

@ -25,13 +25,12 @@ import coil3.size.Precision
import coil3.size.Scale
import coil3.transform.RoundedCornersTransformation
import eu.kanade.tachiyomi.core.security.SecurityPreferences
import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.ui.recents.RecentsPresenter
import eu.kanade.tachiyomi.util.system.dpToPx
import eu.kanade.tachiyomi.util.system.launchIO
import kotlinx.coroutines.MainScope
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import yokai.domain.manga.models.Manga
import yokai.presentation.widget.components.CoverHeight
import yokai.presentation.widget.components.CoverWidth
import yokai.presentation.widget.components.LockedWidget