Reordering Drag & Drop via menu keeps the sort order when adding new manga

This commit is contained in:
Jay 2020-01-14 19:29:24 -08:00
parent f5ffbd4eb4
commit ab75f44d9c
4 changed files with 58 additions and 14 deletions

View File

@ -41,8 +41,12 @@ class CategoryPutResolver : DefaultPutResolver<Category>() {
put(COL_NAME, obj.name)
put(COL_ORDER, obj.order)
put(COL_FLAGS, obj.flags)
val orderString = obj.mangaOrder.joinToString("/")
put(COL_MANGA_ORDER, orderString)
if (obj.mangaSort != null)
put(COL_MANGA_ORDER, obj.mangaSort.toString())
else {
val orderString = obj.mangaOrder.joinToString("/")
put(COL_MANGA_ORDER, orderString)
}
}
}
@ -56,7 +60,12 @@ class CategoryGetResolver : DefaultGetResolver<Category>() {
flags = cursor.getInt(cursor.getColumnIndex(COL_FLAGS))
val orderString = cursor.getString(cursor.getColumnIndex(COL_MANGA_ORDER))
mangaOrder = orderString?.split("/")?.mapNotNull { it.toLongOrNull() } ?: emptyList()
if (orderString.firstOrNull()?.isLetter() == true) {
mangaSort = orderString.first()
mangaOrder = emptyList()
}
else
mangaOrder = orderString?.split("/")?.mapNotNull { it.toLongOrNull() } ?: emptyList()
}
}

View File

@ -14,6 +14,8 @@ interface Category : Serializable {
var mangaOrder:List<Long>
var mangaSort:Char?
val nameLower: String
get() = name.toLowerCase()

View File

@ -12,6 +12,8 @@ class CategoryImpl : Category {
override var mangaOrder: List<Long> = emptyList()
override var mangaSort: Char? = null
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other == null || javaClass != other.javaClass) return false

View File

@ -13,6 +13,7 @@ import eu.davidea.flexibleadapter.SelectableAdapter
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.DatabaseHelper
import eu.kanade.tachiyomi.data.database.models.Category
import eu.kanade.tachiyomi.data.database.models.LibraryManga
import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.library.LibraryUpdateService
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
@ -45,6 +46,7 @@ class LibraryCategoryView @JvmOverloads constructor(context: Context, attrs: Att
*/
private lateinit var controller: LibraryController
private val db: DatabaseHelper by injectLazy()
/**
* Category for this view.
*/
@ -162,6 +164,9 @@ class LibraryCategoryView @JvmOverloads constructor(context: Context, attrs: Att
items.removeAll(selectedManga)
if (it.second == 5) items.addAll(0, selectedManga)
else items.addAll(selectedManga)
adapter.setItems(items)
adapter.notifyDataSetChanged()
saveDragSort()
}
else {
items = when (it.second) {
@ -173,10 +178,14 @@ class LibraryCategoryView @JvmOverloads constructor(context: Context, attrs: Att
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()
}
adapter.setItems(items)
adapter.notifyDataSetChanged()
saveDragSort()
}
}
}
@ -212,13 +221,9 @@ class LibraryCategoryView @JvmOverloads constructor(context: Context, attrs: Att
val sortingMode = preferences.librarySortingMode().getOrDefault()
adapter.isLongPressDragEnabled = canDrag()
var mangaForCategory = event.getMangaForCategory(category).orEmpty()
if (sortingMode == LibrarySort.DRAG_AND_DROP) {
if (category.name == "Default")
category.mangaOrder = preferences.defaultMangaOrder().getOrDefault().split("/")
.mapNotNull { it.toLongOrNull() }
mangaForCategory = mangaForCategory.sortedBy { category.mangaOrder.indexOf(it.manga
.id) }
}
if (sortingMode == LibrarySort.DRAG_AND_DROP)
mangaForCategory = sortMangaInDragAnDrop(mangaForCategory)
// Update the category with its manga.
adapter.setItems(mangaForCategory)
@ -233,6 +238,32 @@ 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.
@ -336,8 +367,8 @@ class LibraryCategoryView @JvmOverloads constructor(context: Context, attrs: Att
private fun saveDragSort() {
val mangaIds = adapter.currentItems.mapNotNull { it.manga.id }
category.mangaSort = null
category.mangaOrder = mangaIds
val db: DatabaseHelper by injectLazy()
if (category.name == "Default")
preferences.defaultMangaOrder().set(mangaIds.joinToString("/"))
else