mirror of
https://github.com/gedoor/legado.git
synced 2024-07-06 23:47:49 +08:00
优化
This commit is contained in:
parent
1a4fca59db
commit
9ffbf1f6aa
@ -5,11 +5,14 @@ import android.graphics.Bitmap
|
||||
import android.graphics.drawable.Drawable
|
||||
import android.net.Uri
|
||||
import androidx.annotation.DrawableRes
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import com.bumptech.glide.Glide
|
||||
import com.bumptech.glide.RequestBuilder
|
||||
import io.legado.app.utils.isAbsUrl
|
||||
import io.legado.app.utils.isContentScheme
|
||||
import io.legado.app.utils.isDataUrl
|
||||
import io.legado.app.utils.lifecycle
|
||||
import splitties.init.appCtx
|
||||
import java.io.File
|
||||
|
||||
//https://bumptech.github.io/glide/doc/generatedapi.html
|
||||
@ -34,6 +37,22 @@ object ImageLoader {
|
||||
}
|
||||
}
|
||||
|
||||
fun load(lifecycle: Lifecycle, path: String?): RequestBuilder<Drawable> {
|
||||
val requestManager = Glide.with(appCtx).lifecycle(lifecycle)
|
||||
return when {
|
||||
path.isNullOrEmpty() -> requestManager.load(path)
|
||||
path.isDataUrl() -> requestManager.load(path)
|
||||
path.isAbsUrl() -> requestManager.load(path)
|
||||
path.isContentScheme() -> requestManager.load(Uri.parse(path))
|
||||
|
||||
else -> kotlin.runCatching {
|
||||
requestManager.load(File(path))
|
||||
}.getOrElse {
|
||||
requestManager.load(path)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun loadBitmap(context: Context, path: String?): RequestBuilder<Bitmap> {
|
||||
return when {
|
||||
path.isNullOrEmpty() -> Glide.with(context).asBitmap().load(path)
|
||||
|
@ -4,6 +4,7 @@ import android.content.Context
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.appcompat.widget.PopupMenu
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import com.bumptech.glide.request.RequestOptions
|
||||
import io.legado.app.R
|
||||
import io.legado.app.base.adapter.ItemViewHolder
|
||||
@ -14,7 +15,7 @@ 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) :
|
||||
class RssAdapter(context: Context, val callBack: CallBack, val lifecycle: Lifecycle) :
|
||||
RecyclerAdapter<RssSource, ItemRssBinding>(context) {
|
||||
|
||||
override fun getViewBinding(parent: ViewGroup): ItemRssBinding {
|
||||
@ -31,7 +32,7 @@ class RssAdapter(context: Context, val callBack: CallBack) :
|
||||
tvName.text = item.sourceName
|
||||
val options = RequestOptions()
|
||||
.set(OkHttpModelLoader.sourceOriginOption, item.sourceUrl)
|
||||
ImageLoader.load(context, item.sourceIcon)
|
||||
ImageLoader.load(lifecycle, item.sourceIcon)
|
||||
.apply(options)
|
||||
.centerCrop()
|
||||
.placeholder(R.drawable.image_rss)
|
||||
|
@ -58,7 +58,7 @@ class RssFragment() : VMBaseFragment<RssViewModel>(R.layout.fragment_rss),
|
||||
|
||||
private val binding by viewBinding(FragmentRssBinding::bind)
|
||||
override val viewModel by viewModels<RssViewModel>()
|
||||
private val adapter by lazy { RssAdapter(requireContext(), this) }
|
||||
private val adapter by lazy { RssAdapter(requireContext(), this, lifecycle) }
|
||||
private val searchView: SearchView by lazy {
|
||||
binding.titleBar.findViewById(R.id.search_view)
|
||||
}
|
||||
|
@ -0,0 +1,24 @@
|
||||
package io.legado.app.utils
|
||||
|
||||
import androidx.lifecycle.DefaultLifecycleObserver
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import androidx.lifecycle.LifecycleOwner
|
||||
import com.bumptech.glide.RequestManager
|
||||
|
||||
fun RequestManager.lifecycle(lifecycle: Lifecycle): RequestManager {
|
||||
|
||||
val observer = object : DefaultLifecycleObserver {
|
||||
override fun onStart(owner: LifecycleOwner) = onStart()
|
||||
override fun onStop(owner: LifecycleOwner) = onStop()
|
||||
override fun onResume(owner: LifecycleOwner) = onStart()
|
||||
override fun onPause(owner: LifecycleOwner) = onStop()
|
||||
override fun onDestroy(owner: LifecycleOwner) {
|
||||
onDestroy()
|
||||
owner.lifecycle.removeObserver(this)
|
||||
}
|
||||
}
|
||||
|
||||
lifecycle.addObserver(observer)
|
||||
|
||||
return this
|
||||
}
|
Loading…
Reference in New Issue
Block a user