Explorar o código

refactor: 清零 TagGroupRuleViewModel DAO 直连

5151561 hai 3 meses
pai
achega
8646d838fa

+ 6 - 0
app/src/main/java/io/legado/app/data/repository/BookRepository.kt

@@ -130,6 +130,12 @@ class BookRepository(
         }
     }
 
+    suspend fun getAll(): List<Book> {
+        return withContext(Dispatchers.IO) {
+            bookDao.getAll()
+        }
+    }
+
     suspend fun replace(oldBook: Book, newBook: Book) {
         withContext(Dispatchers.IO) {
             bookDao.replace(oldBook, newBook)

+ 4 - 0
app/src/main/java/io/legado/app/data/repository/TagGroupRuleRepository.kt

@@ -14,6 +14,10 @@ class TagGroupRuleRepository {
         return dao.flowAll()
     }
 
+    suspend fun getAll(): List<TagGroupRule> = withContext(Dispatchers.IO) {
+        dao.getAll()
+    }
+
     suspend fun insert(vararg rule: TagGroupRule) {
         withContext(Dispatchers.IO) {
             dao.insert(*rule)

+ 7 - 6
app/src/main/java/io/legado/app/ui/tagGroupRule/TagGroupRuleViewModel.kt

@@ -4,8 +4,8 @@ import android.app.Application
 import androidx.lifecycle.viewModelScope
 import io.legado.app.R
 import io.legado.app.base.BaseRuleViewModel
-import io.legado.app.data.appDb
 import io.legado.app.data.entities.TagGroupRule
+import io.legado.app.data.repository.BookRepository
 import io.legado.app.data.repository.TagGroupRuleRepository
 import io.legado.app.data.repository.UploadRepository
 import io.legado.app.help.book.applyTagGroupRules
@@ -29,7 +29,8 @@ import kotlinx.coroutines.withContext
 
 class TagGroupRuleViewModel(
     application: Application,
-    uploadRepository: UploadRepository
+    uploadRepository: UploadRepository,
+    private val bookRepository: BookRepository,
 ) : BaseRuleViewModel<TagGroupRuleItemUi, TagGroupRule, Long, TagGroupRuleUiState>(
     application,
     TagGroupRuleUiState(interaction = InteractionState(isLoading = true)),
@@ -200,8 +201,8 @@ class TagGroupRuleViewModel(
 
     private suspend fun autoApplyRules() {
         withContext(Dispatchers.IO) {
-            val books = appDb.bookDao.getAll()
-            val rules = appDb.tagGroupRuleDao.getAll()
+            val books = bookRepository.getAll()
+            val rules = repository.getAll()
             applyTagGroupRules(books, rules)
         }
     }
@@ -226,8 +227,8 @@ class TagGroupRuleViewModel(
 
     private fun syncGroups() {
         viewModelScope.launch(Dispatchers.IO) {
-            val books = appDb.bookDao.getAll()
-            val rules = appDb.tagGroupRuleDao.getAll()
+            val books = bookRepository.getAll()
+            val rules = repository.getAll()
             applyTagGroupRules(books, rules)
             withContext(Dispatchers.Main) {
                 context.toastOnUi(R.string.tag_group_sync_complete)

+ 0 - 1
build.gradle.kts

@@ -224,7 +224,6 @@ val verifyConfigArchitecture = tasks.register<VerifyConfigArchitectureTask>(
             "io/legado/app/ui/book/manga/ReadMangaViewModel.kt" to 17,
             "io/legado/app/ui/book/read/ReadBookViewModel.kt" to 52,
             "io/legado/app/ui/login/SourceLoginViewModel.kt" to 6,
-            "io/legado/app/ui/tagGroupRule/TagGroupRuleViewModel.kt" to 4,
         )
     )
     // 非 ViewModel 的 UI 层文件直连 DAO 的历史债,只冻结不修复;