From 394b89e76bbc52f430ae59e3f353476a4c93afa3 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Mon, 5 Aug 2019 14:30:42 +0200 Subject: [PATCH] Avoid duplicated code --- .../features/media/ImageContentRenderer.kt | 70 +++++++------------ 1 file changed, 26 insertions(+), 44 deletions(-) diff --git a/vector/src/main/java/im/vector/riotx/features/media/ImageContentRenderer.kt b/vector/src/main/java/im/vector/riotx/features/media/ImageContentRenderer.kt index 4ae9f473..7a7c880c 100644 --- a/vector/src/main/java/im/vector/riotx/features/media/ImageContentRenderer.kt +++ b/vector/src/main/java/im/vector/riotx/features/media/ImageContentRenderer.kt @@ -31,6 +31,7 @@ import im.vector.matrix.android.api.session.content.ContentUrlResolver import im.vector.matrix.android.internal.crypto.attachments.ElementToDecrypt import im.vector.riotx.core.di.ActiveSessionHolder import im.vector.riotx.core.glide.GlideApp +import im.vector.riotx.core.glide.GlideRequest import im.vector.riotx.core.utils.DimensionUtils.dpToPx import kotlinx.android.parcel.Parcelize import timber.log.Timber @@ -67,27 +68,7 @@ class ImageContentRenderer @Inject constructor(private val activeSessionHolder: imageView.layoutParams.height = height imageView.layoutParams.width = width - val glideRequest = if (data.elementToDecrypt != null) { - // Encrypted image - GlideApp - .with(imageView) - .load(data) - } else { - // Clear image - val contentUrlResolver = activeSessionHolder.getActiveSession().contentUrlResolver() - val resolvedUrl = when (mode) { - Mode.FULL_SIZE -> contentUrlResolver.resolveFullSize(data.url) - Mode.THUMBNAIL -> contentUrlResolver.resolveThumbnail(data.url, width, height, ContentUrlResolver.ThumbnailMethod.SCALE) - } - //Fallback to base url - ?: data.url - - GlideApp - .with(imageView) - .load(resolvedUrl) - } - - glideRequest + createGlideRequest(data, mode, imageView, width, height) .dontAnimate() .transform(RoundedCorners(dpToPx(8, imageView.context))) .thumbnail(0.3f) @@ -95,31 +76,11 @@ class ImageContentRenderer @Inject constructor(private val activeSessionHolder: } - fun renderFitTarget(data: Data, mode: Mode, imageView: ImageView, callback :((Boolean) -> Unit)? = null) { + fun renderFitTarget(data: Data, mode: Mode, imageView: ImageView, callback: ((Boolean) -> Unit)? = null) { val (width, height) = processSize(data, mode) - val glideRequest = if (data.elementToDecrypt != null) { - // Encrypted image - GlideApp - .with(imageView) - .load(data) - } else { - // Clear image - val contentUrlResolver = activeSessionHolder.getActiveSession().contentUrlResolver() - val resolvedUrl = when (mode) { - Mode.FULL_SIZE -> contentUrlResolver.resolveFullSize(data.url) - Mode.THUMBNAIL -> contentUrlResolver.resolveThumbnail(data.url, width, height, ContentUrlResolver.ThumbnailMethod.SCALE) - } - //Fallback to base url - ?: data.url - - GlideApp - .with(imageView) - .load(resolvedUrl) - } - - glideRequest - .listener(object: RequestListener { + createGlideRequest(data, mode, imageView, width, height) + .listener(object : RequestListener { override fun onLoadFailed(e: GlideException?, model: Any?, target: Target?, @@ -140,7 +101,28 @@ class ImageContentRenderer @Inject constructor(private val activeSessionHolder: }) .fitCenter() .into(imageView) + } + private fun createGlideRequest(data: Data, mode: Mode, imageView: ImageView, width: Int, height: Int): GlideRequest { + return if (data.elementToDecrypt != null) { + // Encrypted image + GlideApp + .with(imageView) + .load(data) + } else { + // Clear image + val contentUrlResolver = activeSessionHolder.getActiveSession().contentUrlResolver() + val resolvedUrl = when (mode) { + Mode.FULL_SIZE -> contentUrlResolver.resolveFullSize(data.url) + Mode.THUMBNAIL -> contentUrlResolver.resolveThumbnail(data.url, width, height, ContentUrlResolver.ThumbnailMethod.SCALE) + } + //Fallback to base url + ?: data.url + + GlideApp + .with(imageView) + .load(resolvedUrl) + } } fun render(data: Data, imageView: BigImageView) {