Allow to change sorting mode from the chapters fragment

This commit is contained in:
len 2016-05-20 18:07:18 +02:00
parent ca7e5260f0
commit e885469504
5 changed files with 66 additions and 11 deletions

View File

@ -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;

View File

@ -124,6 +124,7 @@ class ChaptersFragment : BaseRxFragment<ChaptersPresenter>(), 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<ChaptersPresenter>(), 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<ChaptersPresenter>(), 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<ChaptersPresenter>(), 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),

View File

@ -123,11 +123,22 @@ class ChaptersPresenter : BasePresenter<ChaptersFragment>() {
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<ChaptersFragment>() {
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
}

View File

@ -33,6 +33,11 @@
android:title="@string/action_display_mode"
app:showAsAction="never" />
<item
android:id="@+id/action_sorting_mode"
android:title="@string/sorting_mode"
app:showAsAction="never" />
<item
android:id="@+id/manga_download"
android:title="@string/manga_download"

View File

@ -225,6 +225,9 @@
<string name="fetch_chapters_error">Error while fetching chapters</string>
<string name="show_title">Show title</string>
<string name="show_chapter_number">Show chapter number</string>
<string name="sorting_mode">Sorting mode</string>
<string name="sort_by_source">By source</string>
<string name="sort_by_number">By chapter number</string>
<string name="manga_download">Download</string>
<string name="download_1">Download next chapter</string>
<string name="download_5">Download next 5 chapters</string>