Skip to content

Commit

Permalink
Merge pull request #20145 from wordpress-mobile/bumo_glide_to_4.16.0
Browse files Browse the repository at this point in the history
Bump Glide to `4.16.0`
  • Loading branch information
wzieba authored Feb 16, 2024
2 parents 04efea3 + c6fd6c0 commit 9cb8b62
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import android.widget.TextView
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentActivity
import com.bumptech.glide.Glide
import com.bumptech.glide.RequestBuilder
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.load.resource.bitmap.CenterCrop
Expand All @@ -38,8 +40,6 @@ import com.bumptech.glide.request.transition.Transition
import com.bumptech.glide.signature.ObjectKey
import kotlinx.coroutines.CoroutineScope
import org.wordpress.android.WordPress
import org.wordpress.android.modules.GlideApp
import org.wordpress.android.modules.GlideRequest
import org.wordpress.android.networking.MShot
import org.wordpress.android.ui.media.VideoLoader
import org.wordpress.android.util.AppLog
Expand Down Expand Up @@ -107,7 +107,7 @@ class ImageManager @Inject constructor(
fun load(imageView: ImageView, imageType: ImageType, imgUrl: String = "", scaleType: ScaleType = CENTER) {
val context = imageView.context
if (!context.isAvailable()) return
GlideApp.with(context)
Glide.with(context)
.load(imgUrl)
.addFallback(imageType)
.addPlaceholder(imageType)
Expand Down Expand Up @@ -137,7 +137,7 @@ class ImageManager @Inject constructor(
videoUrl,
loadAction = {
if (!context.isAvailable()) return@runIfMediaNotTooBig
GlideApp.with(context)
Glide.with(context)
.load(videoUrl)
.addFallback(imageType)
.addPlaceholder(imageType)
Expand All @@ -152,7 +152,7 @@ class ImageManager @Inject constructor(
val fallbackDrawable = placeholderManager.getErrorResource(imageType)?.let {
ColorDrawable(ContextCompat.getColor(context, it))
}
GlideApp.with(context)
Glide.with(context)
.load(fallbackDrawable)
.addPlaceholder(imageType)
.addFallback(imageType)
Expand All @@ -178,7 +178,7 @@ class ImageManager @Inject constructor(
height: Int? = null
) {
if (!context.isAvailable()) return
GlideApp.with(context)
Glide.with(context)
.asBitmap()
.load(imgUrl)
.addFallback(imageType)
Expand All @@ -202,7 +202,7 @@ class ImageManager @Inject constructor(
) {
val context = imageView.context
if (!context.isAvailable()) return
GlideApp.with(context)
Glide.with(context)
.load(imgUrl)
.addFallback(imageType)
.addPlaceholder(imageType)
Expand Down Expand Up @@ -236,7 +236,7 @@ class ImageManager @Inject constructor(
return
}

GlideApp.with(context)
Glide.with(context)
.load(imageData)
.addFallback(imageType)
.addPlaceholder(imageType)
Expand All @@ -262,7 +262,7 @@ class ImageManager @Inject constructor(
val context = imageView.context
if (!context.isAvailable()) return

GlideApp.with(context)
Glide.with(context)
.load(imgUrl)
.transform(CenterCrop(), RoundedCorners(cornerRadius))
.addFallback(imageType)
Expand Down Expand Up @@ -290,7 +290,7 @@ class ImageManager @Inject constructor(
) {
val context = imageView.context
if (!context.isAvailable()) return
GlideApp.with(context)
Glide.with(context)
.load(Uri.parse(imgUrl))
.addFallback(imageType)
.addPlaceholder(imageType)
Expand All @@ -310,7 +310,7 @@ class ImageManager @Inject constructor(
fun preload(context: Context, design: MShot) {
if (!context.isAvailable()) return
try {
GlideApp.with(context)
Glide.with(context)
.downloadOnly()
.load(design)
.submit()
Expand All @@ -330,7 +330,7 @@ class ImageManager @Inject constructor(
fun loadWithResultListener(view: ImageView, design: MShot, requestListener: RequestListener<Drawable>) {
val context = view.context
if (!context.isAvailable()) return
GlideApp.with(context)
Glide.with(context)
.load(design)
.addFallback(ImageType.THEME)
.addPlaceholder(ImageType.THEME)
Expand Down Expand Up @@ -360,7 +360,7 @@ class ImageManager @Inject constructor(
) {
val context = imageView.context
if (!context.isAvailable()) return
GlideApp.with(context)
Glide.with(context)
.load(imgUri)
.addFallback(imageType)
.addPlaceholder(imageType)
Expand All @@ -384,7 +384,7 @@ class ImageManager @Inject constructor(
) {
val context = WordPress.getContext()
if (!context.isAvailable()) return
GlideApp.with(context)
Glide.with(context)
.asFile()
.load(imgUri)
.attachRequestListener(requestListener)
Expand All @@ -404,7 +404,7 @@ class ImageManager @Inject constructor(
fun load(imageView: ImageView, bitmap: Bitmap, scaleType: ScaleType = CENTER) {
val context = imageView.context
if (!context.isAvailable()) return
GlideApp.with(context)
Glide.with(context)
.load(bitmap)
.applyScaleType(scaleType)
.into(imageView)
Expand All @@ -418,7 +418,7 @@ class ImageManager @Inject constructor(
fun load(imageView: ImageView, drawable: Drawable, scaleType: ScaleType = CENTER) {
val context = imageView.context
if (!context.isAvailable()) return
GlideApp.with(context)
Glide.with(context)
.load(drawable)
.applyScaleType(scaleType)
.into(imageView)
Expand All @@ -432,7 +432,7 @@ class ImageManager @Inject constructor(
fun load(imageView: ImageView, @DrawableRes resourceId: Int, scaleType: ScaleType = CENTER) {
val context = imageView.context
if (!context.isAvailable()) return
GlideApp.with(context)
Glide.with(context)
.load(ContextCompat.getDrawable(context, resourceId))
.applyScaleType(scaleType)
.into(imageView)
Expand All @@ -450,7 +450,7 @@ class ImageManager @Inject constructor(
fun loadIntoCustomTarget(viewTarget: ViewTarget<TextView, Drawable>, imageType: ImageType, imgUrl: String) {
val context = WordPress.getContext()
if (!context.isAvailable()) return
GlideApp.with(context)
Glide.with(context)
.load(imgUrl)
.addFallback(imageType)
.addPlaceholder(imageType)
Expand All @@ -472,7 +472,7 @@ class ImageManager @Inject constructor(
scaleType: ScaleType = CENTER
) {
if (!context.isAvailable()) return
GlideApp.with(context)
Glide.with(context)
.asBitmap()
.load(imgUrl)
.applyScaleType(scaleType)
Expand All @@ -488,7 +488,7 @@ class ImageManager @Inject constructor(
if (context is Activity && (context.isFinishing || context.isDestroyed)) {
return
}
GlideApp.with(imageView.context).clear(imageView)
Glide.with(imageView.context).clear(imageView)
}

/**
Expand All @@ -497,12 +497,12 @@ class ImageManager @Inject constructor(
*/
@Suppress("DEPRECATION")
fun <T : Any> cancelRequest(context: Context, target: BaseTarget<T>?) {
GlideApp.with(context).clear(target)
Glide.with(context).clear(target)
}

private fun <T : Any> GlideRequest<T>.applyScaleType(
private fun <T : Any> RequestBuilder<T>.applyScaleType(
scaleType: ScaleType
): GlideRequest<T> {
): RequestBuilder<T> {
return when (scaleType) {
ScaleType.CENTER_CROP -> this.centerCrop()
ScaleType.CENTER_INSIDE -> this.centerInside()
Expand All @@ -521,15 +521,15 @@ class ImageManager @Inject constructor(
}
}

private fun <T : Any> GlideRequest<T>.applySize(width: Int?, height: Int?): GlideRequest<T> {
private fun <T : Any> RequestBuilder<T>.applySize(width: Int?, height: Int?): RequestBuilder<T> {
return if (width != null && height != null) {
this.override(width, height)
} else {
this
}
}

private fun <T : Any> GlideRequest<T>.addPlaceholder(imageType: ImageType): GlideRequest<T> {
private fun <T : Any> RequestBuilder<T>.addPlaceholder(imageType: ImageType): RequestBuilder<T> {
val placeholderImageRes = placeholderManager.getPlaceholderResource(imageType)
return if (placeholderImageRes == null) {
this
Expand All @@ -538,7 +538,7 @@ class ImageManager @Inject constructor(
}
}

private fun <T : Any> GlideRequest<T>.addFallback(imageType: ImageType): GlideRequest<T> {
private fun <T : Any> RequestBuilder<T>.addFallback(imageType: ImageType): RequestBuilder<T> {
val errorImageRes = placeholderManager.getErrorResource(imageType)
return if (errorImageRes == null) {
this
Expand All @@ -550,23 +550,23 @@ class ImageManager @Inject constructor(
/**
* Changing the signature invalidates cache.
*/
private fun <T : Any> GlideRequest<T>.addSignature(signature: Int?): GlideRequest<T> {
private fun <T : Any> RequestBuilder<T>.addSignature(signature: Int?): RequestBuilder<T> {
return if (signature == null) {
this
} else {
this.signature(ObjectKey(signature))
}
}

private fun GlideRequest<Drawable>.addThumbnail(
private fun RequestBuilder<Drawable>.addThumbnail(
context: Context,
thumbnailUrl: String?,
listener: RequestListener<Drawable>
): GlideRequest<Drawable> {
): RequestBuilder<Drawable> {
return if (TextUtils.isEmpty(thumbnailUrl)) {
this
} else {
val thumbnailRequest = GlideApp
val thumbnailRequest = Glide
.with(context)
.load(thumbnailUrl)
.downsample(DownsampleStrategy.AT_MOST)
Expand All @@ -575,40 +575,34 @@ class ImageManager @Inject constructor(
}
}

private fun <T : Any> GlideRequest<T>.attachRequestListener(
private fun <T : Any> RequestBuilder<T>.attachRequestListener(
requestListener: RequestListener<T>?
): GlideRequest<T> {
): RequestBuilder<T> {
return if (requestListener == null) {
this
} else {
this.listener(object : com.bumptech.glide.request.RequestListener<T> {
override fun onLoadFailed(
e: GlideException?,
model: Any?,
target: Target<T>?,
target: Target<T>,
isFirstResource: Boolean
): Boolean {
requestListener.onLoadFailed(e, model)
return false
}

override fun onResourceReady(
resource: T?,
model: Any?,
resource: T,
model: Any,
target: Target<T>?,
dataSource: DataSource?,
dataSource: DataSource,
isFirstResource: Boolean
): Boolean {
if (resource != null) {
requestListener.onResourceReady(resource, model)
} else {
// according to the Glide's JavaDoc, this shouldn't happen
AppLog.e(AppLog.T.UTILS, "Resource in ImageManager.onResourceReady is null.")
requestListener.onLoadFailed(null, model)
}
requestListener.onResourceReady(resource, model)
return false
}
})
})
}
}

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ ext {
facebookShimmerVersion = '0.5.0'
firebaseBomVersion = '32.1.0'
firebaseIidVersion = '21.1.0'
glideVersion = '4.15.1'
glideVersion = '4.16.0'
googleAutoServiceVersion = '1.0.1'
googleExoPlayerVersion = '2.13.3'
googleFlexboxlayoutVersion = '3.0.0'
Expand Down

0 comments on commit 9cb8b62

Please sign in to comment.