This commit is contained in:
Horis 2024-11-21 22:34:33 +08:00
parent 080416a9b3
commit db16345397

View File

@ -1,5 +1,7 @@
package io.legado.app.api.controller
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import androidx.core.graphics.drawable.toBitmap
import com.bumptech.glide.Glide
import io.legado.app.api.ReturnData
@ -29,6 +31,7 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import splitties.init.appCtx
import java.io.File
import java.util.WeakHashMap
import java.util.concurrent.TimeUnit
object BookController {
@ -36,6 +39,7 @@ object BookController {
private lateinit var book: Book
private var bookSource: BookSource? = null
private var bookUrl: String = ""
private val defaultCoverCache by lazy { WeakHashMap<Drawable, Bitmap>() }
/**
* 书架所有书籍
@ -77,14 +81,20 @@ object BookController {
} catch (e: Exception) {
LogUtils.d("BookController", "Error getCover($startAt) $coverPath\n$e")
try {
val defaultBitmap = Glide.with(appCtx)
.asBitmap()
.load(BookCover.defaultDrawable.toBitmap())
.override(84, 112)
.centerCrop()
.submit()
.get(3, TimeUnit.SECONDS)
returnData.setData(defaultBitmap)
val cached = defaultCoverCache[BookCover.defaultDrawable]
if (cached == null) {
val defaultBitmap = Glide.with(appCtx)
.asBitmap()
.load(BookCover.defaultDrawable.toBitmap())
.override(84, 112)
.centerCrop()
.submit()
.get(3, TimeUnit.SECONDS)
defaultCoverCache[BookCover.defaultDrawable] = defaultBitmap
returnData.setData(defaultBitmap)
} else {
returnData.setData(cached)
}
} catch (e: Exception) {
returnData.setErrorMsg(e.localizedMessage ?: "getCover error")
}