Reduce code smell

This commit is contained in:
Florian Renaud 2022-06-14 15:16:17 +02:00
parent 398e98ae85
commit d42a3da5b7

View File

@ -16,6 +16,7 @@
package im.vector.app.features.home.room.detail.timeline.item package im.vector.app.features.home.room.detail.timeline.item
import android.content.res.Resources
import android.text.SpannableString import android.text.SpannableString
import android.text.method.MovementMethod import android.text.method.MovementMethod
import android.text.style.ClickableSpan import android.text.style.ClickableSpan
@ -25,7 +26,6 @@ import android.widget.ImageView
import android.widget.TextView import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.view.isGone
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.core.view.updateLayoutParams import androidx.core.view.updateLayoutParams
import com.airbnb.epoxy.EpoxyAttribute import com.airbnb.epoxy.EpoxyAttribute
@ -39,6 +39,7 @@ import im.vector.app.features.home.room.detail.RoomDetailAction
import im.vector.app.features.home.room.detail.timeline.TimelineEventController import im.vector.app.features.home.room.detail.timeline.TimelineEventController
import im.vector.app.features.home.room.detail.timeline.tools.linkify import im.vector.app.features.home.room.detail.timeline.tools.linkify
import me.gujun.android.span.span import me.gujun.android.span.span
import org.matrix.android.sdk.api.extensions.orFalse
import org.matrix.android.sdk.api.session.room.model.RoomSummary import org.matrix.android.sdk.api.session.room.model.RoomSummary
import org.matrix.android.sdk.api.session.room.model.localecho.RoomLocalEcho import org.matrix.android.sdk.api.session.room.model.localecho.RoomLocalEcho
import org.matrix.android.sdk.api.util.toMatrixItem import org.matrix.android.sdk.api.util.toMatrixItem
@ -62,44 +63,45 @@ abstract class MergedRoomCreationItem : BasedMergedItem<MergedRoomCreationItem.H
} }
private fun bindMergedViews(holder: Holder) { private fun bindMergedViews(holder: Holder) {
val isLocalRoom = RoomLocalEcho.isLocalEchoId(attributes.roomSummary?.roomId.orEmpty()) holder.mergedView.isVisible = !attributes.isLocalRoom
holder.mergedView.isVisible = !isLocalRoom
if (attributes.isCollapsed) { if (attributes.isCollapsed) {
// Take the oldest data // Take the oldest data
val data = distinctMergeData.lastOrNull() val data = distinctMergeData.lastOrNull()
holder.summaryView.text = getSummaryText(holder.expandView.resources, data)
val createdFromCurrentUser = data?.userId == attributes.currentUserId
val summary = if (createdFromCurrentUser) {
if (data?.isDirectRoom == true) {
holder.expandView.resources.getString(R.string.direct_room_created_summary_item_by_you)
} else {
holder.expandView.resources.getString(R.string.room_created_summary_item_by_you)
}
} else {
if (data?.isDirectRoom == true) {
holder.expandView.resources.getString(R.string.direct_room_created_summary_item, data.memberName)
} else {
holder.expandView.resources.getString(R.string.room_created_summary_item, data?.memberName ?: data?.userId ?: "")
}
}
holder.summaryView.text = summary
holder.summaryView.visibility = View.VISIBLE holder.summaryView.visibility = View.VISIBLE
holder.avatarView.visibility = View.VISIBLE
if (data != null) { if (data != null) {
holder.avatarView.visibility = View.VISIBLE holder.avatarView.visibility = View.VISIBLE
attributes.avatarRenderer.render(data.toMatrixItem(), holder.avatarView) attributes.avatarRenderer.render(data.toMatrixItem(), holder.avatarView)
} else { } else {
holder.avatarView.visibility = View.GONE holder.avatarView.visibility = View.GONE
} }
bindEncryptionTile(holder, data) bindEncryptionTile(holder, data)
} else { } else {
holder.avatarView.visibility = View.INVISIBLE holder.avatarView.visibility = View.INVISIBLE
holder.summaryView.visibility = View.GONE holder.summaryView.visibility = View.GONE
holder.encryptionTile.isGone = true holder.encryptionTile.visibility = View.GONE
} }
} }
private fun getSummaryText(resources: Resources, data: Data?): String {
val createdFromCurrentUser = data?.userId == attributes.currentUserId
val isDirectRoom = data?.isDirectRoom.orFalse()
val summary = if (createdFromCurrentUser) {
if (isDirectRoom) {
resources.getString(R.string.direct_room_created_summary_item_by_you)
} else {
resources.getString(R.string.room_created_summary_item_by_you)
}
} else {
if (isDirectRoom) {
resources.getString(R.string.direct_room_created_summary_item, data?.memberName.orEmpty())
} else {
resources.getString(R.string.room_created_summary_item, data?.memberName.orEmpty())
}
}
return summary
}
private fun bindEncryptionTile(holder: Holder, data: Data?) { private fun bindEncryptionTile(holder: Holder, data: Data?) {
if (attributes.hasEncryptionEvent) { if (attributes.hasEncryptionEvent) {
holder.encryptionTile.isVisible = true holder.encryptionTile.isVisible = true
@ -108,17 +110,7 @@ abstract class MergedRoomCreationItem : BasedMergedItem<MergedRoomCreationItem.H
} }
if (attributes.isEncryptionAlgorithmSecure) { if (attributes.isEncryptionAlgorithmSecure) {
holder.e2eTitleTextView.text = holder.expandView.resources.getString(R.string.encryption_enabled) holder.e2eTitleTextView.text = holder.expandView.resources.getString(R.string.encryption_enabled)
holder.e2eTitleDescriptionView.text = when { holder.e2eTitleDescriptionView.text = getE2ESecureDescriptionText(holder.expandView.resources, data)
data?.isDirectRoom == true && RoomLocalEcho.isLocalEchoId(data.roomId.orEmpty()) -> {
holder.expandView.resources.getString(R.string.direct_room_encryption_enabled_tile_description_future)
}
data?.isDirectRoom == true -> {
holder.expandView.resources.getString(R.string.direct_room_encryption_enabled_tile_description)
}
else -> {
holder.expandView.resources.getString(R.string.encryption_enabled_tile_description)
}
}
holder.e2eTitleDescriptionView.textAlignment = View.TEXT_ALIGNMENT_CENTER holder.e2eTitleDescriptionView.textAlignment = View.TEXT_ALIGNMENT_CENTER
holder.e2eTitleTextView.setCompoundDrawablesWithIntrinsicBounds( holder.e2eTitleTextView.setCompoundDrawablesWithIntrinsicBounds(
ContextCompat.getDrawable(holder.view.context, R.drawable.ic_shield_black), ContextCompat.getDrawable(holder.view.context, R.drawable.ic_shield_black),
@ -137,17 +129,32 @@ abstract class MergedRoomCreationItem : BasedMergedItem<MergedRoomCreationItem.H
} }
} }
private fun getE2ESecureDescriptionText(resources: Resources, data: Data?): String {
val isDirectRoom = data?.isDirectRoom.orFalse()
return when {
isDirectRoom -> {
if (attributes.isLocalRoom) {
resources.getString(R.string.direct_room_encryption_enabled_tile_description_future)
} else {
resources.getString(R.string.direct_room_encryption_enabled_tile_description)
}
}
else -> {
resources.getString(R.string.encryption_enabled_tile_description)
}
}
}
private fun bindCreationSummaryTile(holder: Holder) { private fun bindCreationSummaryTile(holder: Holder) {
val roomSummary = attributes.roomSummary val roomSummary = attributes.roomSummary
val roomDisplayName = roomSummary?.displayName val roomDisplayName = roomSummary?.displayName
holder.roomNameText.setTextOrHide(roomDisplayName) holder.roomNameText.setTextOrHide(roomDisplayName)
val isDirect = roomSummary?.isDirect == true val isDirect = roomSummary?.isDirect == true
val isLocalRoom = RoomLocalEcho.isLocalEchoId(roomSummary?.roomId.orEmpty())
val membersCount = roomSummary?.otherMemberIds?.size ?: 0 val membersCount = roomSummary?.otherMemberIds?.size ?: 0
when { when {
isDirect -> { isDirect -> {
if (isLocalRoom) { if (attributes.isLocalRoom) {
holder.roomDescriptionText.text = holder.view.resources.getString( holder.roomDescriptionText.text = holder.view.resources.getString(
R.string.send_your_first_msg_to_invite, R.string.send_your_first_msg_to_invite,
roomSummary?.displayName.orEmpty() roomSummary?.displayName.orEmpty()
@ -261,5 +268,8 @@ abstract class MergedRoomCreationItem : BasedMergedItem<MergedRoomCreationItem.H
val canChangeAvatar: Boolean = false, val canChangeAvatar: Boolean = false,
val canChangeName: Boolean = false, val canChangeName: Boolean = false,
val canChangeTopic: Boolean = false val canChangeTopic: Boolean = false
) : BasedMergedItem.Attributes ) : BasedMergedItem.Attributes {
val isLocalRoom = RoomLocalEcho.isLocalEchoId(roomSummary?.roomId.orEmpty())
}
} }