From 8fbef4b4bb5145cd2b1305d177869ee2408cc411 Mon Sep 17 00:00:00 2001 From: NoodleMage Date: Fri, 4 Mar 2016 18:45:58 +0100 Subject: [PATCH] Can now filter unread manga + Code opt --- .../data/preference/PreferencesHelper.kt | 4 + .../tachiyomi/ui/library/LibraryFragment.kt | 35 ++++++-- .../tachiyomi/ui/library/LibraryPresenter.kt | 89 ++++++++++++++----- app/src/main/res/values/keys.xml | 1 + app/src/main/res/values/strings.xml | 1 + 5 files changed, 99 insertions(+), 31 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 34d355f34..05d1d7abd 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 @@ -186,4 +186,8 @@ class PreferencesHelper(private val context: Context) { return rxPrefs.getBoolean(getKey(R.string.pref_filter_downloaded), false) } + fun filterUnread(): Preference { + return rxPrefs.getBoolean(getKey(R.string.pref_filter_unread), false) + } + } 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 1d02ca142..928854455 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 @@ -116,6 +116,7 @@ class LibraryFragment : BaseRxFragment(), ActionMode.Callback super.onCreate(savedInstanceState) setHasOptionsMenu(true) isFilterDownloaded = presenter.preferences.filterDownloaded().get() as Boolean + isFilterUnread = presenter.preferences.filterUnread().get() as Boolean } override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedState: Bundle?): View? { @@ -194,23 +195,30 @@ 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 - activity.supportInvalidateOptionsMenu(); - ToastUtil.showShort(context, "Filter Unread Clicked") + // Update settings. + presenter.preferences.filterUnread().set(isFilterUnread) + // Apply filter. + onFilterCheckboxChanged() } R.id.action_filter_downloaded -> { + // Change downloaded filter status. isFilterDownloaded = !isFilterDownloaded + // Update settings. presenter.preferences.filterDownloaded().set(isFilterDownloaded) - presenter.updateLibrary() - adapter.notifyDataSetChanged() - activity.supportInvalidateOptionsMenu(); - ToastUtil.showShort(context, "Filter Download Clicked") + // Apply filter. + onFilterCheckboxChanged() } R.id.action_filter_empty -> { + // Remove filter status. isFilterUnread = false isFilterDownloaded = false - activity.supportInvalidateOptionsMenu(); - ToastUtil.showShort(context, "Filter Clear Clicked") + // Update settings. + presenter.preferences.filterUnread().set(isFilterUnread) + presenter.preferences.filterDownloaded().set(isFilterDownloaded) + // Apply filter + onFilterCheckboxChanged() } R.id.action_refresh -> LibraryUpdateService.start(activity) R.id.action_edit_categories -> { @@ -223,6 +231,17 @@ class LibraryFragment : BaseRxFragment(), ActionMode.Callback return true } + /** + * Applies filter change + */ + private fun onFilterCheckboxChanged() { + presenter.updateLibrary() + adapter.notifyDataSetChanged() + adapter.refreshRegisteredAdapters() + activity.supportInvalidateOptionsMenu(); + ToastUtil.showShort(context, getString(R.string.library_filter_change)) + } + /** * Updates the query. * 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 3af88d47e..ea4633632 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 @@ -37,8 +37,6 @@ class LibraryPresenter : BasePresenter() { */ lateinit var selectedMangas: MutableList - lateinit var libraryFragment: LibraryFragment - /** * Search query of the library. */ @@ -95,7 +93,6 @@ class LibraryPresenter : BasePresenter() { override fun onTakeView(libraryFragment: LibraryFragment) { super.onTakeView(libraryFragment) - this.libraryFragment = libraryFragment if (isUnsubscribed(GET_LIBRARY)) { start(GET_LIBRARY) } @@ -112,6 +109,9 @@ class LibraryPresenter : BasePresenter() { .observeOn(AndroidSchedulers.mainThread()) } + /** + * Update the library information + */ fun updateLibrary() { start(GET_LIBRARY) } @@ -134,29 +134,72 @@ class LibraryPresenter : BasePresenter() { */ fun getLibraryMangasObservable(): Observable>> { return db.libraryMangas.asRxObservable() - .flatMap { mangas -> Observable.from(mangas) - .filter { - if (preferences.filterDownloaded().get() as Boolean) { - val downloadManager = DownloadManager(context, sourceManager, preferences) - - val chapters = getChapters(it) - - var hasDownloaded = false - chapters?.forEach { chapter -> - if (downloadManager.isChapterDownloaded(sourceManager.get(it.source), it, chapter)) { - hasDownloaded = true - } - } - hasDownloaded - } else - true - } - .groupBy { it.category } - .flatMap { group -> group.toList().map { Pair(group.key, it) } } - .toMap({ it.first }, { it.second }) + .flatMap { mangas -> + Observable.from(mangas) + .filter { + // Filter library by options + filterLibrary(it) + } + .groupBy { it.category } + .flatMap { group -> group.toList().map { Pair(group.key, it) } } + .toMap({ it.first }, { it.second }) } } + /** + * Filter library by preference + * + * @param manga from library + * @return filter status + */ + fun filterLibrary(manga: Manga): Boolean { + // Check if filter option is selected + if (preferences.filterDownloaded().get() as Boolean || preferences.filterUnread().get() as Boolean) { + + // Does it have downloaded chapters. + var hasDownloaded = false + + // Does it have unread chapters. + var hasUnread = false + + // Get chapters from database. + val chapters = getChapters(manga) + + if (preferences.filterDownloaded().get() as Boolean) { + // Get download manager. + val downloadManager = DownloadManager(context, sourceManager, preferences) + // Loop through chapters and check if library has downloaded manga + chapters?.forEach { chapter -> + if (downloadManager.isChapterDownloaded(sourceManager.get(manga.source), manga, chapter)) { + hasDownloaded = true + } + } + } + if (preferences.filterUnread().get() as Boolean) { + // Loop through chapters and check if library has unread manga + chapters?.forEach { chapter -> + if (!chapter.read) { + hasUnread = true + } + } + } + + // Return correct filter status + if (preferences.filterDownloaded().get() as Boolean && preferences.filterUnread().get() as Boolean) { + return (hasDownloaded && hasUnread) + } else { + return (hasDownloaded || hasUnread) + } + } else + return true + } + + /** + * Returns list of chapters belonging to manga + * + * @param manga manga from library + * @return list of chapters belonging to manga + */ fun getChapters(manga: Manga): MutableList? { return db.getChapters(manga).executeAsBlocking() } diff --git a/app/src/main/res/values/keys.xml b/app/src/main/res/values/keys.xml index cd9d1d73e..b1a061281 100644 --- a/app/src/main/res/values/keys.xml +++ b/app/src/main/res/values/keys.xml @@ -29,6 +29,7 @@ pref_image_decoder_key pref_seamless_mode_key pref_filter_downloaded + pref_filter_unread pref_download_directory_key pref_download_slots_key diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index fb6210728..cd3d342f3 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -162,6 +162,7 @@ Title or author… Selected + Filtering… This source requires you to log in