Browse Source

优化排序号相同导致的排序问题

kunfei 3 years ago
parent
commit
cb3311aaa8

+ 3 - 3
app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt

@@ -54,9 +54,6 @@ interface BookSourceDao {
     @Query("select * from book_sources where enabledExplore = 1 and trim(exploreUrl) <> '' order by customOrder asc")
     fun flowExplore(): Flow<List<BookSource>>
 
-//    @Query("select * from book_sources where enabledReview = 1 order by customOrder asc")
-//    fun flowReview(): Flow<List<BookSource>>
-
     @Query("select * from book_sources where loginUrl is not null and loginUrl != ''")
     fun flowLogin(): Flow<List<BookSource>>
 
@@ -161,6 +158,9 @@ interface BookSourceDao {
     @get:Query("select max(customOrder) from book_sources")
     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> {
         val groups = linkedSetOf<String>()
         list.forEach {

+ 2 - 2
app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt

@@ -58,6 +58,7 @@ class BookSourceActivity : VMBaseActivity<ActivityBookSourceBinding, BookSourceV
     override val viewModel by viewModels<BookSourceViewModel>()
     private val importRecordKey = "bookSourceRecordKey"
     private val adapter by lazy { BookSourceAdapter(this, this) }
+    private val itemTouchCallback by lazy { ItemTouchCallback(adapter) }
     private val searchView: SearchView by lazy {
         binding.titleBar.findViewById(R.id.search_view)
     }
@@ -197,8 +198,6 @@ class BookSourceActivity : VMBaseActivity<ActivityBookSourceBinding, BookSourceV
         dragSelectTouchHelper.attachToRecyclerView(binding.recyclerView)
         dragSelectTouchHelper.activeSlideSelect()
         // Note: need judge selection first, so add ItemTouchHelper after it.
-        val itemTouchCallback = ItemTouchCallback(adapter)
-        itemTouchCallback.isCanDrag = true
         ItemTouchHelper(itemTouchCallback).attachToRecyclerView(binding.recyclerView)
     }
 
@@ -276,6 +275,7 @@ class BookSourceActivity : VMBaseActivity<ActivityBookSourceBinding, BookSourceV
                 AppLog.put("书源界面更新书源出错", it)
             }.conflate().collect { data ->
                 adapter.setItems(data, adapter.diffItemCallback)
+                itemTouchCallback.isCanDrag = sort == Sort.Default
                 delay(500)
             }
         }

+ 13 - 0
app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt

@@ -16,6 +16,19 @@ import java.io.FileOutputStream
  */
 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) {
         execute {
             sources.sortBy { it.customOrder }