Simple code

This commit is contained in:
Benoit Marty 2020-09-30 11:42:07 +02:00
parent d225fb7df0
commit b5311aa3df

View File

@ -16,7 +16,6 @@
package im.vector.app.features.home.room.detail.timeline.format package im.vector.app.features.home.room.detail.timeline.format
import androidx.annotation.StringRes
import im.vector.app.ActiveSessionDataSource import im.vector.app.ActiveSessionDataSource
import im.vector.app.R import im.vector.app.R
import im.vector.app.core.resources.StringProvider import im.vector.app.core.resources.StringProvider
@ -57,26 +56,9 @@ class NoticeEventFormatter @Inject constructor(private val activeSessionDataSour
private fun Event.isSentByCurrentUser() = senderId != null && senderId == currentUserId private fun Event.isSentByCurrentUser() = senderId != null && senderId == currentUserId
private fun isDirectRoom(roomId: String?): Boolean { private fun Event.isDm() = roomId?.let {
return roomId?.let { activeSessionDataSource.currentValue?.orNull()?.getRoomSummary(it)?.isDirect
activeSessionDataSource.currentValue?.orNull()?.getRoomSummary(roomId)?.isDirect }.orFalse()
}.orFalse()
}
private fun chooseResourceByRoomType(event: Event,
@StringRes directRoomResId: Int,
@StringRes directRoomByUserResId: Int,
@StringRes publicRoomResId: Int,
@StringRes publicRoomByUserResId: Int,
userResArgs: Any? = null,
vararg thirdPartyResArgs: Any?
): String {
return if (isDirectRoom(event.roomId)) {
if (event.isSentByCurrentUser()) sp.getString(directRoomByUserResId, userResArgs) else sp.getString(directRoomResId, *thirdPartyResArgs)
} else {
if (event.isSentByCurrentUser()) sp.getString(publicRoomByUserResId, userResArgs) else sp.getString(publicRoomResId, *thirdPartyResArgs)
}
}
fun format(timelineEvent: TimelineEvent): CharSequence? { fun format(timelineEvent: TimelineEvent): CharSequence? {
return when (val type = timelineEvent.root.getClearType()) { return when (val type = timelineEvent.root.getClearType()) {
@ -201,14 +183,10 @@ class NoticeEventFormatter @Inject constructor(private val activeSessionDataSour
return event.getClearContent().toModel<RoomCreateContent>() return event.getClearContent().toModel<RoomCreateContent>()
?.takeIf { it.creator.isNullOrBlank().not() } ?.takeIf { it.creator.isNullOrBlank().not() }
?.let { ?.let {
chooseResourceByRoomType( if (event.isSentByCurrentUser())
event = event, sp.getString(if (event.isDm()) R.string.notice_direct_room_created_by_you else R.string.notice_room_created_by_you)
directRoomResId = R.string.notice_room_created_by_you, else
directRoomByUserResId = R.string.notice_direct_room_created_by_you, sp.getString(if (event.isDm()) R.string.notice_room_created_by_you else R.string.notice_room_created, it.creator)
publicRoomResId = R.string.notice_room_created,
publicRoomByUserResId = R.string.notice_room_created_by_you,
thirdPartyResArgs = *arrayOf(it.creator)
)
} }
} }
@ -230,14 +208,10 @@ class NoticeEventFormatter @Inject constructor(private val activeSessionDataSour
} }
private fun formatRoomTombstoneEvent(event: Event, senderName: String?): CharSequence? { private fun formatRoomTombstoneEvent(event: Event, senderName: String?): CharSequence? {
return chooseResourceByRoomType( return if (event.isSentByCurrentUser())
event = event, sp.getString(if (event.isDm()) R.string.notice_direct_room_update_by_you else R.string.notice_room_update_by_you)
directRoomResId = R.string.notice_direct_room_update, else
directRoomByUserResId = R.string.notice_direct_room_update_by_you, sp.getString(if (event.isDm()) R.string.notice_direct_room_update else R.string.notice_room_update, senderName)
publicRoomResId = R.string.notice_room_update,
publicRoomByUserResId = R.string.notice_room_update_by_you,
thirdPartyResArgs = *arrayOf(senderName)
)
} }
private fun formatRoomTopicEvent(event: Event, senderName: String?): CharSequence? { private fun formatRoomTopicEvent(event: Event, senderName: String?): CharSequence? {
@ -278,15 +252,12 @@ class NoticeEventFormatter @Inject constructor(private val activeSessionDataSour
val historyVisibility = event.getClearContent().toModel<RoomHistoryVisibilityContent>()?.historyVisibility ?: return null val historyVisibility = event.getClearContent().toModel<RoomHistoryVisibilityContent>()?.historyVisibility ?: return null
val formattedVisibility = roomHistoryVisibilityFormatter.format(historyVisibility) val formattedVisibility = roomHistoryVisibilityFormatter.format(historyVisibility)
return chooseResourceByRoomType( return if (event.isSentByCurrentUser())
event = event, sp.getString(if (event.isDm()) R.string.notice_made_future_direct_room_visibility_by_you else R.string.notice_made_future_room_visibility_by_you,
directRoomResId = R.string.notice_made_future_direct_room_visibility, formattedVisibility)
directRoomByUserResId = R.string.notice_made_future_direct_room_visibility_by_you, else
publicRoomResId = R.string.notice_made_future_room_visibility, sp.getString(if (event.isDm()) R.string.notice_made_future_direct_room_visibility else R.string.notice_made_future_room_visibility,
publicRoomByUserResId = R.string.notice_made_future_room_visibility_by_you, senderName, formattedVisibility)
userResArgs = formattedVisibility,
thirdPartyResArgs = *arrayOf(senderName, formattedVisibility)
)
} }
private fun formatRoomThirdPartyInvite(event: Event, senderName: String?): CharSequence? { private fun formatRoomThirdPartyInvite(event: Event, senderName: String?): CharSequence? {
@ -296,27 +267,21 @@ class NoticeEventFormatter @Inject constructor(private val activeSessionDataSour
return when { return when {
prevContent != null -> { prevContent != null -> {
// Revoke case // Revoke case
chooseResourceByRoomType( if (event.isSentByCurrentUser())
event = event, sp.getString(if (event.isDm()) R.string.notice_direct_room_third_party_revoked_invite_by_you else R.string.notice_room_third_party_revoked_invite_by_you,
directRoomResId = R.string.notice_direct_room_third_party_revoked_invite, prevContent.displayName)
directRoomByUserResId = R.string.notice_direct_room_third_party_revoked_invite_by_you, else
publicRoomResId = R.string.notice_room_third_party_revoked_invite, sp.getString(if (event.isDm()) R.string.notice_direct_room_third_party_revoked_invite else R.string.notice_room_third_party_revoked_invite,
publicRoomByUserResId = R.string.notice_room_third_party_revoked_invite_by_you, senderName, prevContent.displayName)
userResArgs = prevContent.displayName,
thirdPartyResArgs = *arrayOf(senderName, prevContent.displayName)
)
} }
content != null -> { content != null -> {
// Invitation case // Invitation case
chooseResourceByRoomType( if (event.isSentByCurrentUser())
event = event, sp.getString(if (event.isDm()) R.string.notice_direct_room_third_party_invite_by_you else R.string.notice_room_third_party_invite_by_you,
directRoomResId = R.string.notice_direct_room_third_party_invite, content.displayName)
directRoomByUserResId = R.string.notice_direct_room_third_party_invite_by_you, else
publicRoomResId = R.string.notice_room_third_party_invite, sp.getString(if (event.isDm()) R.string.notice_direct_room_third_party_invite else R.string.notice_room_third_party_invite,
publicRoomByUserResId = R.string.notice_room_third_party_invite_by_you, senderName, content.displayName)
userResArgs = content.displayName,
thirdPartyResArgs = *arrayOf(senderName, content.displayName)
)
} }
else -> null else -> null
} }
@ -431,23 +396,17 @@ class NoticeEventFormatter @Inject constructor(private val activeSessionDataSour
val eventContent: RoomGuestAccessContent? = event.getClearContent().toModel() val eventContent: RoomGuestAccessContent? = event.getClearContent().toModel()
return when (eventContent?.guestAccess) { return when (eventContent?.guestAccess) {
GuestAccess.CanJoin -> GuestAccess.CanJoin ->
chooseResourceByRoomType( if (event.isSentByCurrentUser())
event = event, sp.getString(if (event.isDm()) R.string.notice_direct_room_guest_access_can_join_by_you else R.string.notice_room_guest_access_can_join_by_you)
directRoomResId = R.string.notice_direct_room_guest_access_can_join, else
directRoomByUserResId = R.string.notice_direct_room_guest_access_can_join_by_you, sp.getString(if (event.isDm()) R.string.notice_direct_room_guest_access_can_join else R.string.notice_room_guest_access_can_join,
publicRoomResId = R.string.notice_room_guest_access_can_join, senderName)
publicRoomByUserResId = R.string.notice_room_guest_access_can_join_by_you,
thirdPartyResArgs = *arrayOf(senderName)
)
GuestAccess.Forbidden -> GuestAccess.Forbidden ->
chooseResourceByRoomType( if (event.isSentByCurrentUser())
event = event, sp.getString(if (event.isDm()) R.string.notice_direct_room_guest_access_forbidden_by_you else R.string.notice_room_guest_access_forbidden_by_you)
directRoomResId = R.string.notice_direct_room_guest_access_forbidden, else
directRoomByUserResId = R.string.notice_direct_room_guest_access_forbidden_by_you, sp.getString(if (event.isDm()) R.string.notice_direct_room_guest_access_forbidden else R.string.notice_room_guest_access_forbidden,
publicRoomResId = R.string.notice_room_guest_access_forbidden, senderName)
publicRoomByUserResId = R.string.notice_room_guest_access_forbidden_by_you,
thirdPartyResArgs = *arrayOf(senderName)
)
else -> null else -> null
} }
} }
@ -571,24 +530,18 @@ class NoticeEventFormatter @Inject constructor(private val activeSessionDataSour
} }
Membership.JOIN -> Membership.JOIN ->
eventContent.safeReason?.let { reason -> eventContent.safeReason?.let { reason ->
chooseResourceByRoomType( if (event.isSentByCurrentUser())
event = event, sp.getString(if (event.isDm()) R.string.notice_direct_room_join_with_reason_by_you else R.string.notice_room_join_with_reason_by_you,
directRoomResId = R.string.notice_direct_room_join_with_reason, reason)
directRoomByUserResId = R.string.notice_direct_room_join_with_reason_by_you, else
publicRoomResId = R.string.notice_room_join_with_reason, sp.getString(if (event.isDm()) R.string.notice_direct_room_join_with_reason else R.string.notice_room_join_with_reason,
publicRoomByUserResId = R.string.notice_room_join_with_reason_by_you, senderDisplayName, reason)
userResArgs = reason,
thirdPartyResArgs = *arrayOf(senderDisplayName, reason)
)
} ?: run { } ?: run {
chooseResourceByRoomType( if (event.isSentByCurrentUser())
event = event, sp.getString(if (event.isDm()) R.string.notice_direct_room_join_by_you else R.string.notice_room_join_by_you)
directRoomResId = R.string.notice_direct_room_join, else
directRoomByUserResId = R.string.notice_direct_room_join_by_you, sp.getString(if (event.isDm()) R.string.notice_direct_room_join else R.string.notice_room_join,
publicRoomResId = R.string.notice_room_join, senderDisplayName)
publicRoomByUserResId = R.string.notice_room_join_by_you,
thirdPartyResArgs = *arrayOf(senderDisplayName)
)
} }
Membership.LEAVE -> Membership.LEAVE ->
// 2 cases here: this member may have left voluntarily or they may have been "left" by someone else ie. kicked // 2 cases here: this member may have left voluntarily or they may have been "left" by someone else ie. kicked
@ -606,24 +559,18 @@ class NoticeEventFormatter @Inject constructor(private val activeSessionDataSour
} }
else -> else ->
eventContent.safeReason?.let { reason -> eventContent.safeReason?.let { reason ->
chooseResourceByRoomType( if (event.isSentByCurrentUser())
event = event, sp.getString(if (event.isDm()) R.string.notice_direct_room_leave_with_reason_by_you else R.string.notice_room_leave_with_reason_by_you,
directRoomResId = R.string.notice_direct_room_leave_with_reason, reason)
directRoomByUserResId = R.string.notice_direct_room_leave_with_reason_by_you, else
publicRoomResId = R.string.notice_room_leave_with_reason, sp.getString(if (event.isDm()) R.string.notice_direct_room_leave_with_reason else R.string.notice_room_leave_with_reason,
publicRoomByUserResId = R.string.notice_room_leave_with_reason_by_you, senderDisplayName, reason)
userResArgs = reason,
thirdPartyResArgs = *arrayOf(senderDisplayName, reason)
)
} ?: run { } ?: run {
chooseResourceByRoomType( if (event.isSentByCurrentUser())
event = event, sp.getString(if (event.isDm()) R.string.notice_direct_room_leave_by_you else R.string.notice_room_leave_by_you)
directRoomResId = R.string.notice_direct_room_leave, else
directRoomByUserResId = R.string.notice_direct_room_leave_by_you, sp.getString(if (event.isDm()) R.string.notice_direct_room_leave else R.string.notice_room_leave,
publicRoomResId = R.string.notice_room_leave, senderDisplayName)
publicRoomByUserResId = R.string.notice_room_leave_by_you,
thirdPartyResArgs = *arrayOf(senderDisplayName)
)
} }
} }
} else { } else {
@ -690,14 +637,11 @@ class NoticeEventFormatter @Inject constructor(private val activeSessionDataSour
val content = event.getClearContent().toModel<RoomJoinRulesContent>() ?: return null val content = event.getClearContent().toModel<RoomJoinRulesContent>() ?: return null
return when (content.joinRules) { return when (content.joinRules) {
RoomJoinRules.INVITE -> RoomJoinRules.INVITE ->
chooseResourceByRoomType( if (event.isSentByCurrentUser())
event = event, sp.getString(if (event.isDm()) R.string.direct_room_join_rules_invite_by_you else R.string.room_join_rules_invite_by_you)
directRoomResId = R.string.direct_room_join_rules_invite, else
directRoomByUserResId = R.string.direct_room_join_rules_invite_by_you, sp.getString(if (event.isDm()) R.string.direct_room_join_rules_invite else R.string.room_join_rules_invite,
publicRoomResId = R.string.room_join_rules_invite, senderName)
publicRoomByUserResId = R.string.room_join_rules_invite_by_you,
thirdPartyResArgs = *arrayOf(senderName)
)
RoomJoinRules.PUBLIC -> RoomJoinRules.PUBLIC ->
if (event.isSentByCurrentUser()) { if (event.isSentByCurrentUser()) {
sp.getString(R.string.room_join_rules_public_by_you) sp.getString(R.string.room_join_rules_public_by_you)