From d9f5a97d561ebf6a2ca0994a9b7c763410731acd Mon Sep 17 00:00:00 2001 From: NoodleMage Date: Thu, 3 Mar 2016 23:56:57 +0100 Subject: [PATCH] Can now filter downloads only on library view. Fix #27 --- .../data/preference/PreferencesHelper.kt | 4 ++ .../tachiyomi/ui/library/LibraryFragment.kt | 48 ++++++++++++++++++ .../tachiyomi/ui/library/LibraryPresenter.kt | 29 +++++++++++ .../ic_filter_list_white_24dp.png | Bin 0 -> 111 bytes .../ic_filter_list_white_24dp.png | Bin 0 -> 90 bytes .../ic_filter_list_white_24dp.png | Bin 0 -> 103 bytes .../ic_filter_list_white_24dp.png | Bin 0 -> 107 bytes .../ic_filter_list_white_24dp.png | Bin 0 -> 106 bytes app/src/main/res/menu/library.xml | 20 ++++++++ app/src/main/res/values/keys.xml | 1 + app/src/main/res/values/strings.xml | 4 ++ app/src/main/res/values/styles.xml | 2 +- 12 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 app/src/main/res/drawable-hdpi/ic_filter_list_white_24dp.png create mode 100644 app/src/main/res/drawable-mdpi/ic_filter_list_white_24dp.png create mode 100644 app/src/main/res/drawable-xhdpi/ic_filter_list_white_24dp.png create mode 100644 app/src/main/res/drawable-xxhdpi/ic_filter_list_white_24dp.png create mode 100644 app/src/main/res/drawable-xxxhdpi/ic_filter_list_white_24dp.png 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 c5ee1cefc..34d355f34 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 @@ -182,4 +182,8 @@ class PreferencesHelper(private val context: Context) { return rxPrefs.getInteger(getKey(R.string.pref_library_update_interval_key), 0) } + fun filterDownloaded(): Preference { + return rxPrefs.getBoolean(getKey(R.string.pref_filter_downloaded), 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 50950803b..1d02ca142 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 @@ -74,6 +74,17 @@ class LibraryFragment : BaseRxFragment(), ActionMode.Callback */ private var selectedCoverManga: Manga? = null + + /** + * TODO + */ + var isFilterDownloaded = false + + /** + * TODO + */ + var isFilterUnread = false + companion object { /** * Key to change the cover of a manga in [onActivityResult]. @@ -104,6 +115,7 @@ class LibraryFragment : BaseRxFragment(), ActionMode.Callback override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setHasOptionsMenu(true) + isFilterDownloaded = presenter.preferences.filterDownloaded().get() as Boolean } override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedState: Bundle?): View? { @@ -116,6 +128,14 @@ class LibraryFragment : BaseRxFragment(), ActionMode.Callback appBar = (activity as MainActivity).appBar tabs = appBar.inflate(R.layout.library_tab_layout) as TabLayout + + // Workaround to prevent: Tab belongs to a different TabLayout. + // Internal bug in Support library v23.2.0. + // See https://code.google.com/p/android/issues/detail?id=201827 + for (j in 0..16) { + tabs.newTab() + } + appBar.addView(tabs) adapter = LibraryAdapter(childFragmentManager) @@ -144,6 +164,8 @@ class LibraryFragment : BaseRxFragment(), ActionMode.Callback inflater.inflate(R.menu.library, menu) // Initialize search menu + val filterDownloadedItem = menu.findItem(R.id.action_filter_downloaded) + val filterUnreadItem = menu.findItem(R.id.action_filter_unread) val searchItem = menu.findItem(R.id.action_search) val searchView = searchItem.actionView as SearchView @@ -153,6 +175,9 @@ class LibraryFragment : BaseRxFragment(), ActionMode.Callback searchView.clearFocus() } + filterDownloadedItem.isChecked = isFilterDownloaded; + filterUnreadItem.isChecked = isFilterUnread; + searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener { override fun onQueryTextSubmit(query: String): Boolean { onSearchTextChange(query) @@ -168,6 +193,25 @@ class LibraryFragment : BaseRxFragment(), ActionMode.Callback override fun onOptionsItemSelected(item: MenuItem): Boolean { when (item.itemId) { + R.id.action_filter_unread -> { + isFilterUnread = !isFilterUnread + activity.supportInvalidateOptionsMenu(); + ToastUtil.showShort(context, "Filter Unread Clicked") + } + R.id.action_filter_downloaded -> { + isFilterDownloaded = !isFilterDownloaded + presenter.preferences.filterDownloaded().set(isFilterDownloaded) + presenter.updateLibrary() + adapter.notifyDataSetChanged() + activity.supportInvalidateOptionsMenu(); + ToastUtil.showShort(context, "Filter Download Clicked") + } + R.id.action_filter_empty -> { + isFilterUnread = false + isFilterDownloaded = false + activity.supportInvalidateOptionsMenu(); + ToastUtil.showShort(context, "Filter Clear Clicked") + } R.id.action_refresh -> LibraryUpdateService.start(activity) R.id.action_edit_categories -> { val intent = CategoryActivity.newIntent(activity) @@ -211,6 +255,10 @@ class LibraryFragment : BaseRxFragment(), ActionMode.Callback // Restore active category. view_pager.setCurrentItem(activeCat, false) if (tabs.tabCount > 0) { + // Prevent IndexOutOfBoundsException + if (tabs.tabCount <= view_pager.currentItem) { + view_pager.currentItem = (tabs.tabCount - 1) + } tabs.getTabAt(view_pager.currentItem)?.select() } 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 23269398f..3af88d47e 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 @@ -5,8 +5,10 @@ import android.util.Pair import eu.kanade.tachiyomi.data.cache.CoverCache import eu.kanade.tachiyomi.data.database.DatabaseHelper import eu.kanade.tachiyomi.data.database.models.Category +import eu.kanade.tachiyomi.data.database.models.Chapter 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.source.SourceManager import eu.kanade.tachiyomi.event.LibraryMangasEvent @@ -35,6 +37,8 @@ class LibraryPresenter : BasePresenter() { */ lateinit var selectedMangas: MutableList + lateinit var libraryFragment: LibraryFragment + /** * Search query of the library. */ @@ -91,6 +95,7 @@ class LibraryPresenter : BasePresenter() { override fun onTakeView(libraryFragment: LibraryFragment) { super.onTakeView(libraryFragment) + this.libraryFragment = libraryFragment if (isUnsubscribed(GET_LIBRARY)) { start(GET_LIBRARY) } @@ -107,6 +112,10 @@ class LibraryPresenter : BasePresenter() { .observeOn(AndroidSchedulers.mainThread()) } + fun updateLibrary() { + start(GET_LIBRARY) + } + /** * Get the categories from the database. * @@ -126,12 +135,32 @@ 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 }) } } + fun getChapters(manga: Manga): MutableList? { + return db.getChapters(manga).executeAsBlocking() + } + /** * Called when a manga is opened. */ diff --git a/app/src/main/res/drawable-hdpi/ic_filter_list_white_24dp.png b/app/src/main/res/drawable-hdpi/ic_filter_list_white_24dp.png new file mode 100644 index 0000000000000000000000000000000000000000..7e8a6b536bdfcba999ba36bfdba7ca975fd7b68c GIT binary patch literal 111 zcmeAS@N?(olHy`uVBq!ia0vp^Dj>|k0wldT1B8K;nWu|mh)3t!ix)W=3^&Hr5(PMPpn nt`GJgTe~DWM4fp6nTa literal 0 HcmV?d00001 diff --git a/app/src/main/res/drawable-xhdpi/ic_filter_list_white_24dp.png b/app/src/main/res/drawable-xhdpi/ic_filter_list_white_24dp.png new file mode 100644 index 0000000000000000000000000000000000000000..9416c70ec0a0a230648075ebc449e2000f239d72 GIT binary patch literal 103 zcmeAS@N?(olHy`uVBq!ia0vp^1|ZDA0wn)(8}a}tJx>?Mkcif|mkfCs7&s0X{F%+h z&%)%Mop4@>VctnaroRklqT+)Y_}1iWG8C+2XgDPM|LJEjE1*sWPgg&ebxsLQ0G~@6 Ar~m)} literal 0 HcmV?d00001 diff --git a/app/src/main/res/drawable-xxhdpi/ic_filter_list_white_24dp.png b/app/src/main/res/drawable-xxhdpi/ic_filter_list_white_24dp.png new file mode 100644 index 0000000000000000000000000000000000000000..1263ae82e71dfbfd5aba9a11483442542e74ce15 GIT binary patch literal 107 zcmeAS@N?(olHy`uVBq!ia0vp^9w5vJBp7O^^}Pa8OeH~n!3+##lh0ZJc`BYRjv*C{ z$r6(m{IloyqulHooWQ^35x=C46!WwH>?aa9)4kR*{W|XsGR4!?&t;ucLK6Tv CS{)|< literal 0 HcmV?d00001 diff --git a/app/src/main/res/drawable-xxxhdpi/ic_filter_list_white_24dp.png b/app/src/main/res/drawable-xxxhdpi/ic_filter_list_white_24dp.png new file mode 100644 index 0000000000000000000000000000000000000000..cb2207f11fb53879554df5541522fa4ee564e975 GIT binary patch literal 106 zcmeAS@N?(olHy`uVBq!ia0vp^2_Vb}Bp6OT_L>T$m`Z~Df*BafCZDwc@{~PY978G? zlNE$}nEw3__|Q0Q&OttfcS{V@Ia-)>EuS<8GB8}PYOf6O+;0yu#M9N!Wt~$(6988y B9v1)r literal 0 HcmV?d00001 diff --git a/app/src/main/res/menu/library.xml b/app/src/main/res/menu/library.xml index 29639aa09..02037e002 100644 --- a/app/src/main/res/menu/library.xml +++ b/app/src/main/res/menu/library.xml @@ -2,6 +2,26 @@ xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity"> + + + + + + + + pref_reader_theme_key pref_image_decoder_key pref_seamless_mode_key + pref_filter_downloaded 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 a4c231ec3..fb6210728 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -14,6 +14,10 @@ Settings + Filter + Downloaded + Unread + Remove filter Search Refresh Select all diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index a86901edf..6023805e5 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -15,7 +15,7 @@ @color/colorPrimarySuperDark @style/ThemeOverlay.AppCompat.Light @style/ThemeOverlay.AppCompat.Light - @color/white + @color/colorAccent