From f264a696265937b811a52aba23cbe30155d4642e Mon Sep 17 00:00:00 2001 From: Jays2Kings Date: Tue, 6 Apr 2021 21:51:50 -0400 Subject: [PATCH] Refactoring RecentMangaAdapter + removing recents header while searching --- .../ui/recents/RecentMangaAdapter.kt | 26 +++++++------------ .../tachiyomi/ui/recents/RecentsController.kt | 8 +++--- 2 files changed, 12 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/recents/RecentMangaAdapter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/recents/RecentMangaAdapter.kt index 35d0943d0b..0e00c09b15 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/recents/RecentMangaAdapter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/recents/RecentMangaAdapter.kt @@ -1,6 +1,7 @@ package eu.kanade.tachiyomi.ui.recents import androidx.recyclerview.widget.ItemTouchHelper +import com.tfcporciuncula.flow.Preference import eu.davidea.flexibleadapter.items.IFlexible import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.ui.manga.chapter.BaseChapterAdapter @@ -32,24 +33,15 @@ class RecentMangaAdapter(val delegate: RecentsInterface) : init { setDisplayHeadersAtStartUp(true) - preferences.showRecentsDownloads() - .asFlow() + preferences.showRecentsDownloads().register { showDownloads = it } + preferences.showRecentsRemHistory().register { showRemoveHistory = it } + preferences.showTitleFirstInRecents().register { showTitleFirst = it } + } + + private fun Preference.register(onChanged: (T) -> Unit) { + asFlow() .onEach { - showDownloads = it - notifyDataSetChanged() - } - .launchIn(delegate.scope()) - preferences.showRecentsRemHistory() - .asFlow() - .onEach { - showRemoveHistory = it - notifyDataSetChanged() - } - .launchIn(delegate.scope()) - preferences.showTitleFirstInRecents() - .asFlow() - .onEach { - showTitleFirst = it + onChanged(it) notifyDataSetChanged() } .launchIn(delegate.scope()) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/recents/RecentsController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/recents/RecentsController.kt index 9b58ca96ce..b15c6e459c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/recents/RecentsController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/recents/RecentsController.kt @@ -149,9 +149,7 @@ class RecentsController(bundle: Bundle? = null) : presenter.onCreate() if (presenter.recentItems.isNotEmpty()) { adapter.updateDataSet(presenter.recentItems) - if (presenter.viewType > 0) { - adapter.addScrollableHeader(presenter.generalHeader) - } + adapter.addScrollableHeader(presenter.generalHeader) } binding.downloadBottomSheet.dlBottomSheet.onCreate(this) @@ -305,11 +303,11 @@ class RecentsController(bundle: Bundle? = null) : binding.swipeRefresh.isRefreshing = LibraryUpdateService.isRunning() adapter.updateItems(recents) adapter.headerItems.forEach { - if (it != presenter.generalHeader) { + if (it != presenter.generalHeader || presenter.query.isNotEmpty()) { adapter.removeScrollableHeader(it) } } - if (!adapter.headerItems.any { it === presenter.generalHeader }) { + if (presenter.query.isEmpty() && !adapter.headerItems.any { it === presenter.generalHeader }) { adapter.addScrollableHeader(presenter.generalHeader) } adapter.onLoadMoreComplete(null)