下载图片采用多线程

pull/1486/head
gedoor 3 years ago
parent a099cab6b3
commit 0f63f81411
  1. 33
      app/src/main/java/io/legado/app/help/BookHelp.kt
  2. 2
      app/src/main/java/io/legado/app/model/webBook/BookContent.kt

@ -10,6 +10,9 @@ import io.legado.app.help.coroutine.Coroutine
import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.model.localBook.LocalBook import io.legado.app.model.localBook.LocalBook
import io.legado.app.utils.* import io.legado.app.utils.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.async
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import org.apache.commons.text.similarity.JaccardSimilarity import org.apache.commons.text.similarity.JaccardSimilarity
import splitties.init.appCtx import splitties.init.appCtx
@ -57,7 +60,19 @@ object BookHelp {
} }
} }
fun saveContent( suspend fun saveContent(
scope: CoroutineScope,
bookSource: BookSource,
book: Book,
bookChapter: BookChapter,
content: String
) {
saveText(book, bookChapter, content)
saveImages(scope, bookSource, book, bookChapter, content)
postEvent(EventBus.SAVE_CONTENT, bookChapter)
}
private fun saveText(
book: Book, book: Book,
bookChapter: BookChapter, bookChapter: BookChapter,
content: String content: String
@ -72,24 +87,28 @@ object BookHelp {
).writeText(content) ).writeText(content)
} }
suspend fun saveContent( private suspend fun saveImages(
scope: CoroutineScope,
bookSource: BookSource, bookSource: BookSource,
book: Book, book: Book,
bookChapter: BookChapter, bookChapter: BookChapter,
content: String content: String
) { ) {
saveContent(book, bookChapter, content) val awaitList = arrayListOf<Deferred<Unit>>()
//保存图片
content.split("\n").forEach { content.split("\n").forEach {
val matcher = AppPattern.imgPattern.matcher(it) val matcher = AppPattern.imgPattern.matcher(it)
if (matcher.find()) { if (matcher.find()) {
matcher.group(1)?.let { src -> matcher.group(1)?.let { src ->
val mSrc = NetworkUtils.getAbsoluteURL(bookChapter.url, src) val mSrc = NetworkUtils.getAbsoluteURL(bookChapter.url, src)
saveImage(bookSource, book, mSrc) awaitList.add(scope.async {
saveImage(bookSource, book, mSrc)
})
} }
} }
} }
postEvent(EventBus.SAVE_CONTENT, bookChapter) awaitList.forEach {
it.await()
}
} }
suspend fun saveImage(bookSource: BookSource?, book: Book, src: String) { suspend fun saveImage(bookSource: BookSource?, book: Book, src: String) {
@ -194,7 +213,7 @@ object BookHelp {
} else if (book.isEpub() && !hasContent(book, bookChapter)) { } else if (book.isEpub() && !hasContent(book, bookChapter)) {
val string = LocalBook.getContent(book, bookChapter) val string = LocalBook.getContent(book, bookChapter)
string?.let { string?.let {
saveContent(book, bookChapter, it) saveText(book, bookChapter, it)
} }
return string return string
} else { } else {

@ -121,7 +121,7 @@ object BookContent {
if (contentStr.isBlank()) { if (contentStr.isBlank()) {
throw ContentEmptyException("内容为空") throw ContentEmptyException("内容为空")
} }
BookHelp.saveContent(bookSource, book, bookChapter, contentStr) BookHelp.saveContent(scope, bookSource, book, bookChapter, contentStr)
return contentStr return contentStr
} }

Loading…
Cancel
Save