From ab75f44d9c7cb3c18bc7587e178bf4f4b87aded4 Mon Sep 17 00:00:00 2001 From: Jay Date: Tue, 14 Jan 2020 19:29:24 -0800 Subject: [PATCH] Reordering Drag & Drop via menu keeps the sort order when adding new manga --- .../database/mappers/CategoryTypeMapping.kt | 15 ++++-- .../data/database/models/Category.kt | 2 + .../data/database/models/CategoryImpl.kt | 2 + .../ui/library/LibraryCategoryView.kt | 53 +++++++++++++++---- 4 files changed, 58 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/database/mappers/CategoryTypeMapping.kt b/app/src/main/java/eu/kanade/tachiyomi/data/database/mappers/CategoryTypeMapping.kt index 3ed871eab9..63ec522cec 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/database/mappers/CategoryTypeMapping.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/database/mappers/CategoryTypeMapping.kt @@ -41,8 +41,12 @@ class CategoryPutResolver : DefaultPutResolver() { 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() { 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() } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/database/models/Category.kt b/app/src/main/java/eu/kanade/tachiyomi/data/database/models/Category.kt index 7e827bcace..258f476303 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/database/models/Category.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/database/models/Category.kt @@ -14,6 +14,8 @@ interface Category : Serializable { var mangaOrder:List + var mangaSort:Char? + val nameLower: String get() = name.toLowerCase() diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/database/models/CategoryImpl.kt b/app/src/main/java/eu/kanade/tachiyomi/data/database/models/CategoryImpl.kt index f4c6d14376..7374effc3d 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/database/models/CategoryImpl.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/database/models/CategoryImpl.kt @@ -12,6 +12,8 @@ class CategoryImpl : Category { override var mangaOrder: List = 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 diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryView.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryView.kt index c68f83f40c..b9fdbe53ad 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryView.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryView.kt @@ -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): List { + 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