From 97e3b5d2ab27d0b4d951a22def5e8cb3670e8c0f Mon Sep 17 00:00:00 2001 From: len Date: Tue, 13 Dec 2016 22:23:49 +0100 Subject: [PATCH] Add unread sorting --- .../ui/library/LibraryNavigationView.kt | 6 +++- .../tachiyomi/ui/library/LibraryPresenter.kt | 28 ++++++++++--------- .../tachiyomi/ui/library/LibrarySort.kt | 1 + 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryNavigationView.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryNavigationView.kt index 5a63ad587..449550a5b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryNavigationView.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryNavigationView.kt @@ -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) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt index acff3cd5f..4c92d07a7 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt @@ -127,27 +127,28 @@ class LibraryPresenter : BasePresenter() { 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() { * @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") } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibrarySort.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibrarySort.kt index a69541070..35cff3a98 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibrarySort.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibrarySort.kt @@ -5,5 +5,6 @@ object LibrarySort { const val ALPHA = 0 const val LAST_READ = 1 const val LAST_UPDATED = 2 + const val UNREAD = 3 } \ No newline at end of file