Clean a bit avatar management

This commit is contained in:
ganfra 2018-11-02 09:12:26 +01:00
parent 18c6472f32
commit 6d9907563e
9 changed files with 58 additions and 71 deletions

View File

@ -1,13 +0,0 @@
package im.vector.riotredesign.core.extensions
import android.content.Context
import android.graphics.drawable.Drawable
import android.support.v4.content.ContextCompat
import com.amulyakhare.textdrawable.TextDrawable
import im.vector.riotredesign.R
fun Context.avatarDrawable(name: String): Drawable {
val avatarColor = ContextCompat.getColor(this, R.color.pale_teal)
return TextDrawable.builder().buildRound(name.firstCharAsString().toUpperCase(), avatarColor)
}

View File

@ -2,7 +2,5 @@ package im.vector.riotredesign.core.utils
object Constants {
const val MEDIA_URL = "https://matrix.org/_matrix/media/v1/download/"
}

View File

@ -0,0 +1,43 @@
package im.vector.riotredesign.features.home
import android.support.v4.content.ContextCompat
import android.widget.ImageView
import com.amulyakhare.textdrawable.TextDrawable
import com.bumptech.glide.request.RequestOptions
import im.vector.matrix.android.api.session.room.model.RoomMember
import im.vector.matrix.android.api.session.room.model.RoomSummary
import im.vector.riotredesign.R
import im.vector.riotredesign.core.extensions.firstCharAsString
import im.vector.riotredesign.core.glide.GlideApp
private const val MEDIA_URL = "https://matrix.org/_matrix/media/v1/download/"
private const val MXC_PREFIX = "mxc://"
object AvatarRenderer {
fun render(roomMember: RoomMember, imageView: ImageView) {
render(roomMember.avatarUrl, roomMember.displayName, imageView)
}
fun render(roomSummary: RoomSummary, imageView: ImageView) {
render(roomSummary.avatarUrl, roomSummary.displayName, imageView)
}
fun render(avatarUrl: String?, name: String?, imageView: ImageView) {
if (name.isNullOrEmpty()) {
return
}
val resolvedUrl = avatarUrl?.replace(MXC_PREFIX, MEDIA_URL)
val avatarColor = ContextCompat.getColor(imageView.context, R.color.pale_teal)
val fallbackDrawable = TextDrawable.builder().buildRound(name.firstCharAsString().toUpperCase(), avatarColor)
GlideApp
.with(imageView)
.load(resolvedUrl)
.placeholder(fallbackDrawable)
.apply(RequestOptions.circleCropTransform())
.into(imageView)
}
}

View File

@ -7,18 +7,15 @@ import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.bumptech.glide.request.RequestOptions
import im.vector.matrix.android.api.Matrix
import im.vector.matrix.android.api.session.events.model.EnrichedEvent
import im.vector.matrix.android.api.session.room.Room
import im.vector.matrix.android.api.session.room.model.RoomSummary
import im.vector.riotredesign.R
import im.vector.riotredesign.core.extensions.avatarDrawable
import im.vector.riotredesign.core.glide.GlideApp
import im.vector.riotredesign.core.platform.RiotFragment
import im.vector.riotredesign.core.platform.ToolbarConfigurable
import im.vector.riotredesign.core.utils.Constants
import im.vector.riotredesign.core.utils.FragmentArgumentDelegate
import im.vector.riotredesign.features.home.AvatarRenderer
import kotlinx.android.synthetic.main.fragment_room_detail.*
import org.koin.android.ext.android.inject
@ -70,13 +67,7 @@ class RoomDetailFragment : RiotFragment() {
private fun renderRoomSummary(roomSummary: RoomSummary?) {
roomSummary?.let {
toolbarTitleView.text = it.displayName
val avatarUrl = it.avatarUrl.replace("mxc://", Constants.MEDIA_URL)
GlideApp
.with(this)
.load(avatarUrl)
.placeholder(riotActivity.avatarDrawable(it.displayName))
.apply(RequestOptions.circleCropTransform())
.into(toolbarAvatarImageView)
AvatarRenderer.render(it, toolbarAvatarImageView)
if (it.topic.isNotEmpty()) {
toolbarSubtitleView.visibility = View.VISIBLE
toolbarSubtitleView.text = it.topic

View File

@ -8,9 +8,7 @@ import im.vector.matrix.android.api.session.events.model.EnrichedEvent
import im.vector.matrix.android.api.session.events.model.EventType
import im.vector.matrix.android.api.session.events.model.roomMember
import im.vector.matrix.android.api.session.room.model.MessageContent
import im.vector.riotredesign.core.extensions.avatarDrawable
import im.vector.riotredesign.core.extensions.localDateTime
import im.vector.riotredesign.core.utils.Constants
import im.vector.riotredesign.features.home.LoadingItemModel_
import org.threeten.bp.format.DateTimeFormatter
import org.threeten.bp.format.FormatStyle
@ -72,16 +70,11 @@ class TimelineEventController(private val context: Context) : EpoxyController(
}
val showInformation = messagesDisplayedWithInformation.contains(event.root.eventId)
val avatarUrl = roomMember.avatarUrl?.replace("mxc://", Constants.MEDIA_URL) ?: ""
TimelineMessageItem(
message = messageContent.body,
avatarUrl = avatarUrl,
avatarUrl = roomMember.avatarUrl,
showInformation = showInformation,
time = date.toLocalTime().format(DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT)),
fallbackAvatarDrawable = context.avatarDrawable(roomMember.displayName
?: ""),
memberName = roomMember.displayName
)
.onBind { timeline?.loadAround(index) }

View File

@ -1,19 +1,16 @@
package im.vector.riotredesign.features.home.room.detail
import android.graphics.drawable.Drawable
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.request.RequestOptions
import im.vector.riotredesign.R
import im.vector.riotredesign.core.epoxy.KotlinModel
import im.vector.riotredesign.core.glide.GlideApp
import im.vector.riotredesign.features.home.AvatarRenderer
data class TimelineMessageItem(
val message: CharSequence? = null,
val time: CharSequence? = null,
val avatarUrl: String,
val fallbackAvatarDrawable: Drawable? = null,
val avatarUrl: String?,
val memberName: CharSequence? = null,
val showInformation: Boolean = true
) : KotlinModel(R.layout.item_event_message) {
@ -29,16 +26,9 @@ data class TimelineMessageItem(
avatarImageView.visibility = View.VISIBLE
memberNameView.visibility = View.VISIBLE
timeView.visibility = View.VISIBLE
GlideApp
.with(avatarImageView)
.load(avatarUrl)
.placeholder(fallbackAvatarDrawable)
.apply(RequestOptions.circleCropTransform())
.into(avatarImageView)
timeView.text = time
memberNameView.text = memberName
AvatarRenderer.render(avatarUrl, memberName?.toString(), avatarImageView)
} else {
avatarImageView.visibility = View.GONE
memberNameView.visibility = View.GONE

View File

@ -36,7 +36,7 @@ class RoomListFragment : RiotFragment(), RoomSummaryController.Callback {
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
roomController = RoomSummaryController(riotActivity, this)
roomController = RoomSummaryController(this)
stateView.contentView = epoxyRecyclerView
epoxyRecyclerView.setController(roomController)

View File

@ -1,13 +1,9 @@
package im.vector.riotredesign.features.home.room.list
import android.content.Context
import com.airbnb.epoxy.Typed2EpoxyController
import im.vector.matrix.android.api.session.room.model.RoomSummary
import im.vector.riotredesign.core.extensions.avatarDrawable
import im.vector.riotredesign.core.utils.Constants
class RoomSummaryController(private val context: Context,
private val callback: Callback? = null
class RoomSummaryController(private val callback: Callback? = null
) : Typed2EpoxyController<List<RoomSummary>, RoomSummary>() {
@ -54,12 +50,9 @@ class RoomSummaryController(private val context: Context,
private fun buildRoomModels(summaries: List<RoomSummary>, selected: RoomSummary?) {
summaries.forEach {
val avatarUrl = it.avatarUrl.replace("mxc://", Constants.MEDIA_URL)
RoomSummaryItem(
title = it.displayName,
avatarUrl = avatarUrl,
fallbackAvatarDrawable = context.avatarDrawable(it.displayName),
roomName = it.displayName,
avatarUrl = it.avatarUrl,
isSelected = it.roomId == selected?.roomId,
listener = { callback?.onRoomSelected(it) }
)

View File

@ -1,19 +1,16 @@
package im.vector.riotredesign.features.home.room.list
import android.graphics.drawable.Drawable
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.request.RequestOptions
import im.vector.riotredesign.R
import im.vector.riotredesign.core.epoxy.KotlinModel
import im.vector.riotredesign.core.glide.GlideApp
import im.vector.riotredesign.core.platform.CheckableFrameLayout
import im.vector.riotredesign.features.home.AvatarRenderer
data class RoomSummaryItem(
val title: CharSequence,
val avatarUrl: String,
val fallbackAvatarDrawable: Drawable,
val roomName: CharSequence,
val avatarUrl: String?,
val isSelected: Boolean,
val listener: (() -> Unit)? = null
) : KotlinModel(R.layout.item_room) {
@ -25,12 +22,7 @@ data class RoomSummaryItem(
override fun bind() {
rootView.isChecked = isSelected
rootView.setOnClickListener { listener?.invoke() }
titleView.text = title
GlideApp
.with(avatarImageView)
.load(avatarUrl)
.placeholder(fallbackAvatarDrawable)
.apply(RequestOptions.circleCropTransform())
.into(avatarImageView)
titleView.text = roomName
AvatarRenderer.render(avatarUrl, roomName.toString(), avatarImageView)
}
}