From 2cfec7e245c65e424c8486f0c00cb96175058695 Mon Sep 17 00:00:00 2001 From: gedoor Date: Sun, 18 Jul 2021 23:00:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/help/ContentProcessor.kt | 27 ++++++++++++++++++- .../io/legado/app/service/help/ReadBook.kt | 2 +- .../association/ImportBookSourceViewModel.kt | 2 ++ .../app/ui/book/cache/CacheViewModel.kt | 4 +-- .../searchContent/SearchContentViewModel.kt | 2 +- .../app/ui/replace/ReplaceRuleActivity.kt | 4 +-- 6 files changed, 34 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/ContentProcessor.kt b/app/src/main/java/io/legado/app/help/ContentProcessor.kt index af19c11ca..5dbb39297 100644 --- a/app/src/main/java/io/legado/app/help/ContentProcessor.kt +++ b/app/src/main/java/io/legado/app/help/ContentProcessor.kt @@ -6,8 +6,33 @@ import io.legado.app.data.entities.Book import io.legado.app.data.entities.ReplaceRule import io.legado.app.utils.toastOnUi import splitties.init.appCtx +import java.lang.ref.WeakReference -class ContentProcessor(private val bookName: String, private val bookOrigin: String) { +class ContentProcessor private constructor( + private val bookName: String, + private val bookOrigin: String +) { + + companion object { + private val processors = hashMapOf>() + + fun get(bookName: String, bookOrigin: String): ContentProcessor { + val processorWr = processors[bookName + bookOrigin] + var processor: ContentProcessor? = processorWr?.get() + if (processor == null) { + processor = ContentProcessor(bookName, bookOrigin) + processors[bookName + bookOrigin] = WeakReference(processor) + } + return processor + } + + fun upReplaceRules() { + processors.forEach { + it.value.get()?.upReplaceRules() + } + } + + } private var replaceRules = arrayListOf() diff --git a/app/src/main/java/io/legado/app/service/help/ReadBook.kt b/app/src/main/java/io/legado/app/service/help/ReadBook.kt index c1d6fc2ca..b0202682b 100644 --- a/app/src/main/java/io/legado/app/service/help/ReadBook.kt +++ b/app/src/main/java/io/legado/app/service/help/ReadBook.kt @@ -46,7 +46,7 @@ object ReadBook { fun resetData(book: Book) { this.book = book - contentProcessor = ContentProcessor(book.name, book.origin) + contentProcessor = ContentProcessor.get(book.name, book.origin) readRecord.bookName = book.name readRecord.readTime = appDb.readRecordDao.getReadTime(book.name) ?: 0 durChapterIndex = book.durChapterIndex diff --git a/app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt index 4cadba770..0659487ad 100644 --- a/app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt @@ -8,6 +8,7 @@ import io.legado.app.base.BaseViewModel import io.legado.app.data.appDb import io.legado.app.data.entities.BookSource import io.legado.app.help.AppConfig +import io.legado.app.help.ContentProcessor import io.legado.app.help.SourceHelp import io.legado.app.help.http.newCall import io.legado.app.help.http.okHttpClient @@ -67,6 +68,7 @@ class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) { } } SourceHelp.insertBookSource(*selectSource.toTypedArray()) + ContentProcessor.upReplaceRules() }.onFinally { finally.invoke() } diff --git a/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt b/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt index 541200142..8e6019c1c 100644 --- a/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt @@ -122,7 +122,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { private fun getAllContents(book: Book, append: (text: String) -> Unit) { val useReplace = AppConfig.exportUseReplace - val contentProcessor = ContentProcessor(book.name, book.origin) + val contentProcessor = ContentProcessor.get(book.name, book.origin) append( "${book.name}\n${ context.getString( @@ -386,7 +386,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { private fun setEpubContent(contentModel: String, book: Book, epubBook: EpubBook) { //正文 val useReplace = AppConfig.exportUseReplace - val contentProcessor = ContentProcessor(book.name, book.origin) + val contentProcessor = ContentProcessor.get(book.name, book.origin) appDb.bookChapterDao.getChapterList(book.bookUrl).forEachIndexed { index, chapter -> BookHelp.getContent(book, chapter).let { content -> var content1 = fixPic(epubBook, book, content ?: "null", chapter) diff --git a/app/src/main/java/io/legado/app/ui/book/searchContent/SearchContentViewModel.kt b/app/src/main/java/io/legado/app/ui/book/searchContent/SearchContentViewModel.kt index 94cb50d51..121248738 100644 --- a/app/src/main/java/io/legado/app/ui/book/searchContent/SearchContentViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/searchContent/SearchContentViewModel.kt @@ -18,7 +18,7 @@ class SearchContentViewModel(application: Application) : BaseViewModel(applicati execute { book = appDb.bookDao.getBook(bookUrl) book?.let { - contentProcessor = ContentProcessor(it.name, it.origin) + contentProcessor = ContentProcessor.get(it.name, it.origin) } }.onSuccess { success.invoke() diff --git a/app/src/main/java/io/legado/app/ui/replace/ReplaceRuleActivity.kt b/app/src/main/java/io/legado/app/ui/replace/ReplaceRuleActivity.kt index 2fc737bd9..56ff1257e 100644 --- a/app/src/main/java/io/legado/app/ui/replace/ReplaceRuleActivity.kt +++ b/app/src/main/java/io/legado/app/ui/replace/ReplaceRuleActivity.kt @@ -21,12 +21,12 @@ import io.legado.app.data.appDb import io.legado.app.data.entities.ReplaceRule import io.legado.app.databinding.ActivityReplaceRuleBinding import io.legado.app.databinding.DialogEditTextBinding +import io.legado.app.help.ContentProcessor import io.legado.app.help.IntentDataHelp import io.legado.app.help.coroutine.Coroutine import io.legado.app.lib.dialogs.alert import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.primaryTextColor -import io.legado.app.service.help.ReadBook import io.legado.app.ui.association.ImportReplaceRuleActivity import io.legado.app.ui.document.FilePicker import io.legado.app.ui.document.FilePickerParam @@ -294,7 +294,7 @@ class ReplaceRuleActivity : VMBaseActivity