获取图片的源放到访问网络时,这样如果有缓存就不会重复获取源,获取源是耗时的

pull/1781/head
kunfei 3 years ago
parent c7858c0177
commit 12a69e1e52
  1. 15
      app/src/main/java/io/legado/app/help/glide/ImageLoader.kt
  2. 1
      app/src/main/java/io/legado/app/help/glide/OkHttpModelLoader.kt
  3. 8
      app/src/main/java/io/legado/app/help/glide/OkHttpStreamFetcher.kt
  4. 7
      app/src/main/java/io/legado/app/ui/main/rss/RssAdapter.kt
  5. 6
      app/src/main/java/io/legado/app/ui/rss/article/RssArticlesAdapter.kt
  6. 8
      app/src/main/java/io/legado/app/ui/rss/article/RssArticlesAdapter1.kt
  7. 8
      app/src/main/java/io/legado/app/ui/rss/article/RssArticlesAdapter2.kt
  8. 7
      app/src/main/java/io/legado/app/ui/rss/favorites/RssFavoritesAdapter.kt
  9. 8
      app/src/main/java/io/legado/app/ui/widget/image/CoverImageView.kt

@ -7,7 +7,6 @@ import android.net.Uri
import androidx.annotation.DrawableRes
import com.bumptech.glide.RequestBuilder
import io.legado.app.constant.AppPattern.dataUriRegex
import io.legado.app.data.appDb
import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.utils.isAbsUrl
import io.legado.app.utils.isContentScheme
@ -19,24 +18,14 @@ object ImageLoader {
/**
* 自动判断path类型
*/
fun load(context: Context, path: String?, sourceOrigin: String? = null): RequestBuilder<Drawable> {
fun load(context: Context, path: String?): RequestBuilder<Drawable> {
return when {
path.isNullOrEmpty() -> GlideApp.with(context).load(path)
dataUriRegex.find(path) != null -> {
//glide内部已经实现dataUri解析
GlideApp.with(context).load(path)
}
path.isAbsUrl() -> {
kotlin.runCatching {
val source = sourceOrigin?.let {
appDb.bookSourceDao.getBookSource(it) ?: appDb.rssSourceDao.getByKey(it)
}
val url = AnalyzeUrl(path, source = source).getGlideUrl()
GlideApp.with(context).load(url)
}.getOrDefault(
GlideApp.with(context).load(path)
)
}
path.isAbsUrl() -> GlideApp.with(context).load(path)
path.isContentScheme() -> GlideApp.with(context).load(Uri.parse(path))
else -> kotlin.runCatching {
val file = File(path)

@ -9,6 +9,7 @@ import java.io.InputStream
object OkHttpModelLoader : ModelLoader<GlideUrl?, InputStream?> {
val loadOnlyWifiOption = Option.memory("loadOnlyWifi", false)
val sourceOriginOption = Option.memory<String>("sourceOrigin")
override fun buildLoadData(
model: GlideUrl,

@ -8,6 +8,7 @@ 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.exception.NoStackTraceException
import io.legado.app.help.http.okHttpClient
import io.legado.app.utils.isWifiConnect
@ -36,6 +37,13 @@ class OkHttpStreamFetcher(private val url: GlideUrl, private val options: Option
return
}
val requestBuilder: Request.Builder = Request.Builder().url(url.toStringUrl())
options.get(OkHttpModelLoader.sourceOriginOption)?.let { sourceUrl ->
val source = appDb.bookSourceDao.getBookSource(sourceUrl)
?: appDb.rssSourceDao.getByKey(sourceUrl)
source?.getHeaderMap(true)?.forEach {
requestBuilder.addHeader(it.key, it.value)
}
}
for ((key, value) in url.headers.entries) {
requestBuilder.addHeader(key, value)
}

@ -4,12 +4,14 @@ import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.widget.PopupMenu
import com.bumptech.glide.request.RequestOptions
import io.legado.app.R
import io.legado.app.base.adapter.ItemViewHolder
import io.legado.app.base.adapter.RecyclerAdapter
import io.legado.app.data.entities.RssSource
import io.legado.app.databinding.ItemRssBinding
import io.legado.app.help.glide.ImageLoader
import io.legado.app.help.glide.OkHttpModelLoader
import splitties.views.onLongClick
class RssAdapter(context: Context, val callBack: CallBack) :
@ -27,7 +29,10 @@ class RssAdapter(context: Context, val callBack: CallBack) :
) {
binding.apply {
tvName.text = item.sourceName
ImageLoader.load(context, item.sourceIcon, item.sourceUrl)
val options = RequestOptions()
.set(OkHttpModelLoader.sourceOriginOption, item.sourceUrl)
ImageLoader.load(context, item.sourceIcon)
.apply(options)
.centerCrop()
.placeholder(R.drawable.image_rss)
.error(R.drawable.image_rss)

@ -7,12 +7,14 @@ import android.view.ViewGroup
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.Target
import io.legado.app.R
import io.legado.app.base.adapter.ItemViewHolder
import io.legado.app.data.entities.RssArticle
import io.legado.app.databinding.ItemRssArticleBinding
import io.legado.app.help.glide.ImageLoader
import io.legado.app.help.glide.OkHttpModelLoader
import io.legado.app.utils.getCompatColor
import io.legado.app.utils.gone
import io.legado.app.utils.visible
@ -38,7 +40,9 @@ class RssArticlesAdapter(context: Context, callBack: CallBack) :
if (item.image.isNullOrBlank() && !callBack.isGridLayout) {
imageView.gone()
} else {
ImageLoader.load(context, item.image, item.origin).apply {
val options =
RequestOptions().set(OkHttpModelLoader.sourceOriginOption, item.origin)
ImageLoader.load(context, item.image).apply(options).apply {
if (callBack.isGridLayout) {
placeholder(R.drawable.image_rss_article)
} else {

@ -1,17 +1,20 @@
package io.legado.app.ui.rss.article
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.drawable.Drawable
import android.view.ViewGroup
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.Target
import io.legado.app.R
import io.legado.app.base.adapter.ItemViewHolder
import io.legado.app.data.entities.RssArticle
import io.legado.app.databinding.ItemRssArticle1Binding
import io.legado.app.help.glide.ImageLoader
import io.legado.app.help.glide.OkHttpModelLoader
import io.legado.app.utils.getCompatColor
import io.legado.app.utils.gone
import io.legado.app.utils.visible
@ -24,6 +27,7 @@ class RssArticlesAdapter1(context: Context, callBack: CallBack) :
return ItemRssArticle1Binding.inflate(inflater, parent, false)
}
@SuppressLint("CheckResult")
override fun convert(
holder: ItemViewHolder,
binding: ItemRssArticle1Binding,
@ -36,7 +40,9 @@ class RssArticlesAdapter1(context: Context, callBack: CallBack) :
if (item.image.isNullOrBlank() && !callBack.isGridLayout) {
imageView.gone()
} else {
ImageLoader.load(context, item.image, item.origin).apply {
val options =
RequestOptions().set(OkHttpModelLoader.sourceOriginOption, item.origin)
ImageLoader.load(context, item.image).apply(options).apply {
if (callBack.isGridLayout) {
placeholder(R.drawable.image_rss_article)
} else {

@ -1,17 +1,20 @@
package io.legado.app.ui.rss.article
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.drawable.Drawable
import android.view.ViewGroup
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.Target
import io.legado.app.R
import io.legado.app.base.adapter.ItemViewHolder
import io.legado.app.data.entities.RssArticle
import io.legado.app.databinding.ItemRssArticle2Binding
import io.legado.app.help.glide.ImageLoader
import io.legado.app.help.glide.OkHttpModelLoader
import io.legado.app.utils.getCompatColor
import io.legado.app.utils.gone
import io.legado.app.utils.visible
@ -24,6 +27,7 @@ class RssArticlesAdapter2(context: Context, callBack: CallBack) :
return ItemRssArticle2Binding.inflate(inflater, parent, false)
}
@SuppressLint("CheckResult")
override fun convert(
holder: ItemViewHolder,
binding: ItemRssArticle2Binding,
@ -36,7 +40,9 @@ class RssArticlesAdapter2(context: Context, callBack: CallBack) :
if (item.image.isNullOrBlank() && !callBack.isGridLayout) {
imageView.gone()
} else {
ImageLoader.load(context, item.image, item.origin).apply {
val options =
RequestOptions().set(OkHttpModelLoader.sourceOriginOption, item.origin)
ImageLoader.load(context, item.image).apply(options).apply {
if (callBack.isGridLayout) {
placeholder(R.drawable.image_rss_article)
} else {

@ -6,12 +6,14 @@ import android.view.ViewGroup
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.Target
import io.legado.app.base.adapter.ItemViewHolder
import io.legado.app.base.adapter.RecyclerAdapter
import io.legado.app.data.entities.RssStar
import io.legado.app.databinding.ItemRssArticleBinding
import io.legado.app.help.glide.ImageLoader
import io.legado.app.help.glide.OkHttpModelLoader
import io.legado.app.utils.gone
import io.legado.app.utils.visible
@ -35,7 +37,10 @@ class RssFavoritesAdapter(context: Context, val callBack: CallBack) :
if (item.image.isNullOrBlank()) {
imageView.gone()
} else {
ImageLoader.load(context, item.image, item.origin)
val options =
RequestOptions().set(OkHttpModelLoader.sourceOriginOption, item.origin)
ImageLoader.load(context, item.image)
.apply(options)
.addListener(object : RequestListener<Drawable> {
override fun onLoadFailed(
e: GlideException?,

@ -187,8 +187,12 @@ class CoverImageView @JvmOverloads constructor(
.centerCrop()
.into(this)
} else {
ImageLoader.load(context, path, sourceOrigin)//Glide自动识别http://,content://和file://
.apply(RequestOptions().set(OkHttpModelLoader.loadOnlyWifiOption, loadOnlyWifi))
var options = RequestOptions().set(OkHttpModelLoader.loadOnlyWifiOption, loadOnlyWifi)
if (sourceOrigin != null) {
options = options.set(OkHttpModelLoader.sourceOriginOption, sourceOrigin)
}
ImageLoader.load(context, path)//Glide自动识别http://,content://和file://
.apply(options)
.placeholder(BookCover.defaultDrawable)
.error(BookCover.defaultDrawable)
.listener(glideListener)

Loading…
Cancel
Save