Use user color for pin background
This commit is contained in:
parent
8f362d83cd
commit
7cad30e6bb
|
@ -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,35 +64,42 @@ class LocationPinProvider @Inject constructor(
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
activeSessionHolder.getActiveSession().getUser(userId)?.toMatrixItem()?.let {
|
activeSessionHolder
|
||||||
val size = dimensionConverter.dpToPx(44)
|
.getActiveSession()
|
||||||
avatarRenderer.render(glideRequests, it, object : CustomTarget<Drawable>(size, size) {
|
.getUser(userId)
|
||||||
override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable>?) {
|
?.toMatrixItem()
|
||||||
Timber.d("## Location: onResourceReady")
|
?.let { userItem ->
|
||||||
val pinDrawable = createPinDrawable(resource)
|
val size = dimensionConverter.dpToPx(44)
|
||||||
cache[userId] = pinDrawable
|
val bgTintColor = matrixItemColorProvider.getColor(userItem)
|
||||||
callback(pinDrawable)
|
avatarRenderer.render(glideRequests, userItem, object : CustomTarget<Drawable>(size, size) {
|
||||||
}
|
override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable>?) {
|
||||||
|
Timber.d("## Location: onResourceReady")
|
||||||
|
val pinDrawable = createPinDrawable(resource, bgTintColor)
|
||||||
|
cache[userId] = pinDrawable
|
||||||
|
callback(pinDrawable)
|
||||||
|
}
|
||||||
|
|
||||||
override fun onLoadCleared(placeholder: Drawable?) {
|
override fun onLoadCleared(placeholder: Drawable?) {
|
||||||
// Is it possible? Put placeholder instead?
|
// Is it possible? Put placeholder instead?
|
||||||
// FIXME The doc says it has to be implemented and should free resources
|
// FIXME The doc says it has to be implemented and should free resources
|
||||||
Timber.d("## Location: onLoadCleared")
|
Timber.d("## Location: onLoadCleared")
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
||||||
|
|
Loading…
Reference in New Issue