Просмотр исходного кода

fix: 保证目录替换和书源排序原子性

5151561 3 месяцев назад
Родитель
Сommit
c13ef38d46

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

@@ -311,6 +311,22 @@ interface BookSourceDao {
     @get:Query("select max(customOrder) from book_sources")
     @get:Query("select max(customOrder) from book_sources")
     val maxOrder: Int
     val maxOrder: Int
 
 
+    @Transaction
+    fun moveToTop(sourceUrl: String): Int? {
+        val source = getBookSource(sourceUrl) ?: return null
+        source.customOrder = minOrder - 1
+        update(source)
+        return source.customOrder
+    }
+
+    @Transaction
+    fun moveToBottom(sourceUrl: String): Int? {
+        val source = getBookSource(sourceUrl) ?: return null
+        source.customOrder = maxOrder + 1
+        update(source)
+        return source.customOrder
+    }
+
     @get:Query(
     @get:Query(
         """select exists (select 1 
         """select exists (select 1 
         from book_sources group by customOrder having count(customOrder) > 1)"""
         from book_sources group by customOrder having count(customOrder) > 1)"""

+ 8 - 4
app/src/main/java/io/legado/app/data/repository/BookRepository.kt

@@ -1,5 +1,6 @@
 package io.legado.app.data.repository
 package io.legado.app.data.repository
 
 
+import io.legado.app.data.AppDatabase
 import io.legado.app.data.dao.BookChapterDao
 import io.legado.app.data.dao.BookChapterDao
 import io.legado.app.data.dao.BookDao
 import io.legado.app.data.dao.BookDao
 import io.legado.app.data.dao.GroupBookCount
 import io.legado.app.data.dao.GroupBookCount
@@ -12,7 +13,8 @@ import kotlinx.coroutines.withContext
 
 
 class BookRepository(
 class BookRepository(
     private val bookDao: BookDao,
     private val bookDao: BookDao,
-    private val bookChapterDao: BookChapterDao
+    private val bookChapterDao: BookChapterDao,
+    private val appDb: AppDatabase,
 ) {
 ) {
     fun flowBook(bookUrl: String): Flow<Book?> {
     fun flowBook(bookUrl: String): Flow<Book?> {
         return bookDao.flowGetBook(bookUrl)
         return bookDao.flowGetBook(bookUrl)
@@ -142,9 +144,11 @@ class BookRepository(
 
 
     suspend fun replaceChaptersAndUpdateBook(book: Book, chapters: List<BookChapter>) {
     suspend fun replaceChaptersAndUpdateBook(book: Book, chapters: List<BookChapter>) {
         withContext(Dispatchers.IO) {
         withContext(Dispatchers.IO) {
-            bookChapterDao.delByBook(book.bookUrl)
-            bookChapterDao.insert(*chapters.toTypedArray())
-            bookDao.update(book)
+            appDb.runInTransaction {
+                bookChapterDao.delByBook(book.bookUrl)
+                bookChapterDao.insert(*chapters.toTypedArray())
+                bookDao.update(book)
+            }
         }
         }
     }
     }
 
 

+ 2 - 8
app/src/main/java/io/legado/app/data/repository/BookSourceRepository.kt

@@ -119,17 +119,11 @@ class BookSourceRepository(private val bookSourceDao: BookSourceDao) {
     }
     }
 
 
     suspend fun moveSourceToTop(sourceUrl: String): Int? = withContext(Dispatchers.IO) {
     suspend fun moveSourceToTop(sourceUrl: String): Int? = withContext(Dispatchers.IO) {
-        val source = bookSourceDao.getBookSource(sourceUrl) ?: return@withContext null
-        source.customOrder = bookSourceDao.minOrder - 1
-        bookSourceDao.update(source)
-        source.customOrder
+        bookSourceDao.moveToTop(sourceUrl)
     }
     }
 
 
     suspend fun moveSourceToBottom(sourceUrl: String): Int? = withContext(Dispatchers.IO) {
     suspend fun moveSourceToBottom(sourceUrl: String): Int? = withContext(Dispatchers.IO) {
-        val source = bookSourceDao.getBookSource(sourceUrl) ?: return@withContext null
-        source.customOrder = bookSourceDao.maxOrder + 1
-        bookSourceDao.update(source)
-        source.customOrder
+        bookSourceDao.moveToBottom(sourceUrl)
     }
     }
 
 
     suspend fun updateSources(vararg sources: BookSource) = withContext(Dispatchers.IO) {
     suspend fun updateSources(vararg sources: BookSource) = withContext(Dispatchers.IO) {