From 5cddc0c3875fffa205bd7fbc7f881cc60871b5be Mon Sep 17 00:00:00 2001 From: Andreas Date: Sun, 17 Jan 2021 21:40:17 +0100 Subject: [PATCH] Add filter for tracking (#4276) * Add filter for tracking or not * Use .any * Access database only when needed --- .../data/database/queries/TrackQueries.kt | 9 ++++ .../data/preference/PreferenceKeys.kt | 2 + .../data/preference/PreferencesHelper.kt | 2 + .../tachiyomi/ui/library/LibraryPresenter.kt | 45 ++++++++++++++++--- .../ui/library/LibrarySettingsSheet.kt | 5 ++- app/src/main/res/values/strings.xml | 1 + 6 files changed, 58 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/TrackQueries.kt b/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/TrackQueries.kt index 7311fe40d..1947a0218 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/TrackQueries.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/TrackQueries.kt @@ -10,6 +10,15 @@ import eu.kanade.tachiyomi.data.track.TrackService interface TrackQueries : DbProvider { + fun getTracks() = db.get() + .listOfObjects(Track::class.java) + .withQuery( + Query.builder() + .table(TrackTable.TABLE) + .build() + ) + .prepare() + fun getTracks(manga: Manga) = db.get() .listOfObjects(Track::class.java) .withQuery( diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt index c2b93868b..7169d5e23 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt @@ -123,6 +123,8 @@ object PreferenceKeys { const val filterCompleted = "pref_filter_library_completed" + const val filterTracking = "pref_filter_library_tracking" + const val librarySortingMode = "library_sorting_mode" const val automaticExtUpdates = "automatic_ext_updates" diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt index b24ded3bb..c4ca0177a 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt @@ -221,6 +221,8 @@ class PreferencesHelper(val context: Context) { fun filterCompleted() = flowPrefs.getInt(Keys.filterCompleted, ExtendedNavigationView.Item.TriStateGroup.State.IGNORE.value) + fun filterTracking() = flowPrefs.getInt(Keys.filterTracking, ExtendedNavigationView.Item.TriStateGroup.State.IGNORE.value) + fun librarySortingMode() = flowPrefs.getInt(Keys.librarySortingMode, 0) fun librarySortingAscending() = flowPrefs.getBoolean("library_sorting_ascending", true) 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 be0be7ec5..71882b27b 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 @@ -10,6 +10,7 @@ import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.database.models.MangaCategory import eu.kanade.tachiyomi.data.download.DownloadManager import eu.kanade.tachiyomi.data.preference.PreferencesHelper +import eu.kanade.tachiyomi.data.track.TrackManager import eu.kanade.tachiyomi.source.SourceManager import eu.kanade.tachiyomi.source.model.SManga import eu.kanade.tachiyomi.source.online.HttpSource @@ -47,7 +48,8 @@ class LibraryPresenter( private val preferences: PreferencesHelper = Injekt.get(), private val coverCache: CoverCache = Injekt.get(), private val sourceManager: SourceManager = Injekt.get(), - private val downloadManager: DownloadManager = Injekt.get() + private val downloadManager: DownloadManager = Injekt.get(), + private val trackManager: TrackManager = Injekt.get() ) : BasePresenter() { private val context = preferences.context @@ -92,8 +94,8 @@ class LibraryPresenter( .combineLatest(badgeTriggerRelay.observeOn(Schedulers.io())) { lib, _ -> lib.apply { setBadges(mangaMap) } } - .combineLatest(filterTriggerRelay.observeOn(Schedulers.io())) { lib, _ -> - lib.copy(mangaMap = applyFilters(lib.mangaMap)) + .combineLatest(getFilterObservable()) { lib, tracks -> + lib.copy(mangaMap = applyFilters(lib.mangaMap, tracks)) } .combineLatest(sortTriggerRelay.observeOn(Schedulers.io())) { lib, _ -> lib.copy(mangaMap = applySort(lib.mangaMap)) @@ -110,11 +112,12 @@ class LibraryPresenter( * * @param map the map to filter. */ - private fun applyFilters(map: LibraryMap): LibraryMap { + private fun applyFilters(map: LibraryMap, trackMap: Map): LibraryMap { val downloadedOnly = preferences.downloadedOnly().get() val filterDownloaded = preferences.filterDownloaded().get() val filterUnread = preferences.filterUnread().get() val filterCompleted = preferences.filterCompleted().get() + val tracking = preferences.filterTracking().get() val filterFnUnread: (LibraryItem) -> Boolean = unread@{ item -> if (filterUnread == State.IGNORE.value) return@unread true @@ -144,11 +147,20 @@ class LibraryPresenter( else !isDownloaded } + val filterFnTracking: (LibraryItem) -> Boolean = tracking@{ item -> + if (tracking == State.IGNORE.value) return@tracking true + + val isTracking = trackMap[item.manga.id ?: -1] ?: false + + return@tracking if (tracking == State.INCLUDE.value) isTracking else !isTracking + } + val filterFn: (LibraryItem) -> Boolean = filter@{ item -> return@filter !( !filterFnUnread(item) || !filterFnCompleted(item) || - !filterFnDownloaded(item) + !filterFnDownloaded(item) || + !filterFnTracking(item) ) } @@ -282,6 +294,29 @@ class LibraryPresenter( } } + /** + * Get the tracked manga from the database and checks if the filter gets changed + * + * @return an observable of tracked manga. + */ + private fun getFilterObservable(): Observable> { + return getTracksObservable().combineLatest(filterTriggerRelay.observeOn(Schedulers.io())) { tracks, _ -> tracks } + } + + /** + * Get the tracked manga from the database + * + * @return an observable of tracked manga. + */ + private fun getTracksObservable(): Observable> { + return db.getTracks().asRxObservable().map { tracks -> + tracks.associate { track -> + val isLogged = tracks.any { trackManager.getService(it.sync_id)?.isLogged ?: false } + Pair(track.manga_id, isLogged) + } + }.observeOn(Schedulers.io()) + } + /** * Requests the library to be filtered. */ diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibrarySettingsSheet.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibrarySettingsSheet.kt index a9d92e571..23642c5cd 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibrarySettingsSheet.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibrarySettingsSheet.kt @@ -68,9 +68,10 @@ class LibrarySettingsSheet( private val downloaded = Item.TriStateGroup(R.string.action_filter_downloaded, this) private val unread = Item.TriStateGroup(R.string.action_filter_unread, this) private val completed = Item.TriStateGroup(R.string.completed, this) + private val tracking = Item.TriStateGroup(R.string.action_filter_tracking, this) override val header = null - override val items = listOf(downloaded, unread, completed) + override val items = listOf(downloaded, unread, completed, tracking) override val footer = null override fun initModels() { @@ -82,6 +83,7 @@ class LibrarySettingsSheet( } unread.state = preferences.filterUnread().get() completed.state = preferences.filterCompleted().get() + tracking.state = preferences.filterTracking().get() } override fun onItemClicked(item: Item) { @@ -97,6 +99,7 @@ class LibrarySettingsSheet( downloaded -> preferences.filterDownloaded().set(newState) unread -> preferences.filterUnread().set(newState) completed -> preferences.filterCompleted().set(newState) + tracking -> preferences.filterTracking().set(newState) } adapter.notifyItemChanged(item) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 599ee6098..25742976d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -34,6 +34,7 @@ Filter Downloaded Bookmarked + Tracking Unread Remove filter Alphabetically