From 30b4c6e7556b4035fad9cc5f1885eaaada3704e4 Mon Sep 17 00:00:00 2001 From: len Date: Sun, 4 Dec 2016 23:58:46 +0100 Subject: [PATCH] Remove some state from the library view --- .../data/preference/PreferencesHelper.kt | 2 + .../tachiyomi/ui/library/LibraryFragment.kt | 49 +++++-------------- .../tachiyomi/ui/library/LibraryPresenter.kt | 3 +- 3 files changed, 15 insertions(+), 39 deletions(-) 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 48b8664645..5301ae60e2 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 @@ -13,6 +13,8 @@ import java.io.File fun Preference.getOrDefault(): T = get() ?: defaultValue()!! +fun Preference.invert(): Boolean = getOrDefault().let { set(!it); !it } + class PreferencesHelper(context: Context) { val keys = PreferenceKeys(context) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryFragment.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryFragment.kt index 146fe47e86..c531daa6fa 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryFragment.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryFragment.kt @@ -18,6 +18,7 @@ import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.library.LibraryUpdateService import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.getOrDefault +import eu.kanade.tachiyomi.data.preference.invert import eu.kanade.tachiyomi.ui.base.fragment.BaseRxFragment import eu.kanade.tachiyomi.ui.category.CategoryActivity import eu.kanade.tachiyomi.ui.main.MainActivity @@ -74,21 +75,6 @@ class LibraryFragment : BaseRxFragment(), ActionMode.Callback */ private var selectedCoverManga: Manga? = null - /** - * Status of isFilterDownloaded - */ - var isFilterDownloaded = false - - /** - * Status of isFilterUnread - */ - var isFilterUnread = false - - /** - * Sorting mode for library - */ - var sortingMode = 0 - /** * Number of manga per row in grid mode. */ @@ -129,9 +115,6 @@ class LibraryFragment : BaseRxFragment(), ActionMode.Callback override fun onCreate(savedState: Bundle?) { super.onCreate(savedState) setHasOptionsMenu(true) - isFilterDownloaded = preferences.filterDownloaded().getOrDefault() - isFilterUnread = preferences.filterUnread().getOrDefault() - sortingMode = preferences.librarySortingMode().getOrDefault() } override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedState: Bundle?): View? { @@ -203,11 +186,11 @@ class LibraryFragment : BaseRxFragment(), ActionMode.Callback val sortModeLastUpdated = menu.findItem(R.id.action_sort_last_updated) // Set correct checkbox filter - filterDownloadedItem.isChecked = isFilterDownloaded - filterUnreadItem.isChecked = isFilterUnread + filterDownloadedItem.isChecked = preferences.filterDownloaded().getOrDefault() + filterUnreadItem.isChecked = preferences.filterUnread().getOrDefault() // Set correct radio button sort - when (sortingMode) { + when (preferences.librarySortingMode().getOrDefault()) { Constants.SORT_LIBRARY_ALPHA -> sortModeAlpha.isChecked = true Constants.SORT_LIBRARY_LAST_READ -> sortModeLastRead.isChecked = true Constants.SORT_LIBRARY_LAST_UPDATED -> sortModeLastUpdated.isChecked = true @@ -243,44 +226,34 @@ class LibraryFragment : BaseRxFragment(), ActionMode.Callback override fun onOptionsItemSelected(item: MenuItem): Boolean { when (item.itemId) { R.id.action_filter_unread -> { - // Change unread filter status. - isFilterUnread = !isFilterUnread // Update settings. - preferences.filterUnread().set(isFilterUnread) + preferences.filterUnread().invert() // Apply filter. onFilterOrSortChanged() } R.id.action_filter_downloaded -> { - // Change downloaded filter status. - isFilterDownloaded = !isFilterDownloaded // Update settings. - preferences.filterDownloaded().set(isFilterDownloaded) + preferences.filterDownloaded().invert() // Apply filter. onFilterOrSortChanged() } R.id.action_filter_empty -> { - // Remove filter status. - isFilterUnread = false - isFilterDownloaded = false // Update settings. - preferences.filterUnread().set(isFilterUnread) - preferences.filterDownloaded().set(isFilterDownloaded) + preferences.filterUnread().set(false) + preferences.filterDownloaded().set(false) // Apply filter onFilterOrSortChanged() } R.id.action_sort_alpha -> { - sortingMode = Constants.SORT_LIBRARY_ALPHA - preferences.librarySortingMode().set(sortingMode) + preferences.librarySortingMode().set(Constants.SORT_LIBRARY_ALPHA) onFilterOrSortChanged() } R.id.action_sort_last_read -> { - sortingMode = Constants.SORT_LIBRARY_LAST_READ - preferences.librarySortingMode().set(sortingMode) + preferences.librarySortingMode().set(Constants.SORT_LIBRARY_LAST_READ) onFilterOrSortChanged() } R.id.action_sort_last_updated -> { - sortingMode = Constants.SORT_LIBRARY_LAST_UPDATED - preferences.librarySortingMode().set(sortingMode) + preferences.librarySortingMode().set(Constants.SORT_LIBRARY_LAST_UPDATED) onFilterOrSortChanged() } R.id.action_library_display_mode -> swapDisplayMode() 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 f1481e63f7..be253b19a1 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 @@ -105,7 +105,8 @@ class LibraryPresenter : BasePresenter() { { library, updateTrigger -> library }) .map { Pair(it.first, applyFilters(it.second)) } .observeOn(AndroidSchedulers.mainThread()) - .subscribeLatestCache({ view, pair -> view.onNextLibraryUpdate(pair.first, pair.second) }) + .subscribeLatestCache( + { view, pair -> view.onNextLibraryUpdate(pair.first, pair.second) }) } }