Skip to content

Commit 3093b25

Browse files
committed
Rework: create extension method for cleaner code.
1 parent c5f5ff3 commit 3093b25

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

libraries/eventformatter/impl/src/main/kotlin/io/element/android/libraries/eventformatter/impl/DefaultRoomLastMessageFormatter.kt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class DefaultRoomLastMessageFormatter @Inject constructor(
6161
val isOutgoing = event.isOwn
6262
val senderDisambiguatedDisplayName = event.senderProfile.getDisambiguatedDisplayName(event.sender)
6363
return when (val content = event.content) {
64-
is MessageContent -> processMessageContents(content, senderDisambiguatedDisplayName, isDmRoom)
64+
is MessageContent -> content.process(senderDisambiguatedDisplayName, isDmRoom)
6565
RedactedContent -> {
6666
val message = sp.getString(CommonStrings.common_message_removed)
6767
if (!isDmRoom) {
@@ -71,7 +71,8 @@ class DefaultRoomLastMessageFormatter @Inject constructor(
7171
}
7272
}
7373
is StickerContent -> {
74-
prefixIfNeeded(sp.getString(CommonStrings.common_sticker) + " (" + content.body + ")", senderDisambiguatedDisplayName, isDmRoom)
74+
val message = sp.getString(CommonStrings.common_sticker) + " (" + content.body + ")"
75+
message.prefixIfNeeded(senderDisambiguatedDisplayName, isDmRoom)
7576
}
7677
is UnableToDecryptContent -> {
7778
val message = sp.getString(CommonStrings.common_waiting_for_decryption_key)
@@ -92,22 +93,22 @@ class DefaultRoomLastMessageFormatter @Inject constructor(
9293
}
9394
is PollContent -> {
9495
val message = sp.getString(CommonStrings.common_poll_summary, content.question)
95-
prefixIfNeeded(message, senderDisambiguatedDisplayName, isDmRoom)
96+
message.prefixIfNeeded(senderDisambiguatedDisplayName, isDmRoom)
9697
}
9798
is FailedToParseMessageLikeContent, is FailedToParseStateContent, is UnknownContent -> {
98-
prefixIfNeeded(sp.getString(CommonStrings.common_unsupported_event), senderDisambiguatedDisplayName, isDmRoom)
99+
val message = sp.getString(CommonStrings.common_unsupported_event)
100+
message.prefixIfNeeded(senderDisambiguatedDisplayName, isDmRoom)
99101
}
100102
is LegacyCallInviteContent -> sp.getString(CommonStrings.common_call_invite)
101103
is CallNotifyContent -> sp.getString(CommonStrings.common_call_started)
102104
}?.take(MAX_SAFE_LENGTH)
103105
}
104106

105-
private fun processMessageContents(
106-
messageContent: MessageContent,
107+
private fun MessageContent.process(
107108
senderDisambiguatedDisplayName: String,
108109
isDmRoom: Boolean,
109110
): CharSequence {
110-
val internalMessage = when (val messageType: MessageType = messageContent.type) {
111+
val message = when (val messageType: MessageType = type) {
111112
// Doesn't need a prefix
112113
is EmoteMessageType -> {
113114
return "* $senderDisambiguatedDisplayName ${messageType.body}"
@@ -143,16 +144,15 @@ class DefaultRoomLastMessageFormatter @Inject constructor(
143144
messageType.body
144145
}
145146
}
146-
return prefixIfNeeded(internalMessage, senderDisambiguatedDisplayName, isDmRoom)
147+
return message.prefixIfNeeded(senderDisambiguatedDisplayName, isDmRoom)
147148
}
148149

149-
private fun prefixIfNeeded(
150-
message: String,
150+
private fun String.prefixIfNeeded(
151151
senderDisambiguatedDisplayName: String,
152152
isDmRoom: Boolean,
153153
): CharSequence = if (isDmRoom) {
154-
message
154+
this
155155
} else {
156-
message.prefixWith(senderDisambiguatedDisplayName)
156+
prefixWith(senderDisambiguatedDisplayName)
157157
}
158158
}

0 commit comments

Comments
 (0)