Can now filter unread manga + Code opt

This commit is contained in:
NoodleMage 2016-03-04 18:45:58 +01:00
parent d9f5a97d56
commit 8fbef4b4bb
5 changed files with 99 additions and 31 deletions

View File

@ -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)
}
}

View File

@ -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.
*

View File

@ -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()
}

View File

@ -29,6 +29,7 @@
<string name="pref_image_decoder_key">pref_image_decoder_key</string>
<string name="pref_seamless_mode_key">pref_seamless_mode_key</string>
<string name="pref_filter_downloaded">pref_filter_downloaded</string>
<string name="pref_filter_unread">pref_filter_unread</string>
<string name="pref_download_directory_key">pref_download_directory_key</string>
<string name="pref_download_slots_key">pref_download_slots_key</string>

View File

@ -162,6 +162,7 @@
<!-- Library fragment -->
<string name="library_search_hint">Title or author…</string>
<string name="library_selection_title">Selected</string>
<string name="library_filter_change">Filtering…</string>
<!-- Catalogue fragment -->
<string name="source_requires_login">This source requires you to log in</string>