Add unread sorting

This commit is contained in:
len 2016-12-13 22:23:49 +01:00
parent 79ab9d80f2
commit 97e3b5d2ab
3 changed files with 21 additions and 14 deletions

View File

@ -108,7 +108,9 @@ class LibraryNavigationView @JvmOverloads constructor(context: Context, attrs: A
private val lastUpdated = Item.MultiSort(R.string.action_sort_last_updated, this) 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) 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 alphabetically.state = if (sorting == LibrarySort.ALPHA) order else SORT_NONE
lastRead.state = if (sorting == LibrarySort.LAST_READ) 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 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) { override fun onItemClicked(item: Item) {
@ -140,6 +143,7 @@ class LibraryNavigationView @JvmOverloads constructor(context: Context, attrs: A
alphabetically -> LibrarySort.ALPHA alphabetically -> LibrarySort.ALPHA
lastRead -> LibrarySort.LAST_READ lastRead -> LibrarySort.LAST_READ
lastUpdated -> LibrarySort.LAST_UPDATED lastUpdated -> LibrarySort.LAST_UPDATED
unread -> LibrarySort.UNREAD
else -> throw Exception("Unknown sorting") else -> throw Exception("Unknown sorting")
}) })
preferences.librarySortingAscending().set(if (item.state == SORT_ASC) true else false) preferences.librarySortingAscending().set(if (item.state == SORT_ASC) true else false)

View File

@ -127,27 +127,28 @@ class LibraryPresenter : BasePresenter<LibraryFragment>() {
val filterUnread = preferences.filterUnread().getOrDefault() val filterUnread = preferences.filterUnread().getOrDefault()
val filterFn: (Manga) -> Boolean = f@ { manga: Manga -> 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 val source = sourceManager.get(manga.source) ?: return@f false
// Filter when there isn't unread chapters.
if (filterUnread && manga.unread == 0) { if (filterUnread && manga.unread == 0) {
return@f false return@f false
} }
// Filter when the download directory doesn't exist or is null.
if (filterDownloaded) { if (filterDownloaded) {
val mangaDirs = mangaDirectories.getOrPut(source.id) { val mangaDirs = mangaDirectories.getOrPut(source.id) {
downloadManager.findSourceDir(source)?.listFiles() ?: emptyArray() downloadManager.findSourceDir(source)?.listFiles() ?: emptyArray()
} }
val mangaDirName = downloadManager.getMangaDirName(manga) 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) { val hasDirs = chapterDirectories.getOrPut(manga.id!!) {
false
} else {
chapterDirectories.getOrPut(manga.id!!) {
(mangaDir.listFiles() ?: emptyArray()).isNotEmpty() (mangaDir.listFiles() ?: emptyArray()).isNotEmpty()
} }
if (!hasDirs) {
return@f false
} }
} }
true true
@ -228,16 +229,17 @@ class LibraryPresenter : BasePresenter<LibraryFragment>() {
* @param manga2 second manga to compare * @param manga2 second manga to compare
*/ */
fun sortManga(sortingMode: Int, manga1: Manga, manga2: Manga): Int { fun sortManga(sortingMode: Int, manga1: Manga, manga2: Manga): Int {
when (sortingMode) { return when (sortingMode) {
LibrarySort.ALPHA -> return manga1.title.compareTo(manga2.title) LibrarySort.ALPHA -> manga1.title.compareTo(manga2.title)
LibrarySort.LAST_READ -> { LibrarySort.LAST_READ -> {
// Get index of manga, set equal to list if size unknown. // Get index of manga, set equal to list if size unknown.
val manga1LastRead = lastReadManga.getOrElse(manga1.id!!, { lastReadManga.size }) val manga1LastRead = lastReadManga.getOrElse(manga1.id!!, { lastReadManga.size })
val manga2LastRead = lastReadManga.getOrElse(manga2.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) LibrarySort.LAST_UPDATED -> manga2.last_update.compareTo(manga1.last_update)
else -> return manga1.title.compareTo(manga2.title) LibrarySort.UNREAD -> manga1.unread.compareTo(manga2.unread)
else -> throw Exception("Unknown sorting mode")
} }
} }

View File

@ -5,5 +5,6 @@ object LibrarySort {
const val ALPHA = 0 const val ALPHA = 0
const val LAST_READ = 1 const val LAST_READ = 1
const val LAST_UPDATED = 2 const val LAST_UPDATED = 2
const val UNREAD = 3
} }