Add filter for tracking (#4276)

* Add filter for tracking or not

* Use .any

* Access database only when needed
This commit is contained in:
Andreas 2021-01-17 21:40:17 +01:00 committed by GitHub
parent cbc01dd6f1
commit 5cddc0c387
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 58 additions and 6 deletions

View File

@ -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(

View File

@ -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"

View File

@ -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)

View File

@ -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<LibraryController>() {
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<Long, Boolean>): 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<Map<Long, Boolean>> {
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<Map<Long, Boolean>> {
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.
*/

View File

@ -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)

View File

@ -34,6 +34,7 @@
<string name="action_filter">Filter</string>
<string name="action_filter_downloaded">Downloaded</string>
<string name="action_filter_bookmarked">Bookmarked</string>
<string name="action_filter_tracking">Tracking</string>
<string name="action_filter_unread">Unread</string>
<string name="action_filter_empty">Remove filter</string>
<string name="action_sort_alpha">Alphabetically</string>