mirror of
				https://github.com/mihonapp/mihon.git
				synced 2025-10-30 22:07:57 +01:00 
			
		
		
		
	Can now filter unread manga + Code opt
This commit is contained in:
		| @@ -186,4 +186,8 @@ class PreferencesHelper(private val context: Context) { | ||||
|         return rxPrefs.getBoolean(getKey(R.string.pref_filter_downloaded), false) | ||||
|     } | ||||
|  | ||||
|     fun filterUnread(): Preference<Boolean> { | ||||
|         return rxPrefs.getBoolean(getKey(R.string.pref_filter_unread), false) | ||||
|     } | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -116,6 +116,7 @@ class LibraryFragment : BaseRxFragment<LibraryPresenter>(), 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<LibraryPresenter>(), 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<LibraryPresenter>(), 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. | ||||
|      * | ||||
|   | ||||
| @@ -37,8 +37,6 @@ class LibraryPresenter : BasePresenter<LibraryFragment>() { | ||||
|      */ | ||||
|     lateinit var selectedMangas: MutableList<Manga> | ||||
|  | ||||
|     lateinit var libraryFragment: LibraryFragment | ||||
|  | ||||
|     /** | ||||
|      * Search query of the library. | ||||
|      */ | ||||
| @@ -95,7 +93,6 @@ class LibraryPresenter : BasePresenter<LibraryFragment>() { | ||||
|  | ||||
|     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<LibraryFragment>() { | ||||
|                 .observeOn(AndroidSchedulers.mainThread()) | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Update the library information | ||||
|      */ | ||||
|     fun updateLibrary() { | ||||
|         start(GET_LIBRARY) | ||||
|     } | ||||
| @@ -134,29 +134,72 @@ 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 }) | ||||
|                 .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<Chapter>? { | ||||
|         return db.getChapters(manga).executeAsBlocking() | ||||
|     } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user