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

refactor: 清零书籍导入 DAO 直连

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

+ 36 - 0
app/src/main/java/io/legado/app/data/repository/BookImportRepository.kt

@@ -0,0 +1,36 @@
+package io.legado.app.data.repository
+
+import io.legado.app.data.AppDatabase
+import io.legado.app.data.entities.Book
+import io.legado.app.help.book.BookHelp
+import io.legado.app.help.book.canSafelyRebindTo
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.flow.Flow
+import kotlinx.coroutines.flow.flowOn
+import kotlinx.coroutines.withContext
+
+class BookImportRepository(
+    private val appDb: AppDatabase,
+) {
+
+    fun flowLocalBooks(): Flow<List<Book>> = appDb.bookDao.flowLocal().flowOn(Dispatchers.IO)
+
+    suspend fun findByFileName(fileName: String): Book? = withContext(Dispatchers.IO) {
+        appDb.bookDao.getBookByFileName(fileName)
+    }
+
+    suspend fun findAndRebind(fileName: String, filePath: String): Book? =
+        withContext(Dispatchers.IO) {
+            val book = appDb.bookDao.getBookByFileName(fileName) ?: return@withContext null
+            if (book.bookUrl == filePath || !book.canSafelyRebindTo(filePath)) {
+                return@withContext book
+            }
+
+            val reboundBook = book.copy(bookUrl = filePath)
+            appDb.runInTransaction {
+                appDb.bookDao.replace(book, reboundBook)
+                BookHelp.updateCacheFolder(book, reboundBook)
+            }
+            reboundBook
+        }
+}

+ 2 - 0
app/src/main/java/io/legado/app/di/appModule.kt

@@ -27,6 +27,7 @@ import io.legado.app.data.repository.BookDomainRepositoryImpl
 import io.legado.app.data.repository.BookExportSettingsRepository
 import io.legado.app.data.repository.BookGroupMutationRepository
 import io.legado.app.data.repository.BookGroupRepository
+import io.legado.app.data.repository.BookImportRepository
 import io.legado.app.data.repository.BookKnowledgeRepository
 import io.legado.app.data.repository.BookRepository
 import io.legado.app.data.repository.BookSourceCallbackRepository
@@ -302,6 +303,7 @@ val appModule = module {
     singleOf(::ReadRecordRepository)
     single<HomeDashboardGateway> { HomeDashboardRepository(get(), get()) }
     singleOf(::BookRepository)
+    singleOf(::BookImportRepository)
     singleOf(::BookGroupRepository)
     singleOf(::BookmarkRepository)
     singleOf(::BookCacheManageRepository)

+ 13 - 19
app/src/main/java/io/legado/app/ui/book/import/local/ImportBookViewModel.kt

@@ -13,14 +13,12 @@ import io.legado.app.constant.AppLog
 import io.legado.app.constant.AppPattern
 import io.legado.app.constant.AppPattern.archiveFileRegex
 import io.legado.app.constant.AppPattern.bookFileRegex
-import io.legado.app.data.appDb
 import io.legado.app.data.entities.Book
+import io.legado.app.data.repository.BookImportRepository
 import io.legado.app.domain.gateway.ImportBookSettingsGateway
 import io.legado.app.domain.gateway.OtherSettingsGateway
 import io.legado.app.domain.model.settings.ImportBookSettings
 import io.legado.app.domain.model.settings.OtherSettings
-import io.legado.app.help.book.BookHelp
-import io.legado.app.help.book.canSafelyRebindTo
 import io.legado.app.model.localBook.LocalBook
 import io.legado.app.ui.widget.components.list.InteractionState
 import io.legado.app.ui.widget.components.list.ListUiState
@@ -118,6 +116,7 @@ class ImportBookViewModel(
     application: Application,
     private val importBookSettingsGateway: ImportBookSettingsGateway,
     private val otherSettingsGateway: OtherSettingsGateway,
+    private val repository: BookImportRepository,
 ) : BaseViewModel(application) {
 
     private enum class SourceMode {
@@ -191,7 +190,7 @@ class ImportBookViewModel(
 
     val uiState = combine(
         _state,
-        appDb.bookDao.flowLocal(),
+        repository.flowLocalBooks(),
         importBookSettingsGateway.settings,
     ) { state, localBooks, settings ->
         val localFileNames = localBooks.asSequence().map { it.originName }.toSet()
@@ -605,19 +604,10 @@ class ImportBookViewModel(
 
     private fun onImportedFileClick(fileDoc: FileDoc) {
         if (!ArchiveUtils.isArchive(fileDoc.name)) {
-            appDb.bookDao.getBookByFileName(fileDoc.name)?.let { book ->
-                val filePath = fileDoc.toString()
-                val bookToOpen = if (book.bookUrl != filePath && book.canSafelyRebindTo(filePath)) {
-                    val reboundBook = book.copy(bookUrl = filePath)
-                    appDb.runInTransaction {
-                        appDb.bookDao.replace(book, reboundBook)
-                        BookHelp.updateCacheFolder(book, reboundBook)
-                    }
-                    reboundBook
-                } else {
-                    book
+            viewModelScope.launch {
+                repository.findAndRebind(fileDoc.name, fileDoc.toString())?.let { book ->
+                    _effects.tryEmit(ImportBookEffect.OpenBook(book))
                 }
-                _effects.tryEmit(ImportBookEffect.OpenBook(bookToOpen))
             }
             return
         }
@@ -635,9 +625,13 @@ class ImportBookViewModel(
     }
 
     private fun onArchiveEntrySelected(fileDoc: FileDoc, fileName: String) {
-        appDb.bookDao.getBookByFileName(fileName)?.let {
-            _effects.tryEmit(ImportBookEffect.OpenBook(it))
-        } ?: _effects.tryEmit(ImportBookEffect.ShowImportArchiveDialog(fileDoc, fileName))
+        viewModelScope.launch {
+            val book = repository.findByFileName(fileName)
+            _effects.tryEmit(
+                book?.let(ImportBookEffect::OpenBook)
+                    ?: ImportBookEffect.ShowImportArchiveDialog(fileDoc, fileName)
+            )
+        }
     }
 
     private fun addArchiveToBookShelf(fileDoc: FileDoc, fileName: String) {

+ 9 - 8
app/src/main/java/io/legado/app/ui/book/import/remote/RemoteBookViewModel.kt

@@ -9,9 +9,9 @@ import io.legado.app.base.BaseViewModel
 import io.legado.app.constant.AppConst
 import io.legado.app.constant.AppLog
 import io.legado.app.constant.BookType
-import io.legado.app.data.appDb
 import io.legado.app.data.entities.Book
 import io.legado.app.data.entities.Server
+import io.legado.app.data.repository.BookImportRepository
 import io.legado.app.data.repository.RemoteBookRepository
 import io.legado.app.domain.gateway.ImportBookSettingsGateway
 import io.legado.app.domain.gateway.OtherSettingsGateway
@@ -113,6 +113,7 @@ class RemoteBookViewModel(
     @Suppress("UNUSED_PARAMETER")
     private val savedStateHandle: SavedStateHandle,
     private val repository: RemoteBookRepository,
+    private val bookImportRepository: BookImportRepository,
     private val importBookSettingsGateway: ImportBookSettingsGateway,
     private val otherSettingsGateway: OtherSettingsGateway,
 ) : BaseViewModel(application) {
@@ -314,9 +315,7 @@ class RemoteBookViewModel(
     }
 
     suspend fun getLocalBook(fileName: String): Book? {
-        return withContext(Dispatchers.IO) {
-            appDb.bookDao.getBookByFileName(fileName)
-        }
+        return bookImportRepository.findByFileName(fileName)
     }
 
     fun navigateToDir(book: RemoteBook) {
@@ -466,10 +465,12 @@ class RemoteBookViewModel(
     }
 
     private fun onArchiveEntrySelected(fileDoc: FileDoc, fileName: String) {
-        appDb.bookDao.getBookByFileName(fileName)?.let { book ->
-            viewModelScope.launch { _effects.emit(RemoteBookEffect.OpenBook(book)) }
-        } ?: viewModelScope.launch {
-            _effects.emit(RemoteBookEffect.ShowImportArchiveDialog(fileDoc, fileName))
+        viewModelScope.launch {
+            val book = bookImportRepository.findByFileName(fileName)
+            _effects.emit(
+                book?.let(RemoteBookEffect::OpenBook)
+                    ?: RemoteBookEffect.ShowImportArchiveDialog(fileDoc, fileName)
+            )
         }
     }
 

+ 0 - 2
build.gradle.kts

@@ -203,8 +203,6 @@ val verifyConfigArchitecture = tasks.register<VerifyConfigArchitectureTask>(
             "io/legado/app/ui/book/changecover/ChangeCoverViewModel.kt" to 3,
             "io/legado/app/ui/book/changesource/ChangeBookSourceViewModel.kt" to 20,
             "io/legado/app/ui/book/changesource/ChangeChapterSourceViewModel.kt" to 5,
-            "io/legado/app/ui/book/import/local/ImportBookViewModel.kt" to 4,
-            "io/legado/app/ui/book/import/remote/RemoteBookViewModel.kt" to 2,
             "io/legado/app/ui/book/info/BookInfoViewModel.kt" to 34,
             "io/legado/app/ui/book/info/edit/BookInfoEditViewModel.kt" to 2,
             "io/legado/app/ui/book/manga/ReadMangaViewModel.kt" to 17,