From 8e32b6b74e64e257167ff09b2500a3f95c14da77 Mon Sep 17 00:00:00 2001 From: Ahmad Ansori Palembani Date: Tue, 18 Jun 2024 11:14:56 +0700 Subject: [PATCH] refactor(library): Use flow to update libraryManga --- .../tachiyomi/ui/library/LibraryController.kt | 18 ++++---- .../tachiyomi/ui/library/LibraryPresenter.kt | 41 +++++++++++-------- .../ui/library/display/LibraryCategoryView.kt | 4 +- .../widget/UpdatesGridGlanceWidget.kt | 3 +- 4 files changed, 37 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt index 7d5ece7a90..6b9adcac77 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt @@ -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() } } 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 c72bccf3ec..eb10f9164a 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 @@ -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 = emptyList() + private var libraryManga: List = 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> { 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() 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() } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/display/LibraryCategoryView.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/display/LibraryCategoryView.kt index 953aadf25b..fcc9e29d28 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/display/LibraryCategoryView.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/display/LibraryCategoryView.kt @@ -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() diff --git a/presentation/widget/src/main/java/yokai/presentation/widget/UpdatesGridGlanceWidget.kt b/presentation/widget/src/main/java/yokai/presentation/widget/UpdatesGridGlanceWidget.kt index fedd1272c5..317899e9ad 100644 --- a/presentation/widget/src/main/java/yokai/presentation/widget/UpdatesGridGlanceWidget.kt +++ b/presentation/widget/src/main/java/yokai/presentation/widget/UpdatesGridGlanceWidget.kt @@ -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