Use relative URL as thumb cache key
Also increase default cache size to 640MB
This commit is contained in:
1 parent
dcea49c682
commit
b39b0a5ca6
11 files changed
+56
-43
No files matched your search
@@ -17,41 +17,33 @@ package com.hippo.drawable
|
||||
|
||||
import android.graphics.drawable.AnimatedImageDrawable
|
||||
import android.graphics.drawable.Drawable
|
||||
import coil.Coil.imageLoader
|
||||
import coil.request.Disposable
|
||||
import coil.imageLoader
|
||||
import coil.request.ImageRequest
|
||||
import com.hippo.ehviewer.coil.imageRequest
|
||||
import com.hippo.widget.ObservedTextView
|
||||
|
||||
class UnikeryDrawable(private val mTextView: ObservedTextView) : WrapDrawable(),
|
||||
class UnikeryDrawable(private val mTextView: ObservedTextView, url: String) : WrapDrawable(),
|
||||
ObservedTextView.OnWindowAttachListener {
|
||||
private var mUrl: String? = null
|
||||
private var task: Disposable? = null
|
||||
private val mImageRequest: ImageRequest
|
||||
|
||||
init {
|
||||
mTextView.setOnWindowAttachListener(this)
|
||||
mImageRequest = mTextView.context.imageRequest(url) {
|
||||
target(onSuccess = { onGetValue(it) })
|
||||
}
|
||||
load()
|
||||
}
|
||||
|
||||
override fun onAttachedToWindow() {
|
||||
load(mUrl)
|
||||
drawable ?: load()
|
||||
}
|
||||
|
||||
override fun onDetachedFromWindow() {
|
||||
if (task != null && !task!!.isDisposed) task!!.dispose()
|
||||
clearDrawable()
|
||||
}
|
||||
|
||||
fun load(url: String?) {
|
||||
if (url != null) {
|
||||
mUrl = url
|
||||
val imageLoader = imageLoader(mTextView.context)
|
||||
val request = ImageRequest.Builder(mTextView.context)
|
||||
.data(url)
|
||||
.memoryCacheKey(url)
|
||||
.diskCacheKey(url)
|
||||
.target(onSuccess = { onGetValue(it) })
|
||||
.build()
|
||||
task = imageLoader.enqueue(request)
|
||||
}
|
||||
fun load() {
|
||||
mTextView.context.imageLoader.enqueue(mImageRequest)
|
||||
}
|
||||
|
||||
private fun clearDrawable() {
|
||||
|
||||
@@ -90,7 +90,7 @@ object Settings {
|
||||
const val KEY_SECURITY = "require_unlock"
|
||||
const val KEY_SECURITY_DELAY = "require_unlock_delay"
|
||||
const val KEY_READ_CACHE_SIZE = "read_cache_size"
|
||||
const val DEFAULT_READ_CACHE_SIZE = 320
|
||||
const val DEFAULT_READ_CACHE_SIZE = 640
|
||||
const val KEY_BUILT_IN_HOSTS = "built_in_hosts_2"
|
||||
const val KEY_DOMAIN_FRONTING = "domain_fronting"
|
||||
const val KEY_BYPASS_VPN = "bypass_vpn"
|
||||
|
||||
@@ -19,4 +19,8 @@ object EhCacheKeyFactory {
|
||||
fun getImageKey(gid: Long, index: Int): String {
|
||||
return "image:$gid:$index"
|
||||
}
|
||||
|
||||
fun getImageKey(url: String): String {
|
||||
return url.removePrefix(EhUrl.URL_PREFIX_THUMB_E).removePrefix(EhUrl.URL_PREFIX_THUMB_EX)
|
||||
}
|
||||
}
|
||||
@@ -127,6 +127,13 @@ object EhUrl {
|
||||
else -> URL_WATCHED_E
|
||||
}
|
||||
|
||||
val thumbUrlPrefix: String
|
||||
get() = when (Settings.gallerySite) {
|
||||
SITE_E -> URL_PREFIX_THUMB_E
|
||||
SITE_EX -> URL_PREFIX_THUMB_EX
|
||||
else -> URL_PREFIX_THUMB_E
|
||||
}
|
||||
|
||||
fun getGalleryDetailUrl(gid: Long, token: String?): String {
|
||||
return getGalleryDetailUrl(gid, token, 0, false)
|
||||
}
|
||||
@@ -165,12 +172,4 @@ object EhUrl {
|
||||
fun getTagDefinitionUrl(tag: String): String {
|
||||
return "https://ehwiki.org/wiki/" + tag.replace(' ', '_')
|
||||
}
|
||||
|
||||
fun getThumbUrlPrefix(): String {
|
||||
return when (Settings.gallerySite) {
|
||||
SITE_E -> URL_PREFIX_THUMB_E
|
||||
SITE_EX -> URL_PREFIX_THUMB_EX
|
||||
else -> URL_PREFIX_THUMB_E
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -146,11 +146,6 @@ object EhUtils {
|
||||
return title.ifEmpty { null }
|
||||
}
|
||||
|
||||
fun fixThumbUrl(url: String): String {
|
||||
return EhUrl.getThumbUrlPrefix() +
|
||||
url.removePrefix(EhUrl.URL_PREFIX_THUMB_E).removePrefix(EhUrl.URL_PREFIX_THUMB_EX)
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun handleThumbUrlResolution(url: String?): String? {
|
||||
if (null == url) {
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.hippo.ehviewer.coil
|
||||
|
||||
import android.content.Context
|
||||
import coil.request.ImageRequest
|
||||
import com.hippo.ehviewer.client.EhCacheKeyFactory
|
||||
import com.hippo.ehviewer.client.EhUrl
|
||||
|
||||
fun ImageRequest.Builder.ehUrl(url: String) = apply {
|
||||
val key = EhCacheKeyFactory.getImageKey(url)
|
||||
val realUrl = EhUrl.thumbUrlPrefix + key
|
||||
data(realUrl)
|
||||
memoryCacheKey(key)
|
||||
diskCacheKey(key)
|
||||
}
|
||||
|
||||
inline fun Context.imageRequest(
|
||||
url: String,
|
||||
builder: ImageRequest.Builder.() -> Unit = {}
|
||||
): ImageRequest {
|
||||
return ImageRequest.Builder(this)
|
||||
.ehUrl(url)
|
||||
.apply(builder)
|
||||
.build()
|
||||
}
|
||||
@@ -846,9 +846,7 @@ class DownloadsScene : BaseToolbarScene(), DownloadInfoListener, OnClickFabListe
|
||||
}
|
||||
|
||||
fun bind(info: DownloadInfo) {
|
||||
info.thumb?.let {
|
||||
binding.thumb.load(EhUtils.fixThumbUrl(it))
|
||||
}
|
||||
info.thumb?.let { binding.thumb.load(it) }
|
||||
binding.title.text = EhUtils.getSuitableTitle(info)
|
||||
binding.uploader.text = info.uploader
|
||||
binding.rating.rating = info.rating
|
||||
|
||||
@@ -26,9 +26,7 @@ import androidx.recyclerview.widget.StaggeredGridLayoutManager
|
||||
import com.hippo.easyrecyclerview.MarginItemDecoration
|
||||
import com.hippo.ehviewer.R
|
||||
import com.hippo.ehviewer.Settings
|
||||
import com.hippo.ehviewer.client.EhUtils
|
||||
import com.hippo.ehviewer.client.data.GalleryInfo
|
||||
import com.hippo.ehviewer.dao.LocalFavoriteInfo
|
||||
import com.hippo.ehviewer.databinding.ItemGalleryGridBinding
|
||||
import com.hippo.ehviewer.databinding.ItemGalleryListBinding
|
||||
import com.hippo.widget.recyclerview.AutoStaggeredGridLayoutManager
|
||||
@@ -154,9 +152,6 @@ internal abstract class GalleryAdapter(
|
||||
|
||||
override fun onBindViewHolder(holder: GalleryHolder, position: Int) {
|
||||
val gi = getDataAt(position) ?: return
|
||||
if (gi is LocalFavoriteInfo) {
|
||||
gi.thumb = EhUtils.fixThumbUrl(gi.thumb!!)
|
||||
}
|
||||
holder.bind(gi)
|
||||
}
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@ import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.layout.ContentScale
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
@@ -30,6 +31,7 @@ import com.hippo.ehviewer.R
|
||||
import com.hippo.ehviewer.Settings
|
||||
import com.hippo.ehviewer.client.EhUtils
|
||||
import com.hippo.ehviewer.client.data.GalleryInfo
|
||||
import com.hippo.ehviewer.coil.imageRequest
|
||||
import com.hippo.ehviewer.download.DownloadManager
|
||||
import eu.kanade.tachiyomi.util.system.pxToDp
|
||||
import java.util.Locale
|
||||
@@ -58,7 +60,9 @@ fun ListInfoCard(
|
||||
) {
|
||||
Card {
|
||||
AsyncImage(
|
||||
model = info.thumb,
|
||||
model = info.thumb?.let {
|
||||
LocalContext.current.imageRequest(it)
|
||||
},
|
||||
contentDescription = "",
|
||||
contentScale = ContentScale.Crop,
|
||||
modifier = Modifier
|
||||
|
||||
@@ -24,6 +24,6 @@ class URLImageGetter(
|
||||
private val mTextView: ObservedTextView,
|
||||
) : ImageGetter {
|
||||
override fun getDrawable(source: String): Drawable {
|
||||
return UnikeryDrawable(mTextView).apply { load(source) }
|
||||
return UnikeryDrawable(mTextView, source)
|
||||
}
|
||||
}
|
||||
@@ -27,6 +27,7 @@ import coil.size.Size
|
||||
import com.hippo.drawable.PreciselyClipDrawable
|
||||
import com.hippo.ehviewer.EhApplication
|
||||
import com.hippo.ehviewer.R
|
||||
import com.hippo.ehviewer.coil.ehUrl
|
||||
|
||||
private val application = EhApplication.application
|
||||
private val errorDrawable = AppCompatResources.getDrawable(application, R.drawable.image_failed)
|
||||
@@ -91,6 +92,7 @@ open class LoadImageView @JvmOverloads constructor(
|
||||
},
|
||||
{ _, _ -> setRetry(false) }
|
||||
)
|
||||
ehUrl(url)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user