Use tristate checkboxes for chapters list filters

This commit is contained in:
arkon 2020-09-27 18:13:20 -04:00
parent 2eab43a669
commit fb3756420b
8 changed files with 111 additions and 84 deletions

View File

@ -98,8 +98,8 @@ object Migrations {
val prefs = PreferenceManager.getDefaultSharedPreferences(context) val prefs = PreferenceManager.getDefaultSharedPreferences(context)
fun convertBooleanPrefToTriState(key: String): Int { fun convertBooleanPrefToTriState(key: String): Int {
val oldPrefValue = prefs.getBoolean(key, false) val oldPrefValue = prefs.getBoolean(key, false)
return if (oldPrefValue) ExtendedNavigationView.Item.TriStateGroup.STATE_INCLUDE return if (oldPrefValue) ExtendedNavigationView.Item.TriStateGroup.State.INCLUDE.value
else ExtendedNavigationView.Item.TriStateGroup.STATE_IGNORE else ExtendedNavigationView.Item.TriStateGroup.State.IGNORE.value
} }
prefs.edit { prefs.edit {
putInt(PreferenceKeys.filterDownloaded, convertBooleanPrefToTriState("pref_filter_downloaded_key")) putInt(PreferenceKeys.filterDownloaded, convertBooleanPrefToTriState("pref_filter_downloaded_key"))

View File

@ -213,11 +213,11 @@ class PreferencesHelper(val context: Context) {
fun categoryTabs() = flowPrefs.getBoolean(Keys.categoryTabs, true) fun categoryTabs() = flowPrefs.getBoolean(Keys.categoryTabs, true)
fun filterDownloaded() = flowPrefs.getInt(Keys.filterDownloaded, ExtendedNavigationView.Item.TriStateGroup.STATE_IGNORE) fun filterDownloaded() = flowPrefs.getInt(Keys.filterDownloaded, ExtendedNavigationView.Item.TriStateGroup.State.IGNORE.value)
fun filterUnread() = flowPrefs.getInt(Keys.filterUnread, ExtendedNavigationView.Item.TriStateGroup.STATE_IGNORE) fun filterUnread() = flowPrefs.getInt(Keys.filterUnread, ExtendedNavigationView.Item.TriStateGroup.State.IGNORE.value)
fun filterCompleted() = flowPrefs.getInt(Keys.filterCompleted, ExtendedNavigationView.Item.TriStateGroup.STATE_IGNORE) fun filterCompleted() = flowPrefs.getInt(Keys.filterCompleted, ExtendedNavigationView.Item.TriStateGroup.State.IGNORE.value)
fun librarySortingMode() = flowPrefs.getInt(Keys.librarySortingMode, 0) fun librarySortingMode() = flowPrefs.getInt(Keys.librarySortingMode, 0)

View File

@ -19,8 +19,7 @@ import eu.kanade.tachiyomi.util.lang.combineLatest
import eu.kanade.tachiyomi.util.lang.isNullOrUnsubscribed import eu.kanade.tachiyomi.util.lang.isNullOrUnsubscribed
import eu.kanade.tachiyomi.util.lang.launchIO import eu.kanade.tachiyomi.util.lang.launchIO
import eu.kanade.tachiyomi.util.removeCovers import eu.kanade.tachiyomi.util.removeCovers
import eu.kanade.tachiyomi.widget.ExtendedNavigationView.Item.TriStateGroup.Companion.STATE_IGNORE import eu.kanade.tachiyomi.widget.ExtendedNavigationView.Item.TriStateGroup.State
import eu.kanade.tachiyomi.widget.ExtendedNavigationView.Item.TriStateGroup.Companion.STATE_INCLUDE
import rx.Observable import rx.Observable
import rx.Subscription import rx.Subscription
import rx.android.schedulers.AndroidSchedulers import rx.android.schedulers.AndroidSchedulers
@ -118,30 +117,30 @@ class LibraryPresenter(
val filterCompleted = preferences.filterCompleted().get() val filterCompleted = preferences.filterCompleted().get()
val filterFnUnread: (LibraryItem) -> Boolean = unread@{ item -> val filterFnUnread: (LibraryItem) -> Boolean = unread@{ item ->
if (filterUnread == STATE_IGNORE) return@unread true if (filterUnread == State.IGNORE.value) return@unread true
val isUnread = item.manga.unread != 0 val isUnread = item.manga.unread != 0
return@unread if (filterUnread == STATE_INCLUDE) isUnread return@unread if (filterUnread == State.INCLUDE.value) isUnread
else !isUnread else !isUnread
} }
val filterFnCompleted: (LibraryItem) -> Boolean = completed@{ item -> val filterFnCompleted: (LibraryItem) -> Boolean = completed@{ item ->
if (filterCompleted == STATE_IGNORE) return@completed true if (filterCompleted == State.IGNORE.value) return@completed true
val isCompleted = item.manga.status == SManga.COMPLETED val isCompleted = item.manga.status == SManga.COMPLETED
return@completed if (filterCompleted == STATE_INCLUDE) isCompleted return@completed if (filterCompleted == State.INCLUDE.value) isCompleted
else !isCompleted else !isCompleted
} }
val filterFnDownloaded: (LibraryItem) -> Boolean = downloaded@{ item -> val filterFnDownloaded: (LibraryItem) -> Boolean = downloaded@{ item ->
if (!downloadedOnly && filterDownloaded == STATE_IGNORE) return@downloaded true if (!downloadedOnly && filterDownloaded == State.IGNORE.value) return@downloaded true
val isDownloaded = when { val isDownloaded = when {
item.manga.isLocal() -> true item.manga.isLocal() -> true
item.downloadCount != -1 -> item.downloadCount > 0 item.downloadCount != -1 -> item.downloadCount > 0
else -> downloadManager.getDownloadCount(item.manga) > 0 else -> downloadManager.getDownloadCount(item.manga) > 0
} }
return@downloaded if (downloadedOnly || filterDownloaded == STATE_INCLUDE) isDownloaded return@downloaded if (downloadedOnly || filterDownloaded == State.INCLUDE.value) isDownloaded
else !isDownloaded else !isDownloaded
} }

View File

@ -8,9 +8,7 @@ import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.preference.PreferenceValues.DisplayMode import eu.kanade.tachiyomi.data.preference.PreferenceValues.DisplayMode
import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.widget.ExtendedNavigationView import eu.kanade.tachiyomi.widget.ExtendedNavigationView
import eu.kanade.tachiyomi.widget.ExtendedNavigationView.Item.TriStateGroup.Companion.STATE_EXCLUDE import eu.kanade.tachiyomi.widget.ExtendedNavigationView.Item.TriStateGroup.State
import eu.kanade.tachiyomi.widget.ExtendedNavigationView.Item.TriStateGroup.Companion.STATE_IGNORE
import eu.kanade.tachiyomi.widget.ExtendedNavigationView.Item.TriStateGroup.Companion.STATE_INCLUDE
import eu.kanade.tachiyomi.widget.TabbedBottomSheetDialog import eu.kanade.tachiyomi.widget.TabbedBottomSheetDialog
import uy.kohesive.injekt.injectLazy import uy.kohesive.injekt.injectLazy
@ -62,7 +60,7 @@ class LibrarySettingsSheet(
* Returns true if there's at least one filter from [FilterGroup] active. * Returns true if there's at least one filter from [FilterGroup] active.
*/ */
fun hasActiveFilters(): Boolean { fun hasActiveFilters(): Boolean {
return filterGroup.items.any { it.state != STATE_IGNORE } return filterGroup.items.any { it.state != State.IGNORE.value }
} }
inner class FilterGroup : Group { inner class FilterGroup : Group {
@ -77,7 +75,7 @@ class LibrarySettingsSheet(
override fun initModels() { override fun initModels() {
if (preferences.downloadedOnly().get()) { if (preferences.downloadedOnly().get()) {
downloaded.state = STATE_INCLUDE downloaded.state = State.INCLUDE.value
downloaded.enabled = false downloaded.enabled = false
} else { } else {
downloaded.state = preferences.filterDownloaded().get() downloaded.state = preferences.filterDownloaded().get()
@ -89,9 +87,9 @@ class LibrarySettingsSheet(
override fun onItemClicked(item: Item) { override fun onItemClicked(item: Item) {
item as Item.TriStateGroup item as Item.TriStateGroup
val newState = when (item.state) { val newState = when (item.state) {
STATE_IGNORE -> STATE_INCLUDE State.IGNORE.value -> State.INCLUDE.value
STATE_INCLUDE -> STATE_EXCLUDE State.INCLUDE.value -> State.EXCLUDE.value
STATE_EXCLUDE -> STATE_IGNORE State.EXCLUDE.value -> State.IGNORE.value
else -> throw Exception("Unknown State") else -> throw Exception("Unknown State")
} }
item.state = newState item.state = newState

View File

@ -27,6 +27,7 @@ import eu.kanade.tachiyomi.util.prepUpdateCover
import eu.kanade.tachiyomi.util.removeCovers import eu.kanade.tachiyomi.util.removeCovers
import eu.kanade.tachiyomi.util.shouldDownloadNewChapters import eu.kanade.tachiyomi.util.shouldDownloadNewChapters
import eu.kanade.tachiyomi.util.updateCoverLastModified import eu.kanade.tachiyomi.util.updateCoverLastModified
import eu.kanade.tachiyomi.widget.ExtendedNavigationView.Item.TriStateGroup.State
import rx.Observable import rx.Observable
import rx.Subscription import rx.Subscription
import rx.android.schedulers.AndroidSchedulers import rx.android.schedulers.AndroidSchedulers
@ -368,17 +369,28 @@ class MangaPresenter(
*/ */
private fun applyChapterFilters(chapters: List<ChapterItem>): Observable<List<ChapterItem>> { private fun applyChapterFilters(chapters: List<ChapterItem>): Observable<List<ChapterItem>> {
var observable = Observable.from(chapters).subscribeOn(Schedulers.io()) var observable = Observable.from(chapters).subscribeOn(Schedulers.io())
if (onlyUnread()) {
val unreadFilter = onlyUnread()
if (unreadFilter == State.INCLUDE) {
observable = observable.filter { !it.read } observable = observable.filter { !it.read }
} else if (onlyRead()) { } else if (unreadFilter == State.EXCLUDE) {
observable = observable.filter { it.read } observable = observable.filter { it.read }
} }
if (onlyDownloaded()) {
val downloadedFilter = onlyDownloaded()
if (downloadedFilter == State.INCLUDE) {
observable = observable.filter { it.isDownloaded || it.manga.isLocal() } observable = observable.filter { it.isDownloaded || it.manga.isLocal() }
} else if (downloadedFilter == State.EXCLUDE) {
observable = observable.filter { !it.isDownloaded && !it.manga.isLocal() }
} }
if (onlyBookmarked()) {
val bookmarkedFilter = onlyBookmarked()
if (bookmarkedFilter == State.INCLUDE) {
observable = observable.filter { it.bookmark } observable = observable.filter { it.bookmark }
} else if (bookmarkedFilter == State.EXCLUDE) {
observable = observable.filter { !it.bookmark }
} }
val sortFunction: (Chapter, Chapter) -> Int = when (manga.sorting) { val sortFunction: (Chapter, Chapter) -> Int = when (manga.sorting) {
Manga.SORTING_SOURCE -> when (sortDescending()) { Manga.SORTING_SOURCE -> when (sortDescending()) {
true -> { c1, c2 -> c1.source_order.compareTo(c2.source_order) } true -> { c1, c2 -> c1.source_order.compareTo(c2.source_order) }
@ -394,6 +406,7 @@ class MangaPresenter(
} }
else -> throw NotImplementedError("Unimplemented sorting method") else -> throw NotImplementedError("Unimplemented sorting method")
} }
return observable.toSortedList(sortFunction) return observable.toSortedList(sortFunction)
} }
@ -412,7 +425,7 @@ class MangaPresenter(
} }
// Force UI update if downloaded filter active and download finished. // Force UI update if downloaded filter active and download finished.
if (onlyDownloaded() && download.status == Download.DOWNLOADED) { if (onlyDownloaded() != State.IGNORE && download.status == Download.DOWNLOADED) {
refreshChapters() refreshChapters()
} }
} }
@ -477,7 +490,7 @@ class MangaPresenter(
fun deleteChapters(chapters: List<ChapterItem>) { fun deleteChapters(chapters: List<ChapterItem>) {
Observable.just(chapters) Observable.just(chapters)
.doOnNext { deleteChaptersInternal(chapters) } .doOnNext { deleteChaptersInternal(chapters) }
.doOnNext { if (onlyDownloaded()) refreshChapters() } .doOnNext { if (onlyDownloaded() != State.IGNORE) refreshChapters() }
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribeFirst( .subscribeFirst(
@ -518,40 +531,42 @@ class MangaPresenter(
/** /**
* Sets the read filter and requests an UI update. * Sets the read filter and requests an UI update.
* @param onlyUnread whether to display only unread chapters or all chapters. * @param state whether to display only unread chapters or all chapters.
*/ */
fun setUnreadFilter(onlyUnread: Boolean) { fun setUnreadFilter(state: State) {
manga.readFilter = if (onlyUnread) Manga.SHOW_UNREAD else Manga.SHOW_ALL manga.readFilter = when (state) {
db.updateFlags(manga).executeAsBlocking() State.IGNORE -> Manga.SHOW_ALL
refreshChapters() State.INCLUDE -> Manga.SHOW_UNREAD
} State.EXCLUDE -> Manga.SHOW_READ
}
/**
* Sets the read filter and requests an UI update.
* @param onlyRead whether to display only read chapters or all chapters.
*/
fun setReadFilter(onlyRead: Boolean) {
manga.readFilter = if (onlyRead) Manga.SHOW_READ else Manga.SHOW_ALL
db.updateFlags(manga).executeAsBlocking() db.updateFlags(manga).executeAsBlocking()
refreshChapters() refreshChapters()
} }
/** /**
* Sets the download filter and requests an UI update. * Sets the download filter and requests an UI update.
* @param onlyDownloaded whether to display only downloaded chapters or all chapters. * @param state whether to display only downloaded chapters or all chapters.
*/ */
fun setDownloadedFilter(onlyDownloaded: Boolean) { fun setDownloadedFilter(state: State) {
manga.downloadedFilter = if (onlyDownloaded) Manga.SHOW_DOWNLOADED else Manga.SHOW_ALL manga.downloadedFilter = when (state) {
State.IGNORE -> Manga.SHOW_ALL
State.INCLUDE -> Manga.SHOW_DOWNLOADED
State.EXCLUDE -> Manga.SHOW_NOT_DOWNLOADED
}
db.updateFlags(manga).executeAsBlocking() db.updateFlags(manga).executeAsBlocking()
refreshChapters() refreshChapters()
} }
/** /**
* Sets the bookmark filter and requests an UI update. * Sets the bookmark filter and requests an UI update.
* @param onlyBookmarked whether to display only bookmarked chapters or all chapters. * @param state whether to display only bookmarked chapters or all chapters.
*/ */
fun setBookmarkedFilter(onlyBookmarked: Boolean) { fun setBookmarkedFilter(state: State) {
manga.bookmarkedFilter = if (onlyBookmarked) Manga.SHOW_BOOKMARKED else Manga.SHOW_ALL manga.bookmarkedFilter = when (state) {
State.IGNORE -> Manga.SHOW_ALL
State.INCLUDE -> Manga.SHOW_BOOKMARKED
State.EXCLUDE -> Manga.SHOW_NOT_BOOKMARKED
}
db.updateFlags(manga).executeAsBlocking() db.updateFlags(manga).executeAsBlocking()
refreshChapters() refreshChapters()
} }
@ -585,29 +600,37 @@ class MangaPresenter(
/** /**
* Whether the display only downloaded filter is enabled. * Whether the display only downloaded filter is enabled.
*/ */
fun onlyDownloaded(): Boolean { fun onlyDownloaded(): State {
return forceDownloaded() || manga.downloadedFilter == Manga.SHOW_DOWNLOADED if (forceDownloaded()) {
return State.INCLUDE
}
return when (manga.downloadedFilter) {
Manga.SHOW_DOWNLOADED -> State.INCLUDE
Manga.SHOW_NOT_DOWNLOADED -> State.EXCLUDE
else -> State.IGNORE
}
} }
/** /**
* Whether the display only downloaded filter is enabled. * Whether the display only downloaded filter is enabled.
*/ */
fun onlyBookmarked(): Boolean { fun onlyBookmarked(): State {
return manga.bookmarkedFilter == Manga.SHOW_BOOKMARKED return when (manga.bookmarkedFilter) {
Manga.SHOW_BOOKMARKED -> State.INCLUDE
Manga.SHOW_NOT_BOOKMARKED -> State.EXCLUDE
else -> State.IGNORE
}
} }
/** /**
* Whether the display only unread filter is enabled. * Whether the display only unread filter is enabled.
*/ */
fun onlyUnread(): Boolean { fun onlyUnread(): State {
return manga.readFilter == Manga.SHOW_UNREAD return when (manga.readFilter) {
} Manga.SHOW_UNREAD -> State.INCLUDE
Manga.SHOW_READ -> State.EXCLUDE
/** else -> State.IGNORE
* Whether the display only read filter is enabled. }
*/
fun onlyRead(): Boolean {
return manga.readFilter == Manga.SHOW_READ
} }
/** /**

View File

@ -10,6 +10,7 @@ import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.ui.manga.MangaPresenter import eu.kanade.tachiyomi.ui.manga.MangaPresenter
import eu.kanade.tachiyomi.util.view.popupMenu import eu.kanade.tachiyomi.util.view.popupMenu
import eu.kanade.tachiyomi.widget.ExtendedNavigationView import eu.kanade.tachiyomi.widget.ExtendedNavigationView
import eu.kanade.tachiyomi.widget.ExtendedNavigationView.Item.TriStateGroup.State
import eu.kanade.tachiyomi.widget.TabbedBottomSheetDialog import eu.kanade.tachiyomi.widget.TabbedBottomSheetDialog
class ChaptersSettingsSheet( class ChaptersSettingsSheet(
@ -81,36 +82,43 @@ class ChaptersSettingsSheet(
* Returns true if there's at least one filter from [FilterGroup] active. * Returns true if there's at least one filter from [FilterGroup] active.
*/ */
fun hasActiveFilters(): Boolean { fun hasActiveFilters(): Boolean {
return filterGroup.items.any { it.checked } return filterGroup.items.any { it.state != State.IGNORE.value }
} }
inner class FilterGroup : Group { inner class FilterGroup : Group {
private val read = Item.CheckboxGroup(R.string.action_filter_read, this) private val unread = Item.TriStateGroup(R.string.action_filter_unread, this)
private val unread = Item.CheckboxGroup(R.string.action_filter_unread, this) private val downloaded = Item.TriStateGroup(R.string.action_filter_downloaded, this)
private val downloaded = Item.CheckboxGroup(R.string.action_filter_downloaded, this) private val bookmarked = Item.TriStateGroup(R.string.action_filter_bookmarked, this)
private val bookmarked = Item.CheckboxGroup(R.string.action_filter_bookmarked, this)
override val header = null override val header = null
override val items = listOf(read, unread, downloaded, bookmarked) override val items = listOf(unread, downloaded, bookmarked)
override val footer = null override val footer = null
override fun initModels() { override fun initModels() {
read.checked = presenter.onlyRead() if (presenter.forceDownloaded()) {
unread.checked = presenter.onlyUnread() downloaded.state = State.INCLUDE.value
downloaded.checked = presenter.onlyDownloaded() downloaded.enabled = false
downloaded.enabled = !presenter.forceDownloaded() } else {
bookmarked.checked = presenter.onlyBookmarked() downloaded.state = presenter.onlyDownloaded().value
}
unread.state = presenter.onlyUnread().value
bookmarked.state = presenter.onlyBookmarked().value
} }
override fun onItemClicked(item: Item) { override fun onItemClicked(item: Item) {
item as Item.CheckboxGroup item as Item.TriStateGroup
item.checked = !item.checked val newState = when (item.state) {
State.IGNORE.value -> State.INCLUDE
State.INCLUDE.value -> State.EXCLUDE
State.EXCLUDE.value -> State.IGNORE
else -> throw Exception("Unknown State")
}
item.state = newState.value
when (item) { when (item) {
read -> presenter.setReadFilter(item.checked) downloaded -> presenter.setDownloadedFilter(newState)
unread -> presenter.setUnreadFilter(item.checked) unread -> presenter.setUnreadFilter(newState)
downloaded -> presenter.setDownloadedFilter(item.checked) bookmarked -> presenter.setBookmarkedFilter(newState)
bookmarked -> presenter.setBookmarkedFilter(item.checked)
} }
initModels() initModels()

View File

@ -112,17 +112,17 @@ open class ExtendedNavigationView @JvmOverloads constructor(
*/ */
class TriStateGroup(resId: Int, group: Group) : MultiStateGroup(resId, group) { class TriStateGroup(resId: Int, group: Group) : MultiStateGroup(resId, group) {
companion object { enum class State(val value: Int) {
const val STATE_IGNORE = 0 IGNORE(0),
const val STATE_INCLUDE = 1 INCLUDE(1),
const val STATE_EXCLUDE = 2 EXCLUDE(2)
} }
override fun getStateDrawable(context: Context): Drawable? { override fun getStateDrawable(context: Context): Drawable? {
return when (state) { return when (state) {
STATE_IGNORE -> tintVector(context, R.drawable.ic_check_box_outline_blank_24dp, R.attr.colorControlNormal) State.IGNORE.value -> tintVector(context, R.drawable.ic_check_box_outline_blank_24dp, R.attr.colorControlNormal)
STATE_INCLUDE -> tintVector(context, R.drawable.ic_check_box_24dp) State.INCLUDE.value -> tintVector(context, R.drawable.ic_check_box_24dp)
STATE_EXCLUDE -> tintVector(context, R.drawable.ic_check_box_x_24dp) State.EXCLUDE.value -> tintVector(context, R.drawable.ic_check_box_x_24dp)
else -> throw Exception("Unknown state") else -> throw Exception("Unknown state")
} }
} }

View File

@ -35,7 +35,6 @@
<string name="action_filter_downloaded">Downloaded</string> <string name="action_filter_downloaded">Downloaded</string>
<string name="action_filter_bookmarked">Bookmarked</string> <string name="action_filter_bookmarked">Bookmarked</string>
<string name="action_filter_unread">Unread</string> <string name="action_filter_unread">Unread</string>
<string name="action_filter_read">Read</string>
<string name="action_filter_empty">Remove filter</string> <string name="action_filter_empty">Remove filter</string>
<string name="action_sort_alpha">Alphabetically</string> <string name="action_sort_alpha">Alphabetically</string>
<string name="action_sort_total">Total chapters</string> <string name="action_sort_total">Total chapters</string>