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

refactor: 清零 ReadMangaViewModel DAO 直连

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

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

@@ -142,6 +142,12 @@ class BookRepository(
         }
         }
     }
     }
 
 
+    suspend fun getLastReadBook(): Book? {
+        return withContext(Dispatchers.IO) {
+            bookDao.lastReadBook
+        }
+    }
+
     suspend fun replace(oldBook: Book, newBook: Book) {
     suspend fun replace(oldBook: Book, newBook: Book) {
         withContext(Dispatchers.IO) {
         withContext(Dispatchers.IO) {
             bookDao.replace(oldBook, newBook)
             bookDao.replace(oldBook, newBook)

+ 7 - 0
app/src/main/java/io/legado/app/data/repository/BookSourceRepository.kt

@@ -87,6 +87,13 @@ class BookSourceRepository(private val bookSourceDao: BookSourceDao) {
             )
             )
         }
         }
 
 
+    suspend fun getAllTextEnabledPart(): List<BookSourcePart> {
+        return withContext(Dispatchers.IO) {
+            bookSourceDao.allTextEnabledPart
+        }
+    }
+
+
     suspend fun topSources(sources: List<BookSourcePart>) = withContext(Dispatchers.IO) {
     suspend fun topSources(sources: List<BookSourcePart>) = withContext(Dispatchers.IO) {
         val minOrder = bookSourceDao.minOrder - 1
         val minOrder = bookSourceDao.minOrder - 1
         val reorderedSources = sources.sortedBy { it.customOrder }.mapIndexed { index, source ->
         val reorderedSources = sources.sortedBy { it.customOrder }.mapIndexed { index, source ->

+ 21 - 18
app/src/main/java/io/legado/app/ui/book/manga/ReadMangaViewModel.kt

@@ -10,11 +10,12 @@ import io.legado.app.base.BaseViewModel
 import io.legado.app.constant.AppLog
 import io.legado.app.constant.AppLog
 import io.legado.app.constant.BookType
 import io.legado.app.constant.BookType
 import io.legado.app.constant.EventBus
 import io.legado.app.constant.EventBus
-import io.legado.app.data.appDb
 import io.legado.app.data.entities.Book
 import io.legado.app.data.entities.Book
 import io.legado.app.data.entities.Book.ReadConfig
 import io.legado.app.data.entities.Book.ReadConfig
 import io.legado.app.data.entities.BookChapter
 import io.legado.app.data.entities.BookChapter
 import io.legado.app.data.entities.BookProgress
 import io.legado.app.data.entities.BookProgress
+import io.legado.app.data.repository.BookRepository
+import io.legado.app.data.repository.BookSourceRepository
 import io.legado.app.domain.model.ReadingProgress
 import io.legado.app.domain.model.ReadingProgress
 import io.legado.app.domain.gateway.MangaSettingsGateway
 import io.legado.app.domain.gateway.MangaSettingsGateway
 import io.legado.app.domain.gateway.OtherSettingsGateway
 import io.legado.app.domain.gateway.OtherSettingsGateway
@@ -59,6 +60,8 @@ class ReadMangaViewModel(
     private val mangaSettingsGateway: MangaSettingsGateway,
     private val mangaSettingsGateway: MangaSettingsGateway,
     private val otherSettingsGateway: OtherSettingsGateway,
     private val otherSettingsGateway: OtherSettingsGateway,
     private val readSettingsGateway: ReadSettingsGateway,
     private val readSettingsGateway: ReadSettingsGateway,
+    private val bookRepository: BookRepository,
+    private val bookSourceRepository: BookSourceRepository,
 ) : BaseViewModel(application) {
 ) : BaseViewModel(application) {
 
 
     private val _mangaSettings = MutableStateFlow(mangaSettingsGateway.currentSettings)
     private val _mangaSettings = MutableStateFlow(mangaSettingsGateway.currentSettings)
@@ -85,8 +88,8 @@ class ReadMangaViewModel(
             ReadManga.chapterChanged = intent.getBooleanExtra("chapterChanged", false)
             ReadManga.chapterChanged = intent.getBooleanExtra("chapterChanged", false)
             val bookUrl = intent.getStringExtra("bookUrl")
             val bookUrl = intent.getStringExtra("bookUrl")
             val book = when {
             val book = when {
-                bookUrl.isNullOrEmpty() -> appDb.bookDao.lastReadBook
-                else -> appDb.bookDao.getBook(bookUrl)
+                bookUrl.isNullOrEmpty() -> bookRepository.getLastReadBook()
+                else -> bookRepository.getBook(bookUrl)
             } ?: ReadManga.book
             } ?: ReadManga.book
             when {
             when {
                 book != null -> initManga(book)
                 book != null -> initManga(book)
@@ -150,7 +153,7 @@ class ReadMangaViewModel(
     fun upBookSource(success: (() -> Unit)?) {
     fun upBookSource(success: (() -> Unit)?) {
         execute {
         execute {
             ReadManga.book?.let { book ->
             ReadManga.book?.let { book ->
-                ReadManga.bookSource = appDb.bookSourceDao.getBookSource(book.origin)
+                ReadManga.bookSource = bookSourceRepository.getBookSource(book.origin)
             }
             }
         }.onSuccess {
         }.onSuccess {
             success?.invoke()
             success?.invoke()
@@ -167,7 +170,7 @@ class ReadMangaViewModel(
         execute {
         execute {
             ReadManga.bookSource?.let {
             ReadManga.bookSource?.let {
                 it.enabled = false
                 it.enabled = false
-                appDb.bookSourceDao.update(it)
+                bookSourceRepository.updateSources(it)
             }
             }
         }
         }
     }
     }
@@ -177,13 +180,13 @@ class ReadMangaViewModel(
             val oldBook = book.copy()
             val oldBook = book.copy()
             WebBook.getChapterListAwait(it, book, true).onSuccess { cList ->
             WebBook.getChapterListAwait(it, book, true).onSuccess { cList ->
                 if (oldBook.bookUrl == book.bookUrl) {
                 if (oldBook.bookUrl == book.bookUrl) {
-                    appDb.bookDao.update(book)
+                    bookRepository.update(book)
                 } else {
                 } else {
-                    appDb.bookDao.replace(oldBook, book)
+                    bookRepository.replace(oldBook, book)
                     BookHelp.updateCacheFolder(oldBook, book)
                     BookHelp.updateCacheFolder(oldBook, book)
                 }
                 }
-                appDb.bookChapterDao.delByBook(oldBook.bookUrl)
-                appDb.bookChapterDao.insert(*cList.toTypedArray())
+                bookRepository.deleteChaptersByBook(oldBook.bookUrl)
+                bookRepository.insertChapters(*cList.toTypedArray())
                 ReadManga.onChapterListUpdated(book)
                 ReadManga.onChapterListUpdated(book)
                 return true
                 return true
             }.onFailure {
             }.onFailure {
@@ -217,7 +220,7 @@ class ReadMangaViewModel(
     private fun autoChangeSource(name: String, author: String) {
     private fun autoChangeSource(name: String, author: String) {
         if (!GlobalReadConfig.autoChangeSource) return
         if (!GlobalReadConfig.autoChangeSource) return
         execute {
         execute {
-            val sources = appDb.bookSourceDao.allTextEnabledPart
+            val sources = bookSourceRepository.getAllTextEnabledPart()
             flow {
             flow {
                 for (source in sources) {
                 for (source in sources) {
                     source.getBookSource()?.let {
                     source.getBookSource()?.let {
@@ -309,8 +312,8 @@ class ReadMangaViewModel(
             )
             )
             book.removeType(BookType.updateError)
             book.removeType(BookType.updateError)
             ReadManga.book?.delete()
             ReadManga.book?.delete()
-            appDb.bookDao.insert(book)
-            appDb.bookChapterDao.insert(*toc.toTypedArray())
+            bookRepository.insert(book)
+            bookRepository.insertChapters(*toc.toTypedArray())
             ReadManga.resetData(book)
             ReadManga.resetData(book)
             ReadManga.loadContent()
             ReadManga.loadContent()
         }.onError {
         }.onError {
@@ -411,7 +414,7 @@ class ReadMangaViewModel(
         book.readConfig = config
         book.readConfig = config
 
 
         execute {
         execute {
-            appDb.bookDao.update(book)
+            bookRepository.update(book)
         }.onError {
         }.onError {
             AppLog.put("更新阅读配置失败\n${it.localizedMessage}", it)
             AppLog.put("更新阅读配置失败\n${it.localizedMessage}", it)
         }
         }
@@ -424,7 +427,7 @@ class ReadMangaViewModel(
 
 
     fun refreshContentDur(book: Book) {
     fun refreshContentDur(book: Book) {
         execute {
         execute {
-            appDb.bookChapterDao.getChapter(book.bookUrl, ReadManga.durChapterIndex)
+            bookRepository.getChapter(book.bookUrl, ReadManga.durChapterIndex)
                 ?.let { chapter ->
                 ?.let { chapter ->
                     BookHelp.delContent(book, chapter)
                     BookHelp.delContent(book, chapter)
                     openChapter(ReadManga.durChapterIndex, ReadManga.durChapterPos)
                     openChapter(ReadManga.durChapterIndex, ReadManga.durChapterPos)
@@ -438,7 +441,7 @@ class ReadMangaViewModel(
     ) {
     ) {
         val book = ReadManga.book ?: return onFailure()
         val book = ReadManga.book ?: return onFailure()
         execute {
         execute {
-            val toc = appDb.bookChapterDao.getChapterList(book.bookUrl)
+            val toc = bookRepository.getChapters(book.bookUrl)
             persistOnBookshelf(book, toc)
             persistOnBookshelf(book, toc)
             ReadManga.inBookshelf = true
             ReadManga.inBookshelf = true
         }.onSuccess {
         }.onSuccess {
@@ -464,9 +467,9 @@ class ReadMangaViewModel(
     private suspend fun persistOnBookshelf(book: Book, toc: List<BookChapter>) {
     private suspend fun persistOnBookshelf(book: Book, toc: List<BookChapter>) {
         book.removeType(BookType.notShelf)
         book.removeType(BookType.notShelf)
         if (book.order == 0) {
         if (book.order == 0) {
-            book.order = appDb.bookDao.minOrder - 1
+            book.order = bookRepository.getMinOrder() - 1
         }
         }
-        appDb.bookDao.insert(book)
-        appDb.bookChapterDao.insert(*toc.toTypedArray())
+        bookRepository.insert(book)
+        bookRepository.insertChapters(*toc.toTypedArray())
     }
     }
 }
 }

+ 0 - 1
build.gradle.kts

@@ -220,7 +220,6 @@ val verifyConfigArchitecture = tasks.register<VerifyConfigArchitectureTask>(
     legacyDaoInjectionBaseline.set(
     legacyDaoInjectionBaseline.set(
         mapOf(
         mapOf(
             "io/legado/app/ui/book/info/BookInfoViewModel.kt" to 34,
             "io/legado/app/ui/book/info/BookInfoViewModel.kt" to 34,
-            "io/legado/app/ui/book/manga/ReadMangaViewModel.kt" to 17,
             "io/legado/app/ui/book/read/ReadBookViewModel.kt" to 52,
             "io/legado/app/ui/book/read/ReadBookViewModel.kt" to 52,
         )
         )
     )
     )