From e8854695045a8febf5158339e1bd9cdf017c6459 Mon Sep 17 00:00:00 2001 From: len Date: Fri, 20 May 2016 18:07:18 +0200 Subject: [PATCH] Allow to change sorting mode from the chapters fragment --- .../tachiyomi/data/database/models/Manga.java | 16 +++++++++-- .../ui/manga/chapter/ChaptersFragment.kt | 26 +++++++++++++++--- .../ui/manga/chapter/ChaptersPresenter.kt | 27 +++++++++++++++---- app/src/main/res/menu/chapters.xml | 5 ++++ app/src/main/res/values/strings.xml | 3 +++ 5 files changed, 66 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/database/models/Manga.java b/app/src/main/java/eu/kanade/tachiyomi/data/database/models/Manga.java index e8f8a24f99..0ac6eaa2d6 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/database/models/Manga.java +++ b/app/src/main/java/eu/kanade/tachiyomi/data/database/models/Manga.java @@ -72,6 +72,9 @@ public class Manga implements Serializable { public static final int SORT_ZA = 0x00000001; public static final int SORT_MASK = 0x00000001; + // Generic filter that does not filter anything + public static final int SHOW_ALL = 0x00000000; + public static final int SHOW_UNREAD = 0x00000002; public static final int SHOW_READ = 0x00000004; public static final int READ_MASK = 0x00000006; @@ -80,8 +83,9 @@ public class Manga implements Serializable { public static final int SHOW_NOT_DOWNLOADED = 0x00000010; public static final int DOWNLOADED_MASK = 0x00000018; - // Generic filter that does not filter anything - public static final int SHOW_ALL = 0x00000000; + public static final int SORTING_NUMBER = 0x00000000; + public static final int SORTING_SOURCE = 0x00000100; + public static final int SORTING_MASK = 0x00000100; public static final int DISPLAY_NAME = 0x00000000; public static final int DISPLAY_NUMBER = 0x00100000; @@ -162,6 +166,10 @@ public class Manga implements Serializable { setFlags(filter, DOWNLOADED_MASK); } + public void setSorting(int sort) { + setFlags(sort, SORTING_MASK); + } + private void setFlags(int flag, int mask) { chapter_flags = (chapter_flags & ~mask) | (flag & mask); } @@ -183,6 +191,10 @@ public class Manga implements Serializable { return chapter_flags & DOWNLOADED_MASK; } + public int getSorting() { + return chapter_flags & SORTING_MASK; + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersFragment.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersFragment.kt index a6c799b85f..605c2b8d96 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersFragment.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersFragment.kt @@ -124,6 +124,7 @@ class ChaptersFragment : BaseRxFragment(), ActionMode.Callbac when (item.itemId) { R.id.action_display_mode -> showDisplayModeDialog() R.id.manga_download -> showDownloadDialog() + R.id.action_sorting_mode -> showSortingDialog() R.id.action_filter_unread -> { item.isChecked = !item.isChecked presenter.setReadFilter(item.isChecked) @@ -135,7 +136,7 @@ class ChaptersFragment : BaseRxFragment(), ActionMode.Callbac R.id.action_filter_empty -> { presenter.setReadFilter(false) presenter.setDownloadedFilter(false) - activity.supportInvalidateOptionsMenu(); + activity.supportInvalidateOptionsMenu() } R.id.action_sort -> presenter.revertSortOrder() else -> return super.onOptionsItemSelected(item) @@ -192,15 +193,14 @@ class ChaptersFragment : BaseRxFragment(), ActionMode.Callbac } private fun showDisplayModeDialog() { - // Get available modes, ids and the selected mode - val modes = listOf(getString(R.string.show_title), getString(R.string.show_chapter_number)) + val modes = intArrayOf(R.string.show_title, R.string.show_chapter_number) val ids = intArrayOf(Manga.DISPLAY_NAME, Manga.DISPLAY_NUMBER) val selectedIndex = if (presenter.manga.displayMode == Manga.DISPLAY_NAME) 0 else 1 MaterialDialog.Builder(activity) .title(R.string.action_display_mode) - .items(modes) + .items(modes.map { getString(it) }) .itemsIds(ids) .itemsCallbackSingleChoice(selectedIndex) { dialog, itemView, which, text -> // Save the new display mode @@ -212,6 +212,24 @@ class ChaptersFragment : BaseRxFragment(), ActionMode.Callbac .show() } + private fun showSortingDialog() { + // Get available modes, ids and the selected mode + val modes = intArrayOf(R.string.sort_by_number, R.string.sort_by_source) + val ids = intArrayOf(Manga.SORTING_NUMBER, Manga.SORTING_SOURCE) + val selectedIndex = if (presenter.manga.sorting == Manga.SORTING_NUMBER) 0 else 1 + + MaterialDialog.Builder(activity) + .title(R.string.sorting_mode) + .items(modes.map { getString(it) }) + .itemsIds(ids) + .itemsCallbackSingleChoice(selectedIndex) { dialog, itemView, which, text -> + // Save the new display mode + presenter.setSorting(itemView.id) + true + } + .show() + } + private fun showDownloadDialog() { // Get available modes val modes = listOf(getString(R.string.download_1), getString(R.string.download_5), getString(R.string.download_10), diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersPresenter.kt index a8938e1daa..7439f59f7b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersPresenter.kt @@ -123,11 +123,22 @@ class ChaptersPresenter : BasePresenter() { if (onlyDownloaded()) { observable = observable.filter { chapter -> chapter.status == Download.DOWNLOADED } } - return observable.toSortedList { chapter, chapter2 -> - if (sortOrder()) - chapter2.chapter_number.compareTo(chapter.chapter_number) - else - chapter.chapter_number.compareTo(chapter2.chapter_number) + return observable.toSortedList { chapter1, chapter2 -> + when (manga.sorting) { + Manga.SORTING_NUMBER -> { + if (sortOrder()) + chapter2.chapter_number.compareTo(chapter1.chapter_number) + else + chapter1.chapter_number.compareTo(chapter2.chapter_number) + } + Manga.SORTING_SOURCE -> { + if (sortOrder()) + chapter1.source_order.compareTo(chapter2.source_order) + else + chapter2.source_order.compareTo(chapter1.source_order) + } + else -> throw AssertionError("Unknown sorting method") + } } } @@ -239,6 +250,12 @@ class ChaptersPresenter : BasePresenter() { db.insertManga(manga).executeAsBlocking() } + fun setSorting(mode: Int) { + manga.sorting = mode + db.insertManga(manga).executeAsBlocking() + refreshChapters() + } + fun onlyDownloaded(): Boolean { return manga.downloadedFilter == Manga.SHOW_DOWNLOADED } diff --git a/app/src/main/res/menu/chapters.xml b/app/src/main/res/menu/chapters.xml index b4108a0017..cb5c01e388 100644 --- a/app/src/main/res/menu/chapters.xml +++ b/app/src/main/res/menu/chapters.xml @@ -33,6 +33,11 @@ android:title="@string/action_display_mode" app:showAsAction="never" /> + + Error while fetching chapters Show title Show chapter number + Sorting mode + By source + By chapter number Download Download next chapter Download next 5 chapters