优化排序号相同导致的排序问题
This commit is contained in:
@@ -321,7 +321,7 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Co
|
|||||||
|
|
||||||
fun getItemByLayoutPosition(position: Int) = items.getOrNull(getActualPosition(position))
|
fun getItemByLayoutPosition(position: Int) = items.getOrNull(getActualPosition(position))
|
||||||
|
|
||||||
fun getItems(): List<ITEM> = items
|
fun getItems(): List<ITEM> = items.toList()
|
||||||
|
|
||||||
protected open fun getItemViewType(item: ITEM, position: Int) = 0
|
protected open fun getItemViewType(item: ITEM, position: Int) = 0
|
||||||
|
|
||||||
|
|||||||
@@ -158,9 +158,6 @@ interface BookSourceDao {
|
|||||||
@get:Query("select max(customOrder) from book_sources")
|
@get:Query("select max(customOrder) from book_sources")
|
||||||
val maxOrder: Int
|
val maxOrder: Int
|
||||||
|
|
||||||
@Query("select count(*) from (select customOrder, count(customOrder) from book_sources group by customOrder having count(customOrder) > 1)")
|
|
||||||
fun sameSortNumberSize(): Int
|
|
||||||
|
|
||||||
private fun dealGroups(list: List<String>): List<String> {
|
private fun dealGroups(list: List<String>): List<String> {
|
||||||
val groups = linkedSetOf<String>()
|
val groups = linkedSetOf<String>()
|
||||||
list.forEach {
|
list.forEach {
|
||||||
|
|||||||
@@ -596,8 +596,8 @@ class BookSourceActivity : VMBaseActivity<ActivityBookSourceBinding, BookSourceV
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun upOrder() {
|
override fun upOrder(items: List<BookSource>) {
|
||||||
viewModel.upOrder()
|
viewModel.upOrder(items)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun toTop(bookSource: BookSource) {
|
override fun toTop(bookSource: BookSource) {
|
||||||
|
|||||||
@@ -263,15 +263,11 @@ class BookSourceAdapter(context: Context, val callBack: CallBack) :
|
|||||||
val srcItem = getItem(srcPosition)
|
val srcItem = getItem(srcPosition)
|
||||||
val targetItem = getItem(targetPosition)
|
val targetItem = getItem(targetPosition)
|
||||||
if (srcItem != null && targetItem != null) {
|
if (srcItem != null && targetItem != null) {
|
||||||
if (srcItem.customOrder == targetItem.customOrder) {
|
val srcOrder = srcItem.customOrder
|
||||||
callBack.upOrder()
|
srcItem.customOrder = targetItem.customOrder
|
||||||
} else {
|
targetItem.customOrder = srcOrder
|
||||||
val srcOrder = srcItem.customOrder
|
movedItems.add(srcItem)
|
||||||
srcItem.customOrder = targetItem.customOrder
|
movedItems.add(targetItem)
|
||||||
targetItem.customOrder = srcOrder
|
|
||||||
movedItems.add(srcItem)
|
|
||||||
movedItems.add(targetItem)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
swapItem(srcPosition, targetPosition)
|
swapItem(srcPosition, targetPosition)
|
||||||
return true
|
return true
|
||||||
@@ -281,7 +277,15 @@ class BookSourceAdapter(context: Context, val callBack: CallBack) :
|
|||||||
|
|
||||||
override fun onClearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) {
|
override fun onClearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) {
|
||||||
if (movedItems.isNotEmpty()) {
|
if (movedItems.isNotEmpty()) {
|
||||||
callBack.update(*movedItems.toTypedArray())
|
val sortNumberSet = hashSetOf<Int>()
|
||||||
|
movedItems.forEach {
|
||||||
|
sortNumberSet.add(it.customOrder)
|
||||||
|
}
|
||||||
|
if (movedItems.size > sortNumberSet.size) {
|
||||||
|
callBack.upOrder(getItems())
|
||||||
|
} else {
|
||||||
|
callBack.update(*movedItems.toTypedArray())
|
||||||
|
}
|
||||||
movedItems.clear()
|
movedItems.clear()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -319,7 +323,7 @@ class BookSourceAdapter(context: Context, val callBack: CallBack) :
|
|||||||
fun toBottom(bookSource: BookSource)
|
fun toBottom(bookSource: BookSource)
|
||||||
fun searchBook(bookSource: BookSource)
|
fun searchBook(bookSource: BookSource)
|
||||||
fun debug(bookSource: BookSource)
|
fun debug(bookSource: BookSource)
|
||||||
fun upOrder()
|
fun upOrder(items: List<BookSource>)
|
||||||
fun upCountView()
|
fun upCountView()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -16,19 +16,6 @@ import java.io.FileOutputStream
|
|||||||
*/
|
*/
|
||||||
class BookSourceViewModel(application: Application) : BaseViewModel(application) {
|
class BookSourceViewModel(application: Application) : BaseViewModel(application) {
|
||||||
|
|
||||||
init {
|
|
||||||
execute {
|
|
||||||
val sameSortNumberSize = appDb.bookSourceDao.sameSortNumberSize()
|
|
||||||
if (sameSortNumberSize > 0) {
|
|
||||||
val sources = appDb.bookSourceDao.all
|
|
||||||
sources.forEachIndexed { index, bookSource ->
|
|
||||||
bookSource.customOrder = index
|
|
||||||
appDb.bookSourceDao.update(bookSource)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun topSource(vararg sources: BookSource) {
|
fun topSource(vararg sources: BookSource) {
|
||||||
execute {
|
execute {
|
||||||
sources.sortBy { it.customOrder }
|
sources.sortBy { it.customOrder }
|
||||||
@@ -64,13 +51,14 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application)
|
|||||||
execute { appDb.bookSourceDao.update(*bookSource) }
|
execute { appDb.bookSourceDao.update(*bookSource) }
|
||||||
}
|
}
|
||||||
|
|
||||||
fun upOrder() {
|
fun upOrder(items: List<BookSource>) {
|
||||||
|
if (items.isEmpty()) return
|
||||||
execute {
|
execute {
|
||||||
val sources = appDb.bookSourceDao.all
|
val firstSortNumber = items[0].customOrder
|
||||||
for ((index: Int, source: BookSource) in sources.withIndex()) {
|
items.forEachIndexed { index, bookSource ->
|
||||||
source.customOrder = index + 1
|
bookSource.customOrder = firstSortNumber + index
|
||||||
|
appDb.bookSourceDao.update(bookSource)
|
||||||
}
|
}
|
||||||
appDb.bookSourceDao.update(*sources.toTypedArray())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user