From 17a828cf409022fc3dcdd61042c6cb8e5f593ce2 Mon Sep 17 00:00:00 2001 From: Horis <821938089@qq.com> Date: Sat, 17 Dec 2022 11:35:57 +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 --- .../ui/association/VerificationCodeDialog.kt | 53 ++++++++++++------- 1 file changed, 33 insertions(+), 20 deletions(-) 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 4e3838996..5fb583ca7 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 @@ -1,13 +1,18 @@ package io.legado.app.ui.association import android.annotation.SuppressLint +import android.graphics.Bitmap +import android.graphics.drawable.Drawable import android.os.Bundle import android.view.MenuItem import android.view.View import android.view.ViewGroup import androidx.appcompat.widget.Toolbar +import androidx.core.graphics.drawable.toBitmap import com.bumptech.glide.load.engine.DiskCacheStrategy import com.bumptech.glide.request.RequestOptions +import com.bumptech.glide.request.target.CustomTarget +import com.bumptech.glide.request.transition.Transition import io.legado.app.R import io.legado.app.base.BaseDialogFragment import io.legado.app.databinding.DialogVerificationCodeViewBinding @@ -22,9 +27,6 @@ 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 /** * 图片验证码对话框 @@ -79,24 +81,35 @@ class VerificationCodeDialog() : BaseDialogFragment(R.layout.dialog_verification @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.remove(url) + ImageLoader.loadBitmap(requireContext(), url).apply { + sourceUrl?.let { + apply( + RequestOptions().set(OkHttpModelLoader.sourceOriginOption, it) + ) } - ImageProvider.bitmapLruCache.put(url, image) - binding.verificationCodeImageView.setImageBitmap(image) - } + }.error(R.drawable.image_loading_error) + .diskCacheStrategy(DiskCacheStrategy.NONE) + .skipMemoryCache(true) + .into(object : CustomTarget() { + override fun onLoadFailed(errorDrawable: Drawable?) { + errorDrawable?.toBitmap()?.let { + onResourceReady(it, null) + } + } + + override fun onResourceReady( + resource: Bitmap, + transition: Transition? + ) { + ImageProvider.bitmapLruCache.put(url, resource) + binding.verificationCodeImageView.setImageBitmap(resource) + } + + override fun onLoadCleared(placeholder: Drawable?) { + // do nothing + } + }) } @SuppressLint("InflateParams")