Fix issue after rebase and use classic request for clear image

This commit is contained in:
Benoit Marty 2019-07-01 19:59:51 +02:00
parent 164c8dab09
commit 014d03893a
3 changed files with 39 additions and 13 deletions

View File

@ -24,6 +24,8 @@ import com.bumptech.glide.GlideBuilder
import com.bumptech.glide.Registry import com.bumptech.glide.Registry
import com.bumptech.glide.annotation.GlideModule import com.bumptech.glide.annotation.GlideModule
import com.bumptech.glide.module.AppGlideModule import com.bumptech.glide.module.AppGlideModule
import im.vector.riotredesign.core.extensions.vectorComponent
import im.vector.riotredesign.features.media.ImageContentRenderer
import java.io.InputStream import java.io.InputStream


@GlideModule @GlideModule
@ -34,7 +36,8 @@ class MyAppGlideModule : AppGlideModule() {
} }


override fun registerComponents(context: Context, glide: Glide, registry: Registry) { override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
// FIXME This does not work registry.append(ImageContentRenderer.Data::class.java,
registry.append(InputStream::class.java, InputStream::class.java, VectorGlideModelLoaderFactory()) InputStream::class.java,
VectorGlideModelLoaderFactory(context.vectorComponent().activeSessionHolder()))
} }
} }

View File

@ -24,8 +24,8 @@ import com.bumptech.glide.load.model.ModelLoader
import com.bumptech.glide.load.model.ModelLoaderFactory import com.bumptech.glide.load.model.ModelLoaderFactory
import com.bumptech.glide.load.model.MultiModelLoaderFactory import com.bumptech.glide.load.model.MultiModelLoaderFactory
import com.bumptech.glide.signature.ObjectKey import com.bumptech.glide.signature.ObjectKey
import im.vector.matrix.android.api.Matrix
import im.vector.matrix.android.internal.crypto.attachments.MXEncryptedAttachments import im.vector.matrix.android.internal.crypto.attachments.MXEncryptedAttachments
import im.vector.riotredesign.core.di.ActiveSessionHolder
import im.vector.riotredesign.features.media.ImageContentRenderer import im.vector.riotredesign.features.media.ImageContentRenderer
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import okhttp3.Request import okhttp3.Request
@ -37,10 +37,11 @@ import java.io.InputStream
import com.bumptech.glide.load.engine.Resource as Resource1 import com.bumptech.glide.load.engine.Resource as Resource1




class VectorGlideModelLoaderFactory : ModelLoaderFactory<ImageContentRenderer.Data, InputStream> { class VectorGlideModelLoaderFactory(private val activeSessionHolder: ActiveSessionHolder)
: ModelLoaderFactory<ImageContentRenderer.Data, InputStream> {


override fun build(multiFactory: MultiModelLoaderFactory): ModelLoader<ImageContentRenderer.Data, InputStream> { override fun build(multiFactory: MultiModelLoaderFactory): ModelLoader<ImageContentRenderer.Data, InputStream> {
return VectorGlideModelLoader() return VectorGlideModelLoader(activeSessionHolder)
} }


override fun teardown() { override fun teardown() {
@ -49,20 +50,23 @@ class VectorGlideModelLoaderFactory : ModelLoaderFactory<ImageContentRenderer.Da


} }


class VectorGlideModelLoader : ModelLoader<ImageContentRenderer.Data, InputStream> { class VectorGlideModelLoader(private val activeSessionHolder: ActiveSessionHolder)
: ModelLoader<ImageContentRenderer.Data, InputStream> {
override fun handles(model: ImageContentRenderer.Data): Boolean { override fun handles(model: ImageContentRenderer.Data): Boolean {
// Always handle // Always handle
return true return true
} }


override fun buildLoadData(model: ImageContentRenderer.Data, width: Int, height: Int, options: Options): ModelLoader.LoadData<InputStream>? { override fun buildLoadData(model: ImageContentRenderer.Data, width: Int, height: Int, options: Options): ModelLoader.LoadData<InputStream>? {
return ModelLoader.LoadData(ObjectKey(model), VectorGlideDataFetcher(model, width, height)) return ModelLoader.LoadData(ObjectKey(model), VectorGlideDataFetcher(activeSessionHolder, model, width, height))
} }
} }


class VectorGlideDataFetcher(private val data: ImageContentRenderer.Data, class VectorGlideDataFetcher(private val activeSessionHolder: ActiveSessionHolder,
private val data: ImageContentRenderer.Data,
private val width: Int, private val width: Int,
private val height: Int) : DataFetcher<InputStream> { private val height: Int)
: DataFetcher<InputStream> {


val client = OkHttpClient() val client = OkHttpClient()


@ -99,7 +103,7 @@ class VectorGlideDataFetcher(private val data: ImageContentRenderer.Data,
callback.onDataReady(FileInputStream(initialFile)) callback.onDataReady(FileInputStream(initialFile))
return return
} }
val contentUrlResolver = Matrix.getInstance().currentSession?.contentUrlResolver() ?: return val contentUrlResolver = activeSessionHolder.getActiveSession().contentUrlResolver()
val url = contentUrlResolver.resolveFullSize(data.url) val url = contentUrlResolver.resolveFullSize(data.url)
?: return ?: return



View File

@ -60,9 +60,28 @@ class ImageContentRenderer @Inject constructor(private val activeSessionHolder:
val (width, height) = processSize(data, mode) val (width, height) = processSize(data, mode)
imageView.layoutParams.height = height imageView.layoutParams.height = height
imageView.layoutParams.width = width imageView.layoutParams.width = width

val glideRequest = if (data.elementToDecrypt != null) {
// Encrypted image
GlideApp GlideApp
.with(imageView) .with(imageView)
.load(data) .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
.dontAnimate() .dontAnimate()
.transform(RoundedCorners(dpToPx(8, imageView.context))) .transform(RoundedCorners(dpToPx(8, imageView.context)))
.thumbnail(0.3f) .thumbnail(0.3f)