mirror of
				https://github.com/mihonapp/mihon.git
				synced 2025-10-30 22:07:57 +01:00 
			
		
		
		
	Can now filter downloads only on library view. Fix #27
This commit is contained in:
		| @@ -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<Boolean> { | ||||
|         return rxPrefs.getBoolean(getKey(R.string.pref_filter_downloaded), false) | ||||
|     } | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -74,6 +74,17 @@ class LibraryFragment : BaseRxFragment<LibraryPresenter>(), 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<LibraryPresenter>(), 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<LibraryPresenter>(), 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<LibraryPresenter>(), 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<LibraryPresenter>(), 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<LibraryPresenter>(), 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<LibraryPresenter>(), 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() | ||||
|         } | ||||
|  | ||||
|   | ||||
| @@ -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<LibraryFragment>() { | ||||
|      */ | ||||
|     lateinit var selectedMangas: MutableList<Manga> | ||||
|  | ||||
|     lateinit var libraryFragment: LibraryFragment | ||||
|  | ||||
|     /** | ||||
|      * Search query of the library. | ||||
|      */ | ||||
| @@ -91,6 +95,7 @@ class LibraryPresenter : BasePresenter<LibraryFragment>() { | ||||
|  | ||||
|     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<LibraryFragment>() { | ||||
|                 .observeOn(AndroidSchedulers.mainThread()) | ||||
|     } | ||||
|  | ||||
|     fun updateLibrary() { | ||||
|         start(GET_LIBRARY) | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Get the categories from the database. | ||||
|      * | ||||
| @@ -126,12 +135,32 @@ class LibraryPresenter : BasePresenter<LibraryFragment>() { | ||||
|     fun getLibraryMangasObservable(): Observable<Map<Int, List<Manga>>> { | ||||
|         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<Chapter>? { | ||||
|         return db.getChapters(manga).executeAsBlocking() | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Called when a manga is opened. | ||||
|      */ | ||||
|   | ||||
		Reference in New Issue
	
	Block a user