From 6d9907563ef00489c9ebcbf0e5c1b37fef0dcf62 Mon Sep 17 00:00:00 2001 From: ganfra Date: Fri, 2 Nov 2018 09:12:26 +0100 Subject: [PATCH] Clean a bit avatar management --- .../riotredesign/core/extensions/Context.kt | 13 ------ .../riotredesign/core/utils/Constants.kt | 2 - .../features/home/AvatarRenderer.kt | 43 +++++++++++++++++++ .../home/room/detail/RoomDetailFragment.kt | 13 +----- .../room/detail/TimelineEventController.kt | 9 +--- .../home/room/detail/TimelineMessageItem.kt | 16 ++----- .../home/room/list/RoomListFragment.kt | 2 +- .../home/room/list/RoomSummaryController.kt | 13 ++---- .../home/room/list/RoomSummaryItem.kt | 18 +++----- 9 files changed, 58 insertions(+), 71 deletions(-) delete mode 100644 app/src/main/java/im/vector/riotredesign/core/extensions/Context.kt create mode 100644 app/src/main/java/im/vector/riotredesign/features/home/AvatarRenderer.kt diff --git a/app/src/main/java/im/vector/riotredesign/core/extensions/Context.kt b/app/src/main/java/im/vector/riotredesign/core/extensions/Context.kt deleted file mode 100644 index 4b750266..00000000 --- a/app/src/main/java/im/vector/riotredesign/core/extensions/Context.kt +++ /dev/null @@ -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) -} \ No newline at end of file diff --git a/app/src/main/java/im/vector/riotredesign/core/utils/Constants.kt b/app/src/main/java/im/vector/riotredesign/core/utils/Constants.kt index d7bbad69..ade18b36 100644 --- a/app/src/main/java/im/vector/riotredesign/core/utils/Constants.kt +++ b/app/src/main/java/im/vector/riotredesign/core/utils/Constants.kt @@ -2,7 +2,5 @@ package im.vector.riotredesign.core.utils object Constants { - const val MEDIA_URL = "https://matrix.org/_matrix/media/v1/download/" - } \ No newline at end of file diff --git a/app/src/main/java/im/vector/riotredesign/features/home/AvatarRenderer.kt b/app/src/main/java/im/vector/riotredesign/features/home/AvatarRenderer.kt new file mode 100644 index 00000000..0af2a3f4 --- /dev/null +++ b/app/src/main/java/im/vector/riotredesign/features/home/AvatarRenderer.kt @@ -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) + } + + +} \ No newline at end of file diff --git a/app/src/main/java/im/vector/riotredesign/features/home/room/detail/RoomDetailFragment.kt b/app/src/main/java/im/vector/riotredesign/features/home/room/detail/RoomDetailFragment.kt index a826720d..2e942dac 100644 --- a/app/src/main/java/im/vector/riotredesign/features/home/room/detail/RoomDetailFragment.kt +++ b/app/src/main/java/im/vector/riotredesign/features/home/room/detail/RoomDetailFragment.kt @@ -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 diff --git a/app/src/main/java/im/vector/riotredesign/features/home/room/detail/TimelineEventController.kt b/app/src/main/java/im/vector/riotredesign/features/home/room/detail/TimelineEventController.kt index fc99e476..580b1753 100644 --- a/app/src/main/java/im/vector/riotredesign/features/home/room/detail/TimelineEventController.kt +++ b/app/src/main/java/im/vector/riotredesign/features/home/room/detail/TimelineEventController.kt @@ -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) } diff --git a/app/src/main/java/im/vector/riotredesign/features/home/room/detail/TimelineMessageItem.kt b/app/src/main/java/im/vector/riotredesign/features/home/room/detail/TimelineMessageItem.kt index 4d3d07c8..ceeab76f 100644 --- a/app/src/main/java/im/vector/riotredesign/features/home/room/detail/TimelineMessageItem.kt +++ b/app/src/main/java/im/vector/riotredesign/features/home/room/detail/TimelineMessageItem.kt @@ -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 diff --git a/app/src/main/java/im/vector/riotredesign/features/home/room/list/RoomListFragment.kt b/app/src/main/java/im/vector/riotredesign/features/home/room/list/RoomListFragment.kt index d7384607..8daa10df 100644 --- a/app/src/main/java/im/vector/riotredesign/features/home/room/list/RoomListFragment.kt +++ b/app/src/main/java/im/vector/riotredesign/features/home/room/list/RoomListFragment.kt @@ -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) diff --git a/app/src/main/java/im/vector/riotredesign/features/home/room/list/RoomSummaryController.kt b/app/src/main/java/im/vector/riotredesign/features/home/room/list/RoomSummaryController.kt index f50ed247..cdcdad9c 100644 --- a/app/src/main/java/im/vector/riotredesign/features/home/room/list/RoomSummaryController.kt +++ b/app/src/main/java/im/vector/riotredesign/features/home/room/list/RoomSummaryController.kt @@ -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, RoomSummary>() { @@ -54,12 +50,9 @@ class RoomSummaryController(private val context: Context, private fun buildRoomModels(summaries: List, 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) } ) diff --git a/app/src/main/java/im/vector/riotredesign/features/home/room/list/RoomSummaryItem.kt b/app/src/main/java/im/vector/riotredesign/features/home/room/list/RoomSummaryItem.kt index 147fbaf8..8e5d837f 100644 --- a/app/src/main/java/im/vector/riotredesign/features/home/room/list/RoomSummaryItem.kt +++ b/app/src/main/java/im/vector/riotredesign/features/home/room/list/RoomSummaryItem.kt @@ -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) } } \ No newline at end of file