mirror of
https://github.com/mihonapp/mihon.git
synced 2025-11-14 21:18:56 +01:00
Added option to sort library (#536)
* Initial code * Added all sort options * Fixes * Removed sort by added. Some renaming * Removed date added database calls * Fixes
This commit is contained in:
committed by
inorichi
parent
d971768056
commit
aba528b227
@@ -21,7 +21,7 @@ import rx.schedulers.Schedulers
|
||||
import rx.subjects.PublishSubject
|
||||
import timber.log.Timber
|
||||
import uy.kohesive.injekt.injectLazy
|
||||
import java.util.NoSuchElementException
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
* Presenter of [CatalogueFragment].
|
||||
|
||||
@@ -11,6 +11,7 @@ import android.support.v7.widget.SearchView
|
||||
import android.view.*
|
||||
import com.afollestad.materialdialogs.MaterialDialog
|
||||
import com.f2prateek.rx.preferences.Preference
|
||||
import eu.kanade.tachiyomi.Constants
|
||||
import eu.kanade.tachiyomi.R
|
||||
import eu.kanade.tachiyomi.data.database.models.Category
|
||||
import eu.kanade.tachiyomi.data.database.models.Manga
|
||||
@@ -83,6 +84,11 @@ class LibraryFragment : BaseRxFragment<LibraryPresenter>(), ActionMode.Callback
|
||||
*/
|
||||
var isFilterUnread = false
|
||||
|
||||
/**
|
||||
* Sorting mode for library
|
||||
*/
|
||||
var sortingMode = 0
|
||||
|
||||
/**
|
||||
* Number of manga per row in grid mode.
|
||||
*/
|
||||
@@ -123,8 +129,9 @@ class LibraryFragment : BaseRxFragment<LibraryPresenter>(), ActionMode.Callback
|
||||
override fun onCreate(savedState: Bundle?) {
|
||||
super.onCreate(savedState)
|
||||
setHasOptionsMenu(true)
|
||||
isFilterDownloaded = preferences.filterDownloaded().get() as Boolean
|
||||
isFilterUnread = preferences.filterUnread().get() as Boolean
|
||||
isFilterDownloaded = preferences.filterDownloaded().getOrDefault()
|
||||
isFilterUnread = preferences.filterUnread().getOrDefault()
|
||||
sortingMode = preferences.librarySortingMode().getOrDefault()
|
||||
}
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedState: Bundle?): View? {
|
||||
@@ -179,12 +186,37 @@ class LibraryFragment : BaseRxFragment<LibraryPresenter>(), ActionMode.Callback
|
||||
super.onSaveInstanceState(outState)
|
||||
}
|
||||
|
||||
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
|
||||
inflater.inflate(R.menu.library, menu)
|
||||
|
||||
/**
|
||||
* Prepare the Fragment host's standard options menu to be displayed. This is
|
||||
* called right before the menu is shown, every time it is shown. You can
|
||||
* use this method to efficiently enable/disable items or otherwise
|
||||
* dynamically modify the contents.
|
||||
*
|
||||
* @param menu The options menu as last shown or first initialized by
|
||||
*/
|
||||
override fun onPrepareOptionsMenu(menu: Menu) {
|
||||
// Initialize search menu
|
||||
val filterDownloadedItem = menu.findItem(R.id.action_filter_downloaded)
|
||||
val filterUnreadItem = menu.findItem(R.id.action_filter_unread)
|
||||
val sortModeAlpha = menu.findItem(R.id.action_sort_alpha)
|
||||
val sortModeLastRead = menu.findItem(R.id.action_sort_last_read)
|
||||
val sortModeLastUpdated = menu.findItem(R.id.action_sort_last_updated)
|
||||
|
||||
// Set correct checkbox filter
|
||||
filterDownloadedItem.isChecked = isFilterDownloaded
|
||||
filterUnreadItem.isChecked = isFilterUnread
|
||||
|
||||
// Set correct radio button sort
|
||||
when (sortingMode) {
|
||||
Constants.SORT_LIBRARY_ALPHA -> sortModeAlpha.isChecked = true
|
||||
Constants.SORT_LIBRARY_LAST_READ -> sortModeLastRead.isChecked = true
|
||||
Constants.SORT_LIBRARY_LAST_UPDATED -> sortModeLastUpdated.isChecked = true
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
|
||||
inflater.inflate(R.menu.library, menu)
|
||||
|
||||
val searchItem = menu.findItem(R.id.action_search)
|
||||
val searchView = searchItem.actionView as SearchView
|
||||
|
||||
@@ -194,9 +226,6 @@ 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)
|
||||
@@ -219,7 +248,7 @@ class LibraryFragment : BaseRxFragment<LibraryPresenter>(), ActionMode.Callback
|
||||
// Update settings.
|
||||
preferences.filterUnread().set(isFilterUnread)
|
||||
// Apply filter.
|
||||
onFilterCheckboxChanged()
|
||||
onFilterOrSortChanged()
|
||||
}
|
||||
R.id.action_filter_downloaded -> {
|
||||
// Change downloaded filter status.
|
||||
@@ -227,7 +256,7 @@ class LibraryFragment : BaseRxFragment<LibraryPresenter>(), ActionMode.Callback
|
||||
// Update settings.
|
||||
preferences.filterDownloaded().set(isFilterDownloaded)
|
||||
// Apply filter.
|
||||
onFilterCheckboxChanged()
|
||||
onFilterOrSortChanged()
|
||||
}
|
||||
R.id.action_filter_empty -> {
|
||||
// Remove filter status.
|
||||
@@ -237,7 +266,22 @@ class LibraryFragment : BaseRxFragment<LibraryPresenter>(), ActionMode.Callback
|
||||
preferences.filterUnread().set(isFilterUnread)
|
||||
preferences.filterDownloaded().set(isFilterDownloaded)
|
||||
// Apply filter
|
||||
onFilterCheckboxChanged()
|
||||
onFilterOrSortChanged()
|
||||
}
|
||||
R.id.action_sort_alpha -> {
|
||||
sortingMode = Constants.SORT_LIBRARY_ALPHA
|
||||
preferences.librarySortingMode().set(sortingMode)
|
||||
onFilterOrSortChanged()
|
||||
}
|
||||
R.id.action_sort_last_read -> {
|
||||
sortingMode = Constants.SORT_LIBRARY_LAST_READ
|
||||
preferences.librarySortingMode().set(sortingMode)
|
||||
onFilterOrSortChanged()
|
||||
}
|
||||
R.id.action_sort_last_updated -> {
|
||||
sortingMode = Constants.SORT_LIBRARY_LAST_UPDATED
|
||||
preferences.librarySortingMode().set(sortingMode)
|
||||
onFilterOrSortChanged()
|
||||
}
|
||||
R.id.action_library_display_mode -> swapDisplayMode()
|
||||
R.id.action_update_library -> {
|
||||
@@ -256,7 +300,7 @@ class LibraryFragment : BaseRxFragment<LibraryPresenter>(), ActionMode.Callback
|
||||
/**
|
||||
* Applies filter change
|
||||
*/
|
||||
private fun onFilterCheckboxChanged() {
|
||||
private fun onFilterOrSortChanged() {
|
||||
presenter.resubscribeLibrary()
|
||||
activity.supportInvalidateOptionsMenu()
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package eu.kanade.tachiyomi.ui.library
|
||||
|
||||
import android.os.Bundle
|
||||
import android.util.Pair
|
||||
import eu.kanade.tachiyomi.Constants
|
||||
import eu.kanade.tachiyomi.data.cache.CoverCache
|
||||
import eu.kanade.tachiyomi.data.database.DatabaseHelper
|
||||
import eu.kanade.tachiyomi.data.database.models.Category
|
||||
@@ -133,10 +134,12 @@ class LibraryPresenter : BasePresenter<LibraryFragment>() {
|
||||
*/
|
||||
fun getLibraryMangasObservable(): Observable<Map<Int, List<Manga>>> {
|
||||
return db.getLibraryMangas().asRxObservable()
|
||||
.flatMap { mangas ->
|
||||
Observable.from(mangas)
|
||||
.flatMap {
|
||||
Observable.from(it)
|
||||
// Filter library by options
|
||||
.filter { filterManga(it) }
|
||||
.toSortedList { manga1, manga2 -> sortManga(manga1, manga2) }
|
||||
.flatMap { Observable.from(it) }
|
||||
.groupBy { it.category }
|
||||
.flatMap { group -> group.toList().map { Pair(group.key, it) } }
|
||||
.toMap({ it.first }, { it.second })
|
||||
@@ -159,6 +162,33 @@ class LibraryPresenter : BasePresenter<LibraryFragment>() {
|
||||
start(GET_LIBRARY)
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares the two manga determined by sorting mode.
|
||||
* Returns zero if this object is equal to the specified other object,
|
||||
* a negative number if it's less than other, or a positive number if it's greater than other.
|
||||
*
|
||||
* @param manga1 first manga to compare
|
||||
* @param manga2 second manga to compare
|
||||
*/
|
||||
fun sortManga(manga1: Manga, manga2: Manga): Int {
|
||||
when (preferences.librarySortingMode().getOrDefault()) {
|
||||
Constants.SORT_LIBRARY_ALPHA -> return manga1.title.compareTo(manga2.title)
|
||||
Constants.SORT_LIBRARY_LAST_READ -> {
|
||||
var a = 0L
|
||||
var b = 0L
|
||||
manga1.id?.let { manga1Id ->
|
||||
manga2.id?.let { manga2Id ->
|
||||
db.getLastHistoryByMangaId(manga1Id).executeAsBlocking()?.let { a = it.last_read }
|
||||
db.getLastHistoryByMangaId(manga2Id).executeAsBlocking()?.let { b = it.last_read }
|
||||
}
|
||||
}
|
||||
return b.compareTo(a)
|
||||
}
|
||||
Constants.SORT_LIBRARY_LAST_UPDATED -> return manga2.last_update.compareTo(manga1.last_update)
|
||||
else -> return manga1.title.compareTo(manga2.title)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters an entry of the library.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user