mirror of
https://github.com/tachiyomiorg/tachiyomi.git
synced 2025-01-09 20:49:25 +01:00
Moved drag & drop sorting to library presenter
Where it always belonged also added radio buttons sort category by (used to be called reorder) also added unread sorting to that^
This commit is contained in:
parent
ab75f44d9c
commit
7008752639
@ -156,35 +156,24 @@ class LibraryCategoryView @JvmOverloads constructor(context: Context, attrs: Att
|
||||
subscriptions += controller.reorganizeRelay
|
||||
.subscribe {
|
||||
if (it.first == category.id) {
|
||||
var items:List<LibraryItem>
|
||||
if (it.second in 5..6) {
|
||||
items = adapter.currentItems.toMutableList()
|
||||
if (it.second in -2..-1) {
|
||||
val items = adapter.currentItems.toMutableList()
|
||||
val mangas = controller.selectedMangas
|
||||
val selectedManga = items.filter { item -> item.manga in mangas }
|
||||
items.removeAll(selectedManga)
|
||||
if (it.second == 5) items.addAll(0, selectedManga)
|
||||
if (it.second == -1) items.addAll(0, selectedManga)
|
||||
else items.addAll(selectedManga)
|
||||
adapter.setItems(items)
|
||||
adapter.notifyDataSetChanged()
|
||||
saveDragSort()
|
||||
}
|
||||
else {
|
||||
items = when (it.second) {
|
||||
1, 2 -> adapter.currentItems.sortedBy {
|
||||
if (preferences.removeArticles().getOrDefault()) it.manga.title.removeArticles()
|
||||
else it.manga.title
|
||||
}
|
||||
3, 4 -> adapter.currentItems.sortedBy { it.manga.last_update }
|
||||
else -> adapter.currentItems.sortedBy { it.manga.title }
|
||||
}
|
||||
if (it.second % 2 == 0) items = items.reversed()
|
||||
adapter.setItems(items)
|
||||
adapter.notifyDataSetChanged()
|
||||
category.mangaSort = ('a' + (it.second - 1))
|
||||
if (category.name == "Default")
|
||||
preferences.defaultMangaOrder().set(category.mangaSort.toString())
|
||||
else
|
||||
db.insertCategory(category).asRxObservable().subscribe()
|
||||
controller.enableReorderItems(category)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -218,11 +207,8 @@ class LibraryCategoryView @JvmOverloads constructor(context: Context, attrs: Att
|
||||
*/
|
||||
fun onNextLibraryManga(event: LibraryMangaEvent) {
|
||||
// Get the manga list for this category.
|
||||
val sortingMode = preferences.librarySortingMode().getOrDefault()
|
||||
adapter.isLongPressDragEnabled = canDrag()
|
||||
var mangaForCategory = event.getMangaForCategory(category).orEmpty()
|
||||
if (sortingMode == LibrarySort.DRAG_AND_DROP)
|
||||
mangaForCategory = sortMangaInDragAnDrop(mangaForCategory)
|
||||
val mangaForCategory = event.getMangaForCategory(category).orEmpty()
|
||||
|
||||
// Update the category with its manga.
|
||||
adapter.setItems(mangaForCategory)
|
||||
@ -238,32 +224,6 @@ class LibraryCategoryView @JvmOverloads constructor(context: Context, attrs: Att
|
||||
}
|
||||
}
|
||||
|
||||
fun sortMangaInDragAnDrop(mangaForCategory: List<LibraryItem>): List<LibraryItem> {
|
||||
if (category.name == "Default") {
|
||||
val defOrder = preferences.defaultMangaOrder().getOrDefault()
|
||||
if (defOrder.first().isLetter()) category.mangaSort = defOrder.first()
|
||||
else category.mangaOrder = defOrder.split("/").mapNotNull { it.toLongOrNull() }
|
||||
}
|
||||
return if (category.mangaSort != null) {
|
||||
var mangas = when (category.mangaSort) {
|
||||
'a', 'b' -> mangaForCategory.sortedBy {
|
||||
if (preferences.removeArticles().getOrDefault()) it.manga.title.removeArticles()
|
||||
else it.manga.title
|
||||
}
|
||||
'c', 'd' -> mangaForCategory.sortedBy { it.manga.last_update }
|
||||
else -> mangaForCategory.sortedBy { it.manga.title }
|
||||
}
|
||||
if (category.mangaSort == 'b' || category.mangaSort == 'd')
|
||||
mangas = mangas.asReversed()
|
||||
mangas
|
||||
|
||||
} else mangaForCategory.sortedBy {
|
||||
category.mangaOrder.indexOf(
|
||||
it.manga.id
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscribe to [LibrarySelectionEvent]. When an event is received, it updates the selection
|
||||
* depending on the type of event received.
|
||||
@ -373,6 +333,8 @@ class LibraryCategoryView @JvmOverloads constructor(context: Context, attrs: Att
|
||||
preferences.defaultMangaOrder().set(mangaIds.joinToString("/"))
|
||||
else
|
||||
db.insertCategory(category).asRxObservable().subscribe()
|
||||
controller.onSortChanged()
|
||||
controller.enableReorderItems(category)
|
||||
}
|
||||
override fun shouldMoveItem(fromPosition: Int, toPosition: Int): Boolean {
|
||||
if (adapter.selectedItemCount > 1)
|
||||
|
@ -22,6 +22,7 @@ import androidx.core.content.ContextCompat.getSystemService
|
||||
import androidx.core.graphics.drawable.DrawableCompat
|
||||
import androidx.core.view.GravityCompat
|
||||
import androidx.drawerlayout.widget.DrawerLayout
|
||||
import androidx.viewpager.widget.ViewPager
|
||||
import com.bluelinelabs.conductor.ControllerChangeHandler
|
||||
import com.bluelinelabs.conductor.ControllerChangeType
|
||||
import com.f2prateek.rx.preferences.Preference
|
||||
@ -158,6 +159,8 @@ class LibraryController(
|
||||
|
||||
var snack: Snackbar? = null
|
||||
|
||||
private var reorderMenuItem:MenuItem? = null
|
||||
|
||||
init {
|
||||
setHasOptionsMenu(true)
|
||||
retainViewMode = RetainViewMode.RETAIN_DETACH
|
||||
@ -185,6 +188,20 @@ class LibraryController(
|
||||
activeCategory = it
|
||||
}
|
||||
|
||||
library_pager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
|
||||
override fun onPageSelected(position: Int) {
|
||||
enableReorderItems(position)
|
||||
}
|
||||
|
||||
override fun onPageScrolled(
|
||||
position: Int,
|
||||
positionOffset: Float,
|
||||
positionOffsetPixels: Int
|
||||
) { }
|
||||
|
||||
override fun onPageScrollStateChanged(state: Int) { }
|
||||
})
|
||||
|
||||
getColumnsPreferenceForCurrentOrientation().asObservable()
|
||||
.doOnNext { mangaPerRow = it }
|
||||
.skip(1)
|
||||
@ -196,6 +213,31 @@ class LibraryController(
|
||||
}
|
||||
}
|
||||
|
||||
fun enableReorderItems(category: Category) {
|
||||
adapter?.categories?.getOrNull(library_pager.currentItem)?.mangaSort = category.mangaSort
|
||||
enableReorderItems(sortType = category.mangaSort)
|
||||
}
|
||||
|
||||
private fun enableReorderItems(position: Int? = null, sortType: Char? = null) {
|
||||
val pos = position ?: library_pager.currentItem
|
||||
val orderOfCat = sortType ?: adapter?.categories?.getOrNull(pos)?.mangaSort
|
||||
if (reorderMenuItem?.isVisible != true) return
|
||||
val subMenu = reorderMenuItem?.subMenu ?: return
|
||||
if (orderOfCat != null) {
|
||||
subMenu.setGroupCheckable(R.id.reorder_group, true, true)
|
||||
when (orderOfCat) {
|
||||
'a' -> subMenu.findItem(R.id.action_alpha_asc)?.isChecked = true
|
||||
'b' -> subMenu.findItem(R.id.action_alpha_dsc)?.isChecked = true
|
||||
'c' -> subMenu.findItem(R.id.action_update_asc)?.isChecked = true
|
||||
'd' -> subMenu.findItem(R.id.action_update_dsc)?.isChecked = true
|
||||
'e' -> subMenu.findItem(R.id.action_unread)?.isChecked = true
|
||||
}
|
||||
}
|
||||
else {
|
||||
subMenu.setGroupCheckable(R.id.reorder_group, false, false)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onChangeStarted(handler: ControllerChangeHandler, type: ControllerChangeType) {
|
||||
super.onChangeStarted(handler, type)
|
||||
if (type.isEnter) {
|
||||
@ -337,7 +379,7 @@ class LibraryController(
|
||||
/**
|
||||
* Called when the sorting mode is changed.
|
||||
*/
|
||||
private fun onSortChanged() {
|
||||
fun onSortChanged() {
|
||||
activity?.invalidateOptionsMenu()
|
||||
presenter.requestSortUpdate()
|
||||
}
|
||||
@ -381,6 +423,8 @@ class LibraryController(
|
||||
|
||||
val reorganizeItem = menu.findItem(R.id.action_reorganize)
|
||||
reorganizeItem.isVisible = preferences.librarySortingMode().getOrDefault() == LibrarySort.DRAG_AND_DROP
|
||||
reorderMenuItem = reorganizeItem
|
||||
enableReorderItems()
|
||||
|
||||
val searchItem = menu.findItem(R.id.action_search)
|
||||
val searchView = searchItem.actionView as SearchView
|
||||
@ -439,8 +483,9 @@ class LibraryController(
|
||||
}
|
||||
R.id.action_alpha_asc -> reOrder(1)
|
||||
R.id.action_alpha_dsc -> reOrder(2)
|
||||
R.id.action_update_dsc -> reOrder(3)
|
||||
R.id.action_update_asc -> reOrder(4)
|
||||
R.id.action_update_asc -> reOrder(3)
|
||||
R.id.action_update_dsc -> reOrder(4)
|
||||
R.id.action_unread -> reOrder(5)
|
||||
else -> return super.onOptionsItemSelected(item)
|
||||
}
|
||||
|
||||
@ -450,6 +495,7 @@ class LibraryController(
|
||||
private fun reOrder(type: Int) {
|
||||
adapter?.categories?.getOrNull(library_pager.currentItem)?.id?.let {
|
||||
reorganizeRelay.call(it to type)
|
||||
onSortChanged()
|
||||
}
|
||||
}
|
||||
|
||||
@ -527,7 +573,7 @@ class LibraryController(
|
||||
}
|
||||
R.id.action_to_top, R.id.action_to_bottom -> {
|
||||
adapter?.categories?.getOrNull(library_pager.currentItem)?.id?.let {
|
||||
reorganizeRelay.call(it to if (item.itemId == R.id.action_to_top) 5 else 6)
|
||||
reorganizeRelay.call(it to if (item.itemId == R.id.action_to_top) -1 else -2)
|
||||
}
|
||||
destroyActionModeIfNeeded()
|
||||
}
|
||||
|
@ -200,6 +200,14 @@ class LibraryPresenter(
|
||||
var counter = 0
|
||||
db.getTotalChapterManga().executeAsBlocking().associate { it.id!! to counter++ }
|
||||
}
|
||||
val catListing by lazy {
|
||||
val default = Category.createDefault()
|
||||
val defOrder = preferences.defaultMangaOrder().getOrDefault()
|
||||
if (defOrder.firstOrNull()?.isLetter() == true) default.mangaSort = defOrder.first()
|
||||
else default.mangaOrder = defOrder.split("/").mapNotNull { it.toLongOrNull() }
|
||||
listOf(default) + db.getCategories().executeAsBlocking()
|
||||
}
|
||||
|
||||
|
||||
val sortFn: (LibraryItem, LibraryItem) -> Int = { i1, i2 ->
|
||||
when (sortingMode) {
|
||||
@ -220,9 +228,35 @@ class LibraryPresenter(
|
||||
if (mangaCompare == 0) sortAlphabetical(i1, i2) else mangaCompare
|
||||
}
|
||||
LibrarySort.DRAG_AND_DROP -> {
|
||||
0
|
||||
if (i1.manga.category == i2.manga.category) {
|
||||
val category = catListing.find { it.id == i1.manga.category }
|
||||
when {
|
||||
category?.mangaSort != null -> {
|
||||
when (category.mangaSort) {
|
||||
'a' -> sortAlphabetical(i1, i2)
|
||||
'b' -> sortAlphabetical(i2, i1)
|
||||
'c' -> i2.manga.last_update.compareTo(i1.manga.last_update)
|
||||
'd' -> i1.manga.last_update.compareTo(i2.manga.last_update)
|
||||
'e' -> i2.manga.unread.compareTo(i1.manga.unread)
|
||||
else -> sortAlphabetical(i1, i2)
|
||||
}
|
||||
}
|
||||
category?.mangaOrder?.isEmpty() == false -> {
|
||||
val order = category.mangaOrder
|
||||
val index1 = order.indexOf(i1.manga.id!!)
|
||||
val index2 = order.indexOf(i2.manga.id!!)
|
||||
when {
|
||||
index1 == -1 -> -1
|
||||
index2 == -1 -> 1
|
||||
else -> index1.compareTo(index2)
|
||||
}
|
||||
}
|
||||
else -> 0
|
||||
}
|
||||
}
|
||||
else 0
|
||||
}
|
||||
else -> sortAlphabetical(i1, i2)
|
||||
else -> 0
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -68,7 +68,7 @@ abstract class PagerViewer(val activity: ReaderActivity) : BaseViewer {
|
||||
pager.offscreenPageLimit = 1
|
||||
pager.id = R.id.reader_pager
|
||||
pager.adapter = adapter
|
||||
pager.addOnPageChangeListener(object : androidx.viewpager.widget.ViewPager.SimpleOnPageChangeListener() {
|
||||
pager.addOnPageChangeListener(object : ViewPager.SimpleOnPageChangeListener() {
|
||||
override fun onPageSelected(position: Int) {
|
||||
val page = adapter.items.getOrNull(position)
|
||||
if (page != null && currentPage != page) {
|
||||
@ -81,7 +81,7 @@ abstract class PagerViewer(val activity: ReaderActivity) : BaseViewer {
|
||||
}
|
||||
|
||||
override fun onPageScrollStateChanged(state: Int) {
|
||||
isIdle = state == androidx.viewpager.widget.ViewPager.SCROLL_STATE_IDLE
|
||||
isIdle = state == ViewPager.SCROLL_STATE_IDLE
|
||||
}
|
||||
})
|
||||
pager.tapListener = { event ->
|
||||
|
@ -134,8 +134,8 @@ fun syncChaptersWithSource(db: DatabaseHelper,
|
||||
db.fixChaptersSourceOrder(sourceChapters).executeAsBlocking()
|
||||
|
||||
// Set this manga as updated since chapters were changed
|
||||
val newestChaper = db.getChapters(manga).executeAsBlocking().maxBy { it.date_fetch }
|
||||
val dateFetch = newestChaper?.date_fetch ?: manga.last_update
|
||||
val newestChatper = db.getChapters(manga).executeAsBlocking().maxBy { it.date_upload }
|
||||
val dateFetch = newestChatper?.date_upload ?: manga.last_update
|
||||
manga.last_update = dateFetch
|
||||
db.updateLastUpdated(manga).executeAsBlocking()
|
||||
}
|
||||
|
@ -1,9 +1,7 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<androidx.viewpager.widget.ViewPager
|
||||
android:layout_width="match_parent"
|
||||
|
@ -34,21 +34,28 @@
|
||||
|
||||
<item
|
||||
android:id="@+id/action_reorganize"
|
||||
android:title="@string/action_reorganize_by"
|
||||
android:title="@string/action_sort_by"
|
||||
app:showAsAction="never">
|
||||
<menu>
|
||||
<item
|
||||
android:id="@+id/action_alpha_asc"
|
||||
android:title="@string/action_sort_alpha"/>
|
||||
<item
|
||||
android:id="@+id/action_alpha_dsc"
|
||||
android:title="@string/action_alpha_reverse"/>
|
||||
<item
|
||||
android:id="@+id/action_update_asc"
|
||||
android:title="@string/action_sort_last_updated"/>
|
||||
<item
|
||||
android:id="@+id/action_update_dsc"
|
||||
android:title="@string/action_sort_first_updated"/>
|
||||
<group android:id="@+id/reorder_group"
|
||||
android:checkableBehavior="single">
|
||||
<item
|
||||
android:id="@+id/action_alpha_asc"
|
||||
android:title="@string/action_sort_alpha"/>
|
||||
<item
|
||||
android:id="@+id/action_alpha_dsc"
|
||||
android:title="@string/action_alpha_reverse"/>
|
||||
<item
|
||||
android:id="@+id/action_update_asc"
|
||||
android:title="@string/action_sort_last_updated"/>
|
||||
<item
|
||||
android:id="@+id/action_update_dsc"
|
||||
android:title="@string/action_sort_first_updated"/>
|
||||
<item
|
||||
android:id="@+id/action_unread"
|
||||
android:title="@string/action_filter_unread"/>
|
||||
|
||||
</group>
|
||||
</menu>
|
||||
</item>
|
||||
|
||||
|
@ -117,6 +117,7 @@
|
||||
<string name="action_move_to_top">Move to top</string>
|
||||
<string name="action_move_to_bottom">Move to bottom</string>
|
||||
<string name="action_track">Track</string>
|
||||
<string name="action_sort_by">Sort category by…</string>
|
||||
|
||||
<!-- Operations -->
|
||||
<string name="deleting">Deleting…</string>
|
||||
|
Loading…
x
Reference in New Issue
Block a user