Use user color for pin background

This commit is contained in:
Maxime Naturel 2022-03-03 10:58:37 +01:00
parent 8f362d83cd
commit 7cad30e6bb
1 changed files with 34 additions and 24 deletions

View File

@ -19,7 +19,9 @@ package im.vector.app.features.home.room.detail.timeline.helper
import android.content.Context import android.content.Context
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.graphics.drawable.LayerDrawable import android.graphics.drawable.LayerDrawable
import androidx.annotation.ColorInt
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.DrawableCompat
import com.bumptech.glide.request.target.CustomTarget import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition import com.bumptech.glide.request.transition.Transition
import im.vector.app.R import im.vector.app.R
@ -37,7 +39,8 @@ class LocationPinProvider @Inject constructor(
private val context: Context, private val context: Context,
private val activeSessionHolder: ActiveSessionHolder, private val activeSessionHolder: ActiveSessionHolder,
private val dimensionConverter: DimensionConverter, private val dimensionConverter: DimensionConverter,
private val avatarRenderer: AvatarRenderer private val avatarRenderer: AvatarRenderer,
private val matrixItemColorProvider: MatrixItemColorProvider
) { ) {
private val cache = mutableMapOf<String, Drawable>() private val cache = mutableMapOf<String, Drawable>()
@ -61,12 +64,17 @@ class LocationPinProvider @Inject constructor(
return return
} }
activeSessionHolder.getActiveSession().getUser(userId)?.toMatrixItem()?.let { activeSessionHolder
.getActiveSession()
.getUser(userId)
?.toMatrixItem()
?.let { userItem ->
val size = dimensionConverter.dpToPx(44) val size = dimensionConverter.dpToPx(44)
avatarRenderer.render(glideRequests, it, object : CustomTarget<Drawable>(size, size) { val bgTintColor = matrixItemColorProvider.getColor(userItem)
avatarRenderer.render(glideRequests, userItem, object : CustomTarget<Drawable>(size, size) {
override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable>?) { override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable>?) {
Timber.d("## Location: onResourceReady") Timber.d("## Location: onResourceReady")
val pinDrawable = createPinDrawable(resource) val pinDrawable = createPinDrawable(resource, bgTintColor)
cache[userId] = pinDrawable cache[userId] = pinDrawable
callback(pinDrawable) callback(pinDrawable)
} }
@ -80,7 +88,7 @@ class LocationPinProvider @Inject constructor(
override fun onLoadFailed(errorDrawable: Drawable?) { override fun onLoadFailed(errorDrawable: Drawable?) {
Timber.w("## Location: onLoadFailed") Timber.w("## Location: onLoadFailed")
errorDrawable ?: return errorDrawable ?: return
val pinDrawable = createPinDrawable(errorDrawable) val pinDrawable = createPinDrawable(errorDrawable, bgTintColor)
cache[userId] = pinDrawable cache[userId] = pinDrawable
callback(pinDrawable) callback(pinDrawable)
} }
@ -88,8 +96,10 @@ class LocationPinProvider @Inject constructor(
} }
} }
private fun createPinDrawable(drawable: Drawable): Drawable { private fun createPinDrawable(drawable: Drawable, @ColorInt bgTintColor: Int): Drawable {
val bgUserPin = ContextCompat.getDrawable(context, R.drawable.bg_map_user_pin)!! val bgUserPin = ContextCompat.getDrawable(context, R.drawable.bg_map_user_pin)!!
// use mutate on drawable to avoid sharing the color when we have multiple different user pins
DrawableCompat.setTint(bgUserPin.mutate(), bgTintColor)
val layerDrawable = LayerDrawable(arrayOf(bgUserPin, drawable)) val layerDrawable = LayerDrawable(arrayOf(bgUserPin, drawable))
val horizontalInset = dimensionConverter.dpToPx(4) val horizontalInset = dimensionConverter.dpToPx(4)
val topInset = dimensionConverter.dpToPx(4) val topInset = dimensionConverter.dpToPx(4)