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

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

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

@ -8,6 +8,7 @@ import com.bumptech.glide.load.data.DataFetcher
import com.bumptech.glide.load.model.GlideUrl import com.bumptech.glide.load.model.GlideUrl
import com.bumptech.glide.util.ContentLengthInputStream import com.bumptech.glide.util.ContentLengthInputStream
import com.bumptech.glide.util.Preconditions import com.bumptech.glide.util.Preconditions
import io.legado.app.data.appDb
import io.legado.app.exception.NoStackTraceException import io.legado.app.exception.NoStackTraceException
import io.legado.app.help.http.okHttpClient import io.legado.app.help.http.okHttpClient
import io.legado.app.utils.isWifiConnect import io.legado.app.utils.isWifiConnect
@ -36,6 +37,13 @@ class OkHttpStreamFetcher(private val url: GlideUrl, private val options: Option
return return
} }
val requestBuilder: Request.Builder = Request.Builder().url(url.toStringUrl()) 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) { for ((key, value) in url.headers.entries) {
requestBuilder.addHeader(key, value) requestBuilder.addHeader(key, value)
} }

@ -4,12 +4,14 @@ import android.content.Context
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.appcompat.widget.PopupMenu import androidx.appcompat.widget.PopupMenu
import com.bumptech.glide.request.RequestOptions
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.ItemViewHolder
import io.legado.app.base.adapter.RecyclerAdapter import io.legado.app.base.adapter.RecyclerAdapter
import io.legado.app.data.entities.RssSource import io.legado.app.data.entities.RssSource
import io.legado.app.databinding.ItemRssBinding import io.legado.app.databinding.ItemRssBinding
import io.legado.app.help.glide.ImageLoader import io.legado.app.help.glide.ImageLoader
import io.legado.app.help.glide.OkHttpModelLoader
import splitties.views.onLongClick import splitties.views.onLongClick
class RssAdapter(context: Context, val callBack: CallBack) : class RssAdapter(context: Context, val callBack: CallBack) :
@ -27,7 +29,10 @@ class RssAdapter(context: Context, val callBack: CallBack) :
) { ) {
binding.apply { binding.apply {
tvName.text = item.sourceName 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() .centerCrop()
.placeholder(R.drawable.image_rss) .placeholder(R.drawable.image_rss)
.error(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.DataSource
import com.bumptech.glide.load.engine.GlideException import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.Target import com.bumptech.glide.request.target.Target
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.ItemViewHolder
import io.legado.app.data.entities.RssArticle import io.legado.app.data.entities.RssArticle
import io.legado.app.databinding.ItemRssArticleBinding import io.legado.app.databinding.ItemRssArticleBinding
import io.legado.app.help.glide.ImageLoader 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.getCompatColor
import io.legado.app.utils.gone import io.legado.app.utils.gone
import io.legado.app.utils.visible import io.legado.app.utils.visible
@ -38,7 +40,9 @@ class RssArticlesAdapter(context: Context, callBack: CallBack) :
if (item.image.isNullOrBlank() && !callBack.isGridLayout) { if (item.image.isNullOrBlank() && !callBack.isGridLayout) {
imageView.gone() imageView.gone()
} else { } 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) { if (callBack.isGridLayout) {
placeholder(R.drawable.image_rss_article) placeholder(R.drawable.image_rss_article)
} else { } else {

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

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

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

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

Loading…
Cancel
Save