Timeline : fix avatar issues / remove glide logs

This commit is contained in:
ganfra 2019-03-28 16:06:43 +01:00
parent a6366e47fe
commit 86a60f7ebd
3 changed files with 34 additions and 39 deletions

View file

@ -16,8 +16,19 @@
package im.vector.riotredesign.core.glide; package im.vector.riotredesign.core.glide;
import android.content.Context;
import android.util.Log;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.annotation.GlideModule; import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.module.AppGlideModule; import com.bumptech.glide.module.AppGlideModule;
@GlideModule @GlideModule
public final class MyAppGlideModule extends AppGlideModule {} public final class MyAppGlideModule extends AppGlideModule {
@Override
public void applyOptions(Context context, GlideBuilder builder) {
builder.setLogLevel(Log.ERROR);
}
}

View file

@ -19,11 +19,10 @@ package im.vector.riotredesign.features.home
import android.content.Context import android.content.Context
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.widget.ImageView import android.widget.ImageView
import androidx.annotation.AnyThread
import androidx.annotation.UiThread import androidx.annotation.UiThread
import androidx.annotation.WorkerThread
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import com.amulyakhare.textdrawable.TextDrawable import com.amulyakhare.textdrawable.TextDrawable
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.DrawableImageViewTarget import com.bumptech.glide.request.target.DrawableImageViewTarget
import com.bumptech.glide.request.target.Target import com.bumptech.glide.request.target.Target
@ -40,8 +39,11 @@ import im.vector.riotredesign.core.glide.GlideRequests
/** /**
* This helper centralise ways to retrieve avatar into ImageView or even generic Target<Drawable> * This helper centralise ways to retrieve avatar into ImageView or even generic Target<Drawable>
*/ */
object AvatarRenderer { object AvatarRenderer {
private const val THUMBNAIL_SIZE = 250
@UiThread @UiThread
fun render(roomMember: RoomMember, imageView: ImageView) { fun render(roomMember: RoomMember, imageView: ImageView) {
render(roomMember.avatarUrl, roomMember.displayName, imageView) render(roomMember.avatarUrl, roomMember.displayName, imageView)
@ -54,7 +56,7 @@ object AvatarRenderer {
@UiThread @UiThread
fun render(avatarUrl: String?, name: String?, imageView: ImageView) { fun render(avatarUrl: String?, name: String?, imageView: ImageView) {
render(imageView.context, GlideApp.with(imageView), avatarUrl, name, imageView.height, DrawableImageViewTarget(imageView)) render(imageView.context, GlideApp.with(imageView), avatarUrl, name, DrawableImageViewTarget(imageView))
} }
@UiThread @UiThread
@ -62,45 +64,18 @@ object AvatarRenderer {
glideRequest: GlideRequests, glideRequest: GlideRequests,
avatarUrl: String?, avatarUrl: String?,
name: String?, name: String?,
size: Int,
target: Target<Drawable>) { target: Target<Drawable>) {
if (name.isNullOrEmpty()) { if (name.isNullOrEmpty()) {
return return
} }
val placeholder = buildPlaceholderDrawable(context, name) val placeholder = getPlaceholderDrawable(context, name)
buildGlideRequest(glideRequest, avatarUrl, size) buildGlideRequest(glideRequest, avatarUrl)
.placeholder(placeholder) .placeholder(placeholder)
.into(target) .into(target)
} }
@WorkerThread @AnyThread
fun getCachedOrPlaceholder(context: Context, fun getPlaceholderDrawable(context: Context, text: String): Drawable {
glideRequest: GlideRequests,
avatarUrl: String?,
text: String,
size: Int): Drawable {
val future = buildGlideRequest(glideRequest, avatarUrl, size).onlyRetrieveFromCache(true).submit()
return try {
future.get()
} catch (exception: Exception) {
buildPlaceholderDrawable(context, text)
}
}
// PRIVATE API *********************************************************************************
private fun buildGlideRequest(glideRequest: GlideRequests, avatarUrl: String?, size: Int): GlideRequest<Drawable> {
val resolvedUrl = Matrix.getInstance().currentSession
.contentUrlResolver()
.resolveThumbnail(avatarUrl, size, size, ContentUrlResolver.ThumbnailMethod.SCALE)
return glideRequest
.load(resolvedUrl)
.apply(RequestOptions.circleCropTransform())
.diskCacheStrategy(DiskCacheStrategy.DATA)
}
private fun buildPlaceholderDrawable(context: Context, text: String): Drawable {
val avatarColor = ContextCompat.getColor(context, R.color.pale_teal) val avatarColor = ContextCompat.getColor(context, R.color.pale_teal)
return if (text.isEmpty()) { return if (text.isEmpty()) {
TextDrawable.builder().buildRound("", avatarColor) TextDrawable.builder().buildRound("", avatarColor)
@ -113,4 +88,15 @@ object AvatarRenderer {
} }
// PRIVATE API *********************************************************************************
private fun buildGlideRequest(glideRequest: GlideRequests, avatarUrl: String?): GlideRequest<Drawable> {
val resolvedUrl = Matrix.getInstance().currentSession.contentUrlResolver()
.resolveThumbnail(avatarUrl, THUMBNAIL_SIZE, THUMBNAIL_SIZE, ContentUrlResolver.ThumbnailMethod.SCALE)
return glideRequest
.load(resolvedUrl)
.apply(RequestOptions.circleCropTransform())
}
} }

View file

@ -37,8 +37,6 @@ import java.lang.ref.WeakReference
* It's needed to call [bind] method to start requesting avatar, otherwise only the placeholder icon will be displayed if not already cached. * It's needed to call [bind] method to start requesting avatar, otherwise only the placeholder icon will be displayed if not already cached.
*/ */
private const val PILL_AVATAR_SIZE = 80
class PillImageSpan(private val glideRequests: GlideRequests, class PillImageSpan(private val glideRequests: GlideRequests,
private val context: Context, private val context: Context,
private val userId: String, private val userId: String,
@ -55,7 +53,7 @@ class PillImageSpan(private val glideRequests: GlideRequests,
@UiThread @UiThread
fun bind(textView: TextView) { fun bind(textView: TextView) {
tv = WeakReference(textView) tv = WeakReference(textView)
AvatarRenderer.render(context, glideRequests, user?.avatarUrl, displayName, PILL_AVATAR_SIZE, target) AvatarRenderer.render(context, glideRequests, user?.avatarUrl, displayName, target)
} }
// ReplacementSpan ***************************************************************************** // ReplacementSpan *****************************************************************************
@ -108,7 +106,7 @@ class PillImageSpan(private val glideRequests: GlideRequests,
textStartPadding = textPadding textStartPadding = textPadding
setChipMinHeightResource(R.dimen.pill_min_height) setChipMinHeightResource(R.dimen.pill_min_height)
setChipIconSizeResource(R.dimen.pill_avatar_size) setChipIconSizeResource(R.dimen.pill_avatar_size)
chipIcon = AvatarRenderer.getCachedOrPlaceholder(context, glideRequests, user?.avatarUrl, displayName, PILL_AVATAR_SIZE) chipIcon = AvatarRenderer.getPlaceholderDrawable(context, displayName)
setBounds(0, 0, intrinsicWidth, intrinsicHeight) setBounds(0, 0, intrinsicWidth, intrinsicHeight)
} }
} }