From cca075230598f91d2eb4319b57918f0c3871e496 Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 14 Apr 2022 08:48:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=9B=BE=E7=89=87=E7=BB=98?= =?UTF-8?q?=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/api/controller/BookController.kt | 9 +-- .../io/legado/app/ui/book/read/PhotoDialog.kt | 5 +- .../app/ui/book/read/page/ContentTextView.kt | 17 +++-- .../book/read/page/provider/ImageProvider.kt | 69 ++++++++++--------- 4 files changed, 52 insertions(+), 48 deletions(-) diff --git a/app/src/main/java/io/legado/app/api/controller/BookController.kt b/app/src/main/java/io/legado/app/api/controller/BookController.kt index 30f887ed3..0abc52b35 100644 --- a/app/src/main/java/io/legado/app/api/controller/BookController.kt +++ b/app/src/main/java/io/legado/app/api/controller/BookController.kt @@ -21,8 +21,8 @@ import io.legado.app.model.localBook.EpubFile import io.legado.app.model.localBook.LocalBook import io.legado.app.model.localBook.UmdFile import io.legado.app.model.webBook.WebBook -import io.legado.app.utils.* import io.legado.app.ui.book.read.page.provider.ImageProvider +import io.legado.app.utils.* import kotlinx.coroutines.delay import kotlinx.coroutines.runBlocking import splitties.init.appCtx @@ -87,9 +87,10 @@ object BookController { this.bookSource = appDb.bookSourceDao.getBookSource(book.origin) } this.bookUrl = bookUrl - return ImageProvider.getImage(book, src, bookSource, width, width)?.let { - returnData.setData(it) - } ?: returnData.setErrorMsg("图片加载失败或不存在") + val bitmap = runBlocking { + ImageProvider.getImage(book, src, bookSource, width, width) + } + return returnData.setData(bitmap) } /** diff --git a/app/src/main/java/io/legado/app/ui/book/read/PhotoDialog.kt b/app/src/main/java/io/legado/app/ui/book/read/PhotoDialog.kt index c072debc4..635a24c6e 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/PhotoDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/PhotoDialog.kt @@ -40,16 +40,13 @@ class PhotoDialog() : BaseDialogFragment(R.layout.dialog_photo_view) { arguments?.let { val path = it.getString("path") if (path.isNullOrEmpty()) { - val chapterIndex = it.getInt("chapterIndex") val src = it.getString("src") ReadBook.book?.let { book -> src?.let { execute { ImageProvider.getImage(book, src, ReadBook.bookSource) }.onSuccess { bitmap -> - if (bitmap != null) { - binding.photoView.setImageBitmap(bitmap) - } + binding.photoView.setImageBitmap(bitmap) } } } diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt b/app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt index 5ba2d0958..40f02f053 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt @@ -10,6 +10,7 @@ import io.legado.app.R import io.legado.app.constant.PreferKey import io.legado.app.data.entities.Bookmark import io.legado.app.help.config.ReadBookConfig +import io.legado.app.help.coroutine.Coroutine import io.legado.app.lib.theme.accentColor import io.legado.app.model.ReadBook import io.legado.app.ui.book.read.PhotoDialog @@ -167,13 +168,15 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at isImageLine: Boolean ) { val book = ReadBook.book ?: return - ImageProvider.getImage( - book, - textChar.charData, - ReadBook.bookSource, - (textChar.end - textChar.start).toInt(), - (lineBottom - lineTop).toInt() - )?.let { bitmap -> + Coroutine.async { + ImageProvider.getImage( + book, + textChar.charData, + ReadBook.bookSource, + (textChar.end - textChar.start).toInt(), + (lineBottom - lineTop).toInt() + ) + }.onSuccess { bitmap -> val rectF = if (isImageLine) { RectF(textChar.start, lineTop, textChar.end, lineBottom) } else { diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ImageProvider.kt b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ImageProvider.kt index 605fc5eff..a0a1bcc36 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ImageProvider.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ImageProvider.kt @@ -13,8 +13,9 @@ import io.legado.app.help.glide.ImageLoader import io.legado.app.model.localBook.EpubFile import io.legado.app.utils.FileUtils import io.legado.app.utils.isXml -import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.suspendCancellableCoroutine +import kotlinx.coroutines.withContext import splitties.init.appCtx import java.io.File import java.io.FileOutputStream @@ -66,53 +67,55 @@ object ImageProvider { } } - fun getImage( + suspend fun getImage( book: Book, src: String, bookSource: BookSource?, width: Int, height: Int - ): Bitmap? { - val vFile = runBlocking { - cacheImage(book, src, bookSource) - } - return try { - ImageLoader.loadBitmap(appCtx, vFile.absolutePath) - .submit(width, height) - .get() - } catch (e: Exception) { - Coroutine.async { - putDebug("${vFile.absolutePath} 解码失败\n${e.toString()}", e) - if (FileUtils.readText(vFile.absolutePath).isXml()) { - putDebug("${vFile.absolutePath}为xml,自动删除") - vFile.delete() + ): Bitmap { + return withContext(IO) { + val vFile = cacheImage(book, src, bookSource) + try { + @Suppress("BlockingMethodInNonBlockingContext") + ImageLoader.loadBitmap(appCtx, vFile.absolutePath) + .submit(width, height) + .get() + } catch (e: Exception) { + Coroutine.async { + putDebug("${vFile.absolutePath} 解码失败\n$e", e) + if (FileUtils.readText(vFile.absolutePath).isXml()) { + putDebug("${vFile.absolutePath}为xml,自动删除") + vFile.delete() + } } + errorBitmap } - errorBitmap } } - fun getImage( + suspend fun getImage( book: Book, src: String, bookSource: BookSource? - ): Bitmap? { - val vFile = runBlocking { - cacheImage(book, src, bookSource) - } - return try { - ImageLoader.loadBitmap(appCtx, vFile.absolutePath) - .submit(ChapterProvider.visibleWidth, ChapterProvider.visibleHeight) - .get() - } catch (e: Exception) { - Coroutine.async { - putDebug("${vFile.absolutePath} 解码失败\n${e.toString()}", e) - if (FileUtils.readText(vFile.absolutePath).isXml()) { - putDebug("${vFile.absolutePath}为xml,自动删除") - vFile.delete() + ): Bitmap { + return withContext(IO) { + val vFile = cacheImage(book, src, bookSource) + try { + @Suppress("BlockingMethodInNonBlockingContext") + ImageLoader.loadBitmap(appCtx, vFile.absolutePath) + .submit(ChapterProvider.visibleWidth, ChapterProvider.visibleHeight) + .get() + } catch (e: Exception) { + Coroutine.async { + putDebug("${vFile.absolutePath} 解码失败\n$e", e) + if (FileUtils.readText(vFile.absolutePath).isXml()) { + putDebug("${vFile.absolutePath}为xml,自动删除") + vFile.delete() + } } + errorBitmap } - errorBitmap } }