mirror of
https://github.com/mihonapp/mihon.git
synced 2024-12-24 18:08:24 +01:00
Add unread sorting
This commit is contained in:
parent
79ab9d80f2
commit
97e3b5d2ab
@ -108,7 +108,9 @@ class LibraryNavigationView @JvmOverloads constructor(context: Context, attrs: A
|
||||
|
||||
private val lastUpdated = Item.MultiSort(R.string.action_sort_last_updated, this)
|
||||
|
||||
override val items = listOf(alphabetically, lastRead, lastUpdated)
|
||||
private val unread = Item.MultiSort(R.string.action_filter_unread, this)
|
||||
|
||||
override val items = listOf(alphabetically, lastRead, lastUpdated, unread)
|
||||
|
||||
override val header = Item.Header(R.string.action_sort)
|
||||
|
||||
@ -122,6 +124,7 @@ class LibraryNavigationView @JvmOverloads constructor(context: Context, attrs: A
|
||||
alphabetically.state = if (sorting == LibrarySort.ALPHA) order else SORT_NONE
|
||||
lastRead.state = if (sorting == LibrarySort.LAST_READ) order else SORT_NONE
|
||||
lastUpdated.state = if (sorting == LibrarySort.LAST_UPDATED) order else SORT_NONE
|
||||
unread.state = if (sorting == LibrarySort.UNREAD) order else SORT_NONE
|
||||
}
|
||||
|
||||
override fun onItemClicked(item: Item) {
|
||||
@ -140,6 +143,7 @@ class LibraryNavigationView @JvmOverloads constructor(context: Context, attrs: A
|
||||
alphabetically -> LibrarySort.ALPHA
|
||||
lastRead -> LibrarySort.LAST_READ
|
||||
lastUpdated -> LibrarySort.LAST_UPDATED
|
||||
unread -> LibrarySort.UNREAD
|
||||
else -> throw Exception("Unknown sorting")
|
||||
})
|
||||
preferences.librarySortingAscending().set(if (item.state == SORT_ASC) true else false)
|
||||
|
@ -127,27 +127,28 @@ class LibraryPresenter : BasePresenter<LibraryFragment>() {
|
||||
val filterUnread = preferences.filterUnread().getOrDefault()
|
||||
|
||||
val filterFn: (Manga) -> Boolean = f@ { manga: Manga ->
|
||||
// Filter out manga without source
|
||||
// Filter out manga without source.
|
||||
val source = sourceManager.get(manga.source) ?: return@f false
|
||||
|
||||
// Filter when there isn't unread chapters.
|
||||
if (filterUnread && manga.unread == 0) {
|
||||
return@f false
|
||||
}
|
||||
|
||||
// Filter when the download directory doesn't exist or is null.
|
||||
if (filterDownloaded) {
|
||||
val mangaDirs = mangaDirectories.getOrPut(source.id) {
|
||||
downloadManager.findSourceDir(source)?.listFiles() ?: emptyArray()
|
||||
}
|
||||
|
||||
val mangaDirName = downloadManager.getMangaDirName(manga)
|
||||
val mangaDir = mangaDirs.find { it.name == mangaDirName }
|
||||
val mangaDir = mangaDirs.find { it.name == mangaDirName } ?: return@f false
|
||||
|
||||
return@f if (mangaDir == null) {
|
||||
false
|
||||
} else {
|
||||
chapterDirectories.getOrPut(manga.id!!) {
|
||||
(mangaDir.listFiles() ?: emptyArray()).isNotEmpty()
|
||||
}
|
||||
val hasDirs = chapterDirectories.getOrPut(manga.id!!) {
|
||||
(mangaDir.listFiles() ?: emptyArray()).isNotEmpty()
|
||||
}
|
||||
if (!hasDirs) {
|
||||
return@f false
|
||||
}
|
||||
}
|
||||
true
|
||||
@ -228,16 +229,17 @@ class LibraryPresenter : BasePresenter<LibraryFragment>() {
|
||||
* @param manga2 second manga to compare
|
||||
*/
|
||||
fun sortManga(sortingMode: Int, manga1: Manga, manga2: Manga): Int {
|
||||
when (sortingMode) {
|
||||
LibrarySort.ALPHA -> return manga1.title.compareTo(manga2.title)
|
||||
return when (sortingMode) {
|
||||
LibrarySort.ALPHA -> manga1.title.compareTo(manga2.title)
|
||||
LibrarySort.LAST_READ -> {
|
||||
// Get index of manga, set equal to list if size unknown.
|
||||
val manga1LastRead = lastReadManga.getOrElse(manga1.id!!, { lastReadManga.size })
|
||||
val manga2LastRead = lastReadManga.getOrElse(manga2.id!!, { lastReadManga.size })
|
||||
return manga1LastRead.compareTo(manga2LastRead)
|
||||
manga1LastRead.compareTo(manga2LastRead)
|
||||
}
|
||||
LibrarySort.LAST_UPDATED -> return manga2.last_update.compareTo(manga1.last_update)
|
||||
else -> return manga1.title.compareTo(manga2.title)
|
||||
LibrarySort.LAST_UPDATED -> manga2.last_update.compareTo(manga1.last_update)
|
||||
LibrarySort.UNREAD -> manga1.unread.compareTo(manga2.unread)
|
||||
else -> throw Exception("Unknown sorting mode")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -5,5 +5,6 @@ object LibrarySort {
|
||||
const val ALPHA = 0
|
||||
const val LAST_READ = 1
|
||||
const val LAST_UPDATED = 2
|
||||
const val UNREAD = 3
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user