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 presenter.groupType = item
shouldScrollToTop = true shouldScrollToTop = true
presenter.getLibrary() presenter.updateLibrary()
true true
}.show() }.show()
} }
@ -1033,7 +1033,7 @@ open class LibraryController(
if (type.isEnter) { if (type.isEnter) {
binding.filterBottomSheet.filterBottomSheet.isVisible = true binding.filterBottomSheet.filterBottomSheet.isVisible = true
if (type == ControllerChangeType.POP_ENTER) { if (type == ControllerChangeType.POP_ENTER) {
presenter.getLibrary() presenter.updateLibrary()
isPoppingIn = true isPoppingIn = true
} }
DownloadJob.callListeners() DownloadJob.callListeners()
@ -1073,7 +1073,7 @@ open class LibraryController(
if (!isBindingInitialized) return if (!isBindingInitialized) return
updateFilterSheetY() updateFilterSheetY()
if (observeLater) { if (observeLater) {
presenter.getLibrary() presenter.updateLibrary()
} }
} }
@ -1384,7 +1384,7 @@ open class LibraryController(
private fun onRefresh() { private fun onRefresh() {
showCategories(false) showCategories(false)
presenter.getLibrary() presenter.updateLibrary()
destroyActionModeIfNeeded() destroyActionModeIfNeeded()
} }
@ -1408,14 +1408,14 @@ open class LibraryController(
val isShowAllCategoriesSet = preferences.showAllCategories().get() val isShowAllCategoriesSet = preferences.showAllCategories().get()
if (!query.isNullOrBlank() && this.query.isBlank() && !isShowAllCategoriesSet) { if (!query.isNullOrBlank() && this.query.isBlank() && !isShowAllCategoriesSet) {
presenter.forceShowAllCategories = preferences.showAllCategoriesWhenSearchingSingleCategory().get() presenter.forceShowAllCategories = preferences.showAllCategoriesWhenSearchingSingleCategory().get()
presenter.getLibrary() presenter.updateLibrary()
} else if (query.isNullOrBlank() && this.query.isNotBlank() && !isShowAllCategoriesSet) { } else if (query.isNullOrBlank() && this.query.isNotBlank() && !isShowAllCategoriesSet) {
if (!isSubClass) { if (!isSubClass) {
preferences.showAllCategoriesWhenSearchingSingleCategory() preferences.showAllCategoriesWhenSearchingSingleCategory()
.set(presenter.forceShowAllCategories) .set(presenter.forceShowAllCategories)
} }
presenter.forceShowAllCategories = false presenter.forceShowAllCategories = false
presenter.getLibrary() presenter.updateLibrary()
} }
if (query != this.query && !query.isNullOrBlank()) { if (query != this.query && !query.isNullOrBlank()) {
@ -1796,7 +1796,7 @@ open class LibraryController(
val category = (adapter.getItem(position) as? LibraryHeaderItem)?.category ?: return val category = (adapter.getItem(position) as? LibraryHeaderItem)?.category ?: return
if (!category.isDynamic) { if (!category.isDynamic) {
ManageCategoryDialog(category) { ManageCategoryDialog(category) {
presenter.getLibrary() presenter.updateLibrary()
}.showDialog(router) }.showDialog(router)
} }
} }
@ -1889,7 +1889,7 @@ open class LibraryController(
isGone = true isGone = true
setOnClickListener { setOnClickListener {
presenter.forceShowAllCategories = !presenter.forceShowAllCategories presenter.forceShowAllCategories = !presenter.forceShowAllCategories
presenter.getLibrary() presenter.updateLibrary()
isSelected = presenter.forceShowAllCategories isSelected = presenter.forceShowAllCategories
} }
val pad = 12.dpToPx val pad = 12.dpToPx
@ -2168,7 +2168,7 @@ open class LibraryController(
private fun showChangeMangaCategoriesSheet() { private fun showChangeMangaCategoriesSheet() {
val activity = activity ?: return val activity = activity ?: return
selectedMangas.toList().moveCategories(presenter.db, activity) { selectedMangas.toList().moveCategories(presenter.db, activity) {
presenter.getLibrary() presenter.updateLibrary()
destroyActionModeIfNeeded() 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.withIOContext
import eu.kanade.tachiyomi.util.system.withUIContext import eu.kanade.tachiyomi.util.system.withUIContext
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
@ -78,7 +79,7 @@ class LibraryPresenter(
private val getLibraryManga: GetLibraryManga by injectLazy() private val getLibraryManga: GetLibraryManga by injectLazy()
private val getChapters: GetChapters 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 context = preferences.context
private val viewContext private val viewContext
@ -164,7 +165,19 @@ class LibraryPresenter(
lastLibraryItems = null lastLibraryItems = null
lastAllLibraryItems = 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()) { if (!preferences.showLibrarySearchSuggestions().isSet()) {
DelayedLibrarySuggestionsJob.setupTask(context, true) DelayedLibrarySuggestionsJob.setupTask(context, true)
} else if (preferences.showLibrarySearchSuggestions().get() && } else if (preferences.showLibrarySearchSuggestions().get() &&
@ -187,8 +200,8 @@ class LibraryPresenter(
} }
} }
/** Get favorited manga for library and sort and filter it */ /** Update library state and sort and filter it */
fun getLibrary() { fun updateLibrary() {
if (categories.isEmpty()) { if (categories.isEmpty()) {
val dbCategories = db.getCategories().executeAsBlocking() val dbCategories = db.getCategories().executeAsBlocking()
if ((dbCategories + Category.createDefault(context)).distinctBy { it.order }.size != dbCategories.size + 1) { 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>> { private suspend fun getLibraryFromDB(): Pair<List<LibraryItem>, List<LibraryItem>> {
removeArticles = preferences.removeArticles().get() removeArticles = preferences.removeArticles().get()
val categories = db.getCategories().executeAsBlocking().toMutableList() val categories = db.getCategories().executeAsBlocking().toMutableList()
var libraryManga = getLibraryManga.await()
val showAll = showAllCategories val showAll = showAllCategories
if (groupType > BY_DEFAULT) {
libraryManga = libraryManga.distinctBy { it.id }
}
val hiddenItems = mutableListOf<LibraryItem>() val hiddenItems = mutableListOf<LibraryItem>()
val items = if (groupType <= BY_DEFAULT || !libraryIsGrouped) { val items = if (groupType <= BY_DEFAULT || !libraryIsGrouped) {
@ -1076,7 +1085,7 @@ class LibraryPresenter(
mangaToDelete.forEach { it.favorite = false } mangaToDelete.forEach { it.favorite = false }
db.insertMangas(mangaToDelete).executeOnIO() db.insertMangas(mangaToDelete).executeOnIO()
getLibrary() updateLibrary()
} }
} }
@ -1102,7 +1111,7 @@ 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() {
presenterScope.launch { presenterScope.launch {
getLibrary() updateLibrary()
} }
} }
@ -1113,7 +1122,7 @@ class LibraryPresenter(
mangaToAdd.forEach { it.favorite = true } mangaToAdd.forEach { it.favorite = true }
db.insertMangas(mangaToAdd).executeOnIO() db.insertMangas(mangaToAdd).executeOnIO()
(view as? FilteredLibraryController)?.updateStatsPage() (view as? FilteredLibraryController)?.updateStatsPage()
getLibrary() updateLibrary()
mangaToAdd.forEach { db.insertManga(it).executeAsBlocking() } mangaToAdd.forEach { db.insertManga(it).executeAsBlocking() }
} }
} }
@ -1201,7 +1210,7 @@ class LibraryPresenter(
db.insertCategory(category).executeAsBlocking() db.insertCategory(category).executeAsBlocking()
} }
} }
getLibrary() updateLibrary()
} }
} }
@ -1235,7 +1244,7 @@ class LibraryPresenter(
} }
preferences.collapsedDynamicCategories().set(categoriesHidden) preferences.collapsedDynamicCategories().set(categoriesHidden)
} }
getLibrary() updateLibrary()
} }
private fun getDynamicCategoryName(category: Category): String = private fun getDynamicCategoryName(category: Category): String =
@ -1266,7 +1275,7 @@ class LibraryPresenter(
} }
} }
} }
getLibrary() updateLibrary()
} }
fun allCategoriesExpanded(): Boolean { fun allCategoriesExpanded(): Boolean {
@ -1309,7 +1318,7 @@ class LibraryPresenter(
mapMangaChapters[manga] = oldChapters mapMangaChapters[manga] = oldChapters
} }
getLibrary() updateLibrary()
} }
return mapMangaChapters return mapMangaChapters
} }
@ -1321,7 +1330,7 @@ class LibraryPresenter(
mangaList.forEach { (_, chapters) -> mangaList.forEach { (_, chapters) ->
db.updateChaptersProgress(chapters).executeAsBlocking() db.updateChaptersProgress(chapters).executeAsBlocking()
} }
getLibrary() updateLibrary()
} }
} }

View file

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

View file

@ -25,13 +25,12 @@ import coil3.size.Precision
import coil3.size.Scale import coil3.size.Scale
import coil3.transform.RoundedCornersTransformation import coil3.transform.RoundedCornersTransformation
import eu.kanade.tachiyomi.core.security.SecurityPreferences 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.dpToPx
import eu.kanade.tachiyomi.util.system.launchIO import eu.kanade.tachiyomi.util.system.launchIO
import kotlinx.coroutines.MainScope import kotlinx.coroutines.MainScope
import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get import uy.kohesive.injekt.api.get
import yokai.domain.manga.models.Manga
import yokai.presentation.widget.components.CoverHeight import yokai.presentation.widget.components.CoverHeight
import yokai.presentation.widget.components.CoverWidth import yokai.presentation.widget.components.CoverWidth
import yokai.presentation.widget.components.LockedWidget import yokai.presentation.widget.components.LockedWidget