diff --git a/app/src/main/java/io/legado/app/help/glide/OkHttpStreamFetcher.kt b/app/src/main/java/io/legado/app/help/glide/OkHttpStreamFetcher.kt index 1bc86e656..cb2b874cc 100644 --- a/app/src/main/java/io/legado/app/help/glide/OkHttpStreamFetcher.kt +++ b/app/src/main/java/io/legado/app/help/glide/OkHttpStreamFetcher.kt @@ -8,21 +8,21 @@ import com.bumptech.glide.load.data.DataFetcher import com.bumptech.glide.load.model.GlideUrl import com.bumptech.glide.util.ContentLengthInputStream import com.bumptech.glide.util.Preconditions -import io.legado.app.data.appDb import io.legado.app.data.entities.BaseSource import io.legado.app.exception.NoStackTraceException import io.legado.app.help.http.addHeaders import io.legado.app.help.http.okHttpClient -import io.legado.app.utils.isWifiConnect +import io.legado.app.help.source.SourceHelp import io.legado.app.utils.ImageUtils +import io.legado.app.utils.isWifiConnect import okhttp3.Call import okhttp3.Request import okhttp3.Response import okhttp3.ResponseBody import splitties.init.appCtx +import java.io.ByteArrayInputStream import java.io.IOException import java.io.InputStream -import java.io.ByteArrayInputStream class OkHttpStreamFetcher(private val url: GlideUrl, private val options: Options) : @@ -44,8 +44,7 @@ class OkHttpStreamFetcher(private val url: GlideUrl, private val options: Option val requestBuilder: Request.Builder = Request.Builder().url(url.toStringUrl()) val headerMap = HashMap() options.get(OkHttpModelLoader.sourceOriginOption)?.let { sourceUrl -> - source = appDb.bookSourceDao.getBookSource(sourceUrl) - ?: appDb.rssSourceDao.getByKey(sourceUrl) + source = SourceHelp.getSource(sourceUrl) source?.getHeaderMap(true)?.let { headerMap.putAll(it) } diff --git a/app/src/main/java/io/legado/app/help/source/SourceHelp.kt b/app/src/main/java/io/legado/app/help/source/SourceHelp.kt index 17fdea0da..3ca560c45 100644 --- a/app/src/main/java/io/legado/app/help/source/SourceHelp.kt +++ b/app/src/main/java/io/legado/app/help/source/SourceHelp.kt @@ -3,6 +3,7 @@ package io.legado.app.help.source import android.os.Handler import android.os.Looper import io.legado.app.data.appDb +import io.legado.app.data.entities.BaseSource import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.RssSource import io.legado.app.help.config.AppConfig @@ -24,6 +25,12 @@ object SourceHelp { } } + fun getSource(key: String?): BaseSource? { + key ?: return null + return appDb.bookSourceDao.getBookSource(key) + ?: appDb.rssSourceDao.getByKey(key) + } + fun insertRssSource(vararg rssSources: RssSource) { rssSources.forEach { rssSource -> if (is18Plus(rssSource.sourceUrl)) { diff --git a/app/src/main/java/io/legado/app/ui/association/VerificationCodeDialog.kt b/app/src/main/java/io/legado/app/ui/association/VerificationCodeDialog.kt index 8ecda5f0b..4e3838996 100644 --- a/app/src/main/java/io/legado/app/ui/association/VerificationCodeDialog.kt +++ b/app/src/main/java/io/legado/app/ui/association/VerificationCodeDialog.kt @@ -16,11 +16,15 @@ import io.legado.app.help.glide.ImageLoader import io.legado.app.help.glide.OkHttpModelLoader import io.legado.app.help.source.SourceVerificationHelp import io.legado.app.lib.theme.primaryColor +import io.legado.app.ui.book.read.page.provider.ImageProvider import io.legado.app.ui.widget.dialog.PhotoDialog import io.legado.app.utils.applyTint import io.legado.app.utils.setLayout import io.legado.app.utils.showDialogFragment import io.legado.app.utils.viewbindingdelegate.viewBinding +import kotlinx.coroutines.Dispatchers.IO +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext /** * 图片验证码对话框 @@ -50,7 +54,6 @@ class VerificationCodeDialog() : BaseDialogFragment(R.layout.dialog_verification setLayout(1f, ViewGroup.LayoutParams.WRAP_CONTENT) } - @SuppressLint("CheckResult") override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { initMenu() binding.run { @@ -59,16 +62,7 @@ class VerificationCodeDialog() : BaseDialogFragment(R.layout.dialog_verification toolBar.subtitle = arguments.getString("sourceName") val sourceOrigin = arguments.getString("sourceOrigin") arguments.getString("imageUrl")?.let { imageUrl -> - ImageLoader.load(requireContext(), imageUrl).apply { - sourceOrigin?.let { - apply( - RequestOptions().set(OkHttpModelLoader.sourceOriginOption, it) - ) - } - }.error(R.drawable.image_loading_error) - .diskCacheStrategy(DiskCacheStrategy.NONE) - //.skipMemoryCache(true) - .into(verificationCodeImageView) + loadImage(imageUrl, sourceOrigin) verificationCodeImageView.setOnClickListener { showDialogFragment(PhotoDialog(imageUrl, sourceOrigin)) } @@ -83,6 +77,28 @@ class VerificationCodeDialog() : BaseDialogFragment(R.layout.dialog_verification binding.toolBar.menu.applyTint(requireContext()) } + @SuppressLint("CheckResult") + private fun loadImage(url: String, sourceUrl: String?) { + launch { + ImageProvider.bitmapLruCache.remove(url) + val image = withContext(IO) { + ImageLoader.loadBitmap(requireContext(), url).apply { + sourceUrl?.let { + apply( + RequestOptions().set(OkHttpModelLoader.sourceOriginOption, it) + ) + } + }.error(R.drawable.image_loading_error) + .diskCacheStrategy(DiskCacheStrategy.NONE) + .skipMemoryCache(true) + .submit() + .get() + } + ImageProvider.bitmapLruCache.put(url, image) + binding.verificationCodeImageView.setImageBitmap(image) + } + } + @SuppressLint("InflateParams") override fun onMenuItemClick(item: MenuItem): Boolean { when (item.itemId) { diff --git a/app/src/main/java/io/legado/app/ui/widget/dialog/PhotoDialog.kt b/app/src/main/java/io/legado/app/ui/widget/dialog/PhotoDialog.kt index 1b1632d79..3e310ab16 100644 --- a/app/src/main/java/io/legado/app/ui/widget/dialog/PhotoDialog.kt +++ b/app/src/main/java/io/legado/app/ui/widget/dialog/PhotoDialog.kt @@ -12,6 +12,7 @@ import io.legado.app.help.glide.ImageLoader import io.legado.app.help.glide.OkHttpModelLoader import io.legado.app.model.BookCover import io.legado.app.model.ReadBook +import io.legado.app.ui.book.read.page.provider.ImageProvider import io.legado.app.utils.setLayout import io.legado.app.utils.viewbindingdelegate.viewBinding @@ -36,31 +37,33 @@ class PhotoDialog() : BaseDialogFragment(R.layout.dialog_photo_view) { @SuppressLint("CheckResult") override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { - arguments?.let { arguments -> - arguments.getString("src")?.let { src -> - val file = ReadBook.book?.let { book -> - BookHelp.getImage(book, src) - } - if (file?.exists() == true) { - ImageLoader.load(requireContext(), file) - .error(R.drawable.image_loading_error) - .into(binding.photoView) - } else { - ImageLoader.load(requireContext(), src).apply { - arguments.getString("sourceOrigin")?.let { sourceOrigin -> - apply( - RequestOptions().set( - OkHttpModelLoader.sourceOriginOption, - sourceOrigin - ) + val arguments = arguments ?: return + arguments.getString("src")?.let { src -> + ImageProvider.bitmapLruCache.get(src)?.let { + binding.photoView.setImageBitmap(it) + return + } + val file = ReadBook.book?.let { book -> + BookHelp.getImage(book, src) + } + if (file?.exists() == true) { + ImageLoader.load(requireContext(), file) + .error(R.drawable.image_loading_error) + .into(binding.photoView) + } else { + ImageLoader.load(requireContext(), src).apply { + arguments.getString("sourceOrigin")?.let { sourceOrigin -> + apply( + RequestOptions().set( + OkHttpModelLoader.sourceOriginOption, + sourceOrigin ) - } - }.error(BookCover.defaultDrawable) - .into(binding.photoView) - } + ) + } + }.error(BookCover.defaultDrawable) + .into(binding.photoView) } } - } }